Search

Search papers and lessons

Residual Vector Quantization

Level 4 · 11 min read

Residual vector quantization stacks several small codebooks, each one quantizing the error left by the stages before it, so a short code can name a vast number of reconstructions.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • Probability
  • Neural network basics
  • Generative models
  • ReadReady to readNot yet

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.

  1. Stage 1 quantizes the point. Let x=(7,4)x = (7, 4). The first codebook is C(1)={(0,0),(8,4)}C^{(1)} = \{(0,0), (8,4)\}. The distance to (0,0)(0,0) is 49+16=6549 + 16 = 65 and to (8,4)(8,4) is 1+0=11 + 0 = 1, so the index is 1 and the first approximation is c(1)=(8,4)c^{(1)} = (8,4). Check: this is the entry with the smaller squared distance.
  2. Compute the residual. The residual is what stage 1 missed: r1=x−c(1)=(7,4)−(8,4)=(−1,0)r_1 = x - c^{(1)} = (7,4) - (8,4) = (-1, 0). Its squared length is 1, which is the error so far. Check: adding the approximation back gives (8,4)+(−1,0)=(7,4)(8,4) + (-1,0) = (7,4).
  3. Stage 2 quantizes the residual, not the point. The second codebook is C(2)={(−0.5,0),(1,1)}C^{(2)} = \{(-0.5, 0), (1,1)\}. The distance from r1=(−1,0)r_1 = (-1,0) to (−0.5,0)(-0.5,0) is 0.250.25, and to (1,1)(1,1) it is 55. So the index is 0 and c(2)=(−0.5,0)c^{(2)} = (-0.5, 0). The new residual is r2=r1−c(2)=(−0.5,0)r_2 = r_1 - c^{(2)} = (-0.5, 0), with squared length 0.250.25. The error dropped from 1 to 0.25.
  4. Stage 3 finishes. The third codebook is C(3)={(−0.5,0),(0.5,0)}C^{(3)} = \{(-0.5, 0), (0.5, 0)\}. The residual r2=(−0.5,0)r_2 = (-0.5, 0) is exactly the first entry, so the index is 0 and the new residual is 00. Check: the error is now zero.
  5. Store the codes. The point is stored as the three indices (1,0,0)(1, 0, 0). Decoding adds the chosen entries back up: (8,4)+(−0.5,0)+(−0.5,0)=(7,4)(8,4) + (-0.5, 0) + (-0.5, 0) = (7, 4), which is the original point.
  6. Compare with one codebook. Three stages of two entries name 2×2×2=82 \times 2 \times 2 = 8 different reconstructions, from only 2+2+2=62 + 2 + 2 = 6 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

Definition: Residual vector quantizer

Given LL codebooks C(1),…,C(L)C^{(1)}, \ldots, C^{(L)}, each with KK vectors, a residual quantizer starts with r0=xr_0 = x. At stage ll it picks the index ili_l of the codebook entry nearest, in Euclidean distance, to the current residual, il=arg⁡min⁡i∥rl−1−ci(l)∥2i_l = \arg\min_i \lVert r_{l-1} - c^{(l)}_i \rVert^2, and sets rl=rl−1−cil(l)r_l = r_{l-1} - c^{(l)}_{i_l}. The code is the list (i1,…,iL)(i_1, \ldots, i_L), and the reconstruction is x^=∑l=1Lcil(l)\hat{x} = \sum_{l=1}^{L} c^{(l)}_{i_l}.

Step 1 in symbols: the first stage

Stage 1 is ordinary vector quantization of xx. The residual after it is r1=x−ci1(1)r_1 = x - c^{(1)}_{i_1}. In the trace, i1=1i_1 = 1, c1(1)=(8,4)c^{(1)}_{1} = (8,4) and r1=(−1,0)r_1 = (-1, 0).

Step 2 in symbols: the residual

The key move is that stage ll quantizes rl−1r_{l-1} rather than xx:

rl=rl−1−cil(l),∥rl∥2=∥rl−1−cil(l)∥2r_l = r_{l-1} - c^{(l)}_{i_l}, \qquad \lVert r_l \rVert^2 = \lVert r_{l-1} - c^{(l)}_{i_l} \rVert^2

Because cil(l)c^{(l)}_{i_l} is the nearest entry to rl−1r_{l-1}, the squared length of the residual after stage ll is as small as this codebook allows. In the trace, ∥r1∥2=1\lVert r_1 \rVert^2 = 1, ∥r2∥2=0.25\lVert r_2 \rVert^2 = 0.25 and ∥r3∥2=0\lVert r_3 \rVert^2 = 0. 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:

