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

模型推理指导

一、模型简介

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 版本配套表

配套版本环境准备指导
Python3.11.10-
torch2.8.0-
CANN8.5.0-

2.1 获取安装包

  • 支持设备:Atlas 800I A2
  • 支持卡数:支持的卡数为1
  • 环境准备指导

2.2 CANN安装

# 增加软件包可执行权限,{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

2.3 Torch_npu安装

2.3.1 安装pytorch框架 版本2.8.0

下载软件包

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

2.3.2 安装torch_npu框架 版本2.8.0.post4

下载软件包

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

2.4 安装gcc、g++

# 若环境镜像中没有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_PATH

三、模型准备

3.1 github源码下载:

https://github.com/facebookresearch/audiocraft

3.2 适配代码下载

下载适配代码覆盖github源码中的相关代码 https://ai.gitcode.com/Ascend-SACT/audiocraft-musicgen

3.3 安装所需依赖

pip install -r requirements.txt

3.4 权重下载

模型权重下载地址
facebook/musicgen-largehttps://hf-mirror.com/facebook/musicgen-large
facebook/encodec_32khzhttps://hf-mirror.com/facebook/encodec_32khz
t5-basehttps://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

四、模型运行

  1. 按需修改inference.sh里的MODEL_PATH等配置;
  2. 执行如下脚本运行推理:
    bash inference.sh
  3. 补充说明:首次执行时需要进行图模式编译缓存,有额外约3分钟耗时;如果非首次运行推理时出现step卡住的问题,可以清理cache_dir缓存目录后重新执行。

声明

  • 本代码仓提到的数据集和模型仅作为示例,这些数据集和模型仅供您用于非商业目的,如您使用这些数据集和模型来完成示例,请您特别注意应遵守对应数据集和模型的License,如您因使用数据集或模型而产生侵权纠纷,华为不承担任何责任。
  • 如您在使用本代码仓的过程中,发现任何问题(包括但不限于功能问题、合规问题),请在本代码仓提交issue,我们将及时审视并解答。