K
KevinLi11/cohere-transcribe-03-2026
模型介绍
文件和版本
Pull Requests
讨论
分析

Cohere Transcribe 03-2026 (LLM-ASR) — Ascend NPU 适配

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 项指标对比验证。

1. 模型简介

  • 模型:CohereLabs/cohere-transcribe-03-2026(约 3.9 GB,bf16)
  • 架构:Conformer 编码器(48 层,d_model=1280)+ Transformer 解码器(8 层, hidden=1024),输出头 16384 类(BPE 词表),CohereAsrForConditionalGeneration
  • 输入:16 kHz 单声道 PCM 音频 → 128 维 filterbank 特征(CohereAsrProcessor)
  • 输出:转录文本(支持 en/fr/de/es/it/pt/nl/pl/el/ar/ja/zh/vi/ko 等语言)
  • 加载方式:trust_remote_code=True,权重为原生 HuggingFace 格式 (model.safetensors),无需转换
  • 说明:config.json 的 auto_map 将 AutoModel 映射到无输出头的 CohereAsrModel,转写必须使用 AutoModelForSpeechSeq2Seq(映射到 CohereAsrForConditionalGeneration,包含 generate() 覆盖与 log-softmax 头)。 模型文件内无 torch.cuda 直接依赖,适配点仅限设备迁移与 bf16 精度。

2. 部署步骤

2.1 环境

组件版本
Python3.11.14
PyTorch2.9.0+cpu
torch_npu2.9.0.post1
transformers4.57.6
CANN8.5.1
硬件Ascend 910 x2(本适配使用 npu:0)

2.2 安装

pip install -r requirements.txt

权重目录 /data/dl/cohere/(含 configuration_cohere_asr.py、 modeling_cohere_asr.py、processing_cohere_asr.py、model.safetensors 等)。

2.3 运行

# NPU 推理
python inference.py --device npu --seed 42

# CPU 推理(对比基线)
python inference.py --device cpu --seed 42

# 6 项指标对比
python compare.py

3. 推理示例

inference.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。

4. 实测 6 指标对比表

同输入(seed=42,16 kHz 3 s 音频)、同 bf16 精度,NPU 与 CPU 各推理 3 次取平均:

指标NPUCPU说明
平均耗时(3 次)0.324 s6.481 sgenerate() 端到端,含同步
耗时比(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 数值精度对齐良好。