模型: MahmoudAshraf/mms-300m-1130-forced-aligner
架构: Wav2Vec2ForCTC
参数规模: 300M
昇腾适配版本: torch_npu 2.9.0.post1 + Ascend910
精度验证: ✅ 与CPU误差 < 1% (相对误差 9.27e-04)
性能加速: ✅ NPU vs CPU 约 265x
MMS-300M-1130-Forced-Aligner 是基于 Meta (Facebook) MMS (Massively Multilingual Speech) 项目中的 Wav2Vec2-XLS-R-300M 检查点,经过强制对齐(Forced Alignment)数据集微调后的模型。支持 100+ 语种的语音-文本对齐任务。
| 项目 | 参数 |
|---|---|
| 架构 | Wav2Vec2ForCTC |
| Hidden Size | 1024 |
| Attention Heads | 16 |
| Hidden Layers | 24 |
| Intermediate Size | 4096 |
| Vocab Size | 31 |
| 采样率 | 16000 Hz |
| 参数总量 | ~300M |
python >= 3.9
torch == 2.9.0+cpu
torch_npu == 2.9.0.post1
transformers >= 4.40.0
soundfile >= 0.12
numpy >= 1.24pip install torch==2.9.0+cpu --index-url https://download.pytorch.org/whl/cpu
pip install torch_npu==2.9.0.post1 # 根据昇腾版本选择
pip install transformers soundfile numpy# NPU 推理(自动选择NPU)
python inference.py \
--model_path . \
--audio sample.wav \
--transcript "hello world" \
--device npu \
--output alignment_result.json
# CPU 推理(用于对比验证)
python inference.py \
--model_path . \
--audio sample.wav \
--transcript "hello world" \
--device cpu \
--output alignment_result_cpu.json参数说明:
--model_path: 模型目录路径--audio: 输入音频文件 (WAV, 推荐 16kHz)--transcript: 需要对齐的文本--device: 推理设备 (npu / cuda / cpu)--dtype: 精度 (float32 / float16 / bfloat16)--output: 输出JSON文件路径# 同时评测 CPU 和 NPU
python evaluate.py \
--model_path . \
--audio sample.wav \
--devices cpu,npu \
--warmup 2 \
--runs 5 \
--output eval_result.json通过 torch_npu 的 transfer_to_npu 模块,原 CUDA 代码可自动映射到 NPU:
torch.cuda → torch.npu.cuda() → .npu()"cuda:0" → "npu:0"| 限制项 | 说明 | 解决方案 |
|---|---|---|
torch.compile | NPU backend 与 transformers Wav2Vec2 存在 dynamo/triton 兼容性问题 | 使用 eager mode 推理(已在代码中禁用 torch.compile) |
| 双精度类型 | NPU 不支持 torch.float64 | 使用 float32 推理 |
| 图编译 | torch.jit.script 被 transfer_to_npu 禁用 | 不影响 eager mode 推理 |
os.environ["PYTORCH_NPU_ALLOC_CONF"] = "expandable_segments:True"启用 expandable_segments 避免 NPU 内存碎片化问题。
以 CPU (float32) 为 Baseline,对比 NPU (float32) 的推理输出:
| 指标 | 数值 | 阈值 | 状态 |
|---|---|---|---|
| Relative Error (Frobenius) | 9.27e-04 | < 1% | ✅ PASS |
| Max Absolute Error | 3.55e-02 | - | ✅ |
| Mean Absolute Error | 7.62e-03 | - | ✅ |
| Elements within 1% | 99.97% | > 99% | ✅ PASS |
| Elements within 0.1% | 44.67% | - | ✅ |
结论: NPU 推理精度与 CPU 完全一致,满足误差 < 1% 的要求。
测试环境: Ascend910, 输入音频 5s, float32
| 设备 | 平均推理时间 | RTF | 吞吐量 |
|---|---|---|---|
| CPU (ARM, 单核) | 3.73s | 0.747 | 1.34x |
| NPU (Ascend910) | 0.014s | 0.003 | 353.9x |
| 加速比 | 265x | 249x | 264x |
RTF (Real-Time Factor): 处理1秒音频所需的时间。RTF < 1 表示可实时处理。
import torch
import torch_npu
from inference import run_inference
# 自动选择 NPU
result = run_inference(
model_path="./mms-300m-1130-forced-aligner",
audio_path="sample.wav",
transcript="hello world this is a test",
device_type="npu", # 或 "cpu" / "cuda"
dtype=torch.float32,
)
print(f"推理设备: {result['device']}")
print(f"推理耗时: {result['infer_time']:.3f}s")
print(f"RTF: {result['rtf']:.4f}")
for seg in result['segments']:
print(f"{seg['token']:12s} {seg['start_time']:7.3f}s - {seg['end_time']:7.3f}s")============================================================
Running inference on: npu
============================================================
[INFO] Loading model from: .
[INFO] Target device: npu, dtype: torch.float32
[INFO] Running in eager mode (torch.compile disabled for stability)
[TIME] Model load: 1.72s
[TIME] Audio load: 0.00s (duration: 5.00s)
[TIME] Inference: 0.22s (RTF: 0.044)
[TIME] Alignment: 3.21s
[RESULT] Aligned 41 segments
[INFO] Results saved to: alignment_result_npu.json
[ALIGNMENT RESULTS]
h 0.000s - 0.020s
e 0.020s - 0.040s
l 0.040s - 0.060s
l 0.060s - 0.080s
o 0.080s - 0.100s
...Q1: NPU 上运行报错 torch.compile 相关错误?
A: 本适配已自动禁用 torch.compile,使用 eager mode 运行。如仍报错,请检查 torch_npu 版本是否为 2.9.0.post1+。
Q2: 精度是否支持 float16 / bfloat16?
A: 当前验证使用 float32。float16 在部分算子上可能存在精度损失,建议生产环境使用 float32。
Q3: 多卡并行是否支持?
A: Wav2Vec2 单条音频推理轻量,单卡即可满足实时性需求。如需批量推理,可在外层用 Python 多进程分发。
Q4: 支持的最大音频长度?
A: 受 NPU 内存限制。Ascend910 (32GB HBM) 可支持约 30分钟 16kHz 音频。
| 文件 | 说明 |
|---|---|
inference.py | NPU 推理脚本 |
evaluate.py | 精度/性能评测脚本 |
eval_result.json | 评测结果 |
alignment_result_npu.json | NPU 对齐结果示例 |
alignment_result_cpu.json | CPU 对齐结果示例 |
readme.md | 本文档 |
适配日期: 2026-05-14
适配者: Ascend-SACT
标签: #昇腾NPU #Ascend910 #Wav2Vec2 #ForcedAlignment #torch_npu