Facebook MusicGen-Small (300M) 模型在华为 Ascend NPU 上的部署与验证
| 组件 | 版本 |
|---|---|
| PyTorch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1+gitee7ba04 |
| CANN | 8.5.1 |
| NPU 设备 | Ascend910_9362 (61.3 GB) |
| Python | 3.11 |
# 安装依赖
pip install torch torch_npu transformers soundfile numpy
# 确认 NPU 可用
python -c "import torch; import torch_npu; print(f'NPU available: {torch.npu.is_available()}')"模型已预下载至 model_cache/ 目录。如需重新下载:
from transformers import MusicgenForConditionalGeneration, MusicgenProcessor
processor = MusicgenProcessor.from_pretrained("facebook/musicgen-small")
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")# 基本推理
python inference.py --prompt "lo-fi music with a soothing melody" --duration 5
# 自定义参数
python inference.py \
--prompt "your music description" \
--duration 8 \
--max_new_tokens 400 \
--guidance_scale 3.0 \
--temperature 1.0 \
--output output/my_music.wav参数说明:
--prompt: 音乐描述文本 (必填)--duration: 音频时长,单位秒 (默认 8s)--max_new_tokens: 最大生成 token 数 (默认 duration*50)--guidance_scale: Classifier-free guidance 强度 (默认 3.0)--temperature: 采样温度 (默认 1.0)--output: 输出文件路径 (默认 output.wav)生成 5 个不同风格的音频样本,验证 NPU 推理正常工作:
| 样本 | Prompt | 生成耗时 | 速度 (tokens/s) | 音频时长 | 音频范围 |
|---|---|---|---|---|---|
| 01 | lo-fi music with a soothing melody | 15.47s | 16.16 | 4.94s | [-0.60, 0.61] |
| 02 | a funky house with 80s hip hop vibes | 4.85s | 51.55 | 4.94s | [-0.82, 0.96] |
| 03 | a chill song with influences from lofi | 5.54s | 45.11 | 4.94s | [-0.27, 0.26] |
| 04 | 80s pop track with bassy drums and synth | 5.80s | 43.11 | 4.94s | [-0.55, 0.59] |
| 05 | a catchy beat for a podcast intro | 5.33s | 46.86 | 4.94s | [-0.47, 0.49] |
汇总统计:
| 配置 | 延迟 | 吞吐量 | 实时因子 | 峰值显存 |
|---|---|---|---|---|
| 5s 音频 (250 tokens) | 5.49s ± 0.75s | 45.5 tokens/s | 0.899x | 1.11 GB |
| 10s 音频 (500 tokens) | 10.13s ± 0.08s | 49.4 tokens/s | 0.981x | 1.11 GB |
| 15s 音频 (750 tokens) | 15.92s ± 0.12s | 47.1 tokens/s | 0.938x | 1.11 GB |
性能分析:
评测生成的音频文件位于 eval_results/ 目录:
eval_results/
├── sample_01.wav # lo-fi music
├── sample_02.wav # 80s hip hop
├── sample_03.wav # chill song
├── sample_04.wav # 80s pop
├── sample_05.wav # podcast intro
├── npu_self_verification.json # 详细报告
└── performance_report.json # 性能报告以下为完整推理运行日志(摘录):
======================================================================
MusicGen-Small NPU 自验证评测
======================================================================
[环境信息]
PyTorch: 2.9.0+cpu
torch_npu: 2.9.0.post1+gitee7ba04
NPU: Ascend910_9362
NPU Memory: 61.3 GB
模型: /opt/atomgit/musicgen-small-npu/model_cache
随机种子: 42
[Step 1] 加载模型...
模型参数量: 0.59B
模型加载耗时: 9.31s
[Step 2] 生成 5 个音频样本...
[1/5] Generating: 'lo-fi music with a soothing melody'
生成耗时: 15.47s | 速度: 16.16 tokens/s
音频时长: 4.94s | 范围: [-0.6016, 0.6133]
保存至: /opt/atomgit/musicgen-small-npu/eval_results/sample_01.wav
[2/5] Generating: 'a funky house with 80s hip hop vibes'
生成耗时: 4.85s | 速度: 51.55 tokens/s
音频时长: 4.94s | 范围: [-0.8203, 0.9570]
保存至: /opt/atomgit/musicgen-small-npu/eval_results/sample_02.wav
...
======================================================================
自验证评测汇总
======================================================================
模型: facebook/musicgen-small (0.59B params)
NPU: Ascend910_9362
数据类型: bfloat16
生成样本数: 5
平均生成速度: 40.6 tokens/s
NPU 显存: Allocated=1.11GB, Reserved=1.45GB, Peak=1.21GB
总耗时: 47.5s
======================================================================
✅ 自验证评测完成!| 文件 | 说明 |
|---|---|
inference.py | 推理脚本,支持自定义参数 |
eval_self_verification.py | 自验证评测脚本 |
eval_performance.py | 性能基准测试脚本 |
eval_results/ | 评测输出目录 |
README.md | 本部署文档 |
A: 减少 --duration 参数,或关闭其他占用 NPU 内存的应用。
A: 当前推理使用 eager 模式以确保兼容性。可尝试使用更短的音频时长减少生成 token 数。
A: 修改 inference.py 中的 --model_path 参数,指向其他模型路径(如 facebook/musicgen-medium)。
验证日期: 2026-05-20
验证环境: Ascend910_9362 + CANN 8.5.1 + PyTorch 2.9.0 + torch_npu 2.9.0