DeepSeek Sparse Attention (DSA) From Scratch
by Sebastian Raschka (LLMs-from-scratch)
A readable PyTorch implementation of DeepSeek Sparse Attention's lightning indexer and top-K token selector inside a GPT model.
Overview
This tutorial sits in the ch04/09_dsa bonus folder of Sebastian Raschka's LLMs-from-scratch repository, the code companion to Build a Large Language Model (From Scratch). It was added in May 2026 from a reader contribution that Raschka announced on his blog. It explains and implements DeepSeek Sparse Attention (DSA), introduced with DeepSeek-V3.2 and first shipped in the experimental V3.2-Exp release. The folder holds a README, a standalone GPT-style reference implementation (gpt_with_kv_dsa.py) and a test file (test_dsa.py). The README opens with the problem: causal attention costs O(L^2) compute, the KV cache grows linearly, and sliding-window attention handles this with a fixed local window. DSA replaces that fixed pattern with a learned one built from two parts. A lightning indexer scores each query and past-token pair with a ReLU and gated sum over a few index heads. A token selector keeps the top-K positions and masks the rest to negative infinity before the softmax. A table maps these to the LightningIndexer, MultiHeadAttentionWithDSA and GPTModel classes, and a uv run command exposes index heads, index head dimension and top-K as flags. The README is upfront about its limits. The code keeps the dense score matrix and applies the mask on top, so it shows the mechanism but not the fused-kernel speedups, and it leaves out DeepSeek's full MLA stack.
At a Glance
- Topic
- Models
- Level
- Advanced
- Format
- Tutorial
- Cost
- Free
- Duration
- ~1-2 hours to read the README and run the script (estimate; one README, one model file, one test file)
- Provider
- Sebastian Raschka (LLMs-from-scratch)
- Hands-on
- Yes — code/exercises
- Certificate
- None
What You’ll Learn
- ✓Why standard causal attention costs O(L^2) compute and how sliding-window attention trades range for cost
- ✓How DeepSeek Sparse Attention replaces a fixed sparse window with a learned, per-query token selection
- ✓How the lightning indexer scores query and past-token pairs with ReLU and a gated sum over index heads
- ✓How the top-K token selector masks unselected positions to negative infinity before the softmax
- ✓How to wire DSA into a GPT-style model with a KV cache through MultiHeadAttentionWithDSA and GPTModel
- ✓How index head count, index head dimension and top-K are exposed as command-line flags
- ✓How the full DeepSeek-V3.2 design differs, running its indexer on MLA's compressed representations
- ✓Why a masked dense implementation teaches the mechanism without delivering production kernel speedups
Highlights
- •Lives in LLMs-from-scratch, one of the most-starred LLM education repos on GitHub (about 109k stars)
- •Ships with tests (test_dsa.py) alongside a single readable model file, not a sprawling framework
- •States plainly what it omits (MLA, fused sparse kernels, deployment optimizations), so readers do not mistake it for a benchmark
- •Sits next to sibling from-scratch folders for GQA, MLA, sliding-window attention, Gated DeltaNet and MoE for side-by-side comparison
- •README links the DeepSeek-V3.2 technical report and V3.2-Exp reference code for going deeper
Who It’s For
Best For
- ✓Engineers reading DeepSeek-V3.2 or V4 papers who want the sparse-attention mechanism in plain PyTorch
- ✓Inference and long-context practitioners evaluating sparse versus hybrid attention designs
- ✓Readers of Build a Large Language Model (From Scratch) continuing into modern attention variants
Prerequisites
- •Solid understanding of multi-head causal self-attention and KV caching
- •Comfort reading and running PyTorch model code (the repo uses uv to run scripts)
- •Helpful: the earlier ch04 bonus folders on GQA, MLA and sliding-window attention
FAQ
What is DeepSeek Sparse Attention (DSA) From Scratch?
A from-scratch PyTorch tutorial in Sebastian Raschka's LLMs-from-scratch repository that implements DeepSeek Sparse Attention (DSA), the learned sparse attention introduced with DeepSeek-V3.2. It is for engineers who know standard causal attention and want to see how a lightweight indexer picks which past tokens each query attends to.
Is DeepSeek Sparse Attention (DSA) From Scratch free?
DeepSeek Sparse Attention (DSA) From Scratch is free to access.
What level is DeepSeek Sparse Attention (DSA) From Scratch for?
DeepSeek Sparse Attention (DSA) From Scratch is aimed at a advanced audience. Recommended background: Solid understanding of multi-head causal self-attention and KV caching, Comfort reading and running PyTorch model code (the repo uses uv to run scripts), Helpful: the earlier ch04 bonus folders on GQA, MLA and sliding-window attention.
How long does DeepSeek Sparse Attention (DSA) From Scratch take?
Expect roughly ~1-2 hours to read the README and run the script (estimate; one README, one model file, one test file). Most learners work through it at their own pace.
What will I learn from DeepSeek Sparse Attention (DSA) From Scratch?
You'll learn: Why standard causal attention costs O(L^2) compute and how sliding-window attention trades range for cost; How DeepSeek Sparse Attention replaces a fixed sparse window with a learned, per-query token selection; How the lightning indexer scores query and past-token pairs with ReLU and a gated sum over index heads; How the top-K token selector masks unselected positions to negative infinity before the softmax; How to wire DSA into a GPT-style model with a KV cache through MultiHeadAttentionWithDSA and GPTModel; How index head count, index head dimension and top-K are exposed as command-line flags; How the full DeepSeek-V3.2 design differs, running its indexer on MLA's compressed representations; Why a masked dense implementation teaches the mechanism without delivering production kernel speedups.
Topics
Sources
This page was written from 3 sources, 1 on domains other than github.com.