Reformer

Reformer is a memory-efficient transformer that introduces two key innovations: Locality-Sensitive Hashing (LSH) attention (reducing complexity from O(n²) to O(n log n)) and reversible residual layers (reducing memory from O(n_layers × n) to O(n)) — targeting extremely long sequences (64K+ tokens) where both compute and memory are prohibitive, by replacing exact full attention with an efficient approximation that attends only to similar tokens.

What Is Reformer?

  • Definition: A transformer architecture (Kitaev et al., 2020, Google Research) that addresses two memory bottlenecks: (1) the O(n²) attention matrix is replaced by LSH attention that groups similar tokens into buckets and computes attention only within buckets, and (2) the O(L × n) activation storage for backpropagation is eliminated by reversible residual layers that recompute activations during the backward pass.
  • The Two Memory Problems: For a sequence of 64K tokens with 12 layers: (1) Attention matrix = 64K² × 12 × 2 bytes ≈ 100 GB (impossible). (2) Stored activations = 64K × hidden_dim × 12 layers × 2 bytes ≈ 6 GB (significant). Reformer attacks both simultaneously.
  • The Approximation: Unlike FlashAttention (which computes exact attention efficiently), LSH attention is an approximation — it assumes that tokens with high attention weights tend to have similar Q and K vectors, and groups them via hashing.

Innovation 1: LSH Attention

ConceptDescription
Core IdeaTokens with similar Q/K vectors will have high attention weights. Hash Q and K into buckets; only attend within same bucket.
LSH HashRandom projection-based hash function that maps similar vectors to the same bucket with high probability
Bucket SizeSequence divided into ~n/bucket_size buckets; attention computed within each bucket
Multi-RoundMultiple hash rounds (typically 4-8) for coverage — reduces chance of missing important attention pairs
ComplexityO(n log n) vs O(n²) for full attention

How LSH Attention Works

StepActionComplexity
1. HashApply LSH to Q and K vectors → bucket assignmentsO(n × rounds)
2. SortSort tokens by bucket assignmentO(n log n)
3. ChunkDivide sorted sequence into chunksO(n)
4. Attend within chunksFull attention within each chunk (small, ~128-256 tokens)O(n × chunk_size)
5. Multi-roundRepeat with different hash functions, average resultsO(n × rounds × chunk_size)

Innovation 2: Reversible Residual Layers

Standard TransformerReformer (Reversible)
Store activations at every layer for backpropagationOnly store final layer activations
Memory: O(L × n × d) where L = layersMemory: O(n × d) regardless of depth
Forward: y = x + F(x)Forward: y₁ = x₁ + F(x₂), y₂ = x₂ + G(y₁)
Backward: need stored activationsBackward: recompute x₂ = y₂ - G(y₁), x₁ = y₁ - F(x₂)

Reformer vs Other Efficient Attention

MethodComplexityExact?MemoryBest For
Full AttentionO(n²)YesO(n²)Short sequences (<2K)
FlashAttentionO(n²) FLOPs, O(n) memoryYesO(n)Standard training (exact, fast)
Reformer (LSH)O(n log n)No (approximate)O(n)Very long sequences (64K+)
LongformerO(n × w)Exact (sparse)O(n × w)Long documents (4K-16K)
PerformerO(n)No (approximate)O(n)When linear complexity critical

Reformer is the pioneering memory-efficient transformer for very long sequences — combining LSH attention (O(n log n) approximate attention that groups similar tokens via hashing) with reversible residual layers (O(n) activation memory regardless of depth), demonstrating that both the compute and memory barriers of standard transformers can be dramatically reduced for processing sequences of 64K+ tokens, trading exact attention for efficient approximation.

Take reformer further

Ask the copilot about this term, or have our engineers assess it against your process.