Back to Blog
AIPublished on July 21, 2026

Beyond the Attention Bottleneck: How Kimi and DeepSeek-V2 Architected Million-Token Context Windows

Discover how next-generation LLM architectures like DeepSeek's MLA and Kimi's system-level optimizations bypass the traditional transformer memory wall. We dive deep into low-rank compression, decoupled RoPE, and distributed Ring Attention.

The Memory Wall of Modern Transformers

For years, the generative AI race was defined by parameter counts. Today, the battleground has shifted to context window length. The ability to ingest entire codebases, academic libraries, or financial histories in a single prompt has transformed LLMs from simple chat interfaces into sophisticated reasoning engines. However, scaling context windows to one million tokens and beyond introduces a brutal bottleneck: the Key-Value (KV) cache.

In standard Multi-Head Attention (MHA) architectures, the memory footprint of the KV cache scales linearly with both the sequence length and the batch size. For a 70-billion parameter model running at a sequence length of 128,000 tokens, the KV cache for a single user request can exceed 32 GB of VRAM. When scaled to a million tokens, the memory requirements become mathematically prohibitive for standard enterprise hardware.

To bypass this physical barrier, next-generation models like Kimi (by Moonshot AI) and DeepSeek-V2 have rewritten the rules of attention mechanisms and distributed systems. By moving beyond naive Grouped-Query Attention (GQA), these architectures leverage advanced mathematical abstractions and hardware-aware parallelism to make ultra-long context windows economically and computationally viable.


The KV Cache Crisis: Why GQA is No Longer Enough

To understand the innovations of DeepSeek and Kimi, we must first look at how standard transformers manage memory during inference.

During the autoregressive decoding phase, the model generates tokens one by one. To avoid recalculating the keys ($K$) and values ($V$) of all previous tokens at every step, the system stores them in VRAM. This is the KV cache.

$$\text{Memory}_{\text{KVCache}} = 2 \times B \times L \times H \times D \times P$$

Where:

  • $B$ is the batch size.
  • $L$ is the sequence length (context depth).
  • $H$ is the number of key-value heads.
  • $D$ is the head dimension.
  • $P$ is the precision in bytes (e.g., 2 bytes for FP16/BF16).

Grouped-Query Attention (GQA) mitigated this by grouping multiple query heads to share a single key-value head, reducing the KV cache footprint by roughly $8\times$. However, as context windows scaled from 32k to 1M+ tokens, even GQA became a bottleneck. Simply reducing the number of KV heads further degrades model capacity and accuracy. A new architectural approach was needed: compressing the key-value representation itself without losing representation capacity.


DeepSeek-V2’s Multi-Head Latent Attention (MLA)

DeepSeek-V2 introduced a breakthrough optimization called Multi-Head Latent Attention (MLA). Instead of caching high-dimensional key and value vectors, MLA compresses keys and values jointly into a low-rank latent vector during the generation process.

Down-Projection and Up-Projection Mechanics

In MLA, the keys and values are projected into a much smaller latent space ($d_c$) before being cached.

Let $h_t \in \mathbb{R}^d$ be the input hidden state at time step $t$. The model compresses this state into a latent vector $c_t \in \mathbb{R}^{d_c}$ where $d_c \ll d$:

$$c_t = W^{DKV} h_t$$

Where $W^{DKV} \in \mathbb{R}^{d_c \times d}$ is the down-projection matrix. During inference, only this tiny latent vector $c_t$ is stored in the KV cache.

When calculating attention, the model dynamically up-projects this latent vector back to the full key and value spaces using two up-projection matrices, $W^{UK} \in \mathbb{R}^{d \times d_c}$ and $W^{UV} \in \mathbb{R}^{d \times d_c}$:

$$K_t = W^{UK} c_t, \quad V_t = W^{UV} c_t$$

Because the up-projection is a matrix multiplication that can be fused with the query projection during the attention computation, the system never needs to keep the expanded keys and values in VRAM. This reduces the KV cache memory footprint by up to 93% compared to traditional Multi-Head Attention, while maintaining equivalent model expressiveness.

The RoPE Compatibility Problem

Rotary Position Embedding (RoPE) is the industry standard for encoding positional information in transformers. However, RoPE is position-sensitive and must be applied to keys and queries before they are multiplied to compute attention scores.

