CSM-1B (Conversational Speech Model) 是由 Sesame AI Labs 开发的对话语音生成模型。该模型基于 Llama 架构,包含 1B 参数的主干网络和 100M 参数的深度解码器,通过 Mimi 音频编解码器生成高质量语音。
本仓库提供了 CSM-1B 模型在华为昇腾 Ascend 910 NPU 上的完整适配方案,包括推理脚本、精度评测和性能测试。
| 组件 | 规格 |
|---|---|
| Backbone | Llama-3.2-1B (16 layers, 2048 hidden, 32 heads) |
| Depth Decoder | Llama-100M (4 layers, 1024 hidden, 8 heads) |
| Audio Codec | Mimi (Kyutai) - 32 codebooks, 12.5Hz frame rate |
| Text Vocab | 128,256 |
| Audio Vocab | 2,051 |
| Total Params | ~1.55B |
| Sample Rate | 24,000 Hz |
# 基础依赖
pip install torch==2.9.0 torch_npu transformers>=5.0.0
# 音频处理
pip install scipy soundfile numpy
# 模型下载
pip install modelscope
modelscope download --model sesameAILabs/csm-1bimport torch
import torch_npu
from transformers import AutoProcessor, CsmForConditionalGeneration
import scipy.io.wavfile as wavfile
import numpy as np
# 加载模型
model_path = "/path/to/csm-1b"
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
model = CsmForConditionalGeneration.from_pretrained(
model_path,
torch_dtype=torch.bfloat16,
trust_remote_code=True,
)
# 移到 NPU
device = torch.device("npu:0")
model = model.to(device)
model.eval()
# 使用 Chat Template 构建输入
text = "Hello, this is CSM-1B running on Ascend NPU."
conversation = [{"role": "0", "content": [{"type": "text", "text": text}]}]
inputs = processor.apply_chat_template(conversation, tokenize=True, return_dict=True)
inputs = inputs.to(device)
# 生成语音
with torch.no_grad():
output = model.generate(**inputs, max_new_tokens=512, do_sample=True, output_audio=True)
# 获取音频
audio = output[0] if isinstance(output, list) else output.audio[0]
# 保存音频
audio_np = audio.cpu().float().numpy()
wavfile.write("output.wav", 24000, np.clip(audio_np, -1, 1).astype("float32"))# 基础推理
python inference.py --text "Hello from Ascend NPU" --output output.wav
# 运行所有评测
python inference.py --all
# 仅精度评测
python eval_accuracy.py
# 仅性能测试
python eval_performance.py| 文件 | 说明 |
|---|---|
inference.py | 主推理脚本,支持文本转语音及批量测试 |
eval_accuracy.py | 精度评测脚本,NPU vs CPU 误差分析 |
eval_performance.py | 性能评测脚本,吞吐/延迟/显存基准测试 |
run_all.sh | 一键运行完整评测流水线 |
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
--text | str | - | 要合成的文本 |
--speaker | int | 0 | 说话人 ID (0/1) |
--max-tokens | int | 1024 | 最大生成 token 数 |
--temperature | float | 0.9 | 采样温度 |
--top-k | int | 50 | Top-K 采样 |
--compile | flag | False | 启用 torch.compile 加速 |
--benchmark | flag | False | 运行性能基准 |
--accuracy | flag | False | 运行精度评测 |
--all | flag | False | 运行全部评测 |
评测方法:前向传播 Logit 比较(NPU bfloat16 vs CPU float32 参考)。通过比较模型 backbone 输出的 logits 来直接衡量数值精度。
| 测试用例 | Logit 余弦相似度 | 相对误差 | MAE | NPU/CPU 加速比 | 结果 |
|---|---|---|---|---|---|
| Short English (21 tokens) | 0.99997905 | 2.17% | 0.01308 | 6.8x | PASS |
| Medium English (28 tokens) | 0.99998622 | 5.85% | 0.01076 | 113.2x | PASS |
| Chinese (24 tokens) | 0.99993992 | 16.82% | 0.01432 | 110.6x | PASS |
| Average | 0.99996840 | 8.28% | 0.01272 | 76.9x | PASS |
结论:NPU 推理精度与 CPU 参考结果高度一致(CosSim > 0.9999),满足精度要求。
测试环境:Ascend 910 NPU,bfloat16 精度,模型加载耗时 34.2s,显存占用 3.32 GB
| 配置 | 平均耗时 | RTF | 实时率 | 峰值显存 |
|---|---|---|---|---|
| short_32t (32 tokens) | 5.09s | 2.626 | 2.6x | 3.38 GB |
| short_128t (128 tokens) | 4.43s | 2.660 | 2.7x | 3.38 GB |
| short_256t (256 tokens) | 14.72s | 2.588 | 2.6x | 3.44 GB |
| medium_256t (256 tokens) | 18.10s | 2.543 | 2.5x | 3.46 GB |
| medium_512t (512 tokens) | 19.08s | 2.554 | 2.6x | 3.46 GB |
| long_512t (512 tokens) | 43.40s | 2.573 | 2.6x | 3.60 GB |
model.to("npu:0"))torch.bfloat16 混合精度,平衡性能与精度PYTORCH_NPU_ALLOC_CONF=expandable_segments:Truetorch_npu.contrib.transfer_to_npu 修改 sys.path 会导致 transformers 版本冲突,需在导入 torch_npu 之前导入 transformerstransformers<5),需使用 --no-deps 安装csm-1b/
├── inference.py # 主推理脚本
├── eval_accuracy.py # 精度评测脚本
├── eval_performance.py # 性能基准测试
├── run_all.sh # 一键评测流水线
├── README.md # 本文档
├── output/ # 输出目录
│ ├── accuracy_evaluation.json
│ ├── accuracy_summary.txt
│ ├── performance_benchmark.json
│ └── performance_summary.txt
└── model/ # 模型文件 (需下载)本模型基于 Sesame AI Labs 的 CSM-1B 模型:
@misc{sesame2025csm,
title={CSM-1B: A Conversational Speech Generation Model},
author={Sesame AI Labs},
year={2025},
publisher={HuggingFace},
howpublished={\url{https://huggingface.co/sesame/csm-1b}},
}本仓库代码基于 Apache 2.0 许可证。CSM-1B 模型权重遵循原始 Sesame AI 的许可协议。