x^=∑l=1Lcil(l)=x−rL\hat{x} = \sum_{l=1}^{L} c^{(l)}_{i_l} = x - r_L

In the trace, x^=(8,4)+(−0.5,0)+(−0.5,0)=(7,4)=x\hat{x} = (8,4) + (-0.5,0) + (-0.5,0) = (7,4) = x, so r3=0r_3 = 0 and the reconstruction is exact.

Step 4 in symbols: the cost and the number of reconstructions

A code with LL stages of KK entries takes Llog⁡2KL \log_2 K bits. It names KLK^L distinct sums, one entry per stage, while storing only LKL K vectors. A single codebook with the same bits would have to store all KLK^L vectors. In the trace, L=3L = 3 and K=2K = 2 give 8 reconstructions from 6 stored vectors.

Worked examples

Example
Tiny: two stages with two entries each

Use x=(5,1)x = (5, 1), C(1)={(0,0),(4,2)}C^{(1)} = \{(0,0), (4,2)\} and C(2)={(0,0),(1,−1)}C^{(2)} = \{(0,0), (1,-1)\}.

  1. Stage 1: distance to (0,0)(0,0) is 25+1=2625 + 1 = 26, and to (4,2)(4,2) is 1+1=21 + 1 = 2. Index 1, so c(1)=(4,2)c^{(1)} = (4,2).
  2. Residual: r1=(5,1)−(4,2)=(1,−1)r_1 = (5,1) - (4,2) = (1,-1).
  3. Stage 2: distance from (1,−1)(1,-1) to (0,0)(0,0) is 22, and to (1,−1)(1,-1) is 00. Index 1, so c(2)=(1,−1)c^{(2)} = (1,-1).
  4. Residual: r2=(1,−1)−(1,−1)=(0,0)r_2 = (1,-1) - (1,-1) = (0,0).
  5. Reconstruction: (4,2)+(1,−1)=(5,1)(4,2) + (1,-1) = (5,1), 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.

Example
Standard: two stages of 256 entries

A common setting for a two-stage quantizer on 64-dimensional embeddings.

  1. Each stage has K=256K = 256 entries, so one index takes log⁡2256=8\log_2 256 = 8 bits, and the code takes 2×8=162 \times 8 = 16 bits, which is 2 bytes.
  2. The two codebooks store 2×256=5122 \times 256 = 512 vectors of 64 numbers each.
  3. The number of distinct reconstructions is 256×256=65,536256 \times 256 = 65{,}536.
  4. 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.

Example
Realistic: four stages in a neural codec

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.

  1. The code per frame is 4×8=324 \times 8 = 32 bits, which is 4 bytes.
  2. The codebooks store 4×256=10244 \times 256 = 1024 vectors of 64 numbers, about 1024×64×4=262,1441024 \times 64 \times 4 = 262{,}144 bytes, roughly 256 kilobytes.
  3. The number of distinct reconstructions is 2564=4,294,967,296256^4 = 4{,}294{,}967{,}296, about 4.3×1094.3 \times 10^9.
  4. A single codebook with the same 32-bit code would need 4.3×1094.3 \times 10^9 stored vectors of 64 numbers, about 4.3×109×256≈1.1×10124.3 \times 10^9 \times 256 \approx 1.1 \times 10^{12} bytes, roughly a terabyte.
  5. 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: 8×256=20488 \times 256 = 2048 stored vectors, and 2568=264≈1.8×1019256^8 = 2^{64} \approx 1.8 \times 10^{19} 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.

MethodHow it splits the workStored vectors to name NN reconstructionsBest when
Vector quantizationOne codebook, one indexNNThe vocabulary is small enough to store fully
Product quantizationSplits dimensions into independent piecesm⋅km \cdot k for kmk^m reconstructionsSearch over very large collections of vectors
Residual quantizationSplits the error into stagesL⋅KL \cdot K for KLK^L reconstructionsGeneration 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 LL codebooks.
  • Each stage quantizes the residual left by the stages before it, so each codebook can stay small.
  • LL stages of KK entries store LKL K vectors yet name KLK^L 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 log⁡216=4\log_2 16 = 4 bits, so the code takes 3×4=123 \times 4 = 12 bits. It stores 3×16=483 \times 16 = 48 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 (−1,0)(-1, 0), and its entry (−0.5,0)(-0.5, 0) 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: KLK^L distinct sums from LKLK stored vectors. A single codebook with the same number of bits must store every one of its KLK^L 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