export-onnx.py
4.6 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
#!/usr/bin/env python3
# Copyright 2025 Xiaomi Corp. (authors: Fangjun Kuang)
import argparse
import os
from typing import Any, Dict, List, Tuple
import onnx
import sentencepiece as spm
import torch
from torch_model import SenseVoiceSmall
def get_args():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--input-len-in-seconds",
type=int,
required=True,
help="""RKNN does not support dynamic shape, so we need to hard-code
how long the model can process.
""",
)
return parser.parse_args()
def add_meta_data(filename: str, meta_data: Dict[str, Any]):
"""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)
onnx.save(model, filename)
def load_cmvn(filename) -> Tuple[List[float], List[float]]:
neg_mean = None
inv_stddev = None
with open(filename) as f:
for line in f:
if not line.startswith("<LearnRateCoef>"):
continue
t = line.split()[3:-1]
if neg_mean is None:
neg_mean = list(map(lambda x: float(x), t))
else:
inv_stddev = list(map(lambda x: float(x), t))
return neg_mean, inv_stddev
def generate_tokens(sp):
with open("tokens.txt", "w", encoding="utf-8") as f:
for i in range(sp.vocab_size()):
f.write(f"{sp.id_to_piece(i)} {i}\n")
print("saved to tokens.txt")
@torch.no_grad()
def main():
args = get_args()
print(vars(args))
sp = spm.SentencePieceProcessor()
sp.load("./chn_jpn_yue_eng_ko_spectok.bpe.model")
vocab_size = sp.vocab_size()
generate_tokens(sp)
print("loading model")
state_dict = torch.load("./model.pt", map_location="cpu")
if "state_dict" in state_dict:
state_dict = state_dict["state_dict"]
neg_mean, inv_stddev = load_cmvn("./am.mvn")
neg_mean = torch.tensor(neg_mean, dtype=torch.float32)
inv_stddev = torch.tensor(inv_stddev, dtype=torch.float32)
model = SenseVoiceSmall(neg_mean=neg_mean, inv_stddev=inv_stddev)
model.load_state_dict(state_dict)
model.eval()
del state_dict
lfr_window_size = 7
lfr_window_shift = 6
# frame shift is 10ms, 1 second has about 100 feature frames
input_len_in_seconds = int(args.input_len_in_seconds)
num_frames = input_len_in_seconds * 100
print("num_frames", num_frames)
# num_input_frames is an approximate number
num_input_frames = int(num_frames / lfr_window_shift + 0.5)
print("num_input_frames", num_input_frames)
x = torch.randn(1, num_input_frames, 560, dtype=torch.float32)
language = 3
text_norm = 15
prompt = torch.tensor([language, 1, 2, text_norm], dtype=torch.int32)
opset_version = 13
filename = f"model-{input_len_in_seconds}-seconds.onnx"
torch.onnx.export(
model,
(x, prompt),
filename,
opset_version=opset_version,
input_names=["x", "prompt"],
output_names=["logits"],
dynamic_axes={},
)
model_author = os.environ.get("model_author", "iic")
comment = os.environ.get("comment", "iic/SenseVoiceSmall")
url = os.environ.get("url", "https://huggingface.co/FunAudioLLM/SenseVoiceSmall")
meta_data = {
"lfr_window_size": lfr_window_size,
"lfr_window_shift": lfr_window_shift,
"num_input_frames": num_input_frames,
"normalize_samples": 0, # input should be in the range [-32768, 32767]
"model_type": "sense_voice_ctc",
"version": "1",
"model_author": model_author,
"maintainer": "k2-fsa",
"vocab_size": vocab_size,
"comment": comment,
"lang_auto": model.lid_dict["auto"],
"lang_zh": model.lid_dict["zh"],
"lang_en": model.lid_dict["en"],
"lang_yue": model.lid_dict["yue"], # cantonese
"lang_ja": model.lid_dict["ja"],
"lang_ko": model.lid_dict["ko"],
"lang_nospeech": model.lid_dict["nospeech"],
"with_itn": model.textnorm_dict["withitn"],
"without_itn": model.textnorm_dict["woitn"],
"url": url,
}
add_meta_data(filename=filename, meta_data=meta_data)
if __name__ == "__main__":
torch.manual_seed(20250717)
main()