↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

Music as codec tokens

MusicGen writes music the way a language model writes text: one token at a time, each drawn from a distribution the model predicts from everything before it. The tokens are the codes of EnCodec, a neural audio codec that turns 32 kHz audio into 50 frames a second, each frame four codes of 2,048 possible values. A text prompt steers the music through cross-attention. This tab records facebook/musicgen-small (587 M parameters: a frozen 110 M T5-base text encoder, a 420 M decoder of 24 layers of width 1024 with 16 heads, and the 57 M EnCodec model) generating 6 s from the model card's first prompt, “a funky house with 80s hip hop vibes”.

Four ideas carry the tab: how the prompt enters (T5 and cross-attention), how four codebooks are predicted by one transformer (the delay pattern), how the prompt is made to matter more (classifier-free guidance), and how codes become sound again (EnCodec's decoder). The weights are released under CC BY-NC 4.0, for non-commercial use with attribution; this explainer is non-commercial and credits them on the page.

The prompt: T5 and cross-attention

T5's tokenizer cuts the prompt into 13 pieces, from a lone ▁ to </s>, and the frozen T5-base encoder turns them into 13 vectors of 768. One linear layer maps each to the decoder's width, with , and every decoder layer reads them through cross-attention: the current step's state asks the question, the prompt's 13 vectors are the keys and values.

What the heads do with this is not what one might guess. Averaged over the 6 s, all 24 layers and 16 heads, 98 % of the weight goes to just two pieces, the leading ▁ and </s>, and the 11 word pieces share 2.2 %. Those two vectors are short (norms 1.5 and 1.8 after the projection, against about 18 for the words), so attending to them changes the state little: they work as attention sinks, a place for a head to put its weight when it has nothing to read. A few heads do read the words. Layers 6 to 10 read them most: head 4 of layer 8 gives the words 21 % of its weight over the run and head 10 of layer 6 gives them 19 %. The Cross-attention card opens on that head of layer 8; the buttons switch between layers 1, 6, 8 and 24.

Four codebooks, one transformer: the delay pattern

EnCodec's four codebooks are a residual quantizer: the first code of a frame describes the frame coarsely, the second its error, and so on, so the finer codes depend on the coarser ones of the same frame. Predicting all four of a frame at once ignores that dependence; writing them one after another, 4 tokens per frame, keeps it but takes 1,200 steps for 300 frames. MusicGen shifts codebook by steps instead, so that at step one forward pass writes

Every frame's four codes then run down a diagonal of the grid: its coarse code is written first and its finest three steps later, after the coarser codes of the same frame are in the context. Cells with no frame (before codebook starts, and after it ends) hold the padding id 2048, which is never drawn. 300 frames take 303 steps, shown in Step 60: four codes at once and in the strip at the bottom. The decoder adds the four codebooks' embeddings into one input vector per step and has four output heads, one per codebook. In the paper's comparison the delay pattern comes close to writing every code in turn, at about a quarter of the steps.

Classifier-free guidance

Every step runs the decoder twice: once with the prompt, once with a null prompt, here 13 zero vectors with the mask set to zero, so cross-attention adds nothing. The two predictions are mixed in logit space with the guidance scale :

With this is just the prompted prediction; pushes past it, away from what the model would play with no prompt at all. It needs a model that has learned both predictions, which is why the text is dropped from some training examples. The effect shows in the entropy of the distributions the codes are drawn from: averaged over the run, 4.02 nats with guidance, 4.36 with the prompt alone () and 4.82 with no prompt (). The two passes usually agree on the most likely code (91 % of draws), but the mixed logits pick a different most likely code than the prompted pass in 18 % of draws. The Classifier-free guidance panel shows the three distributions at the current step and the arithmetic for the drawn code.

Sampling: top 250 at temperature 1

The guided logits are not decoded greedily: MusicGen keeps the 250 most likely of the 2,048 codes, renormalises and draws one, at temperature 1, the defaults of Meta's Audiocraft. The checkpoint's own generation config leaves top_k unset, so transformers alone would keep only the top 50; the recording passes 250. The top 250 hold on average 93 % of the guided distribution, and 59 % at its flattest step.

The draws are far from greedy. Codebook 1 drew its most likely code in 31 % of frames and a code outside its top 10 in 33 %; the three finer codebooks drew their most likely code about 20 % of the time and a code outside the top 10 in 48 %. Each draw used a stored uniform number and took the code whose slice of the sorted distribution holds , so every token on the page can be redrawn from what the page loads. The two other runs reuse the same uniforms but not the same distributions, and they part ways at once: fewer than 1 in 200 of their codes match the guided run's.

Back to sound: EnCodec's decoder

To play the codes, EnCodec looks up one 128-dimensional vector per code and adds a frame's four into one latent vector, . A convolution and a two-layer LSTM read the 300 latent frames, then four transposed convolutions upsample them by 8, 5, 4 and 4, 640 samples per frame, and a last convolution gives the waveform. The EnCodec decode row plays the same guided codes decoded from their first 1, 2, 3 and 4 codebooks: each codebook adds 0.55 kbps (11 bits, 50 times a second), and against the full decode the waveform SNR goes from 7.9 dB to 11.5 and 15.2 dB, while the log-mel distance falls from 4.5 dB to 2.8 and 1.6. The last two clips are the prompt-only and no-prompt runs.

EnCodec's output is not limited to ±1: the guided decode peaks at 1.29. All six clips, their spectrograms and envelopes are stored at one common gain of 0.761, which brings the loudest to 0.98, so they keep their relative loudness.

How the recording was checked

The steps were run with transformers' MusicGen modules on the CPU in float32, one decoder call per step with both passes' caches. Feeding the whole recorded grid back at once gives the same logits to within . Running transformers' own generate with each step's code forced to the recorded one gives the same 1,200 codes and the same audio, sample for sample. The tab's PyTorch drawer, a separate implementation loading the same weights, reproduces the guided logits to within , the delay pattern exactly and EnCodec's latent exactly.

Papers

  1. Simple and Controllable Music Generation Jade Copet, Felix Kreuk, Itai Gat, Tal Remez, David Kant, Gabriel Synnaeve, Yossi Adi, Alexandre Défossez · NeurIPS 2023 MusicGen: one transformer over EnCodec's codebooks in a delay pattern, T5 text conditioning and classifier-free guidance; the model this tab records.
  2. High Fidelity Neural Audio Compression Alexandre Défossez, Jade Copet, Gabriel Synnaeve, Yossi Adi · TMLR 2022 EnCodec, the residual-vector-quantized codec whose 32 kHz, 4-codebook version MusicGen writes and decodes.
  3. Classifier-Free Diffusion Guidance Jonathan Ho, Tim Salimans · arXiv 2022 Mixing a conditional and an unconditional prediction from one jointly trained model, the guidance MusicGen applies to its logits.
  4. AudioGen: Textually Guided Audio Generation Felix Kreuk et al. · ICLR 2022 The text-to-sound language model over codec tokens that brought classifier-free guidance to autoregressive audio, MusicGen's predecessor.
  5. Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer Colin Raffel et al. · JMLR 2019 T5, whose frozen base encoder turns MusicGen's prompt into the vectors its cross-attention reads.
  6. AudioLM: a Language Modeling Approach to Audio Generation Zalán Borsos et al. · IEEE/ACM Transactions on Audio, Speech, and Language Processing 2022 Models codec codebooks with a cascade of transformers, the alternative to one model with a delay pattern that MusicGen compares against.
  7. MusicLM: Generating Music From Text Andrea Agostinelli et al. · arXiv 2023 Text-to-music with AudioLM's hierarchy and MuLan conditioning, the main baseline in the MusicGen paper.

See also