Transformer-VQ: Linear-Time Transformers via Vector Quantization

Lucas Dax LingleICLR 2024

Introduction

Transformer-VQ introduces a novel decoder-only architecture that achieves linear-time complexity for dense self-attention by utilizing vector-quantized keys. It successfully maintains the high generation quality of standard transformers while running significantly faster at long sequence lengths—over 12x faster at 32k tokens—and scaling effortlessly to contexts as large as 131k tokens.

The Quadratic Bottleneck

The "Achilles' heel" of the standard Transformer is its self-attention mechanism. As the sequence length ($T$) grows, the computational cost and memory footprint increase quadratically ($O(T^2)$). Processing a document ten times longer requires a hundred times more computational work.

While many "efficient transformers" have been proposed—using techniques like sparsity, low-rank approximations, or kernel methods—they often trade off model quality or prove difficult to implement for autoregressive decoding (generating text one token at a time). The primary challenge in the field has been finding a method that is truly linear in time, maintains the "dense" representational power of standard attention, and remains competitive on rigorous benchmarks.

The Transformer-VQ Approach

The core innovation of this paper is Vector-Quantized (VQ) Attention. The architecture is built on the Gated Attention Unit (GAU) backbone, a more efficient single-headed alternative to standard multi-head attention, and modifies the attention mechanism in several key ways:

1. Quantized Keys

Instead of using raw, continuous vectors for "keys" in the attention mechanism, Transformer-VQ maps each key to the nearest entry in a learned "codebook" (a set of prototype vectors).

Vector quantization of attention keys.

2. Factorization and Compressive Caching

Because many different keys in a long sequence will inevitably map to the same codebook entry, the model can group them. Instead of calculating attention for every individual key, it calculates attention over the unique prototypes in the codebook.

In a standard transformer, the "KV cache" grows linearly with sequence length, eventually bottlenecking memory. Transformer-VQ introduces a novel compressive caching mechanism. It stores aggregated "sums" of value vectors associated with each codebook entry.

Matrix factorization enabling linear-time attention.

3. Linear-Time Recurrence

The authors mathematically prove that by using these quantized keys and a specific block-level recurrence, the model can compute the exact same results as dense attention but in $O(T)$ time. If the keys are already quantized, the Transformer-VQ cache reduces the cost from quadratic to linear losslessly.

Key Insights

  • VQ as a Computation Compressor: Vector quantization is traditionally used for data compression (e.g., in VQ-VAEs for images). Here, it is ingeniously used to compress the attention computation itself. If two keys are similar enough to be quantized to the same code, they share the same attention weights, eliminating redundant math.
  • Sampling Simplicity: Unlike many hierarchical models that require complex "feature consolidation" steps during generation, Transformer-VQ updates its cache every token. This makes it highly practical and simple to implement for real-world autoregressive deployment.
  • Dense Attention in Linear Time: Most linear-time models (like RNNs or State Space Models) suffer from a bottleneck where they must compress the entire past into a fixed-size state. Transformer-VQ effectively achieves this compression using the codebook, yet still allows the query to "look back" at a dense representation of the past.

Empirical Results

The authors evaluated Transformer-VQ on three major long-context benchmarks: Enwik8 (byte-level language modeling), PG-19 (long-document books), and ImageNet64 (autoregressive image generation).

  • Unprecedented Speed: At a sequence length of 8k, the optimized implementation is 3x faster than a comparable standard transformer. At 32k, it is over 12x faster. It successfully scaled to 131k tokens on a single TPU v3 without running out of memory.
  • Competitive Quality:
    • On Enwik8, it achieved 0.99 bpb, matching the much larger Transformer-XL while using 33% fewer parameters.
  • On PG-19, it reached 26.6 perplexity, highly competitive with state-of-the-art recurrent models.
  • On ImageNet64, it set a new state-of-the-art for small models (3.22 bpb) and absolute state-of-the-art for 1.2B parameter models (3.16 bpb).

Limitations and Future Work

While highly promising, the authors note a few limitations. The compressive cache makes standard i.i.d. attention dropout impossible, which can lead to overfitting on smaller datasets like Enwik8. Additionally, while the theoretical speedup is massive, the current implementation relies on JAX/Flax; porting the custom kernels to lower-level frameworks like Triton or CUDA could unlock even greater hardware efficiency. Finally, future work will need to explore how well these VQ-attention mechanisms follow scaling laws up to frontier model sizes (70B+ parameters).

Summary updated

BibTeX
@inproceedings{lingle2024transformer,
  author = {Lucas Dax Lingle},
  title = {{Transformer-VQ: Linear-Time Transformers via Vector Quantization}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=oDdzXQzP2F},
}
Figure