模型:
此模型是在 GiantMIDI-Piano 数据集上预训练的 Perceiver AR 符号音频模型(134M参数),共经过27个时期(157M令牌)的训练。它使用 rotary embedding 进行相对位置编码。这是 perceiver-io 库的一部分。
Perceiver AR是普通解码器(transformer)(如GPT-2)的一种简单扩展。它们的核心构建块都是解码器层,包含一个自注意层,后面跟着一个位置感知的MLP(多层感知器)。自注意力使用了因果关注掩码。
Perceiver AR在第一层注意力中额外交叉关注输入序列的较长前缀。这一层是一种混合的自注意力和交叉注意力层。自注意力仅作用于输入序列的最后n个位置,并采用因果注意掩码;交叉注意力则从最后n个位置关注到前m个位置。输入序列的长度为m + n。这使得Perceiver AR能够处理比仅基于自注意力的解码器(transformer)更大的上下文。
图1. Perceiver AR中的注意力,其中m=8为前缀记号,n=3为潜在记号。
混合注意力层的输出为n个潜在数组,对应于输入序列的最后n个记号。这些数组由L-1个解码器层进一步处理,其中总注意力层数为L。最后一层(图1中未显示)为每个潜在位置预测目标记号。最终层的权重与输入嵌入层共享。除了初始的交叉关注前缀序列外,Perceiver AR在架构上与纯解码器(transformer)完全相同。
该模型经过27个时期(157M令牌)在 GiantMIDI-Piano 数据集上进行了符号音频建模任务的训练。该数据集包含 MIDI 个文件,使用 Perceiver AR paper 中的方法进行了标记,该方法在 Huang et al (2019) 的A.2节中有详细描述。所有超参数见 training script 。上下文长度设置为6144个令牌,具有2048个潜在位置,因此最大前缀长度为4096。每个示例的实际前缀长度在0到4096之间随机选择。使用 PyTorch Lightning 进行了训练,并使用库特定的转换实用程序将结果检查点转换为此🤗模型。
该模型可用于使用用户定义的初始潜在记号进行音频生成。它主要用于演示如何使用 perceiver-io library 训练Perceiver AR模型。要改善生成的音频样本质量,需要比 GiantMIDI-Piano 更大的数据集进行训练。
要使用此模型,首先需要使用扩展程序audio install perceiver-io库。
pip install perceiver-io[audio]
然后可以使用PyTorch使用模型直接生成MIDI文件:
import torch
from perceiver.model.audio.symbolic import PerceiverSymbolicAudioModel
from perceiver.data.audio.midi_processor import decode_midi, encode_midi
from pretty_midi import PrettyMIDI
repo_id = "krasserm/perceiver-ar-sam-giant-midi"
model = PerceiverSymbolicAudioModel.from_pretrained(repo_id)
prompt_midi = PrettyMIDI("prompt.mid")
prompt = torch.tensor(encode_midi(prompt_midi)).unsqueeze(0)
output = model.generate(prompt, max_new_tokens=64, num_latents=1, do_sample=True, top_p=0.95, temperature=1.0)
output_midi = decode_midi(output[0].cpu().numpy())
type(output_midi)
pretty_midi.pretty_midi.PrettyMIDI
使用符号音频生成流水线生成MIDI输出:
from transformers import pipeline
from pretty_midi import PrettyMIDI
from perceiver.model.audio import symbolic # auto-class registration
repo_id = "krasserm/perceiver-ar-sam-giant-midi"
prompt = PrettyMIDI("prompt.mid")
audio_generator = pipeline("symbolic-audio-generation", model=repo_id)
output = audio_generator(prompt, max_new_tokens=64, num_latents=1, do_sample=True, top_p=0.95, temperature=1.0)
type(output["generated_audio_midi"])
pretty_midi.pretty_midi.PrettyMIDI
或使用 fluidsynth 渲染MIDI符号以生成WAV输出(注意:需要先安装fluidsynth才能使下面的示例正常工作):
from transformers import pipeline
from pretty_midi import PrettyMIDI
from perceiver.model.audio import symbolic # auto-class registration
repo_id = "krasserm/perceiver-ar-sam-giant-midi"
prompt = PrettyMIDI("prompt.mid")
audio_generator = pipeline("symbolic-audio-generation", model=repo_id)
output = audio_generator(prompt, max_new_tokens=64, num_latents=1, do_sample=True, top_p=0.95, temperature=1.0, render=True)
with open("generated_audio.wav", "wb") as f:
f.write(output["generated_audio_wav"])
以下是使用来自 GiantMIDI-Piano 数据集验证子集的各种提示生成的(精选的)音频样本。音频输出不包含输入提示。
| Audio sample | Top-K | Top-p | Temperature | Prefix length | Latents |
|---|---|---|---|---|---|
| Your browser does not support the audio element. | - | 0.95 | 0.95 | 4096 | 1 |
| Your browser does not support the audio element. | - | 0.95 | 1.0 | 4096 | 64 |
| Your browser does not support the audio element. | - | 0.95 | 1.0 | 1024 | 1 |
| Your browser does not support the audio element. | 15 | - | 1.0 | 4096 | 16 |
| Your browser does not support the audio element. | - | 0.95 | 1.0 | 4096 | 1 |
krasserm/perceiver-ar-sam-giant-midi模型是从一个训练检查点创建的。
from perceiver.model.audio.symbolic import convert_checkpoint
convert_checkpoint(
save_dir="krasserm/perceiver-ar-sam-giant-midi",
ckpt_url="https://martin-krasser.com/perceiver/logs-0.8.0/sam/version_1/checkpoints/epoch=027-val_loss=1.944.ckpt",
push_to_hub=True,
)
@inproceedings{hawthorne2022general,
title={General-purpose, long-context autoregressive modeling with perceiver ar},
author={Hawthorne, Curtis and Jaegle, Andrew and Cangea, C{\u{a}}t{\u{a}}lina and Borgeaud, Sebastian and Nash, Charlie and Malinowski, Mateusz and Dieleman, Sander and Vinyals, Oriol and Botvinick, Matthew and Simon, Ian and others},
booktitle={International Conference on Machine Learning},
pages={8535--8558},
year={2022},
organization={PMLR}
}