Sparse Attention Architecture: Indexer and Top-K Selection

    This article breaks down a sparse attention design built on Multi-Head Latent Attention (MLA). It explains how a lightweight indexer scores previous tokens, why selected KV latent vectors are shared across query heads, and how Top-K selection reduces the main attention cost. It also covers FP8 recall risks, RoPE, MHA/MQA switching between training and decoding, deployment checks, and when sparse attention is worth adopting.

    The following analysis is based on public architecture descriptions. The exact model details are not fully public, so specific implementations and numerical results should be confirmed against official documentation and reproduction results.

    Standard Attention Bottleneck

    Standard self-attention grows quadratically with sequence length N. Every time a new token is generated, the model reads the entire historical KV cache. As the context gets longer, both compute and memory-bandwidth pressure increase. Sparse attention aims to reduce the number of key-value pairs each query attends to while preserving model quality.

    MLA as the Base for Sparse Attention

    The design implements sparse attention on Multi-Head Latent Attention (MLA). MLA compresses keys and values into a smaller latent representation through low-rank projection, so the KV cache footprint is smaller.

    After sparse selection is introduced, the model requires each KV latent vector to be shared across multiple query heads for the same query token. The reason is that the selection result must be reused by all heads. If each head selected its own key-value pairs, index and read overhead would multiply. Sharing the selected latent vectors allows all query heads to use the same set, reducing cross-head read and communication cost.

    Fast Indexer: Scoring Previous Tokens

    The fast indexer computes an index score between the current query and each previous token. This score determines which historical tokens enter the later attention computation. The design has three key points:

    • The activation function uses ReLU. ReLU is simple to compute and adds little overhead for an indexing task that only needs ranking.
    • The indexer has few heads and a lightweight network structure.
    • The indexer supports FP8. FP8 reduces memory usage and can use tensor cores to improve throughput, making the indexing step a smaller share of the whole attention pipeline.

    The cost of FP8 must be evaluated separately. The index score is used for ranking, and lower precision may change the ranking and cause important tokens to be missed. The team should measure recall on target tasks and compare the overlap between FP8 and higher-precision Top-K results, rather than looking only at throughput numbers.

    Top-K Selection and Complexity

    After index scores are produced, the system selects the K highest-scoring key-value pairs for each query and only performs standard attention on those K pairs.

    It is important to understand the complexity change accurately. The indexer still scores all previous tokens, so that cost grows with N and is not zero. It is just small per token. What is reduced is the main attention computation after Top-K. When K is much smaller than N, the main attention cost changes from being N-related to being K-related. If the sequence length does not exceed K, sparse selection will not reduce computation, and the system should fall back to full attention.

    K therefore controls the trade-off between efficiency and quality. Too small a K may drop distant but critical information; too large a K saves little. K usually needs to be tuned by task type, such as code completion, long-document QA, and multi-hop reasoning, using validation sets to determine the best value.

    RoPE for Position Information

    Rotary Position Embedding (RoPE) injects position information into vectors. In this design, RoPE is applied separately to the query vector and to indexer-related parameters, with some or all dimensions possibly participating in rotation. Even after keeping only the Top-K tokens, the model can retain relative and absolute position clues from the original sequence. Which dimensions participate in rotation depends on the implementation code.

    MHA and MQA Switching by Phase

    In training and prefill, inputs are usually large batches of complete sequences. This phase is compute-intensive and suits MHA. Each head has independent key/value representations, giving richer feature expression.

    During decoding, one token is generated at a time and the KV cache is read repeatedly. This phase is memory-bandwidth-intensive, so MQA is used: all query heads share the same KV cache, reducing the amount of data read at each step.

    The table below summarizes the differences between the two phases.

    Phase Main bottleneck KV cache access Goal Risks to watch
    Training and prefill Compute Batch reads/writes, regular access pattern Representation quality and fitting quality Higher peak memory; control batch size
    Decoding Memory bandwidth Read historical cache every step Per-token latency and throughput Shared KV may limit expressiveness; evaluate quality

    When the two modes share the same set of weights, outputs should be compared on the same inputs to confirm that switching does not introduce quality deviation.

    Pre-Deployment Checklist

    Before deploying this kind of model, review the following checklist.

    1. Hardware and operators: Confirm that the GPU supports FP8 tensor cores and that the inference framework includes the corresponding sparse attention kernel.
    2. Memory estimation: KV cache size is approximately batch size × sequence length × number of layers × per-layer per-token cache dimension × bytes per element. Use this formula to estimate the largest request, then reserve memory for activations and system overhead.
    3. Interconnect bandwidth: For multi-GPU inference, watch tensor-parallel communication. NVLink or equivalent high-speed interconnect can reduce cross-GPU waiting.
    4. Weight loading: Count the model weight size, measure file storage read throughput, and check whether cold start time is acceptable.
    5. Scaling indicators: Use queue length, time to first token, and GPU utilization as scaling signals, not just request count.
    6. Stress-test metrics: Record at least time to first token (TTFT), time per output token (TPOT), throughput, and peak memory. Run accuracy regression on long-context samples.

    FAQ

    How should Top-K be selected?

    Start from the validation-set quality curve, increase K gradually, find the point where quality plateaus, then set the final value with the latency budget in mind.

    Does FP8 affect accuracy?

    The indexer is relatively tolerant of precision, but it directly determines which tokens are selected. The right approach is to compare Top-K overlap between FP8 and higher-precision indexing and observe downstream task metrics.

    When should MLA use MHA versus MQA?

    Training and prefill should prefer MHA. Decoding can use MQA when memory bandwidth is the bottleneck. The switch depends on the workload mix; the optimal strategy is different for many long batch text requests versus many short interactive replies.

    Applicability Boundary

    Sparse attention is most suitable for tasks with long contexts where the truly relevant information is sparse. For short text, the benefit is small and the extra index overhead may offset the savings. Before choosing an architecture, measure the length distribution of your own business data.

    Overall, this design ties the compute cost of long contexts to the number of selected tokens. Whether the benefit is realized in production depends on kernel implementation, precision trade-offs, and deployment configuration, and needs to be validated with measured data.