Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

HashHop Attention: Exploring Ultra-Long Context Processing

License

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.

The Challenge of Long Context

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

Why Current Evaluations Fall Short

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.

Enter HashHop: A More Rigorous Approach

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.

How HashHop Works

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:

  1. Smart Selection: First, it learns which tokens might be important reference points (using "hash gates")
  2. Focused Hopping: Instead of looking everywhere, it "hops" from one reference to the next
  3. 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             │ │
│ └─────────────────────────────────────────┘ │
└─────────────────────────────────────────────┘

Testing Multi-Hop Reasoning

Real-world tasks often require following multiple connections. Think about debugging code:

  1. Find a variable definition
  2. Track where it's modified
  3. Follow its usage through function calls
  4. Locate where a bug manifests

To test this capability, we present increasingly complex challenges:

  1. Single Hops: Find one matching hash value

    Query: YOJVrdjK → ???
    Expected: WKPUyWON
    
  2. Multi-Hop Chains: Follow a series of connections

    Hash 1 → Hash 2 → Hash 3 → Hash 4
    
  3. Direct Multi-Hop: Skip intermediate steps

    Hash 1 → Hash 6 (without seeing steps 2-5)
    

Implementation

This repository provides a complete implementation of HashHop attention. Key components:

  • model.py: Core HashHop architecture
  • transformer_model.py: Base transformer components
  • dataset.py: Synthetic dataset generation
  • train.py: Training pipeline
  • evals/: Evaluation tools

Getting Started

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.txt

Training

python train.py

Evaluation

python val.py

Work in Progress

This implementation is a first step toward ultra-long context processing. Our roadmap includes:

  1. 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

Citation

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}
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages