1. 环境

显卡:3090Ti
CUDA: 12.8
OS: Ubuntu 24.04
Python: 3.8

如果你根据官方的文档编译失败,或者你不懂如何编译GPU版本的FunASR,均可联系博主进行有偿远程源码编译,微信:lukeewin01,一定要备注来自博客,并且备注添加理由为远程源码编译安装GPU版本FunASR,如果不备注,一律不给通过。
转写速度和资源占用情况,可以看我发布的另外一篇文章,点击这里跳转到GPU版本FunASR测试文章

2. 安装Python依赖

pip install -U funasr
pip3 install pai_blade_gpu==3.27.0+cu113.1.12.1 -f https://pai-blade.oss-cn-zhangjiakou.aliyuncs.com/release/repo_ext.html
pip3 install torch_blade==3.27.0+1.12.1.cu113 -f https://pai-blade.oss-cn-zhangjiakou.aliyuncs.com/release/repo_ext.html
pip install torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113
pip install humanfriendly

具体可以访问下面网址。

https://help.aliyun.com/zh/pai/user-guide/install-blade#section-4vs-cgt-oo3

注意:不要安装过高版本的PyTorch

3. 修改CMakeLisits.txt

需要修改三个文件。

vim FunASR/runtime/websocket/CMakeLists.txt
option(GPU "Whether to build with GPU" OFF) --> option(GPU "Whether to build with GPU" ON)
set(TORCH_DIR "/home/lukeewin/miniconda3/envs/funasr_cpp/lib/python3.8/site-packages/torch") --> 修改为自己服务器中的torch路径
set(TORCH_BLADE_DIR "/home/lukeewin/miniconda3/envs/funasr_cpp/lib/python3.8/site-packages/torch_blade") --> 注释掉
link_directories(${TORCH_DIR}/lib) --> 注释掉

torch_blade不注释掉编译时会报错。

vim FunASR/runtime/onnxruntime/CMakeLists.txt
option(GPU "Whether to build with GPU" OFF) --> option(GPU "Whether to build with GPU" ON)
set(TORCH_DIR "/home/lukeewin/miniconda3/envs/funasr_cpp/lib/python3.8/site-packages/torch") --> 修改为自己服务器中的torch的路径
set(TORCH_BLADE_DIR "/home/lukeewin/miniconda3/envs/funasr_cpp/lib/python3.8/site-packages/torch_blade") --> 注释掉
link_directories(${TORCH_BLADE_DIR}) --> 注释掉
vim FunASR/runtime/onnxruntime/src/CMakeLists.txt
set(TORCH_DEPS torch torch_cuda torch_cpu c10 c10_cuda torch_blade ral_base_context) --> set(TORCH_DEPS torch torch_cuda torch_cpu c10 c10_cuda)

4. 下载onnxruntime和ffmpeg文件

wget https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/dep_libs/onnxruntime-linux-x64-1.14.0.tgz
tar -zxvf onnxruntime-linux-x64-1.14.0.tgz
wget https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/dep_libs/ffmpeg-master-latest-linux64-gpl-shared.tar.xz
tar -xvf ffmpeg-master-latest-linux64-gpl-shared.tar.xz
# openblas
sudo apt-get install libopenblas-dev #ubuntu
# sudo yum -y install openblas-devel #centos

# openssl
sudo apt-get install libssl-dev #ubuntu 
# yum install openssl-devel #centos

5. 编译

cd FunASR/runtime/websocket
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=release .. -DONNXRUNTIME_DIR=/home/lukeewin/Projects/FunASR_New/FunASR/runtime/websocket/onnxruntime-linux-x64-1.14.0 -DFFMPEG_DIR=/home/lukeewin/Projects/FunASR_New/FunASR/runtime/websocket/ffmpeg-master-latest-linux64-gpl-shared
make -j28

6. 转换模型格式

注意上面是没有使用torch_blade,所以不能直接使用modelscope中的model_blade.torchscript,需要自己转换格式。

from funasr import AutoModel

model = AutoModel(
    model="/home/lukeewin/Projects/FunASR_New/models/iic/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch",
)

res = model.export(type="torchscript", quantize=False, bladedisc=False)
# res = model.export(type="bladedisc", input=f"{model.model_path}/example/asr_example.wav")
print(res)

注意:上面中把bladedisc=False参数加上。如果需要量化,把quantize=False改为quantize=True。还需要注意,导出来的模型为model,是没有后缀的,需要添加后缀torchscript,因为执行可执行程序funasr-wss-server时默认加载的是model.torchscript名称的模型文件,如果不需要会报找不到文件错误。

这里已经转换好,保存到自己的Ubuntu系统的路径为:

/home/lukeewin/Projects/FunASR_New/models/iic/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-torchscript

注意:只有ASR模型需要转换为torchscript格式的模型,其它模型不需要转换。

还使用到了下面的这些模型:

speech_ngram_lm_zh-cn-ai-wesp-fst # 语言模型
speech_fsmn_vad_zh-cn-16k-common-onnx # 音频活动性检测模型
punc_ct-transformer_cn-en-common-vocab471067-large-onnx # 标点符号预测模型
fst_itn_zh # 逆文本化

这里为了方便,给出下载指令:

modelscope download --model 'iic/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch' --local_dir './iic/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch'
modelscope download --model 'iic/speech_ngram_lm_zh-cn-ai-wesp-fst' --local_dir './iic/speech_ngram_lm_zh-cn-ai-wesp-fst'
modelscope download --model 'iic/speech_fsmn_vad_zh-cn-16k-common-onnx' --local_dir './iic/speech_fsmn_vad_zh-cn-16k-common-onnx'
modelscope download --model 'iic/punc_ct-transformer_cn-en-common-vocab471067-large-onnx' --local_dir './iic/punc_ct-transformer_cn-en-common-vocab471067-large-onnx'
modelscope download --model 'thuduj12/fst_itn_zh' --local_dir './thuduj12/fst_itn_zh'

7. 启动服务端

./funasr-wss-server --batch-size 32 \
--gpu \
--model-thread-num 16 \
--decoder-thread-num 16 \
--io-thread-num 8 \
--port 8000 \
--listen-ip '0.0.0.0' \
--lm-revision 'master' \
--lm-dir '/home/lukeewin/Projects/FunASR_New/models/iic/speech_ngram_lm_zh-cn-ai-wesp-fst' \
--itn-revision 'master' \
--itn-dir '/home/lukeewin/Projects/FunASR_New/models/thuduj12/fst_itn_zh' \
--punc-quant true \
--punc-revision 'master' \
--punc-dir '/home/lukeewin/Projects/FunASR_New/models/iic/punc_ct-transformer_cn-en-common-vocab471067-large-onnx' \
--vad-quant true  \
--vad-revision 'master' \
--vad-dir '/home/lukeewin/Projects/FunASR_New/models/iic/speech_fsmn_vad_zh-cn-16k-common-onnx' \
--bladedisc false \
--quantize true \
--model-revision 'master'  \
--model-dir '/home/lukeewin/Projects/FunASR_New/models/iic/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-torchscript'  \
--certfile ''

8. 测试

./funasr-wss-client --svs-itn 1 \
--use-itn 1 \
--is-ssl 0 \
--thread-num 1 \
--audio-fs 16000 \
--wav-path '/data/funasr/test_funasr_triton_gpu/Triton-ASR-Client-main/datasets/aishell1_test/wav.scp' \
--port 8000 \
--server-ip '127.0.0.1'

9. 参考

https://github.com/modelscope/FunASR/tree/main/runtime/websocket
https://github.com/modelscope/FunASR/issues/2238

Q.E.D.


热爱生活,热爱程序