whisper-bemba-stt — 基于 Whisper-small(OpenAI Whisper 架构,约 244M 参数) 在 Bemba 语(赞比亚主要语言)语音数据上微调得到的语音转写(ASR)模型。 本项目将其从通用 PyTorch 环境迁移到华为昇腾 Ascend910 NPU,并基于 FastAPI 提供服务化推理(HTTP 接口)。
| 项目 | 内容 |
|---|---|
| 模型 | whisper-bemba-stt(WhisperForConditionalGeneration,whisper-small 尺寸) |
| 输入 | 任意采样率单声道音频(自动重采样到 16kHz),路径或 base64 WAV |
| 输出 | 转写文本(JSON),含生成 token 数与延迟统计 |
| 硬件 | Ascend910(本环境 16 卡,部署使用 npu:0) |
| 软件 | CANN 8.5.1 / Python 3.11 / PyTorch 2.9.0 / torch_npu 2.9.0.post1 / transformers 5.9.0 |
| 推理服务 | FastAPI + uvicorn(HTTP 服务化推理) |
| 测试结果 | 5/5 用例通过,4.4s 语音转写延迟约 226ms |
whisper-bemba-stt-npu/
├── inference.py # 服务化推理脚本(FastAPI HTTP 服务)★ 核心交付
├── test_inference.py # 测试用例(5 项,HTTP 模式)
├── requirements.txt # 运行环境依赖清单
├── README.md # 本文档
├── test_results/
│ └── test_result.txt # 测试结果记录
└── assets/
├── test_speech.wav # 测试音频(4.4s 英语语音)
├── silence_2s.wav # 静音对照音频(2s 全零)
├── npu_device_call.png # 截图1:NPU 设备调用
├── model_result.png # 截图2:模型测试结果
├── agent_workflow.png # 截图3:适配过程工作流
└── _make_screenshots.py # 截图生成脚本(可复现)| 依赖 | 版本 | 说明 |
|---|---|---|
| 操作系统 | openEuler / Ubuntu(aarch64) | 本机 openEuler 2203 SP4 |
| 昇腾硬件 | Ascend910(≥1 卡) | bf16 权重约 2.5GB |
| CANN | ≥ 8.0(本机 8.5.1) | /usr/local/Ascend/ascend-toolkit/set_env.sh |
| Python | 3.11 | — |
| PyTorch | 2.9.0 | 与 torch_npu 版本配套 |
| torch_npu | 2.9.0.post1 | 昇腾 NPU 运行时插件 |
# 1) 加载 CANN 环境
source /usr/local/Ascend/ascend-toolkit/set_env.sh
# 2) 指定可见 NPU 卡
export ASCEND_RT_VISIBLE_DEVICES=0
# 3) 安装依赖(华为镜像源)
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
pip install -r requirements.txt
# 4) 校验 NPU 环境
npu-smi info
python -c "import torch, torch_npu; a=torch.randn(3,4).npu(); print(a+a)"Whisper 为标准的 encoder-decoder transformer(纯 PyTorch 算子,无需 CUDA 内核改写):
音频 (16kHz) → Log-Mel 特征 (80 bins) → 12层 Encoder → 12层 Decoder → 文本inference.py 顶部)import torch_npu
from torch_npu.contrib import transfer_to_nputransfer_to_npu 自动完成 CUDA API → NPU API 的映射。
| # | 适配点 | 说明 |
|---|---|---|
| 1 | bf16 权重 + bf16 输入 | 权重 torch_dtype=torch.bfloat16,输入 mel 特征也必须转 bf16,否则 Conv1d 报 Input type (float) and bias type (c10::BFloat16) |
| 2 | 采样率重采样 | Whisper 固定 16kHz 输入,非 16kHz 音频用 torchaudio.functional.resample 转换(否则 feature extractor 直接报错) |
| 3 | 语言 token | Whisper 语言列表无 Bemba(ValueError: Unsupported language: bem),该微调模型以 <|en|> token 训练,生成时固定 language="en" |
| 4 | use_cache | 加载后设置 model.config.use_cache = True(transformers 5.x 不接受 from_pretrained 的 use_cache 参数) |
| 5 | 静音幻觉 | 静音输入可能输出幻听词(如 "you"),属 Whisper 已知行为,测试用例按短输出判定 |
本项目不涉及 CUDA 自定义算子、DP/DDP 分布式改造、flash_attn 等三方 CUDA 库。
source /usr/local/Ascend/ascend-toolkit/set_env.sh
export ASCEND_RT_VISIBLE_DEVICES=0
python inference.py \
--model-path /mnt/old_data/whl/models/3/AbelZimba/whisper-bemba-stt \
--host 0.0.0.0 --port 8004启动日志:
[inference] Loading model (bf16) -> npu:0 ...
[inference] Model loaded in 0.5s, device=npu:0
INFO: Uvicorn running on http://0.0.0.0:8004| 方法 | 路径 | 说明 |
|---|---|---|
| GET | /health | 健康检查,返回设备/模型/精度信息 |
| POST | /infer | 语音转写(audio 为路径或 base64 WAV) |
| POST | /v1/audio/transcriptions | OpenAI 风格接口 |
请求参数:
{
"audio": "/path/to/audio.wav 或 data:audio/wav;base64,xxxx",
"language": "en",
"max_new_tokens": 224,
"max_seconds": 120.0
}健康检查:
curl -s http://127.0.0.1:8004/health
# {"status":"ok","model":"whisper-bemba-stt","architecture":"WhisperForConditionalGeneration (small)","device":"npu:0","dtype":"torch.bfloat16"}语音转写(Python 客户端):
import base64, requests
with open("assets/test_speech.wav", "rb") as f:
b64 = "data:audio/wav;base64," + base64.b64encode(f.read()).decode()
r = requests.post("http://127.0.0.1:8004/infer", json={"audio": b64})
print(r.json())返回示例:
{
"text": "Hello, this is the Chatterbox Speech Synthesis System running on Ascend and Pew.",
"language": "en",
"generated_tokens": 21,
"duration_s": 4.4,
"latency_ms": 225.94
}python test_inference.py --url http://127.0.0.1:8004用例覆盖:
| 用例 | 内容 | 结果 |
|---|---|---|
| T1 | 健康检查 /health,确认 device=npu:0 | ✅ PASS |
| T2 | 语音转写(4.4s 英语语音)→ 输出包含关键词 "Chatterbox" | ✅ PASS |
| T3 | 静音输入 → 输出为空或非常短 | ✅ PASS |
| T4 | 重复调用 3 次结果一致(稳定性) | ✅ PASS |
| T5 | OpenAI 风格接口 /v1/audio/transcriptions | ✅ PASS |
完整测试结果见 test_results/test_result.txt,汇总:5 通过 / 0 失败。
| 截图 | 文件 | 说明 |
|---|---|---|
| NPU 设备调用 | assets/npu_device_call.png | npu-smi info + torch_npu 探针 + 部署摘要 |
| 测试结果 | assets/model_result.png | 转写文本 + 延迟柱状图(RTF 0.05)+ 测试日志 |
| 适配过程 | assets/agent_workflow.png | 迁移适配工作流(10 步 playbook) |
截图可通过 python assets/_make_screenshots.py 重新生成(需服务运行中)。
| 问题 | 原因 | 解决方案 |
|---|---|---|
Input type (float) and bias type (c10::BFloat16) should be the same | 输入 mel 特征为 fp32 而权重 bf16 | inputs.input_features.to(device, dtype=torch.bfloat16) |
ValueError: ... sampled with 16000 and not 24000 | 输入采样率非 16kHz | 服务已自动 torchaudio 重采样 |
ValueError: Unsupported language: bem | Whisper 语言列表无 Bemba | 使用 language="en"(微调模型以 en token 训练) |
| 静音输入输出幻听词 | Whisper 已知行为(hallucination) | 属正常现象,按输出长度判定 |
No module named 'decorator' | torch_npu 运行时依赖缺失 | pip install decorator |
| 端口被占用 | 环境预装服务占用端口 | 换用空闲端口(如 8004) |