g
gcw_coj3XaOd/musicgen-small
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

MusicGen-Small 昇腾 NPU 部署文档

Facebook MusicGen-Small (300M) 模型在华为 Ascend NPU 上的部署与验证

1. 环境信息

组件版本
PyTorch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
CANN8.5.1
NPU 设备Ascend910_9362 (61.3 GB)
Python3.11

2. 模型信息

  • 模型: facebook/musicgen-small
  • 参数量: 0.59B
  • 任务: Text-to-Audio (文本到音频生成)
  • 音频采样率: 32kHz
  • 数据精度: bfloat16

3. 部署步骤

3.1 环境准备

# 安装依赖
pip install torch torch_npu transformers soundfile numpy

# 确认 NPU 可用
python -c "import torch; import torch_npu; print(f'NPU available: {torch.npu.is_available()}')"

3.2 下载模型

模型已预下载至 model_cache/ 目录。如需重新下载:

from transformers import MusicgenForConditionalGeneration, MusicgenProcessor

processor = MusicgenProcessor.from_pretrained("facebook/musicgen-small")
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")

3.3 运行推理

# 基本推理
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)

4. 评测结果

4.1 自验证评测

生成 5 个不同风格的音频样本,验证 NPU 推理正常工作:

样本Prompt生成耗时速度 (tokens/s)音频时长音频范围
01lo-fi music with a soothing melody15.47s16.164.94s[-0.60, 0.61]
02a funky house with 80s hip hop vibes4.85s51.554.94s[-0.82, 0.96]
03a chill song with influences from lofi5.54s45.114.94s[-0.27, 0.26]
0480s pop track with bassy drums and synth5.80s43.114.94s[-0.55, 0.59]
05a catchy beat for a podcast intro5.33s46.864.94s[-0.47, 0.49]

汇总统计:

  • 总样本数: 5
  • 平均生成速度: 40.6 tokens/s
  • NPU 显存占用: Allocated=1.11GB, Reserved=1.45GB, Peak=1.21GB

4.2 性能基准测试

配置延迟吞吐量实时因子峰值显存
5s 音频 (250 tokens)5.49s ± 0.75s45.5 tokens/s0.899x1.11 GB
10s 音频 (500 tokens)10.13s ± 0.08s49.4 tokens/s0.981x1.11 GB
15s 音频 (750 tokens)15.92s ± 0.12s47.1 tokens/s0.938x1.11 GB

性能分析:

  • 平均吞吐量: ~47 tokens/s
  • 实时因子: 接近 1.0,推理速度接近实时
  • 显存占用稳定: ~1.1GB

4.3 生成的音频样本

评测生成的音频文件位于 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     # 性能报告

5. 推理日志证据

以下为完整推理运行日志(摘录):

======================================================================
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
======================================================================

✅ 自验证评测完成!

6. 交付件清单

文件说明
inference.py推理脚本,支持自定义参数
eval_self_verification.py自验证评测脚本
eval_performance.py性能基准测试脚本
eval_results/评测输出目录
README.md本部署文档

7. 常见问题

Q1: 推理时出现内存不足错误?

A: 减少 --duration 参数,或关闭其他占用 NPU 内存的应用。

Q2: 生成速度较慢?

A: 当前推理使用 eager 模式以确保兼容性。可尝试使用更短的音频时长减少生成 token 数。

Q3: 如何切换到其他 MusicGen 模型?

A: 修改 inference.py 中的 --model_path 参数,指向其他模型路径(如 facebook/musicgen-medium)。

8. 技术支持

  • 昇腾 NPU 文档: https://www.hiascend.com/
  • transformers 库: https://huggingface.co/docs/transformers/
  • MusicGen 论文: https://arxiv.org/abs/2306.05284

验证日期: 2026-05-20
验证环境: Ascend910_9362 + CANN 8.5.1 + PyTorch 2.9.0 + torch_npu 2.9.0