Skip to content
SurogateSurogateDocs
↑↓ to move↵ to openesc to close

DeepSeek sparse-attention kernel review

Reviewed on 2026-09-09 at Dogacel/DeepSeek-Sparse-Attention-Kernels@9734cf8.

The repository has useful decode optimizations, especially split sparse attention, radix top-k, and skipping score computation when every key is selected. Surogate neither uses nor vendors its kernels.

AreaReviewed implementationGLM training and native-colocate
IndexerFP8 Q/K, 64 heads, 128 channelsBF16 projections, learned channel-wise pooling; released GLM has 32 indexer heads
Selection2048 individual keys and 64-token physical pagesSelect complete pools, expand their tokens, optionally include the current incomplete pool
AttentionShared absorbed MLA latent KV, 512 latent and 64 positional channels, 16 query headsNoPE; current cache contains expanded per-head K/V
GradientsDecode forward onlySparse attention backward is required for full-weight and LoRA training
TargetB200 contest workloadsValidated here on RTX 5090

Launching the upstream sparse kernel unchanged on an RTX 5090 fails with a shared-memory resource error: 169,984 bytes required, 101,376 bytes available. The trial used three queries, 16 heads, 512 latent channels, 64 positional channels and 2048 selected keys. It produced no timing or numerical result. The repository’s published speedup is for its B200 contest traces and reference implementation; it does not measure GLM training or RTX decode.

There is no repository-wide license at the reviewed revision and no license notice in the Triton source files. The bundled FlashInfer topk.cuh has an Apache-2.0 notice. Obtain licensing for the other files before copying them, or implement the algorithms independently using appropriately licensed components.

Code-review concerns that need resolution before adapting these kernels:

  • Sparse attention synchronizes CTAs through a global atomic counter and spin barrier. Its target advances on the host. A captured graph would replay a fixed target while the device counter keeps advancing, and could consume stale partial results. This concern was not tested because the kernel did not launch on the RTX 5090. Scheduling assumptions also need review for larger grids.
  • Scratch tensors are cached globally with incomplete device/shape keys. Concurrent streams and changing geometry need explicit ownership and synchronization.
  • The top-k binding fixes refinement scratch at 6K keys based on the contest’s maximum padded context of 5824. Long GLM contexts require a different bound.
  • Sparse attention assumes valid selected indices form a prefix. GLM selection can contain masked holes, which must be preserved or compacted explicitly.

For Surogate, keep the registered GLM-specific kernels in surogate/kernels/triton/glm_dsa.py. Future optimizations can replace full sorting with radix selection, skip scoring when all visible pools fit, and split attention across selected keys. Use separate partial/reduction launches or another graph-safe synchronization scheme, and validate forward, backward, packing, cache resets and graph replay independently.

See also: GLM sparse indexer memory for how the indexer behaves in long-context training, and Single-GPU GRPO for GLM in native co-locate.