CohereLabs 的 cohere-transcribe-03-2026 是一个基于 encoder-decoder Transformer 的 LLM-ASR
(大模型语音识别)模型,将音频帧转换为 16k 词表 token 序列。本仓库完成其在华为
Ascend 910 NPU 上的推理适配:通过 torch_npu + trust_remote_code 加载原始权重,
在 torch.bfloat16 下以 AutoModelForSpeechSeq2Seq + generate() 端到端转写,
并与 CPU 结果做 6 项指标对比验证。
CohereAsrForConditionalGenerationCohereAsrProcessor)trust_remote_code=True,权重为原生 HuggingFace 格式
(model.safetensors),无需转换config.json 的 auto_map 将 AutoModel 映射到无输出头的
CohereAsrModel,转写必须使用 AutoModelForSpeechSeq2Seq(映射到
CohereAsrForConditionalGeneration,包含 generate() 覆盖与 log-softmax 头)。
模型文件内无 torch.cuda 直接依赖,适配点仅限设备迁移与 bf16 精度。| 组件 | 版本 |
|---|---|
| Python | 3.11.14 |
| PyTorch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1 |
| transformers | 4.57.6 |
| CANN | 8.5.1 |
| 硬件 | Ascend 910 x2(本适配使用 npu:0) |
pip install -r requirements.txt权重目录 /data/dl/cohere/(含 configuration_cohere_asr.py、
modeling_cohere_asr.py、processing_cohere_asr.py、model.safetensors 等)。
# NPU 推理
python inference.py --device npu --seed 42
# CPU 推理(对比基线)
python inference.py --device cpu --seed 42
# 6 项指标对比
python compare.pyinference.py 合成确定性 16 kHz 单声道 WAV(440/880 Hz 正弦 + 高斯噪声,3 s,
同 seed 同输入),经 AutoProcessor 提取特征后调用 model.generate() 转写一次,
再补跑 2 次计时(共 3 次,取平均),并记录峰值显存(NPU)或峰值 RSS(CPU)。
NPU 实测输出(seed=42):
input_features shape=(1, 128, 301) dtype=torch.bfloat16
transcription output: [' نهايه الدرس']
generate run 1: 0.564 s
generate run 2: 0.205 s
generate run 3: 0.205 s
NPU peak memory: 4039.1 MB
mean generate time over 3 runs: 0.324 s输出产物:{device}_output.txt、{device}_run.log、{device}_logits.npy、
{device}_times.npy、sample_audio.wav。
同输入(seed=42,16 kHz 3 s 音频)、同 bf16 精度,NPU 与 CPU 各推理 3 次取平均:
| 指标 | NPU | CPU | 说明 |
|---|---|---|---|
| 平均耗时(3 次) | 0.324 s | 6.481 s | generate() 端到端,含同步 |
| 耗时比(NPU/CPU) | — | — | 0.0501(NPU 快约 20×) |
| cos_sim(logits flatten) | — | — | 0.999987 |
| max_abs(logits 最大绝对差) | — | — | 1.000000(个别 logit 尾部饱和,bf16 舍入) |
| mean_abs(logits 平均绝对差) | — | — | 0.119291 |
| 输出文本一致率 | — | — | 1.00(两设备均输出 " نهايه الدرس") |
| 编辑距离(归一化 Levenshtein) | — | — | 0.000000(文本完全一致) |
详细结果见 assets/compare_result.txt,图示见 assets/model_result.png。
注:max_abs=1.0 出现在 logits 饱和区(多个 token 分值相近且值较大时,bf16 相对误差在 1e-3 量级内放大),但 softmax 后解码路径一致:文本 100% 相同、 cos_sim=0.999987,说明 NPU 与 CPU 数值精度对齐良好。