Reformer and LSH Attention
Reformer replaces full attention with locality-sensitive hashing that groups similar queries and keys into the same buckets, so each token only attends within its bucket instead of to every other token, cutting attention's cost from quadratic to near-linear.
Prerequisites: The Self-Attention Mechanism, The Quadratic Cost of Attention
Full self-attention makes every token compute a similarity score with every other token, which is wasteful — for most queries, only a handful of keys actually matter, and the rest contribute near-zero attention weight after the softmax. Reformer's idea is to skip computing the scores that will end up near zero, by first grouping similar queries and keys together and only attending within each group.
It does this with locality-sensitive hashing (LSH): a random hash function is chosen so that vectors pointing in similar directions land in the same hash bucket with high probability, and dissimilar vectors usually land in different buckets. Tokens are hashed into buckets, sorted by bucket, and attention is computed only between tokens sharing a bucket (plus a neighboring chunk, to soften boundary effects) — never across the full sequence. Since bucket sizes are kept roughly constant regardless of how long the input is, total attention cost grows close to linearly with sequence length rather than quadratically.
Reformer pairs this with reversible residual layers, which recompute activations during the backward pass instead of storing them, cutting memory further — the two tricks together are what let it handle sequences of tens of thousands of tokens on ordinary hardware.
Reformer uses locality-sensitive hashing to bucket similar queries and keys together and restricts attention to within-bucket pairs, approximating full attention while cutting its cost from quadratic to near-linear in sequence length — at the cost of occasionally missing a relevant token that hashed into a different bucket.
Related concepts
Practice in interviews
Further reading
- Kitaev, Kaiser & Levskaya, Reformer: The Efficient Transformer (2020)