qflen/nsa-from-scratch — explained in plain English
Analysis updated 2026-05-18
Study a working example of custom GPU kernels for sparse attention in Triton and CUDA.
Benchmark Native Sparse Attention against FlashAttention-3 on Hopper GPUs.
Train small language models at long context lengths using the provided configs.
Compare sparse attention methods like NSA and MoBA on the same benchmark harness.
| qflen/nsa-from-scratch | 1ncendium/aibuster | aaronmayeux/ha-hurricane-tracker | |
|---|---|---|---|
| Stars | 5 | 5 | 5 |
| Language | Python | Python | Python |
| Setup difficulty | hard | moderate | easy |
| Complexity | 5/5 | 3/5 | 2/5 |
| Audience | researcher | ops devops | general |
Figures from each repo's GitHub metadata at analysis time.
Needs a Hopper generation GPU such as an H100 for the CUDA WGMMA kernels and full benchmark suite.
nsa-from-scratch is a from scratch reimplementation of a technique called Native Sparse Attention, based on a February 2025 research paper from DeepSeek (arXiv 2502.11089). The project focuses on making AI language models handle long pieces of text faster and with less memory. Background: attention is the part of a language model that figures out how words relate to each other. Standard, full attention gets slower and more memory hungry as the amount of text grows, roughly by the square of the length. FlashAttention-3 is a well known fast implementation of full attention. Native Sparse Attention takes a different approach: instead of comparing every piece of text to every other piece, it skips over sections that are unlikely to matter for the current calculation. This repository writes all three parts of Native Sparse Attention, called compressed, selected, and sliding window, as custom GPU code using Triton and CUDA. The selected part also has a separate CUDA C++ version built specifically for Hopper generation GPUs, using a low level instruction called WGMMA. On an H100 NVL GPU, the selected forward pass runs 7.4 times faster than FlashAttention-3 at a 64k token context length. Beyond the core kernels, the project includes a set of five trained models ranging from 100 million to 300 million parameters, tests of how well the models predict text, called perplexity, across context lengths from 2k up to 256k tokens, an evaluation called LongBench v2, and a comparison against a rival method called MoBA. The perplexity results stay roughly stable as context length increases, which suggests the sparse method does not hurt language quality much compared to full attention. The project is written in Python, using PyTorch, Triton, and CUDA C++. It comes with an automated test suite and instructions for reproducing the benchmarks, training runs, and evaluations. The license is Apache 2.0, which allows free use, including commercial use.
A from scratch GPU kernel implementation of Native Sparse Attention, a technique that makes long context AI language models faster and more memory efficient than standard attention.
Mainly Python. The stack also includes Python, PyTorch, Triton.
Apache 2.0 lets you use, modify, and distribute the code freely, including for commercial purposes, as long as you keep the license notice.
Setup difficulty is rated hard, with roughly 1day+ to a first successful run.
Mainly researcher.
This repo across BitVibe Labs
Verify against the repo before relying on details.