SenseVoiceSmall
本示例展示如何把SenseVoiceSmall模型量化和编译,部署到后摩芯片的设备上。
模型说明
本例使用的模型实现来源于:
模型名称: SenseVoiceSmall
许可: MIT License (https://github.com/modelscope/FunASR/blob/main/LICENSE)
预训练模型在运行时下载,工程发布不包含该模型。
本例只适用于xh2。
当前目录使用 config.yaml 作为默认值单一真值来源。当前默认模型为:
model_name=sensevoice
model_size=small
ndevice=1
用于校准的数据集
本项目引用 LibriSpeech ASR 数据集仅用于研究和评估目的。
数据集名称: LibriSpeech ASR (openslr/librispeech_asr)
数据集许可: CC BY 4.0
⚠️ 重要提示:
本数据集不作为本仓库的一部分进行分发,由 ptq.py 在量化时通过 HuggingFace 接口按需下载。
本数据集不受本项目 Apache License 2.0 许可的约束。
任何对数据集的使用都必须遵守其原始许可 (CC BY 4.0),包括署名等要求。
用户有责任确保在使用本数据集时遵守相关规定。
快速开始
目前SenseVoiceSmall模型仅提供python脚本编译方式。芯片模型只能在linux环境下量化和编译。
环境准备
安装示例运行所需的python依赖,建议使用venv
pip3 install -r requirements.txt
获取模型和数据
通过 get_model.py 下载模型,可通过参数选择下载原始模型、量化模型或芯片模型。
如果仅查看演示结果,可选择下载芯片模型,下载后的模型存放在当前目录(注意相同模型会覆盖,请自行保存):
python3 get_model.py --type hmm
如果需要自行编译模型,可以下载原始模式,下载方式如下:
python3 get_model.py --type raw
量化
量化需要使用GPU,并在宿主机安装与CUDA12.8兼容的CUDA和驱动。启动docker时将GPU映射到docker内,并指定可用的GPU ID,显存需要达到40GB以上,空闲内存需要达到100GB以上。参考命令:
docker run -it --gpus all --pid=host -w /hmdd -v $PWD:/hmdd --shm-size 64g harbor.houmo.ai/toolchain/release:Dadao-xh2-x.y.z-ubuntu24.04-x86.64 /bin/bash
进入docker后source环境变量,并进入models/asr/sensevoice目录,执行ptq.py脚本。默认的量化方式为 w8a8h1_sefp。
source env.sh
cd models/asr/sensevoice
python3 ptq.py --model_name sensevoice --model_size small
编译
将量化模型编译为在芯片上运行的模型。执行脚本:
python3 build.py --model_name sensevoice --model_size small
默认输出名称分别为:
sensevoice-small.hmm
演示
SenseVoiceSmall 模型使用 python API 进行演示。
demo.py 默认从 config.yaml 读取 model_name、model_size、ndevice,并自动推导 assets_dir 与 model_path。
执行脚本,推理完成将输出性能结果。
python3 demo.py
python3 demo.py --raw_result
python3 demo.py --model_name sensevoice --model_size small
如果需要显式覆盖默认路径,可手工指定:
python3 demo.py \
--model_name sensevoice \
--model_size small \
--model_path output/xh2/sensevoice-small.hmm \
--assets_dir SenseVoiceSmall
一键评估
以上步骤可以通过 test.sh 脚本执行。默认执行 demo 步骤:
# 默认执行 demo,会先下载预编译模型
bash test.sh
# 完整流程:下载原始模型 + 量化 + 编译 + 演示
bash test.sh -s all
test.sh 支持以下参数:
Usage: test.sh [options]
-s, --step Step to run. Default: demo. Choices: demo, build, quant, all.
Supports comma-separated values or repeated flags, e.g. -s quant,build or -s quant -s build.
-size, --model_size Model size. Default: small.
-name, --model_name Model name. Default: sensevoice.
--skip_download Skip model download steps.
-h, --help Show this help message.
当前脚本会把 model_name 和 model_size 透传给 get_model.py、ptq.py、build.py、demo.py。默认值分别是 sensevoice 和 small。
当前支持的模型组合为:
sensevoice + small
示例:
# 仅运行 quant,会先下载原始模型再量化
bash test.sh -s quant -name sensevoice -size small
# 仅运行 build,要求当前目录下已经有量化输出
bash test.sh -s build -name sensevoice -size small
# 仅运行 demo,会先下载预编译模型
bash test.sh -s demo -name sensevoice -size small
# 完整流程(下载原始模型、量化、编译、演示)
bash test.sh -s all -name sensevoice -size small
# 跳过模型下载,使用当前目录下已有的预编译模型进行演示
bash test.sh -s demo --skip_download -name sensevoice -size small
参考结果
演示结果
==================== Process ../../../data/audio/audio.mp3 ====================
Resampled audio from 22050Hz to 16000Hz
Audio duration: 2.43s, Feature frames: 41
Split into 1 feature chunks (max_len=512, overlap=10)
Chunk 1/1: content=41 frames, context=[0, 0], total=41 frames
Clean Text: 每一天都要快乐哦
Performance:
audio_duration: 2431.94 ms
total_infer_time: 424.94 ms
end_to_end_time: 1156.72 ms
Device 0: Max IPU use rate: 100%, Max Temperature: 43.785°C, Max Memory used: 531MB
Device 0 monitor stopped!
Loading tokens from SenseVoiceSmall/tokens.json
feature_chunks: 1
rtf (infer_time/audio_duration): 0.17
Loading model from output/xh2/sensevoice-small.hmm
Model max feature length: 512
Effective chunk size: 502 (max_len=512, overlap=10, max_context_per_side=10)