export-onnx-streaming.py
6.1 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
# Copyright 2023 Xiaomi Corp. (authors: Fangjun Kuang)
# pip install git+https://github.com/wenet-e2e/wenet.git
# pip install onnxruntime onnx pyyaml
# cp -a ~/open-source/wenet/wenet/transducer/search .
# cp -a ~/open-source//wenet/wenet/e_branchformer .
# cp -a ~/open-source/wenet/wenet/ctl_model .
import os
from typing import Dict
import onnx
import torch
import yaml
from onnxruntime.quantization import QuantType, quantize_dynamic
from wenet.utils.init_model import init_model
def add_meta_data(filename: str, meta_data: Dict[str, str]):
"""Add meta data to an ONNX model. It is changed in-place.
Args:
filename:
Filename of the ONNX model to be changed.
meta_data:
Key-value pairs.
"""
model = onnx.load(filename)
while len(model.metadata_props):
model.metadata_props.pop()
for key, value in meta_data.items():
meta = model.metadata_props.add()
meta.key = key
meta.value = str(value)
# model = onnx.version_converter.convert_version(model, 21)
onnx.save(model, filename)
class OnnxModel(torch.nn.Module):
def __init__(self, encoder: torch.nn.Module, ctc: torch.nn.Module):
super().__init__()
self.encoder = encoder
self.ctc = ctc
def forward(
self,
x: torch.Tensor,
offset: torch.Tensor,
required_cache_size: torch.Tensor,
attn_cache: torch.Tensor,
conv_cache: torch.Tensor,
attn_mask: torch.Tensor,
):
"""
Args:
x:
A 3-D float32 tensor of shape (N, T, C). It supports only N == 1.
offset:
A scalar of dtype torch.int64.
required_cache_size:
A scalar of dtype torch.int64.
attn_cache:
A 4-D float32 tensor of shape (num_blocks, head, required_cache_size, encoder_output_size / head /2).
conv_cache:
A 4-D float32 tensor of shape (num_blocks, N, encoder_output_size, cnn_module_kernel - 1).
attn_mask:
A 3-D bool tensor of shape (N, 1, required_cache_size + chunk_size)
Returns:
Return a tuple of 3 tensors:
- A 3-D float32 tensor of shape (N, T, C) containing log_probs
- next_attn_cache
- next_conv_cache
"""
encoder_out, next_att_cache, next_conv_cache = self.encoder.forward_chunk(
xs=x,
offset=offset,
required_cache_size=required_cache_size,
att_cache=attn_cache,
cnn_cache=conv_cache,
att_mask=attn_mask,
)
log_probs = self.ctc.log_softmax(encoder_out)
return log_probs, next_att_cache, next_conv_cache
class Foo:
pass
@torch.no_grad()
def main():
args = Foo()
args.checkpoint = "./final.pt"
config_file = "./train.yaml"
with open(config_file, "r") as fin:
configs = yaml.load(fin, Loader=yaml.FullLoader)
torch_model, configs = init_model(args, configs)
torch_model.eval()
head = configs["encoder_conf"]["attention_heads"]
num_blocks = configs["encoder_conf"]["num_blocks"]
output_size = configs["encoder_conf"]["output_size"]
cnn_module_kernel = configs["encoder_conf"].get("cnn_module_kernel", 1)
right_context = torch_model.right_context()
subsampling_factor = torch_model.encoder.embed.subsampling_rate
chunk_size = 16
left_chunks = 4
decoding_window = (chunk_size - 1) * subsampling_factor + right_context + 1
required_cache_size = chunk_size * left_chunks
offset = required_cache_size
attn_cache = torch.zeros(
num_blocks,
head,
required_cache_size,
output_size // head * 2,
dtype=torch.float32,
)
attn_mask = torch.ones(1, 1, required_cache_size + chunk_size, dtype=torch.bool)
attn_mask[:, :, :required_cache_size] = 0
conv_cache = torch.zeros(
num_blocks, 1, output_size, cnn_module_kernel - 1, dtype=torch.float32
)
sos = torch_model.sos_symbol()
eos = torch_model.eos_symbol()
onnx_model = OnnxModel(
encoder=torch_model.encoder,
ctc=torch_model.ctc,
)
filename = "model-streaming.onnx"
N = 1
T = decoding_window
C = 80
x = torch.rand(N, T, C, dtype=torch.float32)
offset = torch.tensor([offset], dtype=torch.int64)
required_cache_size = torch.tensor([required_cache_size], dtype=torch.int64)
opset_version = 13
torch.onnx.export(
onnx_model,
(x, offset, required_cache_size, attn_cache, conv_cache, attn_mask),
filename,
opset_version=opset_version,
input_names=[
"x",
"offset",
"required_cache_size",
"attn_cache",
"conv_cache",
"attn_mask",
],
output_names=["log_probs", "next_att_cache", "next_conv_cache"],
dynamic_axes={
"x": {0: "N", 1: "T"},
"attn_cache": {2: "T"},
"attn_mask": {2: "T"},
"log_probs": {0: "N"},
"new_attn_cache": {2: "T"},
},
)
# https://wenet.org.cn/downloads?models=wenet&version=aishell_u2pp_conformer_exp.tar.gz
url = os.environ.get("WENET_URL", "")
meta_data = {
"model_type": "wenet_ctc",
"version": "1",
"model_author": "wenet",
"comment": "streaming",
"url": "https://wenet.org.cn/downloads?models=wenet&version=aishell_u2pp_conformer_exp.tar.gz",
"chunk_size": chunk_size,
"left_chunks": left_chunks,
"head": head,
"num_blocks": num_blocks,
"output_size": output_size,
"cnn_module_kernel": cnn_module_kernel,
"right_context": right_context,
"subsampling_factor": subsampling_factor,
"vocab_size": torch_model.ctc.ctc_lo.weight.shape[0],
}
add_meta_data(filename=filename, meta_data=meta_data)
print("Generate int8 quantization models")
filename_int8 = f"model-streaming.int8.onnx"
quantize_dynamic(
model_input=filename,
model_output=filename_int8,
op_types_to_quantize=["MatMul"],
weight_type=QuantType.QInt8,
)
if __name__ == "__main__":
main()