Skip to content

Commit b1db3ea

Browse files
authored
Export Whisper to RK NPU (#2983)
1 parent e2bf3dc commit b1db3ea

17 files changed

Lines changed: 2291 additions & 9 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,5 @@ spacemit-toolchain*
166166
sherpa-onnx-qnn-*
167167
matcha-icefall-*
168168
sherpa-onnx-medasr-ctc-en-int8-2025-12-25
169+
*.raw
170+
*-input-list.txt

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,6 @@ It uses Swift for iOS and Java for Android.
424424
Flet ASR/STT component based on sherpa-onnx.
425425
Example [a chat box agent](https://github.com/SamYuan1990/i18n-agent-action)
426426

427-
### [elderly-companion](https://github.com/SearocIsMe/elderly-companion)
428-
429-
It uses sherpa-onnx's Python API for real-time speech recognition in ROS2 with RK NPU.
430-
431427
### [achatbot-go](https://github.com/ai-bot-pro/achatbot-go)
432428

433429
a multimodal chatbot based on go with sherpa-onnx's speech lib api.

scripts/whisper/export-onnx.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
TextDecoder,
3333
)
3434

35-
torch.set_num_threads(1)
36-
torch.set_num_interop_threads(1)
37-
3835

3936
def get_args():
4037
parser = argparse.ArgumentParser()
@@ -321,7 +318,7 @@ def main():
321318
print(args)
322319
print(name)
323320

324-
opset_version = 13
321+
opset_version = 17
325322

326323
if name == "distil-medium.en":
327324
filename = "./distil-medium-en-original-model.bin"
@@ -640,4 +637,12 @@ def main():
640637

641638

642639
if __name__ == "__main__":
643-
main()
640+
torch.set_num_threads(1)
641+
torch.set_num_interop_threads(1)
642+
# To fix
643+
# TypeError: scaled_dot_product_attention(): argument 'is_causal' must be bool, not Tensor
644+
# See also https://github.com/k2-fsa/sherpa-onnx/issues/1764
645+
from whisper.model import disable_sdpa
646+
647+
with disable_sdpa():
648+
main()

scripts/whisper/export_onnx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export-onnx.py

scripts/whisper/model-info.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# tiny/tiny.en
2+
```
3+
ModelDimensions(
4+
n_mels=80,
5+
n_audio_ctx=1500,
6+
n_audio_state=384,
7+
n_audio_head=6,
8+
n_audio_layer=4,
9+
n_vocab=51865,
10+
n_text_ctx=448,
11+
n_text_state=384,
12+
n_text_head=6,
13+
n_text_layer=4
14+
)
15+
```
16+
17+
# base/base.en
18+
```
19+
ModelDimensions(
20+
n_mels=80,
21+
n_audio_ctx=1500,
22+
n_audio_state=512,
23+
n_audio_head=8,
24+
n_audio_layer=6,
25+
n_vocab=51865,
26+
n_text_ctx=448,
27+
n_text_state=512,
28+
n_text_head=8,
29+
n_text_layer=6
30+
)
31+
```
32+
33+
# small/small.en
34+
```
35+
ModelDimensions(
36+
n_mels=80,
37+
n_audio_ctx=1500,
38+
n_audio_state=768,
39+
n_audio_head=12,
40+
n_audio_layer=12,
41+
n_vocab=51865,
42+
n_text_ctx=448,
43+
n_text_state=768,
44+
n_text_head=12,
45+
n_text_layer=12
46+
)
47+
```
48+
49+
50+
# medium/medium.en
51+
```
52+
ModelDimensions(
53+
n_mels=80,
54+
n_audio_ctx=1500,
55+
n_audio_state=1024,
56+
n_audio_head=16,
57+
n_audio_layer=24,
58+
n_vocab=51865,
59+
n_text_ctx=448,
60+
n_text_state=1024,
61+
n_text_head=16,
62+
n_text_layer=24
63+
)
64+
```
65+
66+
# large
67+
```
68+
ModelDimensions(
69+
n_mels=80,
70+
n_audio_ctx=1500,
71+
n_audio_state=1280,
72+
n_audio_head=20,
73+
n_audio_layer=32,
74+
n_vocab=51865,
75+
n_text_ctx=448,
76+
n_text_state=1280,
77+
n_text_head=20,
78+
n_text_layer=32
79+
)
80+
```
81+

scripts/whisper/rknn/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Usage
2+
3+
You can find pre-exported rknn models for rk3588 at
4+
5+
https://modelscope.cn/models/csukuangfj/2026-01-05-rknn/files
6+
7+
8+
# Download test wave
9+
10+
```
11+
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/en.wav
12+
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/en-16k.wav
13+
```
14+
15+
## Export to onnx
16+
17+
```
18+
./export_onnx.py --model tiny.en
19+
```
20+
21+
## Test onnx
22+
23+
```
24+
./test_onnx.py --model tiny.en
25+
```
26+
27+
## Export to rknn
28+
29+
```
30+
python3 ./export_rknn.py --target-platform rk3588 --in-model ./tiny.en-encoder.onnx --out-model ./tiny.en-encoder.rknn
31+
32+
python3 ./export_rknn.py --target-platform rk3588 --in-model ./tiny.en-decoder.onnx --out-model ./tiny.en-decoder.rknn
33+
```
34+
35+
```
36+
ls -lh tiny.en-*.rknn
37+
38+
-rw-r--r-- 1 kuangfangjun root 95M Jan 5 16:16 tiny.en-decoder.rknn
39+
-rw-r--r-- 1 kuangfangjun root 22M Jan 5 16:15 tiny.en-encoder.rknn
40+
```
41+
42+
## Run it on your rk3588 board
43+
44+
```
45+
wget https://huggingface.co/csukuangfj/sherpa-onnx-whisper-tiny.en/resolve/main/tiny.en-tokens.txt
46+
47+
./test_on_rk3588_board.py --encoder ./tiny.en-encoder.rknn --decoder ./tiny.en-decoder.rknn --tokens ./tiny.en-tokens.txt --wav ./en-16k.wav
48+
```

0 commit comments

Comments
 (0)