-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbert_to_onnx_dynamic_seq.py
53 lines (40 loc) · 2.04 KB
/
bert_to_onnx_dynamic_seq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author: Chunyu Xue
import numpy as np
import torch
import onnxruntime
import argparse
import os
from models.bert_custom import BertModel_custom
parser = argparse.ArgumentParser()
parser.add_argument("--seq_len", default="16") # Sequence length
args = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = "MIG-GPU-9de3d0e8-33f5-10dc-0c79-2c88a7ab0a23/2/0"
def make_position_input(input_ids):
seq_length = input_ids.size(1)
position_ids = torch.arange(0, seq_length, dtype=torch.long, device=input_ids.device)
return position_ids
def make_train_dummy_input(seq_len):
org_input_ids = torch.LongTensor([[i for i in range(seq_len)]])
org_token_type_ids = torch.LongTensor([[1 for i in range(seq_len)]])
org_input_mask = torch.LongTensor([[0 for i in range(int(seq_len/2))] + [1 for i in range(seq_len - int(seq_len/2))]])
org_position_ids = make_position_input(org_input_ids)
return (org_input_ids, org_token_type_ids, org_input_mask, org_position_ids)
if __name__ == '__main__':
MODEL_ONNX_PATH = "./onnx/bert_" + args.seq_len + ".onnx"
OPERATOR_EXPORT_TYPE = torch._C._onnx.OperatorExportTypes.ONNX
model = BertModel_custom.from_pretrained('bert-base-uncased')
model.train(False)
org_dummy_input = make_train_dummy_input(int(args.seq_len))
output = torch.onnx.export(model,
org_dummy_input,
MODEL_ONNX_PATH,
verbose=True,
operator_export_type=OPERATOR_EXPORT_TYPE,
input_names=['input_ids', 'token_type_ids', 'attention_mask', 'position_ids'],
output_names=['output'],
do_constant_folding=True,
dynamic_axes={"input_ids": {0: "batch_size"}, "token_type_ids": {0: "batch_size"}, "attention_mask": {0: "batch_size"}, "output": {0: "batch_size"},}
)
print("Export of torch_model.onnx complete!")