c
zyzoe/voxtral-mini-4b-realtime
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

mistralai/Voxtral-Mini-4B-Realtime-2602 on Ascend NPU (torch_npu)

1. 简介

本文档记录 Mistral Voxtral-Mini-4B-Realtime-2602(多语种实时语音转写模型)在华为昇腾 NPU 上的适配与真机验证结果。

模型结构:VoxtralRealtimeForConditionalGeneration = 约 3.4B 文本 LLM(Ministral-3-3B-Base-2512 骨干,26 层、hidden 3072、GQA 32/8)+ 约 0.97B 因果音频编码器(32 层、sliding window 750),实测总参数量 4.430B,权重 bfloat16、8.86GB,任务类型 automatic-speech-recognition(流式转写)。

适配要点:

  • transformers 直载官方仓权重(VoxtralRealtimeForConditionalGeneration),需 transformers>=5.2.0(本环境实测 5.2.0)+ mistral-common(Tekken 分词器)
  • 引擎 torch_npu,直接 .to("npu:0") 即可
  • _attn_implementation="eager",避免 aclnnFlashAttentionScore 不支持(音频多模态先例 ultraVAD 同款处理)
  • 加载正确性三道门禁全部通过:output_loading_info 0/0/0、config 逐 key 审计 0 丢失、随机初始化对照决定性胜出(详见第 5 节)

相关获取地址:

  • 权重下载地址(HuggingFace):https://huggingface.co/mistralai/Voxtral-Mini-4B-Realtime-2602
  • 权重下载地址(ModelScope,本次实际使用之一):https://www.modelscope.cn/models/mistralai/Voxtral-Mini-4B-Realtime-2602
  • 测试音频数据集:https://huggingface.co/datasets/hf-internal-testing/librispeech_asr_dummy

2. 验证环境

组件版本
CANN8.5.1
torch2.9.0+cpu
torch-npu2.9.0
transformers5.2.0
mistral-common1.6.x
Python3.11.14
  • NPU:Ascend910_9362(Atlas 800I A2,npu-smi 25.5.5,65536 MB HBM/卡)
  • 推理引擎:torch_npu(eager attention,bfloat16)
  • 权重路径:/tmp/bigw/voxtral-mini-4b-realtime(约 8.86GB,model.safetensors 8859446848 字节已校验)
  • 任务类型:automatic-speech-recognition

3. 环境准备与权重获取

pip install -r requirements.txt          # 关键:transformers==5.2.0, mistral-common
npu-smi info | head -12                  # 确认 Ascend910 在位
python -c "import torch,torch_npu;print(torch.npu.is_available())"

权重获取(HF 镜像优先,Range 分块并行下载并校验总字节数 8859446848;镜像连接不稳定时 ModelScope 同样支持 Range 断点续传):

export HF_ENDPOINT=https://hf-mirror.com
python -c "from huggingface_hub import snapshot_download; print(snapshot_download('mistralai/Voxtral-Mini-4B-Realtime-2602', local_dir='/tmp/bigw/voxtral-mini-4b-realtime'))"
# 或 export VOXTRAL_MINI_4B_REALTIME_MODEL_DIR=<目录>,inference.py 内部自动解析

4. 推理运行

已验证通过的命令:

python inference.py --device npu           # 功能验证(三道门禁 + 真机转写)
python inference.py --device npu --perf    # 附加性能基准(5 次预热 + 20 次正式)

5. Smoke 验证

python inference.py --device npu 在昇腾 NPU 上的真实输出(LibriSpeech 真实语音样本,5.86s):

[env] torch=2.9.0+cpu device=Ascend910_9362
[env] transformers=5.2.0
[weights] /tmp/bigw/voxtral-mini-4b-realtime
[gate1] missing=0 unexpected=0 mismatched=0
[gate1] PASS, params=4.430B
[gate2] config audit: 0 dropped/mismatched keys []
[audio] source=librispeech_asr_dummy validation [sample0.wav] dur=5.86s
[npu] transcription (7.59s): ' Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel.'
[npu] reference        : 'MISTER QUILTER IS THE APOSTLE OF THE MIDDLE CLASSES AND WE ARE GLAD TO WELCOME HIS GOSPEL'
[gate3] first-step entropy loaded=0.0000 random=11.1772 (random-init should be near-uniform/max-entropy: ln(V)=11.78); argmax loaded=32 random=1279
[gate3] random-init transcription sample: 'alalalalalalalal visage'

