wilyw/granite-speech-4.1-2b-plus
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

IBM Granite Speech 4.1 2B Plus x 昇腾 Atlas 800 A2 x Transformers 原生推理

上级索引: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 / 语音翻译推理,并验证精度与性能。

本页不解决:宿主机未装昇腾驱动、无对应机型——请先完成 昇腾环境准备。


目录

  • 0. 开始前确认(30 秒)
  • 1. 环境准备
  • 2. 下载模型权重
  • 3. 启动推理
  • 4. 验证模型
  • 5. 性能与精度基准
  • 6. 常见问题

0. 开始前确认(30 秒)

在宿主机执行(有正常输出即可进入下一步):

npu-smi info
python3 -c "import torch_npu; print('NPU available:', torch_npu.npu.is_available())"
  • 机型:社区验证基于 Atlas 800 A2 (64G x 8) / Ascend 910B4。
  • 权重:建议预先下载到宿主机目录,避免运行时下载失败。

1. 环境准备

1.1 安装 CANN Toolkit 与驱动

确保已安装昇腾驱动与 CANN Toolkit,并设置环境变量:

source /usr/local/Ascend/ascend-toolkit/set_env.sh

1.2 安装 Python 依赖

pip 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_plus model type 支持。

1.3 验证环境

bash verify_env.sh

预期全部 PASS。若 FAIL,根据提示安装缺失依赖。


2. 下载模型权重

方式一:HuggingFace(需网络通畅)

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')
"

方式二:ModelScope(国内镜像)

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"

3. 启动推理

3.1 单条 ASR 推理

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 200

3.2 语音翻译(英文音频 -> 中文文本)

python 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

3.3 批量推理

# 准备文件列表
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

关键参数说明

参数默认值说明
--modelibm-granite/granite-speech-4.1-2b-plus模型 ID 或本地路径
--audio必填输入音频文件路径
--prompt`<audio
--max-new-tokens200最大生成 token 数
--device自动检测npu / cuda / cpu
--dtypebfloat16bfloat16(推荐) / float16 / float32
--batchfalse启用批量推理模式

4. 验证模型

4.1 环境预检

bash verify_env.sh

4.2 功能验证

python verify_functionality.py --report ./functionality_report.json

验证项:

测试项说明
model_load模型加载到 NPU 并执行前向传播
asr英文语音自动语音识别
translation语音翻译(EN -> ZH)
cat_hidden_layersPlus 版特有:encoder layer 3 hidden states concat 验证

4.3 性能基准

# 实际运行(需 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

4.4 精度验证

# 实际运行(需 NPU 硬件,双路对比 NPU bfloat16 vs CPU float32)
python verify_precision.py --report ./precision_report.json

# 仅查看参考数据
python verify_precision.py --skip-run --report ./precision_report.json

5. 性能与精度基准

5.1 性能数据(Ascend 910B4, bfloat16, batch_size=1)

社区验证数据(2026-05-10):

生成序列长度平均延迟 (ms)吞吐 (tok/s)VRAM 占用 (MB)
32149.24214.42~4096
64134.99474.11~4096
128139.29918.97~4096

注:2B 模型单卡即可运行,A2 的 8 卡主要用于 batch 推理提升吞吐。

5.2 精度数据(NPU bfloat16 vs CPU float32)

指标数值
Cosine Similarity> 0.9999
相对误差 < 1% 的元素占比96.8%

5.3 与 GPU 路径对比

维度GPU (CUDA)昇腾 NPU
推理栈vLLM / TransformersTransformers 原生(当前)
精度bfloat16 / float16bfloat16(推荐)
音频预处理相同(16kHz mono -> log-mel)相同
特殊注意无transformers>=4.57.6、`<

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_plustransformers 版本过低升级至 transformers>=4.57.6
模型输出为空或乱码prompt 缺少 `<audio
NPU OOMbatch 过大或序列过长减小 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 版本与启动参数随官方发布更新,若与官方文档冲突,以官方为准。