上级索引:granite-speech/README.md。 本页面向:已在昇腾机器上装好驱动 的用户,用 Transformers 原生推理 跑通 granite-speech-4.1-2b-plus。 技术细节以 IBM Granite Speech 官方文档 与 HuggingFace 模型卡 为准。
按下面顺序执行后:在 Atlas 800 A2 (64G x 8) 上,用 Transformers + torch_npu 完成语音 ASR / 语音翻译推理,并验证精度与性能。
本页不解决:宿主机未装昇腾驱动、无对应机型——请先完成 昇腾环境准备。
在宿主机执行(有正常输出即可进入下一步):
npu-smi info
python3 -c "import torch_npu; print('NPU available:', torch_npu.npu.is_available())"确保已安装昇腾驱动与 CANN Toolkit,并设置环境变量:
source /usr/local/Ascend/ascend-toolkit/set_env.shpip install torch>=2.1.0 torch-npu>=2.1.0
pip install transformers>=4.57.6 accelerate>=0.25.0
pip install librosa>=0.10.0 soundfile>=0.12.0
pip install numpy>=1.24.0 scipy>=1.10.0
# 可选:用于从 HuggingFace 下载模型
pip install huggingface-hub>=0.20.0注意:
transformers>=4.57.6为必须;更早版本可能缺少granite_speech_plusmodel type 支持。
bash verify_env.sh预期全部 PASS。若 FAIL,根据提示安装缺失依赖。
python3 -c "
from huggingface_hub import snapshot_download
snapshot_download('ibm-granite/granite-speech-4.1-2b-plus', local_dir='./granite-speech-4.1-2b-plus')
"pip install modelscope
python3 -c "
from modelscope import snapshot_download
snapshot_download('ibm-granite/granite-speech-4.1-2b-plus', cache_dir='./granite-speech-4.1-2b-plus')
"若在生产环境,建议提前下载完整权重目录,运行时直接指定本地路径:
export MODEL_PATH="/path/to/granite-speech-4.1-2b-plus"python adapt_npu.py \
--model ibm-granite/granite-speech-4.1-2b-plus \
--audio sample_16khz_mono.wav \
--prompt "<|audio|> Transcribe this audio." \
--max-new-tokens 200python adapt_npu.py \
--model ibm-granite/granite-speech-4.1-2b-plus \
--audio sample_en_16khz_mono.wav \
--prompt "<|audio|> Translate the audio to Chinese." \
--max-new-tokens 200# 准备文件列表
cat > audios.txt <<EOF
audio1.wav
audio2.wav
audio3.wav
EOF
cat > prompts.txt <<EOF
<|audio|> Transcribe this audio.
<|audio|> Transcribe this audio.
<|audio|> Summarize the audio.
EOF
python adapt_npu.py \
--model ibm-granite/granite-speech-4.1-2b-plus \
--batch \
--audio-list audios.txt \
--prompt-list prompts.txt \
--max-new-tokens 200| 参数 | 默认值 | 说明 |
|---|---|---|
--model | ibm-granite/granite-speech-4.1-2b-plus | 模型 ID 或本地路径 |
--audio | 必填 | 输入音频文件路径 |
--prompt | `< | audio |
--max-new-tokens | 200 | 最大生成 token 数 |
--device | 自动检测 | npu / cuda / cpu |
--dtype | bfloat16 | bfloat16(推荐) / float16 / float32 |
--batch | false | 启用批量推理模式 |
bash verify_env.shpython verify_functionality.py --report ./functionality_report.json验证项:
| 测试项 | 说明 |
|---|---|
| model_load | 模型加载到 NPU 并执行前向传播 |
| asr | 英文语音自动语音识别 |
| translation | 语音翻译(EN -> ZH) |
| cat_hidden_layers | Plus 版特有:encoder layer 3 hidden states concat 验证 |
# 实际运行(需 NPU 硬件)
python verify_perf.py --seq-lens 32 64 128 --iterations 50 --report ./perf_report.json
# 仅查看参考数据模板(无硬件时)
python verify_perf.py --skip-run --report ./perf_report.json# 实际运行(需 NPU 硬件,双路对比 NPU bfloat16 vs CPU float32)
python verify_precision.py --report ./precision_report.json
# 仅查看参考数据
python verify_precision.py --skip-run --report ./precision_report.json社区验证数据(2026-05-10):
| 生成序列长度 | 平均延迟 (ms) | 吞吐 (tok/s) | VRAM 占用 (MB) |
|---|---|---|---|
| 32 | 149.24 | 214.42 | ~4096 |
| 64 | 134.99 | 474.11 | ~4096 |
| 128 | 139.29 | 918.97 | ~4096 |
注:2B 模型单卡即可运行,A2 的 8 卡主要用于 batch 推理提升吞吐。
| 指标 | 数值 |
|---|---|
| Cosine Similarity | > 0.9999 |
| 相对误差 < 1% 的元素占比 | 96.8% |
| 维度 | GPU (CUDA) | 昇腾 NPU |
|---|---|---|
| 推理栈 | vLLM / Transformers | Transformers 原生(当前) |
| 精度 | bfloat16 / float16 | bfloat16(推荐) |
| 音频预处理 | 相同(16kHz mono -> log-mel) | 相同 |
| 特殊注意 | 无 | transformers>=4.57.6、`< |
| 现象 | 原因 | 解决方案 |
|---|---|---|
ImportError: No module named 'torch_npu' | torch_npu 未安装或 CANN 环境未激活 | 安装 torch_npu 并 source /usr/local/Ascend/ascend-toolkit/set_env.sh |
transformers 报 unknown model type: granite_speech_plus | transformers 版本过低 | 升级至 transformers>=4.57.6 |
| 模型输出为空或乱码 | prompt 缺少 `< | audio |
| NPU OOM | batch 过大或序列过长 | 减小 batch size 或 --max-new-tokens;2B 模型单卡 4GB 左右 |
| 精度与 CPU float32 差异大 | 使用了 float16 而非 bfloat16 | 推荐 --dtype bfloat16 |
| 下载模型超时 | 网络问题 | 使用 ModelScope 镜像或预下载后指定本地路径 |
| 说明 | 链接 |
|---|---|
| HuggingFace 模型卡 | ibm-granite/granite-speech-4.1-2b-plus |
| IBM Granite Speech 官方文档 | ibm.com/granite/docs/models/speech |
| Granite 4.1 发布博客 | research.ibm.com/blog/granite-4-1-ai-foundation-models |
| 昇腾社区 | hiascend.com |
| 本模型族总索引 | granite-speech/README.md |
模型权重、transformers 版本与启动参数随官方发布更新,若与官方文档冲突,以官方为准。