AudioCraft 是一款基于 PyTorch、面向音频生成深度学习研究的工具库。该库内置两套业界顶尖的 AI 音频生成模型(AudioGen 与 MusicGen)的推理及训练代码,可生成高品质音频。
MusicGen 是一款简易、可控的音乐生成自回归模型。该模型为单阶段自回归 Transformer,基于 32kHz 采样率的 EnCodec 分词器完成训练,分词器包含 4 个码本,码本采样频率为 50 赫兹。
MusicGen核心模块:文本编码器、声学编码器、Transformer 解码器、波形解码器。
MusicGen推理数据流:
curr_sequence → Embedding → cat(CFG) → + cond_emb → Sin PE → 48层 Transformer → LM heads → logits → EnCodec → save wav表 1 版本配套表
| 配套 | 版本 | 环境准备指导 |
|---|---|---|
| Python | 3.11.10 | - |
| torch | 2.8.0 | - |
| CANN | 8.5.0 | - |
# 增加软件包可执行权限,{version}表示软件版本号,{arch}表示CPU架构,{soc}表示昇腾AI处理器的版本。
chmod +x ./Ascend-cann-toolkit_{version}_linux-{arch}.run
chmod +x ./Ascend-cann-kernels-{soc}_{version}_linux.run
# 校验软件包安装文件的一致性和完整性
./Ascend-cann-toolkit_{version}_linux-{arch}.run --check
./Ascend-cann-kernels-{soc}_{version}_linux.run --check
# 安装
./Ascend-cann-toolkit_{version}_linux-{arch}.run --install
./Ascend-cann-kernels-{soc}_{version}_linux.run --install
# 设置环境变量
source /usr/local/Ascend/ascend-toolkit/set_env.sh下载软件包
wget https://download.pytorch.org/whl/cpu/torch-2.8.0%2Bcpu-cp311-cp311-manylinux_2_28_aarch64.whl安装命令
pip install torch-2.8.0+cpu-cp311-cp311-manylinux_2_28_aarch64.whl下载软件包
wget https://gitcode.com/Ascend/pytorch/releases/download/v26.0.0-pytorch2.8.0/torch_npu-2.8.0.post4-cp311-cp311-manylinux_2_28_aarch64.whl安装命令
pip install torch_npu-2.8.0.post4-cp311-cp311-manylinux_2_28_aarch64.whl# 若环境镜像中没有gcc、g++,请用户自行安装
yum install gcc
yum install g++
# 导入头文件路径
export CPLUS_INCLUDE_PATH=/usr/include/c++/12/:/usr/include/c++/12/aarch64-openEuler-linux/:$CPLUS_INCLUDE_PATHhttps://github.com/facebookresearch/audiocraft
下载适配代码覆盖github源码中的相关代码 https://ai.gitcode.com/Ascend-SACT/audiocraft-musicgen
pip install -r requirements.txt| 模型权重 | 下载地址 |
|---|---|
| facebook/musicgen-large | https://hf-mirror.com/facebook/musicgen-large |
| facebook/encodec_32khz | https://hf-mirror.com/facebook/encodec_32khz |
| t5-base | https://hf-mirror.com/google-t5/t5-base |
参考目录结构:
models/musicgen/
|-- models
| |-- facebook
| | `-- encodec_32khz
| | |-- README.md
| | |-- config.json
| | |-- gitattributes
| | |-- model.safetensors
| | |-- preprocessor_config.json
| | `-- pytorch_model.bin
| `-- t5-base
| |-- README.md
| |-- config.json
| |-- flax_model.msgpack
| |-- generation_config.json
| |-- model.safetensors
| |-- pytorch_model.bin
| |-- special_tokens_map.json
| |-- spiece.model
| |-- tf_model.h5
| |-- tokenizer.json
| `-- tokenizer_config.json
`-- musicgen-large
|-- README.md
|-- compression_state_dict.bin
|-- config.json
|-- generation_config.json
|-- preprocessor_config.json
|-- pytorch_model-00001-of-00002.bin
|-- pytorch_model-00002-of-00002.bin
|-- pytorch_model.bin.index.json
|-- special_tokens_map.json
|-- spiece.model
|-- state_dict.bin
|-- tokenizer.json
`-- tokenizer_config.json
bash inference.sh