Language models, retrieval, prompting, and language adaptation · Long-context and retrieval-augmented language · LNG048 · Architecture
Reformer
Combines locality-sensitive hashing attention with reversible layers to reduce memory demands in long-sequence models.
In CodeSplash
Documented in the Academy only. There's no Set up button.
- Availability
- Explained here
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's own run
- CodeSplash's run
- The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Combines locality-sensitive hashing attention with reversible layers to reduce memory demands in long-sequence models. Hash-based candidate selection approximates content-based attention instead of inspecting every token pair.
Question / objective
Can approximate content-based attention and reversible computation lower long-sequence memory cost?
Architecture / computational structure
LSH groups similar query/key representations and reversible blocks reduce saved activations. Learned embeddings/projections are parameters; hash rounds, bucket counts, chunk sizes, and reversible layout are hyperparameters.
Assumptions
Hash collisions and missed neighbors affect attention quality; approximate retrieval changes with random projections.
Formula / computational definition
Hash queries/keys into buckets, attend primarily within shared or nearby buckets, and combine multiple hash rounds. Reversible residual blocks reconstruct activations during backpropagation, trading recomputation for storage savings.
Parameters and notation
Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Bucket count, hash rounds, chunk sizes, shared query/key design, reversible-layer configuration, attention masks, and random seed.
Fitting, fine-tuning, and adaptation
Use a verified implementation for task loss or language-model training; ordinary dense LoRA recipes may not cover shared projections and reversible blocks. Freeze or adapt modules only with supported tooling. Validate hash-seed variance, gradient correctness, memory, quality, and domain retention.
How to use and calibrate
Compare loss and retrieval-sensitive tasks against dense attention, runtime by sequence length, peak memory, and hash-seed variability. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.
Example application
Explore long-sequence modeling when storing dense attention activations is prohibitive.
Interpretation / pitfalls
Watch out
Memory gains depend on a correct specialized implementation and can be offset by sorting or padding overhead. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.
Python and R code examples
Full implementation route: Transformers documents Reformer support, while the original implementation provides reproduction details. R normally uses reticulate. The native sign-hash illustration is only a simplified LSH component, not the full angular hashing and reversible network used by Reformer.
Example scope: Simplified sign-projection LSH buckets; not Reformer angular hashing or reversible layers.
Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.
Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — Simplified sign-projection LSH buckets; not Reformer angular hashing or reversible layers.
Shared setup for this chapter
import math
def softmax(xs):
m = max(xs)
es = [math.exp(x-m) for x in xs]
return [e/sum(es) for e in es]
def dot(a, b):
return sum(x*y for x, y in zip(a, b))
def matvec(matrix, vector):
return [dot(row, vector) for row in matrix]
def meanvec(rows):
return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]
def sigmoid(x):
return 1/(1+math.exp(-x))
def nll(probabilities):
return -sum(math.log(p) for p in probabilities)
vectors, projection = [[1,0],[0.8,0.2],[-1,0],[0,-1]], [0.5,0.4]
buckets = [int(dot(x,projection)>=0) for x in vectors]
print(buckets)
Component demo, not a trained model — Simplified sign-projection LSH buckets; not Reformer angular hashing or reversible layers.
Shared setup for this chapter
softmax <- function(x) {
shifted <- exp(x-max(x))
shifted/sum(shifted)
}
vectors <- rbind(c(1,0),c(0.8,0.2),c(-1,0),c(0,-1)); projection <- c(0.5,0.4)
buckets <- as.integer(drop(vectors %*% projection)>=0)
print(buckets)
Sources
- Source 1arxiv.org
From the CodeSplash reference, entry LNG048.