The simplest implementation of recent Sparse Attention patterns for efficient LLM inference.
Jupyter Notebook
92
12 commits
updated Jul 17, 2025
nanoSparseAttention provides clean, educational implementations of recent Sparse Attention mechanisms for both prefilling and generation stages of LLM inference. The repository prioritizes clarity and understanding over performance, making it ideal for learning and experimentation.
We implemented a Jupyter notebook that provides:
The notebook has been prepared for the purpose of NeurIPS 2024 Dynamic Sparsity Workshop - check it out if you want to learn more about dynamic execution, not only in the context of self-attention!
Assuming that we want to use Python venv it's as easy as:
git clone https://github.com/PiotrNawrot/nano-sparse-attention
cd nano-sparse-attention
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip setuptools wheel psutil
pip install -e ./
The repository provides two main scripts for experimenting with sparse attention mechanisms:
from nano_sparse_attn.attention import InferenceHandler, DenseAttention, LocalAndSinksAttention
from nano_sparse_attn.utils import load_model_and_tokenizer, load_examples, update_attention, model_forward
# Load model and prepare inputs
model, tokenizer = load_model_and_tokenizer()
model_inputs = load_examples(tokenizer, num_examples=1)
# Create an inference handler with Local Window + Attention Sinks
handler = InferenceHandler(
prefill_attention=LocalAndSinksAttention(
window_size=256,
attention_sinks=16
),
generation_attention=DenseAttention()
)
# Update model's attention mechanism and run forward pass
update_attention(model, handler)
loss = model_forward(model, model_inputs, handler)
# Get information about the attention mechanism
info = handler.info()
print(f"Loss: {loss}")
print(f"Sparsity: {info['prefill']['sparsity']}")
# Assumes imports from the previous example
from nano_sparse_attn.attention import SnapKVAttention
# Create an inference handler with SnapKV for generation
handler = InferenceHandler(
prefill_attention=DenseAttention(),
generation_attention=SnapKVAttention(
approximation_window=64,
token_capacity=256
)
)
# Update model's attention mechanism and run forward pass
update_attention(model, handler)
loss = model_forward(model, model_inputs, handler)
# Get information about the attention mechanism
info = handler.info()
print(f"Loss: {loss}")
print(f"Sparsity: {info['generation']['sparsity']}")
For ready-to-use scripts check out main_prefill.py and main_generate.py. For a detailed walkthrough of the repository and information about extending it to new models, datasets, and attention patterns, refer to this README.
Contributions are welcome! Our goal is to keep this repository up-to-date with the latest Sparse Attention methods, by consistently adding new methods. Feel free to submit a Pull Request if 1) you want a new method to be added or 2) [even better] you have an implementation of a new Sparse Attention method!
Piotr Nawrot - Website - piotr@nawrot.org
Edoardo Maria Ponti - Website - eponti@ed.ac.uk
11 commits
1 commits
Jupyter Notebook
98.4%
Python
1.6%
The simplest implementation of recent Sparse Attention patterns for efficient LLM inference.
Jupyter Notebook
92
12 commits
updated Jul 17, 2025
nanoSparseAttention provides clean, educational implementations of recent Sparse Attention mechanisms for both prefilling and generation stages of LLM inference. The repository prioritizes clarity and understanding over performance, making it ideal for learning and experimentation.
We implemented a Jupyter notebook that provides:
The notebook has been prepared for the purpose of NeurIPS 2024 Dynamic Sparsity Workshop - check it out if you want to learn more about dynamic execution, not only in the context of self-attention!
Assuming that we want to use Python venv it's as easy as:
git clone https://github.com/PiotrNawrot/nano-sparse-attention
cd nano-sparse-attention
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip setuptools wheel psutil
pip install -e ./
The repository provides two main scripts for experimenting with sparse attention mechanisms:
from nano_sparse_attn.attention import InferenceHandler, DenseAttention, LocalAndSinksAttention
from nano_sparse_attn.utils import load_model_and_tokenizer, load_examples, update_attention, model_forward
# Load model and prepare inputs
model, tokenizer = load_model_and_tokenizer()
model_inputs = load_examples(tokenizer, num_examples=1)
# Create an inference handler with Local Window + Attention Sinks
handler = InferenceHandler(
prefill_attention=LocalAndSinksAttention(
window_size=256,
attention_sinks=16
),
generation_attention=DenseAttention()
)
# Update model's attention mechanism and run forward pass
update_attention(model, handler)
loss = model_forward(model, model_inputs, handler)
# Get information about the attention mechanism
info = handler.info()
print(f"Loss: {loss}")
print(f"Sparsity: {info['prefill']['sparsity']}")
# Assumes imports from the previous example
from nano_sparse_attn.attention import SnapKVAttention
# Create an inference handler with SnapKV for generation
handler = InferenceHandler(
prefill_attention=DenseAttention(),
generation_attention=SnapKVAttention(
approximation_window=64,
token_capacity=256
)
)
# Update model's attention mechanism and run forward pass
update_attention(model, handler)
loss = model_forward(model, model_inputs, handler)
# Get information about the attention mechanism
info = handler.info()
print(f"Loss: {loss}")
print(f"Sparsity: {info['generation']['sparsity']}")
For ready-to-use scripts check out main_prefill.py and main_generate.py. For a detailed walkthrough of the repository and information about extending it to new models, datasets, and attention patterns, refer to this README.
Contributions are welcome! Our goal is to keep this repository up-to-date with the latest Sparse Attention methods, by consistently adding new methods. Feel free to submit a Pull Request if 1) you want a new method to be added or 2) [even better] you have an implementation of a new Sparse Attention method!
Piotr Nawrot - Website - piotr@nawrot.org
Edoardo Maria Ponti - Website - eponti@ed.ac.uk
11 commits
1 commits
Jupyter Notebook
98.4%
Python
1.6%