本文档记录 granite-speech-4.1-2b 在 Ascend NPU 环境的适配与验证结果。
granite-speech-4.1-2b 是 IBM 开源的语音理解模型,基于 conformer 音频编码器 + QFormer 投影层 + Granite-4.0-1B-base 语言模型。该模型接收音频与文本输入,可执行语音识别、语音理解等任务。
相关获取地址:
| 组件 | 版本 |
|---|---|
transformers | 4.57.6 |
torch | 2.9.0 |
torch-npu | 2.9.0.post1+gitee7ba04 |
CANN | 8.5.1 |
Ascend 910B4 x 1 逻辑卡/opt/atomgit/granite-speech-4.1-2b2,313Mexport PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
export TASK_QUEUE_ENABLE=1import torch
import torch_npu
from transformers.models.granite_speech import GraniteSpeechForConditionalGeneration
from transformers import AutoProcessor
model_path = "/path/to/granite-speech-4.1-2b"
model = GraniteSpeechForConditionalGeneration.from_pretrained(
model_path, dtype=torch.bfloat16, low_cpu_mem_usage=True,
).npu()
model.eval()
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)python3 inference.py --model-path /path/to/granite-speech-4.1-2b \
--audio-path sample.wav \
--text "<|audio|> Transcribe the audio." \
--max-new-tokens 128python3 benchmark_granite_speech.py| 验证项 | 状态 |
|---|---|
| 模型加载 NPU | ✅ |
| Forward 前向传播 | ✅ |
| Generate 文本生成 | ✅ |
| 权重加载正确 | ✅ |
测试条件:Ascend 910B4 x 1,bfloat16 精度,50 轮平均。
| 序列长度 | 平均耗时 (ms) | 中位数 (ms) | P99 (ms) | Throughput (tok/s) |
|---|---|---|---|---|
| 32 | 134.01 | 131.61 | 167.60 | 238.78 |
| 64 | 139.03 | 136.05 | 164.99 | 460.32 |
| 128 | 136.33 | 134.23 | 160.79 | 938.89 |
显存占用:~4.4 GB(batch_size=1, bfloat16)
NPU (bfloat16) vs CPU (float32) 同输入同权重对比:
| 指标 | 数值 |
|---|---|
| 余弦相似度 | > 0.9999 |
| 平均绝对误差 | 0.030 |
| 最大绝对误差 | 0.401 |
精度达标,NPU bfloat16 推理结果与 CPU float32 参考结果一致。
transformers 原生推理,无需 vLLM-Ascend16kHz 单声道 WAV 格式<|audio|> 标记指定音频嵌入插入位置bfloat16 精度以平衡性能与精度