-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_models_to_onnx.py
More file actions
170 lines (127 loc) · 5.19 KB
/
export_models_to_onnx.py
File metadata and controls
170 lines (127 loc) · 5.19 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# MXNet 호환 패치
import numpy as np
if not hasattr(np, 'bool'):
np.bool = bool
import torch
import torch.nn as nn
from transformers import AutoConfig, BertModel
from kobert.pytorch_kobert import get_pytorch_kobert_model
class TitleBERT(nn.Module):
def __init__(self, pretrained_name: str, num_classes: int):
super().__init__()
config = AutoConfig.from_pretrained(pretrained_name)
self.bert = BertModel.from_pretrained(pretrained_name, config=config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, num_classes)
def forward(self, input_ids, attention_mask=None, token_type_ids=None):
outputs = self.bert(
input_ids=input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
)
pooled = self.dropout(outputs.pooler_output)
return self.classifier(pooled)
class BertForSeg(nn.Module):
def __init__(self, finetune=False):
super().__init__()
self.model, vocab = get_pytorch_kobert_model(cachedir=".cache")
self.model.resize_token_embeddings(len(vocab))
self.finetune = finetune
if not finetune:
for p in self.model.parameters():
p.requires_grad = False
def forward(self, x, segs, mask):
top_vec, _ = self.model(x, token_type_ids=segs, attention_mask=mask)
return top_vec
class SentenceClassifier(nn.Module):
def __init__(self, window_size=3):
super().__init__()
conv_k = window_size * 2 - 2
flat = 256 * 3
ln_size = 1 if window_size == 1 else 3
self.block1 = nn.Sequential(
nn.Conv1d(768, 256, conv_k),
nn.LayerNorm([256, ln_size]),
nn.ReLU()
)
self.block2 = nn.Sequential(
nn.Linear(flat, 2)
)
def forward(self, x):
x = x.transpose(1, 2).contiguous()
x = self.block1(x)
x = x.view(x.size(0), -1)
return self.block2(x)
class KoBERTSeg(nn.Module):
def __init__(self, finetune=False, window_size=3):
super().__init__()
self.bert = BertForSeg(finetune)
self.classifier = SentenceClassifier(window_size)
def forward(self, src, segs, clss, mask_src, mask_cls):
top_vec = self.bert(src, segs, mask_src)
sents_vec = top_vec[torch.arange(top_vec.size(0)).unsqueeze(1), clss]
sents_vec = sents_vec * mask_cls[:, :, None].float()
return self.classifier(sents_vec)
def export_title_onnx(pt_path: str, onnx_path: str):
print("[Export] TitleBERT → ONNX 변환 시작")
model = TitleBERT("skt/kobert-base-v1", 2)
state = torch.load(pt_path, map_location="cpu")
state = {k: v for k, v in state.items() if "position_ids" not in k}
model.load_state_dict(state, strict=True)
model.eval()
dummy_input_ids = torch.zeros((1, 128), dtype=torch.long)
dummy_attention = torch.ones((1, 128), dtype=torch.long)
dummy_token_type = torch.zeros((1, 128), dtype=torch.long)
torch.onnx.export(
model,
(dummy_input_ids, dummy_attention, dummy_token_type),
onnx_path,
input_names=["input_ids", "attention_mask", "token_type_ids"],
output_names=["logits"],
dynamic_axes={
"input_ids": {0: "batch"},
"attention_mask": {0: "batch"},
"token_type_ids": {0: "batch"},
},
opset_version=17,
)
print(f"[Export] TitleBERT ONNX 저장됨: {onnx_path}")
def export_body_onnx(pt_path: str, onnx_path: str):
print("[Export] KoBERTSeg → ONNX 변환 시작")
model = KoBERTSeg(finetune=False, window_size=3)
state = torch.load(pt_path, map_location="cpu")
state = {k: v for k, v in state.items() if "position_ids" not in k}
model.load_state_dict(state, strict=True)
model.eval()
dummy_src = torch.zeros((1, 512), dtype=torch.long)
dummy_segs = torch.zeros((1, 512), dtype=torch.long)
dummy_mask_src = torch.ones((1, 512), dtype=torch.long)
dummy_clss = torch.tensor([[0, 10, 20, 30, 40, 50]], dtype=torch.long)
dummy_mask_cls = torch.ones((1, 6), dtype=torch.long)
torch.onnx.export(
model,
(dummy_src, dummy_segs, dummy_clss, dummy_mask_src, dummy_mask_cls),
onnx_path,
input_names=["src", "segs", "clss", "mask_src", "mask_cls"],
output_names=["logits"],
dynamic_axes={
"src": {0: "batch"},
"segs": {0: "batch"},
"mask_src": {0: "batch"},
"clss": {0: "batch", 1: "num_sent"},
"mask_cls": {0: "batch", 1: "num_sent"},
},
opset_version=17,
)
print(f"[Export] KoBERTSeg ONNX 저장됨: {onnx_path}")
# ------------------------------------------------------
# Main
# ------------------------------------------------------
if __name__ == "__main__":
TITLE_PT = "./saved_model/Part1/BERT/best_model.pt"
BODY_PT = "./saved_model/Part2/KoBERTSeg/best_model.pt"
TITLE_ONNX = "./onnx_models/title_model.onnx"
BODY_ONNX = "./onnx_models/body_model.onnx"
export_title_onnx(TITLE_PT, TITLE_ONNX)
export_body_onnx(BODY_PT, BODY_ONNX)
print("[Export] 모든 ONNX 변환 완료")