Pushing Sparse Attention Decode Kernels for LLMs Past 1000 TFLOPS: A Two-Stage Optimization Journey

    This article details an engineering-driven approach to optimizing the sparse attention (DSA) decode kernel used in the decode phase of large MoE models, built on MLA-style FP8 KV caches. Working from a typical configuration with 96 attention heads, d_qk = 576, d_v = 512, and hundreds of gigabytes of KV cache, it walks through profiling-driven bottleneck analysis and a two-stage optimization path: first matching a high-performance generic MLA decode kernel baseline, then redesigning the kernel for sparse memory-access patterns. The result is measured throughput above 1000 TFLOPS on a flagship GPU—nearly 3x over the baseline—and a reusable methodology for kernel optimization. Readers will learn concrete techniques including block-level async prefetch pipelines, software pipelining and MMA tile alignment, weighted work-list scheduling, fused FP8 dequantization, split-softmax design for context parallelism, and accuracy regression safeguards.

    Pushing Sparse Attention Decode Kernels for LLMs Past 1000 TFLOPS: A Two-Stage Optimization Journey

    TL;DR

    In the decode phase of very large mixture-of-experts (MoE) models, the sparse attention kernel (DSA, a sparse variant based on MLA) is often the hardest part of the inference pipeline to drive at full hardware utilization. Using a typical configuration as our example—96 attention heads, d_qk = 576, d_v = 512, and an FP8-quantized KV cache in the hundreds of gigabytes—this article describes a two-stage, progressively layered optimization approach: first aligning the kernel with the engineering practices of high-performance generic MLA kernels, then making targeted changes for the memory-access characteristics of sparse attention. The final result exceeds 1000+ TFLOPS of measured throughput on a flagship GPU. The focus is not on the specific numbers themselves, but on extracting a reusable, transferable methodology for kernel optimization.

    1. Background: Why Decode Is So Hard to Optimize

    1.1 Compute Characteristics of Decode

    Unlike the prefill phase, which is batch-heavy and compute-intensive, decode generates only one token per request per step. The arithmetic intensity of attention is inherently low, while KV cache reads grow linearly with context length. In the MLA architecture, the KV cache is stored in FP8 after low-rank compression, so a single request’s KV footprint can reach hundreds of MB or even GBs. This means:

    • Memory access is the primary bottleneck: The arithmetic intensity (FLOPs / Bytes) falls well below the GPU’s balance point, so the theoretical ceiling is set by memory bandwidth.
    • Sparsification changes the memory-access pattern: DSA reads only selected KV blocks through token-level selection, which reduces theoretical traffic, but the disjoint, variable-length distribution of selected blocks destroys the regular memory-access pipeline.
    • Intra-batch heterogeneity worsens load imbalance: Different requests hit different numbers of sparse blocks, causing uneven work within the kernel and idle SMs.

    1.2 Goals and Constraints

    The optimization goal is clear: while preserving numerical accuracy (FP8 KV + BF16 computation, with controlled output error) and functional correctness (covering both DCP context-parallel and non-DCP deployment modes), push the actual throughput of the DSA decode kernel toward hardware peak. Constraints include: no changes to model semantics, no additional host-side synchronization, and the kernel must adapt to dynamically changing sparse indices.

    2. Bottleneck Analysis: Find Out Where the Cycles Go

    Before optimizing, we decomposed the baseline kernel layer by layer with a profiler and identified three main bottlenecks:

    Bottleneck 1: Fragmented KV block loading. The sparse block-selection mechanism means the blocks hit by each request are non-contiguous in memory. The baseline issues global memory reads block by block, leading to low TMA/ldg instruction efficiency, no cross-block prefetching, and only about 50% bandwidth utilization.

    Bottleneck 2: Insufficient tensor-core supply. With d_qk=576 and d_v=512, the Q dimension must be split along the head dimension. If software pipelining is poorly laid out, MMA instructions encounter operand-wait bubbles and tensor-core issue rates cannot be sustained.

    Bottleneck 3: High overhead from softmax reductions and sparse-index handling. The number of sparse blocks is variable, and the baseline performs many dynamic branches and LSE (log-sum-exp) correction computations inside the kernel. These consume scarce registers and scheduling resources, causing register spilling and other pressure.

    3. First-Stage Optimization: Align with a High-Performance Baseline

    The first stage follows the principle of “don’t change the algorithm yet; fix the engineering.” We used a mature open MLA decode kernel implementation as the reference and made all the generic optimizations solid:

    3.1 Restructure the Memory-Access Path

    KV reads were changed to a block-level asynchronous prefetch pipeline: using the GPU’s async copy capability, loads for KV blocks N+1 and N+2 are issued while block N is being computed. Because sparse indices are read into shared memory once at kernel entry, prefetch targets are fully precomputable and require no runtime branching. This raised bandwidth utilization from about 50% to over 80%.

    3.2 Software Pipelining and MMA Layout

    The d_qk=576 dimension is split into two segments (e.g., 576 = 512 + 64) so that each MMA’s K dimension aligns with tensor-core-friendly granularity. Warp-level double buffering is applied to both the Q operand and the K/V operands, eliminating wait bubbles between MMAs. In addition, the softmax scale factor is folded into Q during preprocessing to reduce online computation.

    3.3 Load-Balanced Task Partitioning

    To handle large differences in the number of sparse blocks across the batch, the “request × sparse block” pairs are flattened into a work list, and scheduling is weighted by block count rather than dividing work evenly per request. This delivers significant gains for long-tail requests (those hitting especially many blocks) and eliminates inter-SM tail latency.

    After the first stage, the kernel reached 700+ TFLOPS—close to the level of comparable non-sparse MLA decode implementations—but still short of the target. The remaining gap was concentrated in structural overheads introduced by sparsification itself.

    4. Second-Stage Optimization: Targeted Redesign for Sparse Semantics

    4.1 A Sparse-Aware Kernel Form

    The key insight: DSA sparse block hits are highly temporally local—adjacent decode steps select largely overlapping blocks. Based on this, we split the kernel into two levels of granularity: the outer level keeps a “block group” resident on an SM, while the inner level processes blocks within the group with fine-grained pipelining. The prefetch window can now span block boundaries, and LSE correction is batched per group instead of per block, reducing branch overhead by an order of magnitude.

    4.2 Fused FP8 Dequantization

    FP8 KV dequantization was originally a separate elementwise pass. We fused it into the MMA operand-preparation stage: register-level conversion overlaps with MMA issue, hiding dequantization entirely behind tensor-core compute. Combined with vectorized preloading of the scale factors, the separate conversion pass is eliminated.

    4.3 A Unified Code Path for DCP and Non-DCP

    In context-parallel (DCP) deployments, KV shards across ranks introduce additional all-gather or online-correction logic. We adopted a correction-free split-softmax design: each rank independently computes a local LSE, and a single, extremely cheap fusion step at the final reduction merges them. This avoids the global synchronization of a traditional two-pass scheme. The non-DCP path degenerates to the single-rank special case, so both scenarios share the same kernel body, reducing maintenance cost.

    4.4 Accuracy Guardrails

    Every aggressive optimization step is paired with numerical regression against a reference FP16 implementation, monitoring relative error with particular attention to LSE drift on long sequences. In practice, keeping accumulators in FP32 and carefully choosing when to rescale in blocked softmax keeps errors within thresholds validated by training-side testing.

    5. Final Results and Lessons Learned

    After the two progressive stages, the DSA decode kernel achieved a measured throughput of over 1000 TFLOPS on a flagship GPU—nearly 3x over the baseline—while end-to-end decode latency dropped by about 35%. A few transferable lessons:

    1. Align with a mature baseline first, then make targeted changes. Generic optimizations (memory-access pipelining, load balancing, software pipelining) often account for more than half the gains. Don’t skip this step to chase clever tricks.
    2. Realize sparsity gains through structured memory access. Sparsity reduces theoretical traffic, but if the kernel still reads blocks serially in a dense-style layout, the benefits are eaten by fragmented loading.
    3. Bottlenecks are dynamic. Re-profile after every optimization round. Once the first-stage memory bottleneck was resolved, the bottleneck shifted to MMA supply and scalar overhead, and the optimization strategy had to change accordingly.
    4. Guard performance and accuracy together. In FP8 settings, any change to accumulation order or rescaling timing must be backed by numerical regression.

    There is no silver bullet for kernel optimization, but the engineering rhythm of “profiling-driven → progressive refinement → verify at every step” is a reliable path from making a kernel merely usable to making it extreme.