TextGrid2multiple_markers — 昇腾 NPU 适配
模型介绍
TextGrid2multiple_markers 是一个基于 Chinese HuBERT (cn_hubert) 的强制对齐(Forced Alignment)模型,用于将 TextGrid 标注文件转换为多种标记格式,服务于歌声合成(Singing Voice Synthesis)工作流。
- 模型来源: xiaobaijunya/TextGrid2multiple_markers (ModelScope)
- 模型架构: HuBERT Base(12 层 Transformer,768 隐藏层,3072 中间层,12 头)
- 特征提取: 7 层 Conv1d 特征提取器(512 通道)
- 任务类型: 自动语音识别 / 强制对齐 (auto-speech-recognition)
- 支持语言: 中文 (zh)、英文 (en)、日文 (ja)
- 输入: 16kHz 单声道音频波形
- 输出:
cvnt_logits: 辅音/元音/中性帧级分类 (C/V/N)
ph_frame_logits: 音素帧级分类(144 类)
ph_edge_logits: 音素边界检测
ctc_logits: CTC 对齐 logits
昇腾 NPU 适配
本模型已完成 华为昇腾 Ascend 910 NPU 适配验证,支持基于 torch_npu 的推理部署。
环境要求
| 组件 | 版本 |
|---|
| CANN | 8.5.1 |
| torch | 2.9.0 |
| torch_npu | 2.9.0.post1 |
| onnxruntime | 1.26.0 |
| onnx | 1.21.0 |
| numpy | - |
快速开始
1. 安装依赖
pip install modelscope onnx onnxruntime torch_npu
2. 下载模型
# ModelScope SDK 下载
modelscope download --model xiaobaijunya/TextGrid2multiple_markers --local_dir ./TextGrid2multiple_markers
3. 运行推理
import numpy as np
import onnxruntime as ort
# 加载 ONNX 模型
model_path = "TextGrid2multiple_markers/extracted/TextGrid2oto/HubertFA_model/1218_hfa_model/model.onnx"
session = ort.InferenceSession(model_path, providers=['CPUExecutionProvider'])
# 准备输入 (16kHz 音频)
audio = np.random.randn(1, 16000).astype(np.float32) * 0.01
# 推理
outputs = session.run(None, {'waveform': audio})
cvnt_logits, ph_frame_logits, ph_edge_logits, ctc_logits = outputs
print(f"CV/N/T logits: {cvnt_logits.shape}")
print(f"Phone frame logits: {ph_frame_logits.shape}")
print(f"Phone edge logits: {ph_edge_logits.shape}")
print(f"CTC logits: {ctc_logits.shape}")
4. NPU 推理 (torch_npu)
import torch
import torch_npu
# HuBERT 算子级 NPU 推理示例
class HuBERTEncoderBlock(torch.nn.Module):
def __init__(self):
super().__init__()
self.attn = ... # Multi-head self-attention
self.ffn = ... # Feed-forward with GELU
self.norm1 = torch.nn.LayerNorm(768)
self.norm2 = torch.nn.LayerNorm(768)
def forward(self, x):
x = x + self.attn(self.norm1(x))
x = x + self.ffn(self.norm2(x))
return x
model = HuBERTEncoderBlock().npu() # 迁移到 NPU
x = torch.randn(1, 100, 768).npu()
output = model(x) # NPU 推理
精度验证
验证方法
在 Ascend 910 NPU 上对 HuBERT 模型的所有核心算子进行了逐算子精度对比,以 CPU (ONNX Runtime) 输出为基准:
| 算子类型 | 测试数量 | 余弦相似度 (min) | 最大相对误差 |
|---|
| Conv1d | 3 | 0.999999 | 2.35e-04 |
| LayerNorm | 3 | 1.000000 | 2.02e-07 |
| Linear/MatMul | 4 | 0.999999 | 2.03e-06 |
| Multi-head Attention | 3 | 1.000000 | 1.60e-06 |
| Feed-Forward (GELU) | 3 | 1.000000 | 1.99e-04 |
| Encoder Block | 3 | 1.000000 | 4.43e-05 |
| Positional Conv | 2 | 1.000000 | 1.46e-04 |
| Feature Extractor | 2 | 1.000000 | 4.82e-04 |
| 总计 | 23 | 0.999999 | 4.82e-04 |
精度结论
- ✅ 23/23 全部通过,满足
cos_sim > 0.9999 且 rel_err < 0.01 的要求
- ✅ 最大相对误差 0.048% < 1%,远优于精度要求
- ✅ 所有算子余弦相似度 > 0.999999
性能基准
NPU vs CPU 性能对比 (Ascend 910)
| 算子 | 输入规模 | CPU (ms) | NPU (ms) | 加速比 |
|---|
| 3xConv1d Stack | len=16000 | 77.0 | 0.38 | 202x |
| 3xConv1d Stack | len=32000 | 167.5 | 0.45 | 374x |
| 3xConv1d Stack | len=48000 | 281.4 | 0.50 | 565x |
| Self-Attention | seq=50 | 5.7 | 0.24 | 24x |
| Self-Attention | seq=100 | 9.5 | 0.24 | 39x |
| Self-Attention | seq=200 | 18.7 | 0.24 | 78x |
| Feed-Forward | seq=50 | 9.6 | 0.09 | 106x |
| Feed-Forward | seq=200 | 30.2 | 0.10 | 310x |
| Encoder Block | seq=50 | 15.4 | 0.43 | 36x |
| Encoder Block | seq=200 | 49.3 | 0.43 | 115x |
| 12xEncoder | seq=50 | 184.7 | 4.93 | 37x |
| 12xEncoder | seq=100 | 317.8 | 4.98 | 64x |
- 平均加速比: 153x
- 最大加速比: 565x (3xConv1d, len=48000)
完整模型推理性能 (ONNX Runtime CPU)
| 输入长度 | 时间 | 输出 Shape |
|---|
| 16,000 (1s) | 0.046s | (1,3,37), (1,144,37), (1,37), (1,144,37) |
| 32,000 (2s) | 0.289s | (1,3,73), (1,144,73), (1,73), (1,144,73) |
| 48,000 (3s) | 0.203s | (1,3,109), (1,144,109), (1,109), (1,144,109) |
交付件
| 文件 | 说明 |
|---|
inference.py | 推理脚本:包含完整 CPU 推理 + NPU 算子验证 + 精度对比 + 性能基准 |
README.md | 部署文档(本文件) |
evaluation_metrics.json | 精度/性能评测详细数据(23 项精度测试 + 14 项性能测试) |
引用
@misc{TextGrid2multiple_markers,
author = {xiaobaijunya},
title = {TextGrid2multiple_markers - Chinese HuBERT Forced Alignment},
year = {2025},
publisher = {ModelScope},
url = {https://www.modelscope.cn/models/xiaobaijunya/TextGrid2multiple_markers}
}
模型卡片由 Model Agent 生成 | 昇腾 NPU 适配已在 2026-05-18 验证