# FunASR_npu **Repository Path**: jetydu/FunASR_npu ## Basic Information - **Project Name**: FunASR_npu - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-11-16 - **Last Updated**: 2026-11-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # FunASR 昇腾 NPU 迁移适配指南 本项目将 [FunASR](https://github.com/modelscope/FunASR) 语音识别模型迁移适配至华为昇腾 (Ascend) NPU,支持在昇腾 310P/910 系列芯片上高性能推理。 ## 环境要求 | 组件 | 版本 | |------|------| | 操作系统 | Linux aarch64/x86_64 | | CANN (Ascend Toolkit) | >= 7.0 | | Python | 3.8 - 3.10 | | PyTorch | 2.1.0 | | torch_npu | 2.1.0.post6 | | FunASR | 1.3.1 | | ModelScope | >= 1.10.0 | ## 快速开始 ### 1. 配置 CANN 环境 ```bash source /usr/local/Ascend/ascend-toolkit/set_env.sh ``` ### 2. 一键安装 ```bash bash install_npu_patch.sh ``` 该脚本自动完成以下操作: - 检查 CANN 和 torch_npu 环境 - 安装 FunASR 1.3.1 及依赖 - 应用 NPU 适配补丁到 funasr 和 modelscope - 验证安装 ### 3. 运行测试 ```bash source /usr/local/Ascend/ascend-toolkit/set_env.sh python3 test_npu_inference.py ``` 预期输出: ``` NPU text : 欢迎大家来体验达摩院推出的语音识别模型 CPU text : 欢迎大家来体验达摩院推出的语音识别模型 Match : True [PASS] NPU inference matches CPU - migration successful! ``` ### 4. 在你的代码中使用 NPU ```python import torch_npu # 务必在 import torch 之后导入 from funasr import AutoModel model = AutoModel( model="iic/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch", device="npu:0", # 指定 NPU 设备 ) result = model.generate(input="your_audio.wav") print(result[0]["text"]) ``` ## 性能测试结果 | 指标 | NPU (310P3) | CPU | |------|-------------|-----| | 首次推理(含算子编译) | ~39s | 13.6s | | Warmup 后推理 | **1.33s** | 13.6s | | 稳态加速比 | **~10x** | 1x | | 识别结果 | 欢迎大家来体验达摩院推出的语音识别模型 | 欢迎大家来体验达摩院推出的语音识别模型 | | 结果一致性 | ✅ 完全匹配 | - | > 注:首次推理慢是因为昇腾 NPU 需要编译算子图。后续推理自动使用缓存,性能提升显著。 ## 迁移适配详情 ### 修改的文件 #### FunASR 侧 | 文件 | 修改说明 | |------|----------| | `funasr/auto/auto_model.py` | 添加 NPU device 检测逻辑,自动回退:cuda → npu → cpu;添加 NPU empty_cache 支持 | | `funasr/frontends/fused.py` | 添加 NPU device 检测分支 | #### ModelScope 侧 | 文件 | 修改说明 | |------|----------| | `modelscope/utils/device.py` | 全面支持 npu device 类型,verify_device/create_device/get_device/device_placement 均已适配 | | `modelscope/utils/constant.py` | Devices 类新增 npu 常量 | ### 核心适配策略 1. **自动设备检测**:当用户指定 `device="cuda"` 但系统无 CUDA 时,自动检测并回退到 NPU 2. **显式 NPU 指定**:支持 `device="npu"` 或 `device="npu:0"` 直接指定 NPU 设备 3. **内存管理**:NPU 推理后自动调用 `torch.npu.empty_cache()` 释放显存 4. **零侵入设计**:所有补丁均为增量修改,不影响原有 CUDA/CPU 功能 ## 文件结构 ``` FunASR/ ├── README.md # 本文档 ├── install_npu_patch.sh # 一键安装脚本 ├── test_npu_inference.py # NPU 推理测试(含 CPU 对比) ├── test_npu_warmup.py # NPU warmup 性能测试 ├── test_report.json # 测试结果报告 ├── test_audio/ │ └── asr_example_zh.wav # 测试音频 └── npu_patches/ # NPU 适配补丁文件 ├── funasr/ │ ├── auto_model.py # FunASR AutoModel 适配 │ └── fused.py # FunASR Frontend 适配 └── modelscope_utils/ ├── device.py # ModelScope device 适配 └── constant.py # ModelScope 常量适配 ``` ## 移植到其他机器 ### 打包 ```bash cd /data tar czf FunASR_NPU_Migration.tar.gz FunASR/ ``` ### 在目标机器部署 ```bash # 1. 解压 tar xzf FunASR_NPU_Migration.tar.gz cd FunASR # 2. 确保目标机器已安装: # - CANN Toolkit (>= 7.0) # - PyTorch 2.1.0 # - torch_npu 2.1.0 # 3. 配置环境并安装 source /usr/local/Ascend/ascend-toolkit/set_env.sh bash install_npu_patch.sh # 4. 运行测试 python3 test_npu_inference.py ``` ## 常见问题 ### Q: 报错 "libhccl.so: cannot open shared object file" **A:** 未加载 CANN 环境,请先运行: ```bash source /usr/local/Ascend/ascend-toolkit/set_env.sh ``` ### Q: 首次推理非常慢 **A:** 正常现象。昇腾 NPU 首次推理需要编译算子图(约 30-40s),后续推理自动使用缓存,速度会降至 1-2 秒。 ### Q: 如何指定使用的 NPU 设备 **A:** 通过环境变量或参数指定: ```bash export ASCEND_RT_VISIBLE_DEVICES=0 # 使用第 0 号 NPU ``` 或在代码中: ```python model = AutoModel(model="...", device="npu:0") ``` ### Q: 如何验证 NPU 是否可用 ```python import torch import torch_npu print(torch.npu.is_available()) # True print(torch.npu.device_count()) # NPU 数量 print(torch.npu.get_device_name(0)) # 设备名称 ```