ohhoohho/Voxtral-Mini-4B-Realtime-2602
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Voxtral-Mini-4B-Realtime-2602 — Ascend NPU 适配

硬件平台: 华为昇腾 Ascend NPU (910B/910C)
推理框架: torch_npu 2.9.0.post1 + transformers 4.57.6
模型来源: mistralai/Voxtral-Mini-4B-Realtime-2602
适配仓库: ohhoohho/Voxtral-Mini-4B-Realtime-2602

模型简介

Voxtral-Mini-4B-Realtime-2602 是 Mistral 推出的实时语音转文字(Speech-to-Text)模型,参数量约 4B。模型架构包含:

  • 音频编码器(VoxtralRealtimeEncoder):32 层 Whisper 风格 Transformer,将 log-mel 频谱图编码为音频特征
  • 多模态投影器(VoxtralRealtimeMultiModalProjector):将音频特征投影到文本空间
  • 文本解码器(VoxtralRealtimeTextModel):26 层 Mistral 风格 Transformer(GQA,8 KV heads),带滑动窗口注意力和时序嵌入
  • LM Head:映射到 131072 词汇表

特点:

  • 支持实时流式语音识别
  • 使用 Tekken tokenizer(128k 词汇)
  • 输入 16kHz 单声道音频,输出文本

环境要求

组件版本
Python3.11
torch_npu2.9.0.post1
CANN8.5.1
transformers4.57.6
mistral_common1.11.1
soundfile≥0.13
safetensors≥0.4

项目结构

Voxtral-Mini-4B-Realtime-2602/
├── inference.py              # 推理脚本(主入口)
├── README.md                 # 本文档
├── requirements.txt          # 环境依赖
├── voxtral_realtime/         # Vendored 模型源码包
│   ├── __init__.py
│   ├── configuration_voxtral_realtime.py
│   ├── modeling_voxtral_realtime.py
│   ├── feature_extraction_voxtral_realtime.py
│   └── processing_voxtral_realtime.py
└── assets/                   # 截图素材
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png

快速开始

1. 安装依赖

pip install -r requirements.txt

2. 下载权重并运行推理

# 自动下载权重并运行推理(使用内置测试音频)
python inference.py

# 使用自定义音频文件
python inference.py --audio /path/to/your/audio.wav --max-tokens 256

# 仅下载权重
python inference.py --download-only

3. 推理示例输出

[INFO] NPU available: True, device=npu:0
[INFO] Downloading weights from https://hf-mirror.com/mistralai/Voxtral-Mini-4B-Realtime-2602 ...
[INFO] Creating model (vocab=131072, text_layers=26, audio_layers=32) ...
[INFO] Loaded 268 weight tensors, loading into model ...
[INFO] Weights loaded successfully
[INFO] Extracting mel features ...
[INFO] Running inference ...
[INFO] Generated 42 tokens in 2.35s (17.9 tok/s)
================================================================
[OUTPUT] <transcribed text from audio>
================================================================
[NPU] Device: Ascend 910B
[NPU] Memory: 62.0 GB
[NPU] Cached: 8.2 GB | Allocated: 7.6 GB

权重下载

权重从 HuggingFace Mirror 自动下载,本地缓存至 /tmp/models/Voxtral-Mini-4B-Realtime-2602/。

支持两种下载通道:

  1. HF Mirror(默认):https://hf-mirror.com,通过 huggingface_hub.snapshot_download
  2. GitCode 镜像(回退):https://gitcode.com/hf_mirrors/mistralai/Voxtral-Mini-4B-Realtime-2602

模型架构适配说明

适配策略

由于 transformers 4.57.6 未内置 VoxtralRealtimeForConditionalGeneration 架构,本项目采用 vendor 源码 策略:

  1. 从 transformers 主线 复制模型源码文件
  2. 将相对 import 改写为 transformers.xxx 绝对 import
  3. 移除 @auto_docstring 装饰器(仅在 transformers 5.x 中可用)
  4. 将 PretrainedConfig 的 __post_init__ 改为 __init__(transformers 4.57.6 的 PretrainedConfig 非 dataclass)
  5. 移除 @use_kernelized_func 装饰器(4.57.6 无此功能)
  6. 替换 maybe_autocast → torch.autocast
  7. 替换 merge_with_config_defaults / capture_outputs(非必需装饰器)

算子兼容性

算子类型昇腾兼容性备注
PyTorch 原生算子✅ 全兼容主要运算均为原生 torch 操作
RoPE 旋转位置编码✅ 兼容使用 eager 实现
滑动窗口注意力✅ 兼容使用 eager 注意力后端
因果卷积(Conv1d)✅ 兼容Conv1d 在 NPU 上原生支持
RMSNorm✅ 兼容纯 PyTorch 实现
STFT/Mel 频谱✅ 兼容使用 torch.stft,在 CPU 上提取

性能参考

以下为初步测试数据,实际性能取决于硬件配置和负载。

指标数值
模型参数量~4B
推理精度bfloat16
生成速度~15-20 tok/s (NPU 910B)
峰值显存~8-10 GB

注意事项

  1. 流式模式:当前推理脚本仅支持非流式(整段音频)模式。流式推理需使用 VoxtralRealtimeProcessor 和 padding_cache 机制。
  2. 音频格式:输入音频需为 16kHz 单声道 WAV/FLAC 格式。
  3. NPU 资源:首次加载模型时需约 8-10GB HBM,建议确保 NPU 有足够空闲显存。
  4. 权重缓存:若需清理缓存,删除 /tmp/models/Voxtral-Mini-4B-Realtime-2602/ 目录。
  5. Eager 注意力:NPU 上使用 eager 注意力后端(attn_implementation="eager"),避免 flash attention 兼容性问题。

License

Apache 2.0