Free How much of your firm runs on you? Take the 2-minute Key Man Score.
Lesson reading
0%

Lesson 23 / Unit 7: Adaptation and Serving

Codec Tokens

VALL-E cloned a voice from a 3-second recording by treating speech as tokens. How codec-token TTS works, what Magpie-TTS adds, and where it breaks.

A neural codec turns audio into discrete codes. A TTS model can predict those codes and use the codec decoder to produce speech. This is a model-family comparison, not an extension we trained.

Our tiny model is a cascade. Piper is end-to-end. Codec-token models replace the mel with discrete codes and a codec decoder.

Neural Codecs

A neural audio codec is an encoder and decoder trained together to compress a waveform into a short sequence of discrete codes and rebuild it. The codes are integers from a fixed codebook, which is exactly what a language-model-style network knows how to predict.

SoundStream is the reference design. Its encoder turns audio into latent frames, a residual vector quantiser (RVQ) maps each frame to codebook entries, and a decoder turns the codes back into audio. RVQ works in layers: the first codebook captures a coarse approximation, and each later codebook encodes what the previous ones missed. The paper reports bitrates from 3 kbps to 18 kbps from one model, and running “in real time on a smartphone CPU”. EnCodec, from Meta, follows the same shape: a streaming encoder-decoder with a quantised latent space.

Compare this to the mel recipe from unit 1. A mel spectrogram is continuous, throws away phase, and needs a separately trained vocoder whose recipe must match exactly (the mismatch lesson measured what happens when it does not). A codec token sequence is discrete, and the decoder that turns it into audio was trained with the encoder, so the pair agree by construction.

Token Generation

It treats synthesis as sequence prediction. Text goes in, the model predicts codec tokens one step at a time, and the codec decoder produces the waveform.

VALL-E framed TTS as “a conditional language modeling task rather than continuous signal regression.” That framing is what makes prompt-based cloning cheap. Encode a few seconds of a new speaker with the same codec, put those tokens in front of the sequence, and the model continues in the voice it was shown. The paper also notes that the prompt’s emotion and acoustic environment carry through, which cuts both ways: a noisy reference produces noisy output.

The price is the failure mode. The autoregressive token models discussed here do not use our explicit per-character length regulator. Our model from training from scratch allocates frames to each encoded character, but allocation alone does not ensure intelligible speech. (Digits it was never given are another matter: they are dropped before the model runs.) A token model decides for itself when to move on, so it can repeat, skip or keep talking past the end of the text. That is the subject of TTS hallucinations.

Magpie Architecture

Magpie-TTS is NVIDIA’s codec-token model in NeMo. The Magpie-TTS documentation describes “an encoder-decoder transformer architecture that operates on discrete audio tokens produced by a neural audio codec.” The encoder reads the text; the decoder generates audio tokens autoregressively while attending to the text and to any audio context.

Four pieces from the docs are worth knowing:

  • Monotonic alignment. A CTC loss and an attention prior bias the decoder’s cross-attention towards moving forward through the text. This is the direct answer to skips and repeats.
  • Classifier-free guidance. During training the model sometimes drops its conditioning. At inference the difference between conditioned and unconditioned outputs is amplified, which the docs say increases fidelity and consistency.
  • Context encoder for cloning. Reference audio goes through a dedicated context encoder, so the model can speak in a voice from “just a few seconds of reference audio”. Text descriptions can also condition speaking style.
  • Frame stacking. A base decoder predicts one latent per group of frames and a small local transformer expands it, with a stacking factor up to 4. Fewer big decoder steps means faster inference.

The docs list a 357M-parameter multilingual checkpoint. For scale, our tiny model has 1,231,185 parameters; Magpie is a different class of system, built for GPUs.

Explicit durations allocate frames to symbols; the decoder must still render them intelligibly. Magpie instead guides autoregressive alignment with its training losses and attention priors.

Preference Optimisation

Preference optimisation fine-tunes a trained model towards outputs that score better on measurable rewards. NeMo’s Magpie-TTS preference optimisation page describes two routes, and both use the same rewards: character error rate from an ASR model for intelligibility, speaker similarity for voice consistency, and optionally PESQ for audio quality.

DPO is offline. Generate several samples per text and reference pair (the docs default to 6), keep the best and worst by the metrics, and fine-tune on those chosen and rejected pairs. Recommended settings on the page are a DPO beta of 0.01 and a learning rate of 2e-7.

GRPO is online. It generates candidates during training (12 per example in the recommended settings, at temperature 0.7 to 0.8), scores them, and pushes the model towards higher reward, with a learning rate of 1e-7. The docs call it “generally recommended over DPO”, partly because nothing needs to be pre-generated and stored.

Notice the reward. CER from an ASR model is the same idea as the output WER we compute in the eval harness: transcribe the synthesis and compare it to the input text. The difference is where it sits. We use it to judge a model; Magpie’s preference stage uses it to train one.

Next: when to fine-tune a TTS model, because most adaptation problems never need a training run at all.

Common questions

What is a codec-token TTS model?

It is a text-to-speech model that predicts discrete tokens from a neural audio codec instead of a mel spectrogram. A codec encoder turns audio into a few streams of integer codes, the TTS model learns to generate those codes from text, and the codec decoder turns the codes back into a waveform.

How does Magpie-TTS reduce hallucinations?

According to the NeMo documentation, Magpie-TTS adds a CTC loss and an attention prior that push the cross-attention between text and audio towards a monotonic alignment, so the decoder moves forward through the text instead of skipping or repeating it. It also uses classifier-free guidance at inference time.

Did you train a codec-token model for the build-along?

No. Everything on this page about codecs, VALL-E and Magpie-TTS is sourced description from the papers and the NVIDIA NeMo documentation. Our own measured work is the small mel-based model trained on a 4-core cloud container with no GPU.

We do this work for teams shipping voice agents: Self-Hosted Voice Agent .

Edit this lesson on GitHub ↗

Discussion

Questions, corrections and war stories welcome. Sign in with GitHub to comment.

Free guide

Be The 10x Entrepreneur

Get the firm out of your head.

  • 8 chapters
  • 7 worksheets
  • 30 day plan