This presents a major mathematical conflict with low-rank compression: if you apply RoPE to the keys before compressing them, the linear up-projection can no longer recover the positional orientation.

MLA elegantly solves this by decoupling the keys and queries into two distinct parts: a compressed content part and an uncompressed positional part:

  1. Content Keys ($K_t^C$): Generated via the low-rank up-projection of the latent vector $c_t$. These do not undergo RoPE.
  2. Positional Keys ($K_t^R$): A small, decoupled vector that is explicitly embedded with RoPE.

During attention calculation, the query and key vectors are split, and attention is computed as the sum of the content-to-content dot product and the position-to-position dot product. This mathematical decoupling preserves the benefits of both low-rank KV cache compression and precise relative positional encoding.


Kimi's System-Level Optimization: Distributed Ring Attention

While architectural shifts like MLA tackle the physical capacity of VRAM, handling millions of tokens also requires massive distributed systems engineering. This is where Moonshot AI's Kimi excels. To process ultra-long prompts across clusters of GPUs, Kimi relies on advanced context parallelization, most notably Ring Attention.

How Ring Attention Works

Traditional sequence parallelism splits the sequence dimension across multiple GPUs. However, computing the attention matrix requires every query token to interact with every key-value token, which typically demands a massive, blocking All-Gather communication operation across all GPUs.

Ring Attention eliminates this communication bottleneck by organizing GPUs in a logical ring topology.

[ GPU 0 ]  --->  [ GPU 1 ]  --->  [ GPU 2 ]  --->  [ GPU 3 ]
   ^                                                    |
   |____________________________________________________|

Instead of gathering all KV pairs globally, each GPU computes attention for its local chunk of queries using its local chunk of keys and values. Once the local computation is complete, the GPUs concurrently send their key-value blocks to the next GPU in the ring while receiving the next block from the previous GPU.

This communication is entirely overlapped with the computation of the next attention block (GEMM operations). By pipelining the transfer of KV blocks across the ring, the communication overhead is virtually hidden. This allows Kimi to scale its effective context window near-linearly with the number of GPUs in the cluster, bypassing the physical memory limits of any individual accelerator.


Hardware-Aware Co-Design: FlashAttention-3 and Chunked Prefilling

To make these long-context architectures highly performant, systems must optimize for the memory hierarchy of modern GPUs (such as NVIDIA H100s). This involves two critical execution strategies: chunked prefilling and asynchronous execution.

Chunked Prefilling

When a user submits a massive prompt (the "prefill" phase), computing the attention matrix all at once can cause huge memory spikes and compute stalls. Kimi and other modern long-context engines use chunked prefilling. The prompt is split into manageable chunks (e.g., 4096 tokens each).

The model processes these chunks sequentially, updating the KV cache incrementally. This keeps VRAM usage predictable and prevents the system from running out of memory (OOM) during the initial prompt ingestion phase.

Asynchronous Execution with FlashAttention-3

Modern GPUs feature dedicated hardware units for tensor operations (Tensor Cores) and memory transfers (TMA - Tensor Memory Accelerator). FlashAttention-3 exploits these hardware features by decoupling memory layout transformations from compute.

By running block-wise softmax and matrix multiplications on the Tensor Cores while simultaneously using TMA to load the next block of keys and values from HBM (High Bandwidth Memory) to SRAM, the attention computation becomes completely compute-bound rather than memory-bandwidth-bound. This represents the absolute pinnacle of hardware-software co-design, extracting near-maximum theoretical FLOPS from modern silicon during ultra-long context processing.


The New Paradigm of LLM Efficiency

The architectural innovations pioneered by DeepSeek-V2 and Kimi demonstrate that the path to artificial general intelligence is not just about brute-forcing larger clusters; it is about algorithmic and system-level efficiency. By mathematically compressing the attention state through Multi-Head Latent Attention and distributing the computational workload via Ring Attention, these frameworks have democratized access to long-context processing.

As the industry continues to evolve, these efficiency-first paradigms will likely become the standard, proving that elegant engineering can triumph over raw physical constraints.

#Transformer Architecture#Deep Learning#LLM Inference#Ring Attention#Hardware Optimization