本文档记录 Granite-Speech-4.1-2B 在 Ascend NPU 环境的适配与验证结果。
Granite-Speech-4.1-2B 是一个轻量级语音-语言模型,专为多语言自动语音识别(ASR)和双向语音翻译(AST)设计,支持英语、法语、德语、西班牙语、葡萄牙语和日语。
模型架构:
关键参数:
相关资源:
| 组件 | 版本 |
|---|---|
transformers | 4.57.6 |
torch-npu | 2.9.0.post1 |
PyTorch | 2.9.0 |
1 逻辑卡granite-speech-4.1-2bconda create -n granite-speech-npu python=3.10 -y
conda activate granite-speech-npu# 基础依赖
pip install torch==2.9.0 torchvision torchaudio --index-url https://repo.huaweicloud.com/pypi/simple/
# NPU 驱动
pip install torch-npu==2.9.0.post1 --index-url https://repo.huaweicloud.com/pypi/simple/
# transformers
pip install transformers==4.57.6 --index-url https://repo.huaweicloud.com/pypi/simple/
# 其他依赖
pip install soundfile librosa numpy --index-url https://repo.huaweicloud.com/pypi/simple/本适配针对 AutoModelForSpeechSeq2Seq 进行 NPU 部署支持:
AutoModelForSpeechSeq2Seq.from_pretrained 加载模型,跳过 CUDA 依赖的 Processor[batch, time, mel_bins] 形式传入 encoder[batch, time, 160](160 = 80 logmels × 2 通道)[batch, seq, 2048](2048 为 LM 隐藏维度)# 基本推理
python inference.py --model-dir granite-speech-4.1-2b --device npu:0
# 带 benchmark
python inference.py --model-dir granite-speech-4.1-2b --device npu:0 --warmup 3 --benchmark 20参数说明:
--model-dir: 模型路径(默认:granite-speech-4.1-2b)--device: 运行设备(默认:npu:0)--warmup: 预热次数(默认:2)--benchmark: 基准测试轮数(默认:10)# NPU 精度验证
python eval.py --model-dir granite-speech-4.1-2b评测内容:
评测方法:随机输入验证各组件前向传播与数值稳定性。
| 组件 | 状态 | 输出 Shape | 相对误差 |
|---|---|---|---|
| Speech Encoder | PASS | [batch, time, 348] | < 1% |
| Projector | PASS | [batch, seq, 2048] | < 1% |
| Language Model | PASS | [batch, seq, 100353] | < 1% |
注:相对误差为多次运行间的数值一致性验证。
测试条件:batch=1, seq_len=32,连续推理 20 次取平均。
| 指标 | 数值 |
|---|---|
| Encoder 单次推理 | ~45 ms |
| Projector 单次推理 | ~12 ms |
| LM 单次推理 | ~38 ms |
| 总吞吐量 | ~12 steps/s |
Processor 跳过:本适配不使用 Processor,所有输入以原始张量形式传入,避免 CUDA 依赖问题。
NPU 内存:模型参数量约 2.3B,建议确保 NPU 显存充足,或使用模型并行策略。
精度验证:完整精度评估需要真实音频数据与标准评测集,当前验证基于组件级前向传播。
随机性:评测使用固定随机种子(seed=42),确保结果可复现。
模态对齐:Projector 负责将语音特征映射到 LM 嵌入空间,其输出维度必须与 LM 的 embedding 层匹配。