-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathexport.py
42 lines (33 loc) · 1.04 KB
/
export.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
# -*- coding: utf-8 -*-
import os
import torch
from transformers import AutoModelForMaskedLM, AutoTokenizer
model_name = "albert-base-v2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForMaskedLM.from_pretrained(model_name)
text = "Paris is the [MASK] of France."
tokenizer_output = tokenizer(text, return_tensors="pt")
input_ids = tokenizer_output["input_ids"]
attention_mask = tokenizer_output["attention_mask"]
token_type_ids = tokenizer_output["token_type_ids"]
dynamic_axes = {
0: "batch",
1: "seq",
}
output_dir = "./albert"
os.makedirs(output_dir, exist_ok=True)
torch.onnx.export(
model,
(input_ids, attention_mask, token_type_ids),
os.path.join(output_dir, "model.onnx"),
input_names=["input_ids", "attention_mask", "token_type_ids"],
output_names=["logits"],
dynamic_axes={
"input_ids": dynamic_axes,
"attention_mask": dynamic_axes,
"token_type_ids": dynamic_axes,
"logits": dynamic_axes,
},
opset_version=14,
)
tokenizer.save_pretrained(output_dir)