TL;DR
Residual vector quantization approximates a vector by a sum of entries, one from each of several small codebooks. The first codebook approximates the vector coarsely. The second approximates what is left over, the residual, and the third approximates what is left after that. A short list of indices then names a very large number of possible reconstructions, while the codebooks stay small. This is why it is a common way to turn audio and images into a few discrete tokens that a generative model can predict.
Builds on: Vector Quantization · Leads to: autoregressive token models over residual codes
Where it's used
- Neural audio codecs. Codecs such as SoundStream and EnCodec compress audio by encoding each short frame into a stack of residual codes. Each extra stack adds detail, so the same codec can run at a low bit rate for speech or a higher one for music. A language model can then generate the codes, one step at a time, and the codec turns them back into sound.
- Image generation. Image generators that work in a discrete token space, such as the RQ-Transformer, use residual codes to describe each patch with a short stack of indices, so a large effective vocabulary is covered by a few small codebooks.
- Generative recommendation. Some recommendation systems give each item a short sequence of codes, built by residual quantization of its embedding, as in TIGER. Items that are similar share their first codes, so the codes carry a coarse-to-fine meaning that a generator can learn to predict.
How it works
Take one two-number point and trace it through three stages. Each stage has a codebook of two or three entries, and each stage quantizes the residual left by the stage before.
- Stage 1 quantizes the point. Let . The first codebook is . The distance to is and to is , so the index is 1 and the first approximation is . Check: this is the entry with the smaller squared distance.
- Compute the residual. The residual is what stage 1 missed: . Its squared length is 1, which is the error so far. Check: adding the approximation back gives .
- Stage 2 quantizes the residual, not the point. The second codebook is . The distance from to is , and to it is . So the index is 0 and . The new residual is , with squared length . The error dropped from 1 to 0.25.
- Stage 3 finishes. The third codebook is . The residual is exactly the first entry, so the index is 0 and the new residual is . Check: the error is now zero.
- Store the codes. The point is stored as the three indices . Decoding adds the chosen entries back up: , which is the original point.
- Compare with one codebook. Three stages of two entries name different reconstructions, from only stored vectors. A single codebook that names 8 reconstructions must store all 8. With more stages this gap grows very quickly.
flowchart LR X["Vector x"] --> S1["Stage 1: nearest entry in codebook 1"] S1 --> R1["Residual r1 = x minus stage 1 entry"] R1 --> S2["Stage 2: nearest entry in codebook 2"] S2 --> R2["Residual r2"] R2 --> S3["Stage 3: nearest entry in codebook 3"] S3 --> Codes["Codes: one index per stage"]
The math
Given codebooks , each with vectors, a residual quantizer starts with . At stage it picks the index of the codebook entry nearest, in Euclidean distance, to the current residual, , and sets . The code is the list , and the reconstruction is .
Step 1 in symbols: the first stage
Stage 1 is ordinary vector quantization of . The residual after it is . In the trace, , and .
Step 2 in symbols: the residual
The key move is that stage quantizes rather than :
Because is the nearest entry to , the squared length of the residual after stage is as small as this codebook allows. In the trace, , and . Training makes each codebook shrink the residual, so the error generally decreases with each stage. The method does not guarantee this for every input, because a codebook with no entry near the residual can leave it as large as before, or larger.
Step 3 in symbols: reconstruct and check
Adding the stage entries telescopes back to the original vector minus the final residual:
In the trace, , so and the reconstruction is exact.
Step 4 in symbols: the cost and the number of reconstructions
A code with stages of entries takes bits. It names distinct sums, one entry per stage, while storing only vectors. A single codebook with the same bits would have to store all vectors. In the trace, and give 8 reconstructions from 6 stored vectors.
Worked examples
Use , and .
- Stage 1: distance to is , and to is . Index 1, so .
- Residual: .
- Stage 2: distance from to is , and to is . Index 1, so .
- Residual: .
- Reconstruction: , exact.
Interpretation: two small stages, each with two entries, reproduced a point exactly, because the point is a sum of one entry from each codebook.
A common setting for a two-stage quantizer on 64-dimensional embeddings.
- Each stage has entries, so one index takes bits, and the code takes bits, which is 2 bytes.
- The two codebooks store vectors of 64 numbers each.
- The number of distinct reconstructions is .
- A single codebook with the same 16-bit code would store all 65,536 vectors of 64 numbers each, which is 128 times more storage.
Interpretation: stacking the stages buys a large vocabulary of reconstructions for the price of a small set of stored vectors.
An illustrative neural audio codec encodes each frame of audio as four residual codes, each from a codebook of 256 entries over 64-dimensional features.
- The code per frame is bits, which is 4 bytes.
- The codebooks store vectors of 64 numbers, about bytes, roughly 256 kilobytes.
- The number of distinct reconstructions is , about .
- A single codebook with the same 32-bit code would need stored vectors of 64 numbers, about bytes, roughly a terabyte.
- Try it: suppose the codec uses 8 stages of 256 entries instead of 4. How many codebook vectors does it store, and how many distinct reconstructions can its codes name?
Answer: stored vectors, and distinct reconstructions. More stages give finer detail, and the storage grows only linearly.
Common mistakes and tradeoffs
It is tempting to think that residual quantization is just running vector quantization twice on the same data. In fact each stage sees only the residual of the stages before it, so the second codebook never has to represent the parts of the vector that the first codebook already captured. That is what keeps the codebooks small.
It is tempting to think that more stages are always better. Each stage adds bits and a decoding step, and the gains shrink as the residual becomes small and hard to predict. Encoding is also greedy: each stage picks the nearest entry for the current residual, which is not guaranteed to give the best total approximation across all stages. Practical systems accept this trade for speed and then use training to make the first stages carry most of the information.
It is tempting to think that the stages separate meaning cleanly, with stage 1 always holding the content and later stages holding only texture. The first stage usually captures the coarse structure, which is why codes can be read coarse to fine, but the codebooks are learned, not designed, so the split is approximate.
| Method | How it splits the work | Stored vectors to name reconstructions | Best when |
|---|---|---|---|
| Vector quantization | One codebook, one index | The vocabulary is small enough to store fully | |
| Product quantization | Splits dimensions into independent pieces | for reconstructions | Search over very large collections of vectors |
| Residual quantization | Splits the error into stages | for reconstructions | Generation or compression where coarse-to-fine detail matters |
History
Vector quantization for compressing speech and images was developed in the 1980s, and speech coders of that era already used multiple stages, quantizing the error left by earlier stages. Residual quantization was a way to get large effective codebooks without storing them in full.
In 2017, the VQ-VAE used a single learned codebook to turn images into discrete codes, and it showed that a generative model could work on those codes. Residual codes were later used for higher fidelity. In 2021, neural audio codecs such as SoundStream used stacks of residual codebooks to compress speech and music into a few codes per frame, and later codecs and image generators adopted the same design. The idea caught on because a model that predicts a few small codes per position is easier to train than one that predicts one code from a huge vocabulary.
Recap
- Residual vector quantization approximates a vector as a sum of entries, one from each of codebooks.
- Each stage quantizes the residual left by the stages before it, so each codebook can stay small.
- stages of entries store vectors yet name reconstructions.
- It is greedy and not guaranteed to be optimal, but it is fast, and its codes read coarse to fine.
- Read next: Vector Quantization for the single-codebook case it extends, and autoregressive models for how generators predict these codes.
Check yourself
QuestionA residual quantizer has 3 stages with 16 entries each. How many bits does one code take, and how many codebook vectors does it store?Show answer
Each stage needs bits, so the code takes bits. It stores vectors, one set of 16 per codebook.
QuestionIn the first trace, the squared length of the residual after stage 1 is 1, and after stage 2 it is 0.25. Why does the error fall at stage 2, and what would happen if the second codebook had no entry near the residual?Show answer
Stage 2 quantizes the residual , and its entry is only 0.5 away, so the squared length of the residual falls to 0.25. If the codebook had no entry near the residual, the nearest entry would still be chosen, and the residual could stay as large as before or grow. This is why codebooks are trained to cover the residuals they will see.
QuestionWhy can a residual quantizer name far more reconstructions than it stores vectors, and why does a single codebook not do the same?Show answer
Each reconstruction is a sum with one entry chosen from each stage, so the choices multiply: distinct sums from stored vectors. A single codebook with the same number of bits must store every one of its vectors explicitly, since each reconstruction is one stored vector.
QuestionA generator must predict discrete codes for each frame of audio. Would you prefer one codebook of 65,536 entries or two stages of 256 entries? Give one reason for each choice's cost.Show answer
Both give 16 bits per frame. One codebook of 65,536 entries stores 65,536 vectors and the generator must choose among 65,536 values at each step. Two stages of 256 store 512 vectors and the generator chooses among 256 values at each of 2 steps. The two-stage version is cheaper to store and easier to train, but its codes are chosen greedily, one stage at a time, and a generator that predicts them must respect the coarse-to-fine order, which the single codebook does not impose.
Sources
- van den Oord, A., Vinyals, O., and Kavukcuoglu, K. (2017). Neural Discrete Representation Learning. https://arxiv.org/abs/1711.00937
- Vector quantization, Wikipedia. https://en.wikipedia.org/wiki/Vector_quantization