Whisper端侧运行

本章节将以whisper-medium模型为例,为您介绍该模型的端侧运行流程。 此流程适用于量化后模型直接上板运行的场景,请您先阅读 端侧运行准备 章节做好端侧运行准备并了解模型和示例的对应关系。

使用限制

whisper-medium仅支持16kHz采样率的单通道音频,且单次识别的最大音频长度为30秒。

示例文件说明

oellm_runtime目录中,whisper-medium使用到的文件和文件夹如下所示:

. ├── configs │ └── Whisper_Medium_config # Tokenizer文件 ├── examples │ └── whisper_demo │ ├── essentia # essentia库头文件 │ ├── build_whisper.sh # 交叉编译脚本 │ ├── CMakeLists.txt │ ├── whisper_demo.cc # 可执行文件源码 │ ├── whisper # 可执行文件 │ ├── 0.wav # 音频测试用例 │ ├── run_whisper.sh # 端侧运行脚本 │ └── whisper_config.json # 端侧运行配置文件 ├── include ├── lib └── model ├── whisper_medium │ ├── whisper-medium_audio_encode_duration_30s_sr_16k_w8_nash-p_corenum_4.hbm │ └── whisper-medium_audio_decode_w8_nash-p_corenum_1_1.hbm └── resolve_model_nash-p.md # 端侧模型下载方式

配置文件说明

端侧运行配置文件whisper_config.json可配置参数及说明:

参数名称参数说明可选/必选
model_dir参数描述:板端模型存放的文件夹路径
参数类型string
必选
encode_model_file参数描述:encode模型文件名
参数类型string
必选
decode_model_file参数描述:decode模型文件名
参数类型string
必选
encode_bpu_core参数描述:encode模型使用的bpu核,设定多个值时按照[0,1,2,3]填写
参数类型[int]
必选
decode_bpu_core参数描述:decode模型使用的bpu核,设定多个值时按照[0,1,2,3]填写
参数类型[int]
必选
vocabulary_path参数描述:Tokenizer配置文件路径
参数类型string
必选
language参数描述:识别的语言类型
参数类型string
取值范围zh:中文,en:英文
默认配置zh
可选

参考配置示例:

{ "model_dir": "../../model/whisper_medium/", "encode_model_file": "whisper-medium_audio_encode_duration_30s_sr_16k_w8_nash-p_corenum_4.hbm", "decode_model_file": "whisper-medium_audio_decode_w8_nash-p_corenum_1_1.hbm", "encode_bpu_core": [ 0,1,2,3 ], "decode_bpu_core": [ 0 ], "vocabulary_path": "../../configs/Whisper_Medium_config/", "language": "en" }

端侧运行说明

示例提供一键运行脚本run_whisper.sh,运行指令参考:

cd /userdata/oellm_runtime/examples/whisper_demo/ bash run_whisper.sh

该脚本内容如下:

export LD_LIBRARY_PATH=../../lib:$LD_LIBRARY_PATH # 指定动态库 export HB_DNN_USER_DEFINED_L2M_SIZES=6:6:6:6 # 为BPU分配L2M大小 ./whisper --config_path ./whisper_config.json --audio_path ./0.wav

可执行文件的参数信息:

Usage: ./whisper --config_path <config_path> --audio_path <audio_path> [options] Options: -c, --config_path <config_path> Path to the whisper config file (required) -a, --audio_path <audio_path> Path to the local audio file (required) -h, --help Show this help message Examples: ./whisper --config_path ./whisper_config.json --audio_path ./0.wav

运行结果展示

测试用例0.wav的运行结果:

[Transcription] Mr. Quilter is the apostle of the middle classes and we are glad to welcome his gospel. [Performance] TTFT: 110.557000 ms TPS: 61.050993 tokens/s decode_tokens: 21

补充说明

  1. whisper_demo可直接读取本地音频文件,支持mp3wavflac等常见格式,程序会使用essentia的MonoLoader方法,以16khz重采样,并按照float32(-1.0~1.0)的一维vector数组格式,通过xlm_feed_audio_online接口送给ASR模型做推理识别。
  2. xlm_feed_audio_online支持送入最大30秒的音频数据,模型推理前会丢弃超时部分并抛出警告。
  3. 若您的音频数据类型为int16_t,则需要先处理成float32格式,通常除以32768并做类型转换即可。