AI models learn in two ways: during training and through context at inference time. Until now, training has dominated because context windows were relatively short. But what if we could dramatically extend these context windows? What if a model could reason over entire codebases, documentation, and libraries during inference?
This repository implements HashHop Attention, an experimental approach to ultra-long context processing inspired by Magic.dev's research.
Current language models face a fundamental limitation: they struggle with long contexts. When you feed a large document into a model, it's like asking someone to remember every detail of a 700-page book after a single reading. The computational costs grow quadratically with context length, and memory requirements become astronomical.
To put this in perspective:
- A standard transformer processing 100M tokens would require 638 H100 GPUs just to store its attention cache
- The computational cost would be millions of times higher than processing short contexts
- Traditional attention mechanisms become impractical beyond a few thousand tokens
The popular way to test long-context models has been the "Needle in a Haystack" approach: hide a random fact in a long document and ask the model to find it. Sounds reasonable, right?
There's a catch. When you place "Alice and Bob had coffee at Blue Bottle" in the middle of a novel about whales, it stands out like a sore thumb. The model doesn't need to truly understand or remember the context - it just needs to spot the anomaly.
This creates a false sense of capability. Models appear to handle long contexts well, but they're really just exploiting these semantic shortcuts.
HashHop introduces a fundamentally different way to test and implement long-context processing. Instead of natural language, we use random hash pairs:
jJWlupoT → KmsFrnRa
vRLWdcwV → sVLdzfJu
YOJVrdjK → WKPUyWON
Why random hashes? Because they're:
- Completely meaningless (no semantic hints)
- Incompressible (can't be summarized or simplified)
- Position-independent (no structural hints)
This forces the model to actually store and retrieve information rather than relying on patterns or context clues.
Imagine trying to follow a trail of references through a massive document. Traditional attention tries to look at everything at once - like attempting to read every page simultaneously. HashHop instead works more like a detective following leads:
- Smart Selection: First, it learns which tokens might be important reference points (using "hash gates")
- Focused Hopping: Instead of looking everywhere, it "hops" from one reference to the next
- Information Transformation: As it moves between hops, it transforms what it's learned to help find the next connection
The architecture looks like this:
┌─────────────────────────────────────────────┐
│ HashHopTransformer │
├─────────────────────────────────────────────┤
│ ┌─────────────────────────────────────────┐ │
│ │ Embedding Layer │ │
│ └─────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────┐ │
│ │ Positional Encoding │ │
│ └─────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────┐ │
│ │ HashHopBlock × N │ │
│ │ ┌───────────────────────────────────┐ │ │
│ │ │ HashHopAttention │ │ │
│ │ │ ┌─────────────┐ ┌─────────────┐ │ │ │
│ │ │ │ Hash Gate │ │Hash Project │ │ │ │
│ │ │ └─────────────┘ └─────────────┘ │ │ │
│ │ └───────────────────────────────────┘ │ │
│ │ ┌───────────────────────────────────┐ │ │
│ │ │ Feed Forward │ │ │
│ │ └───────────────────────────────────┘ │ │
│ └─────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────┐ │
│ │ Projection Layer │ │
│ └─────────────────────────────────────────┘ │
└─────────────────────────────────────────────┘
Real-world tasks often require following multiple connections. Think about debugging code:
- Find a variable definition
- Track where it's modified
- Follow its usage through function calls
- Locate where a bug manifests
To test this capability, we present increasingly complex challenges:
-
Single Hops: Find one matching hash value
Query: YOJVrdjK → ??? Expected: WKPUyWON -
Multi-Hop Chains: Follow a series of connections
Hash 1 → Hash 2 → Hash 3 → Hash 4 -
Direct Multi-Hop: Skip intermediate steps
Hash 1 → Hash 6 (without seeing steps 2-5)
This repository provides a complete implementation of HashHop attention. Key components:
model.py: Core HashHop architecturetransformer_model.py: Base transformer componentsdataset.py: Synthetic dataset generationtrain.py: Training pipelineevals/: Evaluation tools
git clone https://github.com/theharshithh/hash-hop-attn.git
cd hash-hop-attn
uv venv; source .venv/bin/activate
uv pip install -r requirements.txtpython train.pypython val.pyThis implementation is a first step toward ultra-long context processing. Our roadmap includes:
- Scaling Efficiency
- Multi-scale hash indexing
- Increasing the number of hops
- Efficent ways to handle the hashes such that we can scale to true ultra long contexts
If you use HashHop in your research, please cite:
@misc{hashhop2025,
author = {Harshith Murthy},
title = {HashHop Attention for Ultra-Long Context Processing},
url = {https://github.com/theharshithh/hash-hop-attn},
year = {2025}
}
This project is licensed under the MIT License - see the LICENSE file for details.