Indexing Cost of Latent-Space Sparse Attention at 10M Tokens: How to Calculate It

    This article calculates the indexing cost of latent-space sparse attention for a 10M-token context with per-head dimension d=128 and K=2048 tokens per query, showing that two-stage indexing can be about 3.2× the cost of sparse attention itself. It compares full attention, sparse attention, and ordinary Top-K retrieval, and explains how latent-space indexing selects tokens in a lower-dimensional space.

    1. Start with the Conclusion: Indexing Cost Can Exceed Attention Itself

    In long-context scenarios, the compute saved by sparse attention may be consumed by the step of “finding which tokens to attend to.” This article uses a concrete configuration: the context length N is 10 million (10^7) tokens, the per-head dimension d is 128, and each query keeps K=2048 tokens. With these parameters, the two-stage indexing overhead is about 3.2× that of sparse attention itself. When choosing an approach, indexing cost must be calculated alongside attention cost; do not look only at attention-kernel latency.

    To make the numbers comparable, all compute totals below are counted for one layer, one head, and the prefill stage, and only multiply-add operations are counted. For simplicity, the roughly 50% reduction from causal masking is ignored, as are softmax and memory-access overhead.

    2. Cost of Full Attention vs. Sparse Attention

    In full attention, each query computes relevance against all historical tokens. Across N queries, total compute is O(N² · d). With N=10^7 and d=128, this is 1.28×10^16. FlashAttention-style operators avoid writing the N×N score matrix to GPU memory, but the compute remains quadratic.

    In sparse attention, each query computes attention only against K selected tokens. Total compute is O(N · K · d). Under the same configuration, this is 2.62×10^12, about 1/4900 of full attention.

    3. What Is Latent Space, and How Is It Different from Ordinary Top-K?

    In this article, latent space refers to compressing queries and keys into a low-dimensional vector space using a set of learnable projection matrices; this dimension is denoted d_idx. The indexer performs selection in this low-dimensional space, while the final attention computation still occurs in the original dimension d. The benefit is that the cost of each similarity comparison drops from d to d_idx.

    Ordinary Top-K retrieval computes the similarity between each query and all keys, then selects the K with the highest scores. Total overhead is O(N² · d_idx). Under the same configuration, this term reaches 6.4×10^15, far exceeding sparse attention itself.

    Latent-space sparse attention splits indexing into two steps. In the coarse-filtering stage, keys in latent space are first clustered into C clusters, each with a center vector. Each query is first compared with the C centers.