Prefrontal-AI/latent-sparse-attention

Code for Latent Sparse Attention: Towards Subquadratic Ultra-Long Context Attention

Latent Sparse Attention (LSA)

What it is: A method for making the attention mechanism in large language models much faster when processing very long texts (up to 10 million tokens). It's a research project with a paper, code, and pre-trained weights.

The problem it solves

In standard attention, every word (query) must look at every other word (key) in the text to decide what to focus on. For a text of N words, that's N×N comparisons — which grows quadratically and becomes painfully slow for long documents.

The core idea

Instead of scoring every key individually, LSA groups keys into clusters ("buckets") in a learned latent space. Each query first scores only the cluster centroids — a tiny number of representatives — then attends only to the keys inside the few best clusters, plus a small fixed "sink" and "window" of nearby keys.

Crucially, the attention kernel itself is unchanged — LSA only decides which keys get read, not how they're processed. This means it can be dropped into existing models.

Two variants

Variant How it's made Reported results
D-LSA (Distilled) Applied to an already-trained model by distilling its indexer (teaching it to cluster well) On GLM-5.3-Flash at 10M tokens: 22.7× faster prefill, 4.6× faster decode vs. the model's own sparse attention. On Qwen3.8-27B: 72.4× and 42.5× faster than full attention.
N-LSA (Native) Trained from scratch on next-token prediction alone Matches full attention's accuracy; training is 1.6× faster per layer at 128k tokens.

D-LSA has three sub-flavors: flat search, tree search, and a training-free version using PCA.

What's in the repository

  • Core LSA code (src/lsa/core) — the flat and tree search implementations
  • Distillation code (src/lsa/training/distillation) — for teaching a pre-trained model's indexer
  • Native training code (src/lsa/training/native) — for N-LSA and its baselines (Full Attention, SWA, NSA, PISA)
  • Baselines (src/lsa/baselines) — Block Sparse Attention implementations for comparison
  • Evaluation harness (src/lsa/evaluation) — accuracy, kernel-speed, and end-to-end benchmarks
  • Configs — YAML files for every experiment in the paper

How it's used

LSA is a plugin for SGLang, a serving framework. You:

  1. Pull a special SGLang Docker image (from their fork, which adds long-context support)
  2. Install this repo into the container
  3. Provide a "distilled map" (a file of learned cluster projections)
  4. Launch the server with an lsa block in the model config

The server log prints armed when LSA is active — without that line, the model runs its original attention.

Reproducing the paper

The repo includes a full evaluation harness that submits Slurm jobs. It covers:

  • Accuracy — using the RULER benchmark (26 tasks at 4k and 32k token lengths)
  • Performance — kernel speed on one GPU (128k to 10M tokens) and end-to-end served requests
  • Native training — pre-training a 24-layer model on 100B tokens, then a 128k-token decay on 12B tokens
  • Distillation — learning the index space from a frozen model on long documents

Expected results are documented (e.g., D-LSA Flat on Qwen3.8-27B scores 0.974 at 4k and 0.968 at 32k on RULER, matching the paper).

Released artifacts

  • SGLang fork (tag lsa-v0.1.0) — required for serving
  • LongDocs — a long-document corpus for distillation and training
  • Distilled maps for Qwen3.8-27B and GLM-5.3-Flash (release d-lsa-weights-v1)
  • Checkpoints of all ten 128k-decay models from the native comparison (release n-lsa-weights-v1)
  • RULER haystack sources — pinned by checksum so benchmark sets match the paper byte-for-byte
  • Agent traces and PaperTrail — additional evaluation data

Practical notes

  • Requires Python 3.12 and the uv package manager
  • Training needs the CUDA toolkit; results were measured on NVIDIA H200 GPUs
  • The --disable-prefill-cuda-graph flag is mandatory when serving — LSA selects keys on the host during prefill, which CUDA graphs can't capture
  • License: Apache-2.0 (third-party code keeps its own licenses)

Bottom line

LSA is a drop-in attention optimization that makes ultra-long-context inference and training dramatically faster while keeping accuracy close to the original model. The repo is a complete research package: code, configs, evaluation harness, and pre-trained artifacts for reproducing every result in the paper.

Related

  • Project
  • Project
  • Project
  • Project