验证结果:

  • 转写与参考文本完全一致(忽略大小写与标点后逐词全对,WER=0):"MISTER QUILTER IS THE APOSTLE OF THE MIDDLE CLASSES AND WE ARE GLAD TO WELCOME HIS GOSPEL" → "Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel."
  • 门禁 1(权重加载):missing=0 unexpected=0 mismatched=0,4.430B 参数全部从官方权重加载
  • 门禁 2(config 审计):仓内 config.json(含 audio_config/text_config 嵌套键)与 model.config 生效值逐一比对,0 丢失/偏差
  • 门禁 3(随机初始化对照):同架构随机初始化模型在相同音频上首步熵 11.1772(接近均匀分布上限 ln(131072)=11.78)、输出 'alalalalalalalal visage' 无语义;加载模型首步熵 0.0000(完全确定)且转写正确 —— 决定性胜出

6. 性能参考

测试条件:单卡 npu:0、bfloat16、eager attention、batch=1、5.86s 真实语音、max_new_tokens=64、预热 5 次 + 正式 20 次、每次 torch.npu.synchronize() 计时(flock /tmp/npu_card0_perf.lock 独占卡)。

指标数值
avg_ms5355 ms
p50_ms / p90_ms / p95_ms5349 / 5563 / 5628 ms
min_ms / max_ms5173 / 5628 ms
RTF(5.86s 音频)0.915
峰值 HBM(torch 分配)16.57 GiB

p95/p50 = 1.05,无并发污染双峰;首帧含算子编译的完整转写(max_new_tokens=256)为 7.59s。

7. 精度评测

指标数值
数据集hf-internal-testing/librispeech_asr_dummy(clean/validation)
样本数1(sample0.wav,5.86s,说话人无关朗读句)
评测方式NPU 真机转写 vs 数据集自带标注,忽略大小写/标点逐词比对
WER0.00(18/18 词全对)
随机初始化对照加载模型熵 0.0000 / 随机模型熵 11.1772(无语义输出)

8. 适配截图

  • agent workflow
  • npu device call
  • model result

9. 注意事项

直接 forward() 音频路径会因序列长度不匹配报 aclnnInplaceAdd 广播错误,必须走 generate()。

实际失败特征如下:

  • 现象:model(**processor_inputs) 直接前向报 NPU 算子错误
  • 关键报错:AclNN_Parameter_Error(EZ1001): 123 and 39 cannot broadcast(aclnnInplaceAdd failed, error code 161002)
  • 位置:modeling_voxtral_realtime.py::VoxtralRealtimeForConditionalGeneration.forward 中 inputs_embeds += audio_outputs.pooler_output

原因不是 NPU 算子缺失,而是 VoxtralRealtimeProcessor 产出的 input_ids(39 个延迟/prompt token)与音频嵌入 token 数(123)长度不一致,长度对齐在 prepare_inputs_for_generation 内部完成——只有 generate() 路径会做该对齐。

当前环境的可用处理方式:转写/对照实验一律用 model.generate(**inputs);熵对照用 generate(..., return_dict_in_generate=True, output_scores=True) 取首步 scores。

其余注意事项:

  1. transformers 版本硬门槛:VoxtralRealtime* 系列类自 5.2.0 才有;4.57.x 下 from transformers import VoxtralRealtimeForConditionalGeneration 直接 ImportError。requirements 已固定 transformers==5.2.0。
  2. 必须 eager attention:_attn_implementation="eager";该音频多模态架构在昇腾上直开默认 attention 会触发 aclnnFlashAttentionScore 不支持(同 ultraVAD 8.72B 先例)。
  3. 分词器依赖 mistral-common:processor 用 MistralCommonBackend 加载 tekken.json,缺包时 AutoProcessor.from_pretrained 报 requires(backends=("mistral-common",))。
  4. 下载校验:hf-mirror 对该仓单连接限速明显且偶发 Range 响应错位(重试块体积超限即为污染),务必分块下载并逐块校验 369143619 字节、总长 8859446848;ModelScope 同仓支持标准 Range 续传,可作补救源。

10. 标签

#NPU #Ascend