explaingit

qflen/nsa-from-scratch

Analysis updated 2026-05-18

5PythonAudience · researcherComplexity · 5/5LicenseSetup · hard

TLDR

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.

Mindmap

mindmap
  root((repo))
    What it does
      Sparse attention kernels
      Faster than FlashAttention-3
      Long context support
    Tech stack
      Python
      PyTorch
      Triton
      CUDA
    Use cases
      Study GPU kernels
      Benchmark attention methods
      Train small models
    Audience
      Researchers
      ML engineers
    Evaluation
      Perplexity sweep
      LongBench v2
      MoBA comparison

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Study a working example of custom GPU kernels for sparse attention in Triton and CUDA.

USE CASE 2

Benchmark Native Sparse Attention against FlashAttention-3 on Hopper GPUs.

USE CASE 3

Train small language models at long context lengths using the provided configs.

USE CASE 4

Compare sparse attention methods like NSA and MoBA on the same benchmark harness.

What is it built with?

PythonPyTorchTritonCUDA C++

How does it compare?

qflen/nsa-from-scratch1ncendium/aibusteraaronmayeux/ha-hurricane-tracker
Stars555
LanguagePythonPythonPython
Setup difficultyhardmoderateeasy
Complexity5/53/52/5
Audienceresearcherops devopsgeneral

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · hard Time to first run · 1day+

Needs a Hopper generation GPU such as an H100 for the CUDA WGMMA kernels and full benchmark suite.

Apache 2.0 lets you use, modify, and distribute the code freely, including for commercial purposes, as long as you keep the license notice.

In plain English

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.

Copy-paste prompts

Prompt 1
Explain how the compressed, selected, and sliding window branches of Native Sparse Attention differ in this repo.
Prompt 2
Walk me through reproducing the 64k context throughput benchmark from nsa-from-scratch on an H100 GPU.
Prompt 3
Help me adapt the Triton selected kernel in this repo to a different attention head dimension.
Prompt 4
Summarize how NSA's perplexity results compare to the dense baseline in this repo's evaluation.

Frequently asked questions

What is nsa-from-scratch?

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.

What language is nsa-from-scratch written in?

Mainly Python. The stack also includes Python, PyTorch, Triton.

What license does nsa-from-scratch use?

Apache 2.0 lets you use, modify, and distribute the code freely, including for commercial purposes, as long as you keep the license notice.

How hard is nsa-from-scratch to set up?

Setup difficulty is rated hard, with roughly 1day+ to a first successful run.

Who is nsa-from-scratch for?

Mainly researcher.

Open on GitHub → Explain another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.