[{"content":"On January 27, 2025, Nvidia lost nearly $600 billion in market value in a single day, the largest one-day loss in US stock market history, and DeepSeek set it off. People were stunned to learn that DeepSeek-V3, the base model behind DeepSeek-R1, had been trained for less than a tenth of what GPT-4 reportedly cost, yet it matched GPT-4o on benchmarks. How could it be so cheap? One of the key pieces of its architecture is Multi-Head Latent Attention, or MLA, which drastically cuts the memory that attention needs, above all the KV cache at inference time. This post takes MLA apart one step at a time.\nMulti-head attention: every head caches its own keys and values I\u0026rsquo;ve covered how much memory the KV cache takes in an earlier post, so here\u0026rsquo;s a quick recap. During Transformer inference, multi-head attention stores the key and value vectors it computes for every token. That\u0026rsquo;s the KV cache, and the longer the context, the more memory it takes. Each head has its own keys and values. If the Transformer has \\(L\\) decoder layers, each with \\(h\\) heads of dimension \\(d_h\\), the KV cache for one token holds:\n$$ \\begin{aligned} \\text{KV cache per token}\u0026=2\\times L\\times h\\times d_h\\\\ \u0026=2\\times L\\times 128\\times 128=32{,}768\\,L \\end{aligned} $$ Take DeepSeek-V2 as an example. It has 128 attention heads, each 128-dimensional, so \\(2 \\times 128 \\times 128\\) means every token takes \\(L \\times 32{,}768\\) elements. That\u0026rsquo;s the problem with multi-head attention: the KV cache takes far too much memory.\nThere are some direct fixes for this. The middle diagram below is Multi-Query Attention, or MQA: every query head shares a single set of keys and values, so the number of KV heads drops to 1. That certainly saves memory, but the cost is just as direct. All the attention heads now work from the same keys and values, which takes away much of the point of having multiple heads and can hurt the quality of the model\u0026rsquo;s output. On the right is Grouped-Query Attention, or GQA. The query heads are split into groups, and the heads in a group share one set of keys and values, so the number of KV heads falls to a fraction of what it was. GQA is the general form of MQA, and the cost is the same in kind: several heads have to share one set of K and V matrices, the differences between those heads are shaved away, and model quality becomes a trade-off.\n(The diagrams use 8 query heads to keep them readable.)\nLow-rank joint compression: cache one latent vector Is there any other way to shrink the KV cache besides MQA and GQA? DeepSeek came up with a new one, called low-rank key-value joint compression. The core idea is to compress each token\u0026rsquo;s vector first. The compressed result is called a latent vector, and the KV cache stores this latent instead of the keys and values. Later, when the attention scores are computed, every head\u0026rsquo;s keys and values are decompressed, that is, projected back out, from the latent. \u0026ldquo;Low-rank\u0026rdquo; means the latent has a very small dimension, far smaller than the total size of all the keys and values. \u0026ldquo;Joint\u0026rdquo; means the keys and values aren\u0026rsquo;t compressed and decompressed separately; they share a single compressed latent. Attention computed with low-rank joint compression is Multi-head Latent Attention, MLA.\nCall the latent\u0026rsquo;s dimension \\(d_c\\). Then MLA\u0026rsquo;s KV cache for each token becomes:\n$$ \\text{KV cache per token}=L\\times d_c=512\\,L $$ DeepSeek-V2\u0026rsquo;s latent has 512 dimensions, so each token takes only \\(L \\times 512\\) elements. Compare that with the \\(L \\times 32{,}768\\) for multi-head attention earlier: it\u0026rsquo;s 1/64 of what plain multi-head attention needs.\nThe definitions: project down, then up So how exactly is the latent computed? Let \\(h_t\\) be the current token\u0026rsquo;s input vector, which has 5,120 dimensions in DeepSeek-V2. The latent \\(c_t^{KV}\\) is a down-projection matrix \\(W^{DKV}\\) times \\(h_t\\); the D stands for down. The latent has 512 dimensions, a tenth of the input, and it\u0026rsquo;s what gets cached at inference. When multi-head attention needs the keys and values, it multiplies the latent by the up-projection matrices \\(W^{UK}\\) and \\(W^{UV}\\) to get \\(k_t\\) and \\(v_t\\); the U stands for up. The key and value vectors have 16,384 dimensions, but logically they\u0026rsquo;re 128 heads of 128 dimensions each, concatenated into one \\(128 \\times 128\\) vector to make the computation convenient.\n(The shapes are illustrative, not to scale.)\nAt this point you might wonder whether compressing the KV cache this hard also sacrifices model quality. Go back to the original multi-head attention. There, too, the keys and values are projections of the input vector \\(h_t\\). But \\(h_t\\) isn\u0026rsquo;t designed only for attention. The FFN and every later decoder layer use it as well, so think of it as the data bus of the Transformer, of which attention only needs a part. MLA takes advantage of exactly that. The latent vector is designed to capture the information that actually needs to go into attention, while the full multi-head structure is kept: different heads can still have different keys and values, so architecturally it\u0026rsquo;s a complete multi-head attention. What it compresses away is mostly information that never needed to enter attention, so the effect on output quality is small. (MLA is trained with this architecture from the start; this isn\u0026rsquo;t a claim that any multi-head attention model could be squeezed into 512 dimensions without loss.)\nLow-rank compression works on the query too, not just on the keys and values. Multiplying the input \\(h_t\\) by a down-projection matrix \\(W^{DQ}\\) gives the query latent \\(c_t^Q\\), which has 1,536 dimensions. When attention needs the query, \\(c_t^Q\\) is multiplied by the up-projection matrix \\(W^{UQ}\\) to get \\(q_t\\). There are a lot of symbols here, but you only need to hold on to the two latent vectors; everything revolves around them. Like the keys, \\(q_t\\) has 16,384 dimensions, again 128 heads of 128 dimensions.\nYou may have noticed that the query never goes into the KV cache: \\(c_t^Q\\) can be thrown away as soon as it\u0026rsquo;s used. So why compress it? It does nothing for inference. Compressing the query pays off in training. During training the intermediate results of every layer are kept for backpropagation, and squeezing the query down to 1,536 dimensions reduces the memory those activations take. So the KV compression shrinks the KV cache at inference, and the query compression shrinks the activations during training.\nMatrix absorption: compression that also saves compute Low-rank joint compression doesn\u0026rsquo;t just shrink the KV cache. It can also cut the compute at inference dramatically. That may sound backwards. Compression normally means decompression, so with every new token, attention now has to decompress on top of its usual work, which should add a lot of computation. This is where a trick called matrix absorption comes in. Say the latest token is token \\(i\\). To predict the next token, we dot \\(q_i\\) with the key of every earlier token. Each term \\(q_i \\cdot k_j\\) can be written as \\(q_i^{\\top} k_j\\). Now replace \\(k_j\\) with its compressed definition, \\(W^{UK} c_j^{KV}\\). Matrix multiplication is associative, so we can move the parentheses and compute \\(q_i^{\\top} W^{UK}\\) first, then multiply the result by \\(c_j^{KV}\\). Both forms give exactly the same result; only the order of the computation changes.\nWhat difference does the order make? Suppose there are 100 tokens before the current one, and compare the two ways of computing. On the left, \\(W^{UK}\\) is merged into the key, which is the original order. For every earlier token, step one computes \\(W^{UK} c_j^{KV}\\). That matrix multiplication costs \\(16{,}384 \\times 512 \\times 1\\), because \\(W^{UK}\\) is a \\(16{,}384 \\times 512\\) up-projection matrix. Step two multiplies the result by \\(q_i^{\\top}\\), which costs \\(1 \\times 16{,}384 \\times 1\\). Both steps run for every earlier token, so 100 tokens cost the two steps added together, times 100. The exact count doesn\u0026rsquo;t matter; what to notice is that step one is a very expensive matrix multiplication.\nNow see what happens when \\(W^{UK}\\) is merged into the query. Step one multiplies \\(q_i^{\\top}\\) by \\(W^{UK}\\), which costs the same \\(16{,}384 \\times 512 \\times 1\\). But once \\(W^{UK}\\) is merged into the query, it doesn\u0026rsquo;t have to be done again: the result is kept and multiplied by each earlier token\u0026rsquo;s latent \\(c_j^{KV}\\). Each of those dot products costs only \\(1 \\times 512 \\times 1\\), so the total is step one plus step two times 100. The expensive step runs only once, and what\u0026rsquo;s left in the loop is a small dot product in the latent\u0026rsquo;s tiny dimension. Put the two orders side by side, and at \\(n = 100\\), merging \\(W^{UK}\\) into the query does roughly 100 times less work. The longer the context, the bigger the gap.\nAt heart, matrix absorption is what programmers call hoisting a loop invariant. \\(W^{UK}\\) is the same for every key, so it shouldn\u0026rsquo;t be computed \\(n\\) times inside the loop; it should be moved out of the loop and computed once. Low-rank compression keeps the cached tokens small, and matrix absorption means they never have to be decompressed. Together, these two are why MLA saves both memory and compute.\nThe RoPE conflict: a different rotation at every position MLA looks perfect so far, but we\u0026rsquo;ve forgotten one thing: positional encoding. Nearly all of today\u0026rsquo;s frontier models use rotary position embedding, RoPE, which rotates the query and the key by an angle that depends on each token\u0026rsquo;s position. Let\u0026rsquo;s add it to the formula. \\(q_i\\) becomes \\(\\mathrm{RoPE}(i)\\,q_i\\), \\(k_j\\) becomes \\(\\mathrm{RoPE}(j)\\,k_j\\), and then we take the dot product. Expanding it, the transposed query becomes \\(q_i^{\\top}\\) times \\(\\mathrm{RoPE}(i)^{\\top}\\). The steps in between are the standard RoPE manipulations, which I derived in full in the RoPE post, so I won\u0026rsquo;t repeat them here. In the last step, we expand \\(k_j\\) into \\(W^{UK}\\) times the latent \\(c_j^{KV}\\).\nThe trouble is that \\(\\mathrm{RoPE}(j-i)\\). If we try to absorb the matrix as before, it no longer works, because this RoPE matrix now sits between \\(q_i^{\\top}\\) and the projection matrix \\(W^{UK}\\), and it changes with the token on the right. Associativity still holds, but every time \\(c_j^{KV}\\) changes, \\(\\mathrm{RoPE}(j-i)\\) changes with it, so there\u0026rsquo;s no way to precompute a single left-hand result that\u0026rsquo;s independent of \\(j\\). All that\u0026rsquo;s left is to compute everything in the original order. Adding RoPE breaks matrix absorption and sends the dot products back to the original, expensive dimension. So how do we fix it?\nDecoupled RoPE: content and position in separate branches Since mixing RoPE into the content projections causes the problem, DeepSeek\u0026rsquo;s answer is to separate them. Every head\u0026rsquo;s key and query are split into two parts. The first is the content branch: the keys and queries projected from the latents as before. This part gets no RoPE at all, so matrix absorption works as usual. The second is the RoPE branch, dedicated to encoding position. When the two parts are concatenated and dotted together, the result is exactly the sum of the two parts\u0026rsquo; separate dot products, so the attention score becomes the content score plus the RoPE score. This is called decoupled RoPE.\nSo how is the RoPE branch computed? For the key, the input vector \\(h_t\\) is multiplied by a new projection matrix \\(W^{KR}\\) and then rotated by RoPE according to the token\u0026rsquo;s position, giving \\(k_t^R\\). It never goes through the latent\u0026rsquo;s compression and decompression. It has only 64 dimensions, half the size of the content part \\(k_t^C\\), and all heads share this one copy. For the query, the starting point is the query latent \\(c_t^Q\\), which is multiplied by the projection matrix \\(W^{QR}\\) and then rotated, giving \\(q_t^R\\). It has 8,192 dimensions, which logically means 128 heads of 64 dimensions each. In all these symbols, the R stands for rotate.\nWhy does the RoPE branch give the key only one copy, but the query one per head? It comes down to the fact that \\(k_t^R\\) has to go into the KV cache. It isn\u0026rsquo;t decompressed from the latent \\(c_t^{KV}\\), and \\(h_t\\) isn\u0026rsquo;t cached at inference, so the only option is to store the rotated \\(k_t^R\\) directly. If every head had its own, each token would need \\(128 \\times 64 = 8{,}192\\) more cached elements, and the cache we worked so hard to shrink would grow right back. \\(q_t^R\\), on the other hand, is thrown away after use and takes no cache at all, so it can have one copy per head and keep as much of each head\u0026rsquo;s query diversity as possible.\nThe KV cache for each token becomes:\n$$ \\text{KV cache per token}=L\\times(d_c+d_r)=L\\times(512+64)=576\\,L $$ Here \\(d_c\\) is the latent\u0026rsquo;s 512 dimensions and \\(d_r\\) is the RoPE branch\u0026rsquo;s 64, so the cache grows only slightly, to about 1/57 of multi-head attention\u0026rsquo;s. As I see it, the design is a trade-off: within the limits of the KV cache, it keeps as much of the model\u0026rsquo;s expressive power as possible.\nValue absorption: weight and sum first, project after Now let\u0026rsquo;s walk through the full attention computation after decoupling. First, the dot product splits into two parts. The content part contributes \\(q_i^C \\cdot k_j^C\\), and the RoPE part contributes \\(q_i^R \\cdot k_j^R\\). Since \\(k_j^R\\) has only 64 dimensions and sits in the cache as is, it\u0026rsquo;s computed directly. The two terms are added, divided by the square root of each head\u0026rsquo;s content dimension plus position dimension, \\(128 + 64\\), and passed through softmax to give the attention weight \\(A(i, j)\\) of the current token \\(i\\) on each earlier token \\(j\\).\nNext comes the weighted sum over the value vectors. The output \\(o_i\\) uses these weights to add up every earlier token\u0026rsquo;s \\(v_j\\), written as the sum over all \\(j\\) of \\(A(i, j)\\,v_j\\). Expand \\(v_j\\) into \\(W^{UV} c_j^{KV}\\). Since \\(W^{UV}\\) doesn\u0026rsquo;t depend on \\(j\\), it can be pulled all the way out of the sum and put in front. In other words, the weights first mix all the earlier tokens\u0026rsquo; latents, and only then is the result projected up, once. The weights are already fixed by the softmax at this point, so only linear steps are being rearranged. And because every head\u0026rsquo;s output is then multiplied by the output matrix \\(W^O\\), and both \\(W^O\\) and \\(W^{UV}\\) are fixed weights, the two can be merged into one matrix ahead of time. This is matrix absorption again.\n(Merging \\(W^O\\) and \\(W^{UV}\\) is an algebraic identity, not a requirement. For DeepSeek-V3, storing the merged matrices would take nearly four times as many weights as keeping the two projections separate, so an implementation can keep them apart and still apply them after the weighted sum.)\nNotice the pattern in matrix absorption. MLA has three up-projection matrices. The key\u0026rsquo;s \\(W^{UK}\\) can be merged ahead of time with the query\u0026rsquo;s \\(W^{UQ}\\), and the value\u0026rsquo;s \\(W^{UV}\\) can be merged with the output matrix \\(W^O\\). So at inference, the up-projections don\u0026rsquo;t need to run on their own, and the cached tokens only ever take part through their 512-dimensional latents \\(c_j^{KV}\\), plus the shared 64-dimensional RoPE key. The cache is compressed, yet it never has to be decompressed. That\u0026rsquo;s the cleverest part of MLA.\nThe full MLA architecture To finish, let\u0026rsquo;s review MLA\u0026rsquo;s overall architecture. The input vector \\(h_t\\) goes through three projections at once. On the left, \\(W^{DQ}\\) projects it down to the query latent \\(c_t^Q\\). On the right, \\(W^{DKV}\\) projects it down to the KV latent \\(c_t^{KV}\\). In the middle, \\(W^{KR}\\) projects it and RoPE rotates the result, giving \\(k_t^R\\). From the query latent \\(c_t^Q\\), two branches go up. The content branch multiplies it by \\(W^{UQ}\\) to give each head\u0026rsquo;s \\(q_t^C\\), and the RoPE branch multiplies it by \\(W^{QR}\\) and rotates the result to give each head\u0026rsquo;s \\(q_t^R\\). Concatenated, the two parts form each head\u0026rsquo;s full query. From the KV latent \\(c_t^{KV}\\), \\(W^{UK}\\) and \\(W^{UV}\\) give each head\u0026rsquo;s \\(k_t^C\\) and \\(v_t^C\\). Each head\u0026rsquo;s \\(k_t^C\\) is concatenated with the shared \\(k_t^R\\) in the middle to form its full key, and \\(v_t^C\\) is its value. Finally, the queries, keys and values all go into multi-head attention to compute the scores.\nReferences DeepSeek-AI (@deepseek_ai). “DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model.” arXiv, 2024. ","permalink":"https://zhangchitc.com/posts/multi-head-latent-attention/","summary":"MLA caches one 512-dimensional latent per token instead of 128 heads\u0026rsquo; worth of keys and values, and matrix absorption means it never has to decompress them. We derive it step by step: joint compression, absorption, why RoPE breaks it, and the decoupled RoPE branch that fixes it.","title":"How DeepSeek's Multi-Head Latent Attention Works"},{"content":"When an LLM runs inference on one request at a time, most of the GPU\u0026rsquo;s compute sits idle. Think of a road at rush hour: every car could carry at least four people, but each one has only the driver, so most of the road\u0026rsquo;s capacity goes to waste. That\u0026rsquo;s what happens on the GPU, and it\u0026rsquo;s the reason inference engines batch requests. Batching sends several inputs through the model together, which keeps the Tensor Cores busy and raises the model\u0026rsquo;s throughput dramatically. Frameworks like vLLM and SGLang go a step further with a technique called continuous batching. This post walks through how it works.\nNaive batching: the whole batch finishes together I\u0026rsquo;ve covered how a Transformer runs inference in earlier posts, so here\u0026rsquo;s a quick recap. Generating a response happens in two phases. The first is prefill: the model reads your entire prompt in a single pass to get ready to answer.\nThe second is decode. The model starts writing its answer, and each forward pass produces exactly one token. That token is appended to the input and used to compute the next one. Generating text this way is called autoregressive generation. To keep things simple, this description covers a single request.\nSo what does the most naive form of batching look like? Say requests A, B and C arrive at the same time. The simplest approach is to pad all three to the length of the longest one, send them through the Transformer as three tensors of the same length, and produce the next token for all three requests on every forward pass. When one request emits EOS, the end-of-sequence token, the batch still isn\u0026rsquo;t done. All three requests keep computing until every one of them has finished, and only then is the batch complete.\nPut simply, a naive batch starts together and finishes together, and nothing is released until the longest request is done. Imagine one request with a very long answer. The slots of the requests that already finished burn compute for nothing, new requests can only wait, and the GPU\u0026rsquo;s capacity is wasted.\nContinuous batching: a new request joins as soon as one finishes Can we do better? That\u0026rsquo;s what continuous batching is for. Take the same example. As soon as we see that request A has emitted EOS, we stop keeping A in the batch and let a waiting request, D, take its place. Say D\u0026rsquo;s prompt is 4 tokens long. The next input sent through the Transformer is then D\u0026rsquo;s 4 tokens plus 1 token from B and 1 token from C. One step later, C finishes as well, and the next waiting request takes C\u0026rsquo;s place in the same way.\nThe scheduling decision has moved. Instead of waiting for the whole batch to finish, it happens at every forward step: as soon as one request finishes, a waiting one fills its slot. The GPU never idles, and new requests don\u0026rsquo;t have to wait for the batch to drain.\nPacking the inputs: pad into a block, or flatten The idea is easy to follow, but making it work raises plenty of practical questions. As we just saw, a single batch can contain both prefill and decode requests. How is the Transformer supposed to handle that? Let\u0026rsquo;s take it one step at a time.\nThe first question is how to combine these requests\u0026rsquo; inputs. Say the embedding dimension is d. Request D is in prefill, so its input is a [4, d] matrix. Requests B and C are both decoding, so each of their inputs is a [1, d] matrix. There are two ways to combine them. The first is to add a dimension, turning each 2D input into a 3D one, so [4, d] becomes [1, 4, d]. Since the three inputs aren\u0026rsquo;t the same length, the two [1, d] matrices have to be padded with blank tokens up to [4, d], and the combined input becomes a [3, 4, d] tensor. Now imagine a new request with a very long prompt joins the batch. Every decode request would have to be padded to that same length, which is a huge waste of compute. Padding is not the way to go.\nThe second way is to skip the extra dimension and join the three matrices end to end into one flat [6, d] matrix. There\u0026rsquo;s no padding at all, but it creates new problems further along in the computation, and those need solving too.\nOne layer: which operators care how the input is packed Let\u0026rsquo;s look at what a Transformer decoder computes. Here\u0026rsquo;s the structure of one decoder layer. The input first goes through a layer norm and into masked multi-head attention. The result is added back to the input as a residual, goes through another layer norm into the FFN, the feed-forward network, and gets one more residual add. That\u0026rsquo;s the layer\u0026rsquo;s output.\nGoing one level deeper, multi-head attention projects its input into the Q, K and V matrices with a linear layer, computes attention, and passes the result through one more linear layer, the output projection. The FFN is simpler: a linear layer that projects up to a wider dimension, an activation function, and a linear layer that projects back down.\nWith the operators laid out, a pattern stands out. Layer norm, the QKV projection, the whole FFN, in fact everything except attention, works on each token independently. Each operation turns one row vector into another row vector, with no reference to the tokens above or below it. That property is perfect for an input made of several requests joined together. These operators don\u0026rsquo;t care which request a token belongs to, because every token goes through the same transformation.\nAttention is a different story. At its core, each token\u0026rsquo;s query vector is dotted with other tokens\u0026rsquo; key vectors, and that\u0026rsquo;s where the packed input breaks down. Take a token from request D. Dotting it with the tokens before it in D is fine. Dotting it with the tokens after it is not, because those belong to B and C, a different request entirely, and a token from D has no business looking at another request\u0026rsquo;s tokens. So every computation in the Transformer is indifferent to how the input is packed, except attention. Attention can\u0026rsquo;t be applied directly: it has to know which tokens in the packed input belong to which request.\nRagged batching: attention in per-request blocks So how does attention change to handle a packed input? The technique is called ragged batching. This figure shows the attention score matrix it computes.\nStart with the queries. They come from projecting this step\u0026rsquo;s input, so just as the input has 6 rows, the query matrix has 6 rows too. The keys and values they\u0026rsquo;re dotted with are a different matter. Request D is in prefill, so its keys and values are also projected from the input and cover the same 4 tokens. Dotting them pairwise gives a 4×4 lower-triangular score matrix. Request B is decoding, so its one query has to be dotted with every token it has seen so far. Where are those earlier tokens? They\u0026rsquo;re stored in the KV cache. Read them out, add the current token, and B\u0026rsquo;s keys and values cover 5 tokens, so B\u0026rsquo;s scores form a 1×5 matrix. Request C works the same way. After softmax, each row of scores weights the value vectors, giving one output vector per query, and the last row of each request is the one that produces that request\u0026rsquo;s next token.\nWhen a query and a key belong to different requests, the pair is masked out and gets zero weight. Most of the matrix is masked this way, and in practice those pairs aren\u0026rsquo;t computed at all. They\u0026rsquo;re drawn here only to make it clear which values actually get computed. The upshot of ragged batching is that the attention matrix no longer has as many columns as rows. The queries are this step\u0026rsquo;s new tokens joined together, the keys are each request\u0026rsquo;s cached history plus its new tokens, and Q and K don\u0026rsquo;t need to be the same length at all.\nInside the kernel: cu_seqlens splits six rows back into three With the idea clear, let\u0026rsquo;s look at how ragged batching is implemented. The diagram shows the operator\u0026rsquo;s data flow. The packed input first goes through the QKV projection to get the query, key and value matrices. The keys and values are then written into the KV cache, each into its own request\u0026rsquo;s entry. New request D starts with an empty cache and adds 4 new tokens; request B adds 1 new token to the 4 it already has. Once the cache is written, all the keys and values are read back and sent, along with the queries, into the ragged attention kernel, which splits the work by request and computes it in parallel. D\u0026rsquo;s scores are 4 queries against 4 keys, B\u0026rsquo;s are 1 query against 5 keys, and C\u0026rsquo;s are 1 query against 3. The outputs are joined back into a matrix with the same 6 rows.\nThe pseudocode uses a special array called cu_seqlens. It\u0026rsquo;s a boundary table that records the row where each request starts and ends in the packed input. With 3 requests of [4, 1, 1] tokens, cu_seqlens is [0, 4, 5, 6], meaning request 0 covers tokens 0 through 4 − 1, and so on. With cu_seqlens, it\u0026rsquo;s easy to reach any single request inside the large packed matrix. Real kernels take exactly this kind of table: FlashAttention\u0026rsquo;s flash_attn_varlen_func takes it as cu_seqlens_q and cu_seqlens_k, and PyTorch\u0026rsquo;s varlen_attn calls them cu_seq_q and cu_seq_k.\nFirst, we project the input once to get Q, K and V. Then, for every request in the batch, we use its slice of the new keys and values to update that request\u0026rsquo;s KV cache. With that done, attention can begin. For each request, cu_seqlens tells us which rows of Q are its own, and we read that request\u0026rsquo;s own part of the KV cache. The cache is stored in pages of physical blocks, so it\u0026rsquo;s read one block at a time. For each block, we dot this request\u0026rsquo;s queries with the block\u0026rsquo;s keys to get attention scores, use the scores to weight the block\u0026rsquo;s values, and accumulate the result into a running state. This is online softmax, which I covered in full in the FlashAttention post; if it\u0026rsquo;s unfamiliar, the complete online softmax code is there. When every request is done, the output matrix has all 6 rows again, and one last output projection finishes attention for this layer.\n(The function names in the pseudocode are made up for teaching, apart from cu_seqlens. A real implementation is a single kernel launch, and the two Python loops become CUDA blocks running in parallel.)\nSummary: what it saves is compute spent on empty slots Back to the question we started with. Sending several requests through one forward pass does put idle compute to work, but forming a batch isn\u0026rsquo;t enough on its own: once the roster is fixed, the requests that finish first leave their slots burning compute. Continuous batching comes down to three steps. First, scheduling moves from the end of the batch to every forward step: whoever finishes leaves, and whoever is waiting joins. Second, the mixed inputs aren\u0026rsquo;t padded but flattened into one long sequence, so every per-token operator runs over it in one pass, unchanged. Third, attention, the one operator that can\u0026rsquo;t be applied directly, is computed per request, with each request reading only its own part of the KV cache. What continuous batching really saves isn\u0026rsquo;t any one user\u0026rsquo;s waiting time. It\u0026rsquo;s the compute the GPU would otherwise spend, every step, on empty slots.\nReferences Rémi Ouazan Reboul et al. “Continuous batching from first principles.” Hugging Face Blog, 2025. PyTorch (@PyTorch). “torch.nn.attention.varlen.” PyTorch documentation. ","permalink":"https://zhangchitc.com/posts/continuous-batching/","summary":"Naive batching holds every slot until the longest request finishes. Continuous batching swaps requests in and out at every forward step, which means packing prefill and decode tokens into one flat input and splitting attention back out per request with a cu_seqlens boundary table.","title":"How Continuous Batching Enables High-Concurrency LLM Inference"},{"content":"In an earlier post I covered vLLM\u0026rsquo;s PagedAttention and how it manages the KV cache in fixed-size blocks of GPU memory. Plenty of readers asked about SGLang next, so this post covers SGLang\u0026rsquo;s core attention mechanism, RadixAttention.\nThe radix tree: three prompts, one tree SGLang\u0026rsquo;s key idea is a data structure called a radix tree. It finds the prefixes that different requests have in common quickly, and reuses the KV cache earlier requests already computed for them, so that work isn\u0026rsquo;t done twice. Take three prompts: \u0026ldquo;Why my code throws errors\u0026rdquo;, \u0026ldquo;Why GPU memory runs out\u0026rdquo; and \u0026ldquo;Why my code is slow\u0026rdquo;. Put them into a radix tree and you get the shape below. Everything starts from one shared root, identical prefixes are merged, and the tree branches where the prompts start to differ. Each edge holds a run of tokens, and a prompt is what you get by concatenating the tokens on every edge along a path from the root.\n\u0026ldquo;Why my code throws errors\u0026rdquo;, for example, is the path from the root that keeps to the left all the way down to a leaf. Each node also stores a list that says where in GPU memory the KV cache for the tokens on its incoming edge lives: the indices of the physical slots holding it. The column on the right shows which token sits in each slot.\n(To keep the figure readable, leader lines are drawn for three of the nodes. The numbers inside every node give the full mapping.)\nThe radix tree on the left organizes the prompt prefixes so they can be reused, and the slot indices in its nodes tie it one-to-one to the real KV cache in GPU memory on the right. As a result, a shared prefix is computed and cached only once, and every later request with that prefix reuses its KV cache instead of computing it again.\nThe physical KV pool: slots are fixed cells in GPU memory Now for the physical slots themselves. When SGLang starts, it carves GPU memory into two arrays in one go, k_buffer and v_buffer. At the top level each array is split into L layers, because the Transformer has L decoder layers. Each layer is then cut into physical slots, and each slot holds one token\u0026rsquo;s K and V for that layer. So a physical slot is a column that runs through every layer: slot 4, the one inside the amber dashed outline, is the same position sliced out of every layer. Along one layer there are num_tokens slots, the capacity of the whole pool in tokens.\nThe indices of every free slot live in a tensor called free_pages. At startup it holds 1 through num_tokens. Two operations work on it. alloc(n) takes n free slots from the front of the queue, so the alloc(2) in the figure takes slots 1 and 2. free() takes a list of slot indices and returns them to the pool, appended at the back, which is why the indices a later alloc() hands out aren\u0026rsquo;t guaranteed to be contiguous. The detail that matters most is that free_pages is itself a tensor on the GPU. Indices taken from it can be used directly to read and write k_buffer and v_buffer, with no copy between CPU and GPU.\nZoom into a single slot and it is a tensor of shape num_heads × head_dim, the number of attention heads times the size of each head. Put that together with the layout above, and k_buffer and v_buffer each have the shape [L, num_tokens, num_heads, head_dim].\nTreeNode: what a node actually holds Back to the radix tree and how it\u0026rsquo;s implemented. The central data structure is TreeNode, and it has a few important fields. children is a map that holds all of the node\u0026rsquo;s child nodes. parent points to the parent node. More important is key, of type RadixKey, which holds the tokens on the edge from the parent to this node. As the definition of RadixKey below shows, it is essentially a list of token IDs. (Its extra_key field keeps, for example, requests on different LoRA adapters from sharing a cache entry.)\nThe value that goes with the key is a tensor: the indices of the physical slots where those tokens\u0026rsquo; KV cache ended up. Finally, last_access_time records when the node was last touched. When GPU memory gets tight, this timestamp decides which nodes are evicted to make room.\nOne detail about children. It\u0026rsquo;s a map, so what are its keys and values? The values are, of course, the child nodes. The key for each child is the first token on the edge from the parent to that child. Why only one token rather than the whole edge? Because children only exist where the prompts diverge: their edges have no common prefix, so they must already differ at the very first token. That one token is enough to tell every child apart.\nHere is the \u0026ldquo;my code\u0026rdquo; node from the tree, zoomed in to show the value of each field right now. children holds two entries: \u0026ldquo;throws\u0026rdquo;, the first token of \u0026ldquo;throws errors\u0026rdquo;, points to node [4,5], and \u0026ldquo;is\u0026rdquo;, the first token of \u0026ldquo;is slow\u0026rdquo;, points to [10,11]. parent points to [1]. key is every token on the edge from the parent, so it\u0026rsquo;s \u0026ldquo;my\u0026rdquo; and \u0026ldquo;code\u0026rdquo;. value is tensor([2, 3]). It follows that a node\u0026rsquo;s key and value always have the same length, because their elements correspond one to one.\nInserting a node: walk the tree, split where the match stops The core operation on a radix tree is inserting a node. Say we want to add a new prompt: \u0026ldquo;Why GPU memory runs out\u0026rdquo;. First, walk down from the root, comparing the prompt with each edge in turn. Wherever it matches, keep going. Where it stops matching, you\u0026rsquo;ve found the branch point. Here the very first edge from the root doesn\u0026rsquo;t match all the way: only its first token, \u0026ldquo;Why\u0026rdquo;, does.\nSecond, split the node. At the point where the match stopped, pull the longest common prefix out and cut the existing edge there. One edge and one node become a common-prefix node plus a node for the rest of the branch, and the common prefix becomes the parent. Here \u0026ldquo;Why\u0026rdquo; is split off into a new common-prefix node, [1], and the old node becomes its child.\nThird, under the common-prefix node, create a new child node and edge for the part of the new prompt that didn\u0026rsquo;t match, and allocate that many physical KV slots for it. The tokens after \u0026ldquo;Why\u0026rdquo;, \u0026ldquo;GPU memory runs out\u0026rdquo;, didn\u0026rsquo;t match anything, so they get a new child node, with slots [6,7,8,9].\n(The starting tree here is for illustration. In the walkthrough below the same final tree is reached in a different order.)\nNotice what steps two and three change: only the slot indices stored in TreeNodes. The KV cache itself never moves in GPU memory. Splitting and inserting rearrange the indices that point at the KV cache, while the actual KV data stays put in physical memory the whole time. Compare this with PagedAttention, which finds the longest common prefix by computing a hash for each block and looking it up in a table. RadixAttention finds it by walking the tree to the node where the longest common prefix ends. They\u0026rsquo;re two different ways to answer the same question, and both are neat.\nEvicting a node: pop the oldest leaf off a heap Besides inserting, we also need to evict nodes when physical memory runs out. The key data structure here is evictable_leaves, a set of TreeNodes that tracks the leaves of the radix tree that can currently be evicted. When it\u0026rsquo;s time to evict, these leaves are arranged into a min-heap ordered by last_access_time. Each eviction pops the node at the top of the heap, the one that hasn\u0026rsquo;t been accessed for the longest time, deletes it and frees the physical slots it held. Deleting a leaf can turn its parent into a leaf, so evictable_leaves and the heap are updated to include it, and the process repeats until enough physical memory has been freed.\nIn this tree, evictable_leaves holds three leaves, circled in red. Their last_access_time values are time 2, time 2 and time 1. So when a node has to go, the one the min-heap gives us is the leaf for \u0026ldquo;GPU memory runs out\u0026rdquo;, and its four physical slots, 6, 7, 8 and 9, are freed.\nThe full walkthrough: three requests, every structure updated That\u0026rsquo;s every piece of the radix tree. Now let\u0026rsquo;s run one complete example to see how RadixAttention behaves. To keep it small, assume the pool has only 11 slots, every request generates a single token and then finishes, and the requests arrive one after another.\nAt time 0, request 0, \u0026ldquo;Why my code throws errors\u0026rdquo;, arrives. The tree is still empty, so the walk stops at the root, and the hit value, the set of cached slots the request can reuse, is empty. None of the five tokens can be skipped, and all of them go through the forward pass. So we alloc(5) from free_pages, which hands out slots 1 to 5, and after the forward pass those slots hold the KV cache for \u0026ldquo;Why my code throws errors\u0026rdquo;. Once that\u0026rsquo;s done, the whole prompt is inserted into the tree as a single edge of five tokens. Its node\u0026rsquo;s value is [1,2,3,4,5], and its last_access_time is time 0.\nAt time 1, request 1, \u0026ldquo;Why GPU memory runs out\u0026rdquo;, arrives. Walking the tree with this prompt, the first edge only matches as far as its first token, \u0026ldquo;Why\u0026rdquo;. So the node is split here, and \u0026ldquo;Why\u0026rdquo; becomes the common prefix. Its slot, [1], is a hit and doesn\u0026rsquo;t need to be recomputed. The remaining four tokens, \u0026ldquo;GPU memory runs out\u0026rdquo;, do need computing, so alloc(4) gives us slots [6,7,8,9]. Once the Transformer has computed their K and V, the remaining tokens and their slots are inserted into the tree, as shown in the dashed outline. Notice that both leaves now have a last_access_time of time 1: the left leaf is also new, because it was just created by the split.\nAt time 2, request 2, \u0026ldquo;Why my code is slow\u0026rdquo;, arrives. The walk starts at the root again. \u0026ldquo;Why\u0026rdquo; matches the first edge, which brings us to the first node. The next token is \u0026ldquo;my\u0026rdquo;, which is in the children map, so we go left. But that edge doesn\u0026rsquo;t match all the way: only \u0026ldquo;my\u0026rdquo; and \u0026ldquo;code\u0026rdquo; do. So the node is split again, and those two tokens become a common node. The slot values collected along the way are [1,2,3]. That KV cache doesn\u0026rsquo;t need recomputing, and only \u0026ldquo;is slow\u0026rdquo; has to be computed. Two more slots, 10 and 11, come from free_pages, and at this point the free slots have all been handed out. Finally the computed slots and tokens are inserted into the radix tree, giving the node in the dashed outline. Again, the newly inserted node and its sibling on the left were both created by this request, so both have a last_access_time of time 2.\nEviction kicks in: out of slots, so a leaf goes At time 3 something interesting happens. A new request, \u0026ldquo;Why my code won\u0026rsquo;t compile\u0026rdquo;, comes in. The first step is still the walk. \u0026ldquo;Why\u0026rdquo; matches completely, so we go down; \u0026ldquo;my code\u0026rdquo; also matches completely, so we keep going left. Below that, the next token, \u0026ldquo;won\u0026rsquo;t\u0026rdquo;, isn\u0026rsquo;t in the children map. This time there\u0026rsquo;s no need to split anything: the walk lands exactly on the node where the longest common prefix ends. The collected slots, [1, 2, 3], can be skipped, and only 2 tokens need computing.\nBut remember that after the last request, free_pages had no free slots left, so this request triggers an eviction. The path the request just matched, circled in green, is locked first, so the eviction can\u0026rsquo;t take the prefix this request is about to use. By the min-heap rule, the least recently accessed node in evictable_leaves is the one on the right, \u0026ldquo;GPU memory runs out\u0026rdquo;, with a last_access_time of time 1. It\u0026rsquo;s removed from the tree, and its four slots, 6, 7, 8 and 9, are returned to free_pages. Eviction works a whole node at a time, so four slots come back even though this request needs only two.\nNow there\u0026rsquo;s room to compute. Just as before, the remaining two tokens, \u0026ldquo;won\u0026rsquo;t\u0026rdquo; and \u0026ldquo;compile\u0026rdquo;, get two slots, 6 and 7. They\u0026rsquo;re computed and inserted into the radix tree. Slots 8 and 9 are struck through in the slot row: free() only returns their numbers to the pool, and the old KV for \u0026ldquo;runs out\u0026rdquo; stays in them until a later alloc() overwrites it.\nSummary: all the savings come from the prefix match Back to where we started. RadixAttention puts the prefixes of every request into a single tree, with runs of tokens on the edges and slot indices in the nodes. A new request first walks its prompt down the tree and goes as far as it can. Where it gets stuck, the tree is split and the rest of the prompt is hung underneath. When memory runs short, the leaf that\u0026rsquo;s gone longest without being touched is removed. In this whole pipeline, only one step saves compute: the moment match_prefix finds a hit. What it saves isn\u0026rsquo;t memory but the entire prefill for that prefix. Caching here was never about storing answers. It keeps the K and V that were already computed right where they are, so the next request can pick up from there.\nReferences SGLang (@lmsysorg). “radix_cache.py.” SGLang source code, commit 49cb7d546e, 2026. SGLang (@lmsysorg). “Radix Cache Eviction Policies.” SGLang documentation. Lianmin Zheng et al. (@lm_zheng). “SGLang: Efficient Execution of Structured Language Model Programs.” 2023. ","permalink":"https://zhangchitc.com/posts/radix-attention/","summary":"SGLang keeps the KV cache in a radix tree: edges hold runs of tokens, nodes hold the GPU slot indices where their KV lives. We walk through the slot pool, TreeNode, inserting and splitting nodes, LRU eviction, and four requests that fill the pool and force an eviction.","title":"RadixAttention: How SGLang Reuses the KV Cache Across Requests"},{"content":"In the last post we looked at why Transformers need positional encoding, and at how the sinusoidal scheme encodes a token\u0026rsquo;s position the way the second, minute and hour hands of a clock encode the time. Yet today\u0026rsquo;s frontier models, DeepSeek-V3, GLM-4.5 and Qwen 3 among them, all use something else: rotary position embedding, or RoPE. Why have the newest models moved away from sinusoidal encoding? What goes wrong with it, and how does RoPE fix it? This post walks through how RoPE works.\nWhat we want from an attention score Let\u0026rsquo;s start with an intuition for what an attention score should look like. Take the example from last time, \u0026ldquo;Alice hits Bob, who\u0026rsquo;s hurt\u0026rdquo;, and look at the score between \u0026ldquo;hurt\u0026rdquo; at position 4 and \u0026ldquo;hits\u0026rdquo; at position 1. Whether the sentence stands on its own or turns up after a long stretch of text, we want that score to come out the same. In both cases \u0026ldquo;hits\u0026rdquo; sits 3 positions before \u0026ldquo;hurt\u0026rdquo;, so it should contribute the same amount to the result for \u0026ldquo;hurt\u0026rdquo;. The score shouldn\u0026rsquo;t change just because both tokens moved more than 1,000 positions further into the text.\nIn other words, the attention score should depend on the two tokens\u0026rsquo; embeddings and on the relative position between them, and not on their absolute positions.\nHow sinusoidal encoding represents relative position Since relative position is what we care about, let\u0026rsquo;s see how sinusoidal encoding behaves across an offset. Recall from the last post that each sin/cos pair in the encoding can be read as a hand on a clock. Every time \\(pos\\) goes up by 1, the hand turns counterclockwise by an angle of \\(\\omega_i\\).\nNow suppose we\u0026rsquo;re handed one encoding but not the position it came from. Can we work out the encoding \\(k\\) positions later? In clock terms: we know which way the hand points, we turn it counterclockwise by \\(k\\) steps of \\(\\omega_i\\), and we want to know where it ends up.\nHere\u0026rsquo;s how to solve it. Call the unknown position \\(pos\\), so what we want is the encoding at \\(pos + k\\). Write it out as a vector using the definition, with \\(pos + k\\) as the argument, and expand the sine and cosine with the angle-addition identities. (I\u0026rsquo;ll skip the trigonometry itself; any high-school textbook has it.) The result factors neatly into a matrix times a vector. The vector on the right is the encoding we already have. The matrix on the left depends only on the offset \\(k\\), and \\(pos\\) appears nowhere in it. So given any encoding, multiplying it by this matrix gives the encoding at any offset from it, without knowing where it started. In clock terms this operation is a counterclockwise turn by \\(k\\) steps, so what we\u0026rsquo;ve done is derive the rotation matrix \\(R_i(k)\\) by calculation.\nWe can also ask the reverse question: given two encodings, how far apart are their positions? In clock terms, that\u0026rsquo;s asking for the angle between two hands, and angles are what the dot product measures. The dot product of two vectors is the product of their lengths times the cosine of the angle between them.\nEvery pair of the encoding lies on the unit circle and has length 1, so the dot product of two hands is the cosine of the angle between them. We can check this against the definition. Take two positions, \\(pos_1\\) and \\(pos_2\\), write their encodings as vectors and take the dot product. One more application of the cosine identity turns the result into \\(\\cos(\\omega_i(pos_1 - pos_2))\\). Both absolute positions have disappeared, and only their difference is left. We don\u0026rsquo;t need to know either position: a single dot product gives us the relative position.\n(Both derivations are for a single pair. A full encoding is \\(d/2\\) of these pairs stacked together, so the dot product of two full encodings is the sum over \\(i\\) of \\(\\cos(\\omega_i(pos_1 - pos_2))\\).)\nSo computationally, sinusoidal encoding has some very nice properties. How two encodings relate to each other doesn\u0026rsquo;t depend on absolute position, and you can get from one encoding to the next by multiplying by a rotation matrix. That rotation matrix matters a great deal. It\u0026rsquo;s the heart of RoPE, and we\u0026rsquo;ll be using it again shortly.\nWhere it breaks in the real dot product Here\u0026rsquo;s the catch. What attention actually multiplies together isn\u0026rsquo;t the positional encodings. It\u0026rsquo;s the query and key vectors, and \\(q\\) and \\(k\\) come from adding the positional encoding to the embedding and then multiplying by the projection matrices \\(W_Q\\) and \\(W_K\\).\nSay the two tokens sit at absolute positions \\(m\\) and \\(n\\). In matrix form, their dot product is \\(q_m\\) transposed times \\(k_n\\). Substitute the definitions of \\(q_m\\) and \\(k_n\\) and expand. First, deal with the outermost transpose. The transpose of a product is the product of the transposes in reverse order, so \\(W_Q\\) transposed moves to the right, next to \\(W_K\\). Neither projection matrix depends on position, so we merge the two into a single matrix, \\(W\\). Next comes the transpose outside the left-hand parentheses. The transpose of a sum is the sum of the transposes, so it distributes over \\(x_m\\) and \\(\\mathrm{PE}(m)\\). Finally, multiply out the two sums to get four terms.\nThe first term, \\(x_m^{\\top} W x_n\\), pairs the two token embeddings with each other. It depends only on meaning, which is exactly what we want.\nThe two middle terms each multiply an embedding by \\(W\\) and by a positional encoding. They carry absolute position straight into the attention score, which is exactly the interference we don\u0026rsquo;t want. These terms exist because sinusoidal encoding is added: expanding a sum inevitably produces them, and there\u0026rsquo;s no way around it.\nNow look at the fourth term, \\(\\mathrm{PE}(m)^{\\top} W\\, \\mathrm{PE}(n)\\). It should remind you of what we just showed: the dot product of two encodings is the cosine of their relative position. The difference is the \\(W\\) sitting in the middle. That identity only survives if \\(W\\) leaves the encodings untouched, the way the identity matrix would, and a learned projection has no reason to be anything like that. So this term could have depended on relative position alone, but the projection matrix gets in the way. The reason is that sinusoidal encoding is added before the projection, so the projection matrix inevitably acts on the positional encoding too and distorts the role it plays in the attention score.\nRoPE: rotate instead of add That pins down the root of the problem. Sinusoidal encoding is added, and it\u0026rsquo;s added before the projection. RoPE changes exactly those two things. First, it multiplies instead of adding. Second, it\u0026rsquo;s applied after the projection instead of before.\nConcretely, take a \\(d\\)-dimensional input vector \\(x\\) for the token at position \\(pos\\). RoPE splits its elements into consecutive pairs, \\(d/2\\) pairs in all. The encoding is now the rotation matrix we met earlier: each pair is treated as a vector in the plane and multiplied by a rotation matrix, and pair \\(i\\) is rotated by the angle \\(\\omega_i \\cdot pos\\). I haven\u0026rsquo;t said which vector the input is, because the same operation is applied to both \\(q\\) and \\(k\\).\n(The matrix shown is the block for pair \\(i\\). The full RoPE matrix is block-diagonal, with \\(d/2\\) of these \\(2 \\times 2\\) blocks along the diagonal.)\nThe clock picture makes this intuitive. At first, \\(q\\) and \\(k\\) have just come out of the projection of \\(x\\) and carry no position information. Again, let their absolute positions be \\(m\\) and \\(n\\). First \\(q\\) is rotated counterclockwise by \\(m\\omega_i\\) and becomes \\(q'_m\\). Then \\(k\\) is rotated by \\(n\\omega_i\\) and becomes \\(k'_n\\). Now \\(q'\\) and \\(k'\\) each carry their own position, \\(m\\) and \\(n\\).\nPutting RoPE into the dot product What happens when the rotation goes into the dot product? This time \\(q\\) and \\(k\\) are defined differently. The embedding is multiplied by \\(W_Q\\) and \\(W_K\\) first, and only once \\(q\\) and \\(k\\) exist does the rotation matrix multiply them. So the rotation sits on the outside.\nSubstitute these two definitions into \\((q'_m)^{\\top} k'_n\\). Again, start with the outermost transpose. The transpose of a product is the product of the transposes in reverse order, so \\(\\mathrm{RoPE}(m)\\) transposed lands in the middle, right next to \\(\\mathrm{RoPE}(n)\\). Notice that this time there\u0026rsquo;s no plus sign inside the parentheses, so there\u0026rsquo;s nothing left to split. The expression stays a single term from start to finish.\nSo what do the two RoPE matrices in the middle multiply out to? Go back to the definition from the previous section. Transposing the rotation matrix swaps its two sine entries, the top-right and the bottom-left, and the only effect is that their signs trade places. Cosine is even and sine is odd, so plugging \\(-pos\\) into the definition gives exactly the same matrix. In other words, \\(\\mathrm{RoPE}(m)^{\\top} = \\mathrm{RoPE}(-m)\\), which is easy to verify on paper. And \\(\\mathrm{RoPE}(-m)\\) times \\(\\mathrm{RoPE}(n)\\) is \\(\\mathrm{RoPE}(n - m)\\): turn counterclockwise by \\(n\\) steps, then turn back by \\(m\\), and the net effect is a counterclockwise turn of \\(n - m\\) steps. Finally, write the projected vectors \\(W_Q x_m\\) and \\(W_K x_n\\) as \\(q\\) and \\(k\\), and the result is as compact as it gets.\nHow should we read the result? We set out to compute the dot product of \\(q'_m\\) and \\(k'_n\\). A dot product is the product of the two lengths times the cosine of the angle between the vectors, so rotating both vectors by the same angle leaves the angle, and therefore the dot product, unchanged. Let\u0026rsquo;s rotate both of them clockwise by \\(m\\) steps. That takes \\(q'_m\\) right back to \\(q\\). As for \\(k'_n\\), it was \\(k\\) turned counterclockwise by \\(n\\) steps, so turning it back by \\(m\\) leaves \\(k\\) turned counterclockwise by \\(n - m\\) steps.\nThat\u0026rsquo;s what the formula says: the dot product of \\(q'_m\\) and \\(k'_n\\) equals the dot product of \\(q\\) with \\(k\\) rotated counterclockwise by \\(n - m\\) steps. This is exactly the form we asked for at the start. It involves only \\(q\\), \\(k\\) and their relative position \\(n - m\\), which is a very clean result.\nSide by side To wrap up, let\u0026rsquo;s line the two encodings up against each other.\nReferences Jianlin Su. “Transformer升级之路：2、博采众长的旋转式位置编码” (“The Road to Upgrading Transformers, Part 2: Rotary Position Embedding, the Best of Both Worlds”, in Chinese). 2021. Jianlin Su et al. “RoFormer: Enhanced Transformer with Rotary Position Embedding.” 2021. ","permalink":"https://zhangchitc.com/posts/rope/","summary":"Sinusoidal encoding has a neat rotation property, but it breaks once the encoding goes through attention\u0026rsquo;s projection matrices. We expand the dot product to see why, then put RoPE through the same expansion and watch the absolute positions cancel out.","title":"How RoPE Improves on Sinusoidal Positional Encoding"},{"content":"\u0026ldquo;Dog bites man\u0026rdquo; and \u0026ldquo;man bites dog\u0026rdquo; contain exactly the same words. Swap two of them and the meaning flips.\nLeft to itself, a Transformer can\u0026rsquo;t tell these two sentences apart. Attention, the mechanism at the heart of the model, never looks at where a token sits. As far as attention is concerned, a sentence is a bag of tokens, not a sequence.\nThis post walks through why Transformers need positional encoding, and how the sine-and-cosine scheme from the original paper captures position so neatly.\nWhat attention sees without positions Let\u0026rsquo;s start with plain attention and see what goes wrong when it has no position information. I won\u0026rsquo;t re-derive attention here. All we need is the formula:\n$$ \\text{Attention}(Q,K,V)=\\text{softmax}\\!\\left(\\frac{QK^{\\top}}{\\sqrt{d_k}}\\right)V $$ Multiply \\(Q\\) by \\(K\\) transposed, scale by \\(\\sqrt{d_k}\\), apply softmax, and use the resulting weights to mix the value vectors in \\(V\\).\nNow take two sentences:\n\u0026ldquo;Alice hits Bob. Who\u0026rsquo;s hurt?\u0026rdquo; \u0026ldquo;Bob hits Alice. Who\u0026rsquo;s hurt?\u0026rdquo; Suppose the model has just read the last token, \u0026ldquo;hurt\u0026rdquo;, and is about to predict the next one. Here\u0026rsquo;s what happens in that decoding step.\nIn the first sentence, the query vector for \u0026ldquo;hurt\u0026rdquo; is dotted with the key vector of every earlier token. That produces a row of scores: \\(q_{\\text{hurt}} \\cdot k_{\\text{Alice}}\\), \\(q_{\\text{hurt}} \\cdot k_{\\text{hits}}\\), \\(q_{\\text{hurt}} \\cdot k_{\\text{Bob}}\\), and so on. Softmax turns those scores into weights, and the output is a weighted sum of the tokens\u0026rsquo; value vectors.\nIn the second sentence, Alice and Bob trade places, so the scores come out as \\(q_{\\text{hurt}} \\cdot k_{\\text{Bob}}\\), \\(q_{\\text{hurt}} \\cdot k_{\\text{hits}}\\), \\(q_{\\text{hurt}} \\cdot k_{\\text{Alice}}\\). It\u0026rsquo;s the same set of terms in a different order, and the value vectors get reshuffled in exactly the same way.\nThat\u0026rsquo;s the whole problem. A value vector\u0026rsquo;s weight depends only on its own score, not on where it shows up in the list. So every value vector gets the same weight in both sentences, and since addition doesn\u0026rsquo;t care about order, both sentences produce exactly the same output vector.\nGeneration is autoregressive, so the same output means the model continues both sentences the same way. It has no way to tell who\u0026rsquo;s hurt. On its own, attention is blind to word order.\nTwo obvious fixes: integers and fractions The most direct fix is to add each token\u0026rsquo;s position number to its embedding.\nSay the sequence is 64 tokens long and \u0026ldquo;Bob\u0026rdquo; is token 24. Every token is a \\(d\\)-dimensional embedding vector.\nThe naive version adds 24 to every dimension of Bob\u0026rsquo;s embedding. It\u0026rsquo;s simple, but the problem is obvious. Embedding values are small numbers like the ones below, and position numbers are one or two orders of magnitude larger. Add the two together and the position drowns out whatever the embedding was saying about the word.\nSo let\u0026rsquo;s scale it down. Divide the position by the sequence length \\(L\\), and every value lands between 0 and 1.\nNow the position no longer drowns out the embedding, but we\u0026rsquo;ve traded one problem for another: the encoding depends on how long the sequence is. Token 24 gets 0.375 in a 64-token sequence but 0.024 in a 1,000-token one.\n$$ \\begin{aligned} L = 64: \u0026\\quad \\frac{24}{64} = {\\color{#0a7d61}0.375} \\\\[4pt] L = 1000: \u0026\\quad \\frac{24}{1000} = {\\color{#d64545}0.024} \\end{aligned} $$ The same position gets a different code every time the length changes, so the model can never learn a stable meaning for any particular value.\nBinary: bounded and length-independent, but discontinuous Let\u0026rsquo;s try again. Another way to write down a position is in binary.\n24 in binary is 011000. Every digit is either 0 or 1, so the values stay in a fixed range. And position 24 is always 011000, however long the sequence is. Add those six bits to six dimensions of the embedding, one bit each, and you have a binary positional encoding.\nIt helps to picture each bit as a square wave. The horizontal axis is position, from 0 to 63, and each row tracks one bit:\nThe top row is the lowest bit. It flips at every position. The next row flips every 2 positions. The rows below flip every 4, 8, 16 and 32 positions. To read the code for a position, read down its column. For 24, going from the lowest bit to the highest, you get 0, 0, 0, 1, 1, 0. Position 0 is all zeros and position 63 is all ones.\nBinary gets a lot right, but it has one big flaw: it changes in jumps. Look at what happens between 23 and 24:\n$$ \\begin{aligned} 23 \u0026= 0\\,1\\,0\\,1\\,1\\,1_{(2)} \\\\[2pt] 24 \u0026= 0\\,1\\,{\\color{#d64545}\\mathbf{1\\,0\\,0\\,0}}_{(2)} \\end{aligned} $$ One step forward flips four bits at once. That\u0026rsquo;s bad news for a neural network. Networks learn by gradient descent, and gradient descent works best on smooth, continuous functions: if the position changes a little, the encoding should change a little too. That\u0026rsquo;s what lets the gradient tell the model which way things are moving.\nSinusoidal encoding: smoothing out the steps This is where \u0026ldquo;Attention Is All You Need\u0026rdquo;, the paper that introduced the Transformer, comes in. Its answer is sinusoidal positional encoding.\nThe idea is to split the encoding vector\u0026rsquo;s \\(d\\) dimensions into pairs. In each pair, the even dimension gets a sine and the odd dimension gets a cosine:\nBoth lines share the same argument. One takes its sine and the other its cosine. Here \\(pos\\) is the position, \\(i\\) is the index of the pair, \\(d\\) is the size of the vector, and 10000 is a fixed constant.\nIf the formula looks arbitrary, don\u0026rsquo;t worry. We\u0026rsquo;ll come at it from a few angles until the design makes sense.\nFirst, let\u0026rsquo;s plot it. Notice how much it resembles the square waves from before. The first pair changes fastest: nudge the position and its values already move. Each pair after that changes more slowly.\nThe difference is that the square waves have become smooth curves. Where binary jumped, these change continuously.\nIt ticks the other boxes too:\nBounded: sine and cosine always stay between \\(-1\\) and \\(1\\). Length-independent: the formula only involves \\(pos\\), \\(i\\) and \\(d\\). The sequence length \\(L\\) appears nowhere, so a position\u0026rsquo;s encoding depends only on the position itself. Every requirement we\u0026rsquo;ve collected so far is met.\nWavelengths: a geometric sequence from 2π to 2π·10000 Now for a different angle: the wavelength of each sine wave.\nA quick refresher. The basic sine function, \\(y = \\sin x\\) (blue), has a wavelength of \\(2\\pi\\), meaning the curve repeats every \\(2\\pi\\).\nWhat about \\(y = \\sin(x/2)\\) (orange)? Its wavelength is \\(4\\pi\\). Because \\(x\\) is divided by 2, it has to travel twice as far to complete one cycle, so the wave is stretched to twice the length.\nThe general rule: dividing \\(x\\) by \\(a\\) stretches the wave by a factor of \\(a\\).\n$$ y = \\sin\\frac{x}{a} \\quad\\Longrightarrow\\quad \\text{wavelength} = 2\\pi \\cdot a $$ In the positional encoding, the divisor is \\(10000^{2i/d}\\), so the wavelength of pair \\(i\\) is:\n$$ y = \\sin\\frac{x}{10000^{2i/d}} \\quad\\Longrightarrow\\quad \\text{wavelength} = 2\\pi \\cdot 10000^{2i/d} $$ Let\u0026rsquo;s plug in \\(d = 8\\), which gives four pairs.\nFor \\(i = 0\\), the exponent \\(2i/d\\) is 0, and \\(10000^0 = 1\\). The wavelength is \\(2\\pi\\), so this pair repeats roughly every 6 positions. For \\(i = 1\\), the exponent is 0.25, and \\(10000^{0.25}\\) is exactly 10. The wavelength is \\(20\\pi\\). The pattern continues: each pair\u0026rsquo;s wavelength is 10 times the previous one. (The curves on the right show the pattern, not the true scale. Each row is drawn twice as wide as the one above. At the real 10× ratio, the bottom rows would look flat.)\nIn general, the wavelengths form a geometric sequence that starts at \\(2\\pi\\) and climbs toward \\(2\\pi \\cdot 10000\\) for the last pair. You can read a wavelength as the number of positions before that pair\u0026rsquo;s values repeat.\nThis is the same structure as binary. In binary, the bits flip every 1, 2, 4, 8 positions, doubling each time. In the sinusoidal encoding, the ratio between neighbors is set by 10000 and \\(d\\) instead of being 2. Either way, low dimensions change fast and high dimensions change slowly.\nWavelengths also explain the choice of 10000. The longest wavelength is close to\n$$ 2\\pi \\cdot 10000 \\approx 62{,}832 \\ \\text{tokens} $$ So the slowest pair doesn\u0026rsquo;t repeat for roughly 60,000 positions, which means any sequence shorter than that gets a distinct code at every position. That\u0026rsquo;s plenty for typical context lengths. You could use a bigger base, like 100,000, but there\u0026rsquo;s little reason to: when processing text, a token rarely needs context from 100,000 tokens back.\nEach sin/cos pair is a clock hand One question is left: why does the encoding come in sine/cosine pairs?\nRecall from high-school trig that the cosine and sine of the same angle are the coordinates of a point on the unit circle.\nSo let\u0026rsquo;s rewrite the formula. Define \\(\\omega_i\\) as the reciprocal of the divisor, which physicists call the angular frequency:\n$$ \\omega_i = \\frac{1}{10000^{2i/d}} $$ Pair \\(i\\) then becomes \\(\\sin(\\omega_i \\cdot pos)\\) and \\(\\cos(\\omega_i \\cdot pos)\\), and \\(\\omega_i \\cdot pos\\) is simply an angle.\nSeen this way, the encoding becomes intuitive: each sine/cosine pair is a hand on a clock. Every time \\(pos\\) goes up by 1, the hand rotates counterclockwise by \\(\\omega_i\\).\nWe saw earlier that \\(10000^{2i/d}\\) grows with \\(i\\). Since \\(\\omega_i\\) is its reciprocal, it shrinks as \\(i\\) grows, so higher pairs turn more slowly.\nThe four circles below show the four hands at the same \\(pos\\). The low pairs spin quickly, and each pair up turns more slowly. At normal speed you can barely see the bottom two move, but speed things up and they start turning:\nSound familiar? It\u0026rsquo;s a clock face with a second hand, a minute hand and an hour hand. No single hand tells you the exact time, but together they pin down one unique moment.\nPositional encoding works the same way. Each pair of dimensions is a hand, and the position \\(pos\\) is the time. The wavelengths from the previous section get a new meaning too: a pair\u0026rsquo;s wavelength is how many positions its hand needs to go around once.\nReferences [1] Christopher Fleetwood. “You could have designed state of the art positional encoding.” Hugging Face Blog, 2024.\n[2] Naoki Shibuya. “Transformer’s Positional Encoding.” 2021.\n[3] Vaswani et al. “Attention Is All You Need.” NeurIPS 2017.\n","permalink":"https://zhangchitc.com/posts/positional-encoding/","summary":"Attention can\u0026rsquo;t tell \u0026ldquo;dog bites man\u0026rdquo; from \u0026ldquo;man bites dog\u0026rdquo;. We try integer, fractional and binary encodings, see where each one breaks, and arrive at sines and cosines: a set of clock hands spinning at different speeds.","title":"Why Transformers Need Positional Encoding"},{"content":"When we judge how capable a model is, the first number we look at is its parameter count. Scaling laws back that instinct up: a model\u0026rsquo;s loss falls predictably as you scale up model size, training data and compute together. The catch is that a bigger model also costs more compute, in training and in inference. Mixture of Experts, or MoE, breaks that link. It lets a model\u0026rsquo;s parameter count grow tens of times over while the compute spent on each token barely moves. The Switch Transformers paper puts it this way: \u0026ldquo;a sparsely-activated model \u0026ndash; with outrageous numbers of parameters \u0026ndash; but a constant computational cost.\u0026rdquo; Today almost every flagship open model, DeepSeek, Qwen and Kimi among them, is built on it. This post walks through how it works, one step at a time.\nDense models: every parameter in the FFN does work for every token I\u0026rsquo;ve covered the Transformer decoder in earlier posts, so here\u0026rsquo;s a quick recap. A decoder layer has two main blocks. The input first goes through multi-head attention, whose job is to move information between tokens. It then goes through the FFN, the feed-forward network. Once each token has picked up its context, the FFN mixes and transforms that information to produce the layer\u0026rsquo;s output. Attention is the only block in a Transformer that looks across tokens. The FFN treats every token independently, so all tokens can go through it in parallel.\nInside the FFN, each token\u0026rsquo;s \\(d\\)-dimensional vector is first projected up to a wider intermediate size, \\(d_{\\mathit{ff}}\\). A nonlinear activation function is applied in that wider space, and the result is projected back down to \\(d\\) dimensions.\nIn matrix form, say the prompt has \\(n\\) tokens and each token is a \\(d\\)-dimensional vector. The input \\(X\\) is an \\(n \\times d\\) matrix. Multiplying it by the up-projection \\(W_1\\) gives an \\(n \\times d_{\\mathit{ff}}\\) intermediate result. After the activation, multiplying by the down-projection \\(W_2\\) brings the output \\(Y\\) back to \\(n \\times d\\). The shape that goes in is the shape that comes out.\n(The sizes in these diagrams are illustrative. In real models \\(d\\) is in the thousands.)\nAn FFN built this way is what makes a dense model: every token goes through the same complete network, and every neuron and every parameter in the FFN takes part in the computation. So the bigger the model, the more compute each token needs. In a dense model, adding parameters means adding compute.\nSparse models: four experts, and each token uses one A sparse model does it differently. It makes many copies of the FFN, and each copy is called an expert. When the model runs, each token goes through only one of them.\nTake the prompt \u0026ldquo;What is 1 + 1 ?\u0026rdquo; and four experts that handle punctuation, verbs, conjunctions and numbers. When the sparse model sees that the current token is most closely related to numbers, it sends the token to the numbers expert. The other three experts sit idle and do no computation at all, and the output of the whole sparse layer is the numbers expert\u0026rsquo;s output.\n(The expert labels are for illustration. Real experts don\u0026rsquo;t divide the work this neatly: the Mixtral paper found no clear specialization by topic.)\nAt its core, then, a sparse model picks a compute path based on its input. Each token activates only part of the model\u0026rsquo;s parameters, which is how the total parameter count can grow without adding compute per token.\nCompare that with the multi-head attention block. Whatever the input looks like, every head computes its attention. The results of all the heads are concatenated into one large matrix, and a linear projection turns that into the block\u0026rsquo;s output. So of the two blocks in a decoder layer, attention puts every one of its neurons to work, while the sparse layer uses only some of them.\nRouting: a router scores the experts, and the top one runs That raises the key question: who decides which expert a token goes to? MoE adds a component for this called the router. It is a small layer followed by a softmax. Its input is the token\u0026rsquo;s vector, and its output is \\(E\\) numbers, one probability per expert. The higher the probability, the better suited the router thinks that expert is for this token.\nSay a token comes in and the router\u0026rsquo;s softmax gives the four experts probabilities of 0.45, 0.19, 0.05 and 0.31. The first expert scores highest, so the token goes to expert 1, and that expert\u0026rsquo;s output is multiplied by its probability, 0.45, to give the layer\u0026rsquo;s output.\nThat multiplication matters. Picking the expert means taking the maximum, and taking a maximum can\u0026rsquo;t be differentiated, so no gradient flows back through the choice itself. Multiplying by 0.45 puts the probability into the formula for the output. The gradient can then flow back through that multiplication into the router\u0026rsquo;s parameters and tell it whether this choice was a good one.\nIn matrix form, if the input is an \\(n \\times d\\) matrix, the router produces an \\(n \\times E\\) matrix, and softmax is applied to each row. Each row is one token\u0026rsquo;s probability distribution over the \\(E\\) experts.\nThe router box in the figure is labeled FFN, but in Mixtral and Switch Transformers the router is in fact a single linear layer: one \\(d \\times E\\) matrix, \\(W_g\\) above, followed by the softmax.\nThe forward pass: dispatch, then combine Now let\u0026rsquo;s follow one complete forward pass through an MoE layer. The input is six tokens, a 6×3 matrix, and each token gets a color tag so we can track where it goes.\nNext the router scores them, producing a 6×4 matrix. Each row holds one token\u0026rsquo;s probabilities for the four experts, and the highest cell in each row is highlighted in that expert\u0026rsquo;s color. t1\u0026rsquo;s highest score is 0.62, for expert 2. t2\u0026rsquo;s is 0.70, for expert 1.\nThen comes the important step, dispatch. Following the experts just chosen, the six tokens are regrouped into four piles. t2 and t5 both went to expert 1, so they are pulled out into a new matrix of their own. In the same way, t1, t4 and t6 are rearranged into a separate matrix that becomes expert 2\u0026rsquo;s input, and t3 goes to expert 4. Notice that expert 3 got no tokens at all, so its input is empty.\nEach expert then runs its FFN on its own input matrix and multiplies every row of the result by the probability that token was routed with. The last step, combine, puts all the weighted outputs back in the tokens\u0026rsquo; original order, so each position again holds the result for the token that started there.\nThe dispatch and combine steps look a lot like MapReduce from distributed computing. Much as attention\u0026rsquo;s query, key and value borrow from information retrieval, ideas from classic algorithms turn up all over the LLM stack.\nOne thing to keep in mind from this example: how many tokens each expert receives is decided entirely by the data. The counts are naturally unequal and can even be zero. This is one of the central problems in MoE models, load balancing, and we\u0026rsquo;ll come back to it shortly.\nTop-K: several experts, added up by weight Routing each token to a single expert has a weakness. A model that can send its input through only one expert is limited in what it can express. When several experts score about the same, a tiny difference in routing scores can send a token down a completely different path. That easily makes training unstable and feeds the load-balancing problem we just saw. So real models pick more than one expert, usually the \\(K\\) with the highest scores. DeepSeek-V4-Pro has 384 experts and activates 6 of them for each token. Kimi-K3 picks 16 out of 896. Mixtral, by comparison, picks 2 out of 8. The number of experts has grown much faster than \\(K\\), so each token touches a smaller and smaller share of the model.\nTop-K routing changes little in the computation. The one new step comes after the top \\(K\\) experts are chosen: their weights are renormalized. In the left example, the two highest probabilities are 0.45 and 0.31. Before the outputs are combined, 0.45 is divided by their sum, 0.76, to get 0.59, and 0.31 divided by 0.76 gives 0.41. These renormalized weights are what the two experts\u0026rsquo; outputs are multiplied by and added up with. The right column runs the same steps for the token \u0026ldquo;is\u0026rdquo;, which lands on two different experts.\nLoad balancing: how collapse happens, and how it\u0026rsquo;s kept in check We saw in the forward pass that different experts get different amounts of work. An expert that isn\u0026rsquo;t chosen gets no gradient at all, and that sets up a feedback loop. At the start of training the router is randomly initialized. Some expert happens to receive a few more tokens, so it gets a little more gradient and its parameters improve a little more. At the next step the router is more inclined to pick it, so it receives a few more tokens still. The router gradually funnels tokens to a handful of experts, and the rest, rarely chosen, barely get trained. Shazeer et al. described this back in 2017: the gating network \u0026ldquo;tends to converge to a state where it always produces large weights for the same few experts.\u0026rdquo; We want every expert to receive roughly the same number of tokens, so that all of them get trained. This is load balancing in MoE: keeping the network from leaning on a few experts.\nThe fix is an auxiliary loss built from two quantities, \\(P_i\\) and \\(f_i\\). \\(P_i\\) is the average probability the router gives expert \\(i\\) across the tokens in a batch.\n$$ P_i=\\frac{1}{n}\\sum_{x\\in B}p_i(x) $$ On the left of the table below is an unbalanced router. \\(P_1 = 0.58\\) is the average of the first probability across the four tokens\u0026rsquo; distributions, so \\(P_i\\) shows that the router strongly favors expert 1 and gives the others little. On the right, with balanced routing, each expert\u0026rsquo;s average probability hovers around a quarter, 0.25.\n\\(f_i\\) is the fraction of tokens in the batch that the router actually sends to expert \\(i\\), which is how much traffic that expert gets in practice.\n$$ f_i=\\frac{1}{n}\\sum_{x\\in B}\\mathbf{1}\\{\\arg\\max p(x)=i\\} $$ On the left, expert 1\u0026rsquo;s average probability is only 0.58, but it is always the largest one, so the router picks it every time. Its traffic is 100%, \\(f_1 = 1\\): one expert takes everything. On the right, \\(f\\) is four 0.25s, since each expert is picked once.\nThe auxiliary loss is \\(\\alpha\\) times the number of experts \\(E\\) times the sum, over all experts, of \\(f_i\\) times \\(P_i\\).\n$$ \\text{Auxiliary Loss}=\\alpha\\cdot E\\sum_{i=1}^{E} f_i\\,P_i $$ On the left, only the first expert contributes: \\(4 \\times 1 \\times 0.58 \\approx 2.33\\) (\\(P_1\\) is 0.5825 before rounding). The right is easy to see too. Every \\(f_i\\) is 0.25, so \\(E \\cdot f_i = 1\\), and the \\(P_i\\) add up to 1 because every token\u0026rsquo;s probabilities do. When the load is perfectly balanced, the loss reaches its minimum of 1. The table shows the part after \\(\\alpha\\); Switch Transformers sets \\(\\alpha\\) to \\(10^{-2}\\), so the terms added to the total loss are about 0.0233 and 0.01. The product matters because \\(f_i\\) is a count and can\u0026rsquo;t be differentiated, while \\(P_i\\) comes straight from the softmax and can. Adding this term to the total loss gradually evens out how many tokens each expert gets during training.\nSummary: more experts, without more of them per token Back to the question we started with. A dense model can only add parameters by making its FFN wider, and every bit of extra width is extra compute for every token. MoE instead makes more copies of the FFN. The number of experts can grow very large, while the number each token activates doesn\u0026rsquo;t have to grow with it. That is how parameter count and compute come apart: the parameters grow tens of times over while the compute per token barely moves.\nReferences Noam Shazeer et al. (@NoamShazeer). “Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer.” ICLR 2017. William Fedus et al. (@LiamFedus). “Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity.” JMLR 2022. Maarten Grootendorst (@MaartenGr). “A Visual Guide to Mixture of Experts (MoE).” 2024. ","permalink":"https://zhangchitc.com/posts/mixture-of-experts/","summary":"Mixture of Experts lets a model\u0026rsquo;s parameter count grow tens of times over while the compute per token barely moves. We build it up step by step: copying the FFN into experts, the router, dispatch and combine, top-K routing, and the auxiliary loss that keeps experts balanced.","title":"How Mixture of Experts Scales Model Parameters"},{"content":"The FlashAttention paper has a striking benchmark. Training GPT-2 small on eight A100s took 9.5 days with the Hugging Face implementation and 4.7 days with NVIDIA\u0026rsquo;s Megatron-LM. With FlashAttention it took 2.7 days, and the trained model was exactly as good: all three reached the same perplexity of 18.2. In another benchmark, reads and writes to GPU memory dropped from 40.3 GB to 4.4 GB, about 9× less. (These are the paper\u0026rsquo;s 2022 comparisons. Both baselines have since adopted fused attention kernels, so the gap would be smaller today.)\nFlashAttention-style kernels are now a standard part of the modern LLM stack. PyTorch 2, Hugging Face and vLLM all ship them as attention backends. So how does it pull this off? This post builds it up one step at a time.\nGPU memory comes in tiers Start with the fact that GPU memory is layered. At the top is SRAM, which sits on each streaming multiprocessor (SM), the GPU\u0026rsquo;s compute units. On an A100 its bandwidth is about 19 TB/s, but all the SRAM across the whole chip adds up to only about 20 MB. In the middle is HBM, high-bandwidth memory, the kind SK hynix and Micron make. An A100 has 40 GB of it, at 1.5 TB/s. At the bottom is the CPU\u0026rsquo;s DRAM, which can reach terabytes but delivers only 12.8 GB/s, in a different league entirely from the two above.\nThink of HBM as the warehouse. Model weights, the KV cache and every layer\u0026rsquo;s activations all live there. SRAM is the workbench: it holds only the small piece of data being worked on right now and is freed as soon as that work is done. The compute units can\u0026rsquo;t operate on numbers in HBM directly. Any number that takes part in a computation has to be copied from HBM into on-chip SRAM first. So the way to make a GPU fast is to finish as much work as possible in SRAM and avoid reading and writing HBM in the middle of a computation.\nAttention: the matrices don\u0026rsquo;t fit, the vectors do With that in mind, look at how much data attention handles. I covered the formula in my post on the KV cache, so here\u0026rsquo;s the short version. Say the prompt has n tokens, and each token\u0026rsquo;s Q, K and V vectors have d dimensions. Then Q, K and V are each an \\(n \\times d\\) matrix. Attention multiplies Q by K transposed, divides by the square root of d, applies softmax to each row to get an \\(n \\times n\\) table, and finally multiplies by V to get the output O, another \\(n \\times d\\) matrix.\nThe key point is that n is the context length, which can reach a hundred thousand tokens or even a million, while d is the dimension of one head, a constant like 32, 64 or 128 in most models. Both the \\(n \\times d\\) matrices and the \\(n \\times n\\) table are far bigger than SRAM, so they can only live in HBM. (At n = 128K and d = 128 in FP16, a single \\(n \\times d\\) matrix is 32 MB and the \\(n \\times n\\) table is 32 GB.) Only data that doesn\u0026rsquo;t grow with n, like a single token\u0026rsquo;s q, k, v and o vectors, fits into SRAM in one go. This is the most fundamental constraint on running a Transformer on a GPU.\nThree steps, and every row is independent So where exactly does attention lose time? Let\u0026rsquo;s break the computation down as far as it goes and follow the first row through each step.\nStep one is S = QK transposed. The first row is q1 dotted with k1, k2, all the way to kN: n numbers that are the first token\u0026rsquo;s attention scores against every token. At this point they\u0026rsquo;re raw scores, not yet normalized. (The diagrams leave out the division by the square root of d to keep them clean. It doesn\u0026rsquo;t change anything here.)\nStep two is P = softmax(S), applied row by row. Softmax turns a row of raw scores into a probability distribution that sums to 1: take the exponential of each score and divide by the sum of the exponentials across the row. So p11, p12, through p1N add up to exactly 1.\nStep three is O = PV. For the first token, p1 times V expands to v1 times p11, plus v2 times p12, all the way to vN times p1N. In other words, every token\u0026rsquo;s value vector is weighted by those attention probabilities and summed into one output vector, o1.\nRemember that nothing whose size depends on n fits in SRAM. So in step one, we can only bring in one q and one k from HBM at a time, compute their dot product, and write the result straight back to HBM, because SRAM has no room for a whole row of n results. Steps two and three work the same way: each one reads the previous step\u0026rsquo;s results from HBM, computes, and writes its own results back to HBM.\nKernel fusion: three round trips into one The left half of this diagram shows the standard attention computation on a GPU. Each step reads part of the data from HBM, computes, and writes the result back to HBM, and this happens three times. Since HBM is so much slower than SRAM, a large share of the time goes into writing intermediate results to HBM and reading them back.\nThat suggests an obvious idea: what if we never stored those intermediate results in HBM at all, and instead chained every step together and wrote to HBM only once at the end? In terms of the matrix math, we\u0026rsquo;d read in q1, k1 and v1, compute the dot product q1·k1, and instead of writing it back, go straight to the next step: somehow turn it into the softmax probability p11, multiply p11 by v1, and add the result into o1. Then loop over k2 and v2, and so on up to N, and write only the final o vector back to HBM.\nThat\u0026rsquo;s the intuition behind kernel fusion, shown in the right half of the diagram.\nA fused kernel, and an overflow problem There\u0026rsquo;s a fairly direct way to write this. In the code below, q is one row\u0026rsquo;s query vector, and K and V are the full matrices. The variable d is the softmax denominator, starting at 0 (here d means the denominator, not the head dimension), and o is the output vector, starting at all zeros. The loop over k and v stands for reading one k and one v at a time from HBM into SRAM. x is q dotted with k, times scale, which is the \\(1/\\sqrt{d}\\) scaling factor from the attention formula. Then exp(x) times v is added straight into o.\nWhy does that work? Softmax is exp(x) divided by the sum of all the exponentials. When we reach a given x, we don\u0026rsquo;t know that sum yet, so we set it aside and use only the numerator as the weight on v, adding it into o. Once every x has been processed, d holds the full denominator, and we return o divided by d in one step. Since the denominator is the same number for the whole row, dividing before or after the sum gives the same result. We\u0026rsquo;ve only tracked the numerator\u0026rsquo;s and the denominator\u0026rsquo;s contributions to o separately, without changing o.\nThis is the fused kernel we were after. K and V are read from HBM only once, the intermediate values x, o and d don\u0026rsquo;t grow with n so they all stay in SRAM, and only the final o is written to HBM. But it has a fatal flaw: exp(x) overflows. x is a dot product of two vectors, so its size is unbounded, while floating-point numbers have a limited range. In FP16, whose largest value is 65,504, \\(e^{12}\\) is already out of range. So real softmax implementations subtract a baseline m before taking the exponential, a trick called safe softmax. The two are clearly equivalent: it amounts to multiplying the numerator and the denominator by \\(e^{-m}\\). The usual choice is m = the maximum of all the x values. Then every \\(x_i - m\\) is at most 0, every \\(e^{x_i-m}\\) falls between 0 and 1, and the exponential can never overflow.\n$$ \\begin{aligned} \\mathrm{softmax}(x_i) \u0026= \\dfrac{e^{x_i}}{\\sum_{j} e^{x_j}}\\\\[10pt] \\text{safe softmax}(x_i) \u0026= \\dfrac{\\textcolor{#0a7d61}{e^{-m}}\\cdot e^{x_i}}{\\textcolor{#0a7d61}{e^{-m}}\\cdot\\sum_{j} e^{x_j}} = \\dfrac{e^{x_i\\textcolor{#0a7d61}{-m}}}{\\sum_{j} e^{x_j\\textcolor{#0a7d61}{-m}}} \\end{aligned} $$ Online softmax: three passes become two Here\u0026rsquo;s a straightforward implementation of safe softmax. It takes three passes. The first finds the row\u0026rsquo;s maximum m. The second subtracts m from each value, takes the exponential and adds it into the denominator d. Only the third divides each term by d to produce the softmax. Here \\(d_i\\) is the sum of the first i terms, and every term subtracts the same baseline, \\(m_N\\), the maximum over the entire row. Since \\(m_N\\) is known only after scanning the whole row, the second pass can\u0026rsquo;t start until the first one finishes.\n$$ d_i = \\sum_{j\\le i} e^{\\,x_j-m_N} $$ Is there a way to avoid all these passes? That\u0026rsquo;s what online softmax does. If the global maximum \\(m_N\\) is what forces the extra pass, get rid of it. Redefine the denominator: let \\(d'_i\\) also be the sum of the first i terms, but have each term subtract \\(m_i\\), the maximum of the first i terms, instead of the global maximum. With that change, \\(d'_i\\) depends only on the first i terms, so it can be computed while scanning. And the \\(d_N\\) we ultimately need is the same thing as \\(d'_N\\), because at i = N the two definitions coincide.\n$$ d'_i = \\sum_{j\\le i} e^{\\,x_j-\\textcolor{#0a7d61}{m_i}} $$ The key is that with the new definition, \\(d'_i\\) and \\(d'_{i-1}\\) are linked by a recurrence: \\(d'_i\\) equals \\(d'_{i-1}\\) times a correction factor \\(e^{m_{i-1}-m_i}\\), plus \\(e^{x_i-m_i}\\). Every term in \\(d'_{i-1}\\) was computed against the baseline \\(m_{i-1}\\). Now the baseline changes to \\(m_i\\), so we multiply the whole old sum by the correction factor. When the exponents add, the \\(m_{i-1}\\) in the middle cancels, leaving \\(e^{x_j-m_i}\\), so every term now uses the new baseline. Then we add the i-th term against the new baseline, and that\u0026rsquo;s the new \\(d'_i\\).\n$$ \\begin{aligned} d'_i \u0026= d'_{i-1}\\,\\textcolor{#0a7d61}{e^{\\,m_{i-1}-m_i}} + e^{\\,x_i-m_i}\\\\[6pt] \u0026= \\Big(\\sum\\nolimits_{j\\le i-1} e^{\\,x_j-m_{i-1}}\\Big)\\textcolor{#0a7d61}{e^{\\,m_{i-1}-m_i}} + e^{\\,x_i-m_i}\\\\[4pt] \u0026= \\sum\\nolimits_{j\\le i-1} e^{\\,x_j-m_i} + e^{\\,x_i-m_i} \\;=\\; \\sum\\nolimits_{j\\le i} e^{\\,x_j-m_i} \\end{aligned} $$ So the online softmax code needs only a small change. It introduces m_old for the previous maximum, updates m, and then follows the recurrence: d becomes d times exp(m_old − m), plus exp(xi − m). The maximum m and the denominator d are now computed together in a single pass. Three passes have become two.\nFlashAttention: the same trick, applied once more Now let\u0026rsquo;s apply online softmax to attention. That gives the code below, which needs two passes. The first loops over every k vector and uses the recurrence to compute the maximum m and the denominator d. The second goes over K and V again, computes exp(q·k·scale − m) for each term as safe softmax defines it, weights v by it, and adds the result into o.\nThis version can\u0026rsquo;t overflow, it\u0026rsquo;s code that actually runs, and it needs only two passes compared with three for the naive approach. But can we go further? Look at how \\(o_i\\) is defined under safe softmax: every term uses the global maximum \\(m_N\\) and the global denominator \\(d_N\\). Doesn\u0026rsquo;t that look a lot like \\(d_i\\) in the previous section? Can we use the same trick, redefining \\(o_i\\) so its terms no longer depend on the global maximum and the global denominator?\n$$ \\boldsymbol{o}_i = \\sum_{j\\le i}\\frac{e^{\\,x_j-m_N}}{d_N}\\,\\boldsymbol{v}_j $$ We can. Define \\(o'_i\\) by replacing \\(m_N\\) and \\(d_N\\) with \\(m_i\\), the maximum of the first i terms, and \\(d'_i\\), the denominator of the first i terms. Now \\(o'_i\\) depends only on the first i terms, and the \\(o_N\\) we ultimately need is the same thing as \\(o'_N\\).\n$$ \\boldsymbol{o}'_i = \\sum_{j\\le i}\\frac{e^{\\,x_j-\\textcolor{#0a7d61}{m_i}}}{\\textcolor{#0a7d61}{d'_i}}\\,\\boldsymbol{v}_j $$ Just as before, \\(o'_i\\) and \\(o'_{i-1}\\) are linked by a recurrence: \\(o'_i\\) equals \\(o'_{i-1}\\) times a correction factor, plus the i-th term\u0026rsquo;s new contribution. The correction factor is \\(d'_{i-1}\\) times \\(e^{m_{i-1}-m_i}\\), divided by \\(d'_i\\). You can read it in two parts. The exponential in the numerator does the same job as before, switching the old baseline to the new one. The ratio of denominators swaps the old denominator for the new one. Multiplied together, they make this single factor.\n$$ \\begin{aligned} \\boldsymbol{o}'_i \u0026= \\boldsymbol{o}'_{i-1}\\,\\textcolor{#0a7d61}{\\frac{d'_{i-1}\\,e^{\\,m_{i-1}-m_i}}{d'_i}} + \\frac{e^{\\,x_i-m_i}}{d'_i}\\,\\boldsymbol{v}_i\\\\[8pt] \u0026= \\Big(\\sum\\nolimits_{j\\le i-1}\\frac{e^{\\,x_j-m_{i-1}}}{d'_{i-1}}\\boldsymbol{v}_j\\Big)\\textcolor{#0a7d61}{\\frac{d'_{i-1}\\,e^{\\,m_{i-1}-m_i}}{d'_i}} + \\frac{e^{\\,x_i-m_i}}{d'_i}\\boldsymbol{v}_i\\\\[6pt] \u0026= \\sum\\nolimits_{j\\le i}\\frac{e^{\\,x_j-m_i}}{d'_i}\\boldsymbol{v}_j \\end{aligned} $$ From this recurrence we can write the code below. Compared with the two-pass version, it adds one line, d_old = d, to keep the denominator from before the update. The rest follows the recurrence directly: compute the new m and the new d, assemble the correction factor rescale, then set o to o times rescale, plus exp(x − m) divided by d, times v. Once q, k and v are read in from HBM, all the intermediate values x, m, d and o stay in SRAM. Nothing intermediate is read from or written to HBM until every k and v has been scanned and the final o vector is written back. And that\u0026rsquo;s FlashAttention.\nTiling: moving blocks, not vectors To keep the explanation simple, we\u0026rsquo;ve been reading one q vector and one k, v pair at a time, computing one dot product at a time, and producing one output vector. A real GPU doesn\u0026rsquo;t work that way, because it would leave both SRAM and the Tensor Cores mostly idle. In practice, FlashAttention splits Q, K and V into blocks of many vectors, called tiles, and sends a whole tile to the compute units at once. Where before each step produced one output vector o, now each step produces a batch of output vectors, a block of O. And since different rows of Q are completely independent, all of those outputs can be computed in parallel.\nThe code changes very little. The maximum m and the denominator d were scalars; now they\u0026rsquo;re vectors, holding one maximum and one denominator per row. The output o was a vector; now it\u0026rsquo;s a block of rows, a matrix. The rest is the same algorithm as before, just running on tile_size vectors in parallel instead of one. If the previous version made sense, this one needs no new ideas.\n(The loop order here follows FlashAttention-2, with Q in the outer loop and K and V in the inner loop. The original FlashAttention paper put K and V on the outside.)\nWhat FlashAttention actually saves To sum up, FlashAttention comes down to three techniques. The first is kernel fusion, which collapses the three round trips for computing scores, applying softmax and multiplying by V into one, so intermediate results never touch HBM. The second is online softmax, which uses a correction factor so that the denominator and the output can be computed while scanning, without waiting for the whole row. That\u0026rsquo;s what makes the fusion possible in the first place. The third is tiling, which replaces \u0026ldquo;read one vector at a time\u0026rdquo; with \u0026ldquo;move one block at a time,\u0026rdquo; so the SRAM and the Tensor Cores are actually kept busy.\nBack to the question we started with. FlashAttention doesn\u0026rsquo;t do less arithmetic. Every score in that \\(n \\times n\\) table still gets computed, so attention is still \\(O(n^2)\\) work; it isn\u0026rsquo;t true that FlashAttention turns attention into \\(O(n)\\). What it saves is all the extra data movement between HBM and SRAM. A lot of optimization on modern GPUs isn\u0026rsquo;t about making them compute less. It\u0026rsquo;s about making them wait less.\nReferences Zihao Ye. “From Online Softmax to FlashAttention.” UW CSE 599M lecture notes, 2023. Dao et al. (@tri_dao). “FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness.” NeurIPS 2022. Tri Dao (@tri_dao). “FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning.” ICLR 2024. Horace He (@cHHillee). “Making Deep Learning Go Brrrr From First Principles.” 2022. Biao He (@hebiao064). “What is Flash Attention?” 2025. ","permalink":"https://zhangchitc.com/posts/flash-attention/","summary":"FlashAttention does every multiplication standard attention does, yet runs several times faster. We build it up step by step from the GPU memory hierarchy: kernel fusion, safe softmax, online softmax, and tiling.","title":"Why FlashAttention is So Fast"},{"content":"When I was digging into prompt caching in OpenAI\u0026rsquo;s Codex, I noticed something odd: the number of cached tokens always went up in steps of 512. The reason is that before a prompt reaches the model, it gets cut into blocks of a fixed number of tokens, and both prefix matching and the KV cache work one block at a time.\nThat block-based design has a name: PagedAttention. I only sketched the idea back then. This post walks through how PagedAttention works in vLLM, the open-source inference engine it was built for. (I\u0026rsquo;m using vLLM because its implementation is public. I\u0026rsquo;m not claiming OpenAI\u0026rsquo;s servers run the same code.)\nTwo kinds of memory fragmentation To understand PagedAttention, start with the problem it solves. Serving LLMs runs into two problems with how GPU memory gets used.\nThe first is internal fragmentation. When a request starts, you usually don\u0026rsquo;t know how long its output will be, so the simplest approach is to reserve memory for the maximum context length up front. Most requests never use all of it, and whatever they don\u0026rsquo;t use is wasted.\nThe second is external fragmentation. Some systems allocate memory on demand, in regions of different sizes. But as requests finish and free their memory, the free space breaks up into gaps of different sizes scattered between allocations. You might have 1 GB of free memory in total and still be unable to fit a new request, because no single contiguous 1 GB region exists.\nPagedAttention was designed to get rid of both kinds of fragmentation and use GPU memory far more efficiently.\nBorrowing paging from the operating system Operating systems solved this problem decades ago with paging. A program doesn\u0026rsquo;t need one contiguous chunk of physical memory. Its address space is cut into fixed-size pages, each page goes wherever physical memory has room, and a page table maps the contiguous virtual addresses the program sees onto physical pages scattered anywhere.\nPagedAttention brings the same idea to GPU memory, with a block playing the role of a page. In this example the block size is 4 tokens, so prompt 1 and prompt 2 each split into two blocks. The block table in the middle is the page table: it records which physical block each logical block lives in. ABCD sits in physical block 5 and EFGH in physical block 0. The numbers have nothing to do with each other, and blocks that are adjacent logically can sit anywhere physically. Both prompts start with ABCD, so their first blocks point at the same physical block.\nThat\u0026rsquo;s the payoff of paging. Every block is the same size, so any free block can go to any request, and there\u0026rsquo;s never a gap that\u0026rsquo;s too small to use. External fragmentation disappears. The only waste left is inside a request\u0026rsquo;s last block, which may not be full. In vLLM a block is typically 16 tokens, so each request wastes fewer than 16 token slots, which is negligible.\nWhat a physical block looks like Now let\u0026rsquo;s look at how vLLM manages the physical memory. When vLLM starts up, it cuts the GPU memory set aside for the KV cache into physical blocks all at once. The free blocks are chained into a doubly linked list called free_block_queue. When a block is needed, it\u0026rsquo;s taken off this list, and when it\u0026rsquo;s no longer needed, it goes back. Each node in the list is a KVCacheBlock object, the metadata for one physical block. It holds four kinds of information:\nblock_id, the number of the physical block it describes. ref_cnt, how many requests are currently using the block. _block_hash, the hash of the prefix stored in the block, which we\u0026rsquo;ll come back to shortly. prev_free_block and next_free_block, the two pointers that chain free blocks into the list. This free list lives in CPU memory and only does the bookkeeping. The actual blocks live in GPU memory. At the top level they\u0026rsquo;re split into L layers: a Transformer has L decoder layers, and each layer needs its own K and V. So a physical block is really a column that runs through every layer. In the diagram, the dashed yellow outline marks block_id 2, the same slot in every layer. Across a single layer there are num_blocks physical blocks.\nZoom in on one block and it\u0026rsquo;s a tensor of shape [2, block_size, num_kv_heads, head_dim], where the 2 holds K and V.\nSo the total size of the KV cache is all of these numbers multiplied together: L layers, num_blocks blocks per layer, block_size tokens per block, 2 for K and V, the number of KV heads, the dimension of each head, and finally the bytes per number.\n$$ \\begin{aligned} \\text{KV cache} = {}\u0026\\textcolor{#1d4ed8}{L} \\times \\textcolor{#b45309}{\\mathit{num\\_blocks}} \\times \\mathit{block\\_size} \\times \\textcolor{#b45309}{2}\\\\ \u0026\\times \\mathit{num\\_kv\\_heads} \\times \\mathit{head\\_dim} \\times \\textcolor{#6b7280}{\\text{bytes per element}} \\end{aligned} $$ This is the same formula from my previous post on KV cache size. The only difference is that the total context length is now written as num_blocks × block_size.\nBlock hash: telling whether two prefixes match How is the _block_hash in each KVCacheBlock computed? With a chained hash. The formula is short: the hash of block n is the hash of block n−1\u0026rsquo;s hash together with all the tokens in block n.\n$$ \\textcolor{#6d28d9}{\\mathrm{hash}(\\text{Block } n)} = \\mathrm{hash}\\big(\\textcolor{#6d28d9}{\\mathrm{hash}(\\text{Block } n-1)},\\ \\textcolor{#b45309}{\\mathrm{tokens}(n)}\\big) $$\nBecause the hash is chained, block n\u0026rsquo;s hash already folds in every token from the first block through block n. So when two blocks have the same hash, we know that block and everything before it are identical. A matching prefix is exactly what prompt caching needs before it can reuse anything.\nTwo lookup structures PagedAttention in vLLM relies on two more data structures. The first is BlockHashToBlockMap, a map from block hash to KVCacheBlock. There\u0026rsquo;s a single global copy shared by all requests. When a new request arrives, vLLM looks up each of its block hashes in this map. A hit means some earlier request already computed that prefix, and the map says where in GPU memory the result is stored.\nThe second is the block table, called req_to_blocks in the code. For each request it keeps the list of KVCacheBlocks that hold the request\u0026rsquo;s KV cache, in order. This list is exactly the logical-to-physical mapping from the paging picture earlier.\nThat\u0026rsquo;s every concept PagedAttention needs. Now let\u0026rsquo;s follow a complete example.\nWalking through two requests At time 0, the first request arrives. Its prompt is 12 tokens, ABCD through IJKL, which splits into 3 logical blocks at a block size of 4. The three block hashes come out as 0xABCD, 0xABGH and 0xABKL. (Since each hash covers the whole prefix, I\u0026rsquo;m labeling it with the first two and last two tokens of that prefix to keep it readable. Real hashes look nothing like this.) BlockHashToBlockMap starts out empty, so all three lookups miss. Every logical block needs a new physical block, so vLLM takes blocks 0, 1 and 2 off the front of free_block_queue and records them in req_to_blocks[0]. On the GPU side, each of the three blocks gets ref_cnt 1, meaning one request is using it, and records its own hash. Since nothing hit, all three blocks have to go through prefill, so they\u0026rsquo;re all marked COMPUTE.\nAt time 1, the second request arrives. Its first eight tokens are identical to the first request\u0026rsquo;s, followed by XYZ. Its first two block hashes therefore come out as 0xABCD and 0xABGH, matching the first request exactly. The third block holds only three tokens, and there\u0026rsquo;s a rule here: only full blocks get a hash. This block is the tail of the prompt and will soon be filled by tokens from decode, so its hash is computed once it\u0026rsquo;s full.\nLooking up BlockHashToBlockMap, the first two hashes are already there. Those two blocks don\u0026rsquo;t need new physical blocks or copies, because their KV cache is already sitting in physical blocks 0 and 1. Only the third block needs a new physical block, so vLLM takes block 3 off the queue, and req_to_blocks[1] becomes blocks 0, 1 and 3. The ref_cnt of blocks 0 and 1 goes from 1 to 2, meaning two requests now share that memory. Block 3 isn\u0026rsquo;t full, so its hash is empty and its ref_cnt is 1. It\u0026rsquo;s also the only block that needs computing. The results for the first two blocks are already in memory, so they\u0026rsquo;re skipped.\nThat\u0026rsquo;s the core rule for new requests. When a block\u0026rsquo;s prefix hash hits, the block is neither copied nor recomputed. vLLM finds the physical block, adds 1 to its ref_cnt and reads the result directly. Only blocks freshly allocated from the free queue need actual computation.\nWhen a request finishes What happens when a request is done? At time 2, the first request finishes. Its three blocks, 0, 1 and 2, each have their ref_cnt reduced by 1. Blocks 0 and 1 are still in use by the second request, so their counts drop to 1 and they stay put. Block 2 drops to 0, meaning no one is using it, so it goes back into free_block_queue.\nNotice that BlockHashToBlockMap hasn\u0026rsquo;t changed at all. The entry mapping 0xABKL to block 2 is still there. Block 2 can now be handed to a future request, but its computed KV data is still sitting in GPU memory. If some future request produces a hash that matches what\u0026rsquo;s in block 2, that result shouldn\u0026rsquo;t go to waste. It can be reused instead of recomputed.\nAt time 3, the second request finishes too. The ref_cnt of blocks 0, 1 and 3 all drop to zero, and all three go back into the free queue. The hash map still has the same three entries, none deleted.\n(The diagrams simplify the queue order. In vLLM, freed blocks are appended to the end of the queue and new blocks are taken from the front, so the blocks that have sat unused the longest are the first to be reused.)\nSo when a request ends, returning a block to the free queue doesn\u0026rsquo;t mean throwing it away. The KV data in those four physical blocks is still there, and so are the hash map entries. The blocks are only marked as unused and available. Until one of them is actually handed out again, any request with the same prefix can still hit it.\nPicking freed blocks back up At time 4, a third request arrives, and it\u0026rsquo;s an interesting one. Its first eight tokens are again A through H, the same as the first two requests, followed by four Ps and four Qs, for four blocks in total. The first two hashes are again 0xABCD and 0xABGH, and the last two are new: 0xABPP and 0xABQQ. The lookup gives two hits and two misses. The hit blocks, 0 and 1, are sitting in the free queue with ref_cnt 0. But as we just saw, their results are still valid and still in GPU memory, so vLLM pulls them straight out of the queue and sets their ref_cnt from 0 back to 1. None of their KV cache needs recomputing, so prefill skips the first two blocks.\nThe last two blocks missed, so they need new blocks from the free queue: physical blocks 3 and 2, taken from the front. And here\u0026rsquo;s the interesting part. Block 2 still holds the data for hash 0xABKL. Now that block is about to store the four Qs, its old data will be overwritten, so the 0xABKL entry in the hash map is no longer valid. Only at this moment is it evicted, replaced by 0xABQQ. Eviction isn\u0026rsquo;t a periodic cleanup, and it isn\u0026rsquo;t a cache expiring. A hash entry becomes invalid only when its physical block is handed out again and the old contents are about to be overwritten. The rest follows naturally: req_to_blocks[2] is blocks 0, 1, 3 and 2, and only the last two need computing.\nWrapping up To sum up, PagedAttention cuts the KV cache into blocks that can be paged, mapped and shared. Physical blocks in GPU memory are small fixed-size slots for KV tensors. The hash map lets different requests find the same block by its content. ref_cnt keeps a shared block from being freed while someone still uses it. And the free queue lets blocks from finished requests be picked back up by the next request. Together, these make far better use of GPU memory when serving LLMs.\n(The example uses a block size of 4, a tiny pool of blocks and short labels like 0xABCD so everything fits in a diagram. A real scheduler also handles preemption, swapping and more, which this post leaves out.)\nReferences Kwon et al. (@woosuk_k). “Efficient Memory Management for Large Language Model Serving with PagedAttention.” SOSP 2023. vLLM (@vllm_project). “Automatic Prefix Caching.” vLLM documentation. vLLM (@vllm_project). “Paged Attention.” vLLM documentation. Aleksa Gordić (@gordic_aleksa). “Inside vLLM: Anatomy of a High-Throughput LLM Inference System.” 2025. Sankalp (@dejavucoder). “How prompt caching works: Paged Attention and Automatic Prefix Caching plus practical tips.” 2025. ","permalink":"https://zhangchitc.com/posts/paged-attention/","summary":"Why serving LLMs fragments GPU memory, and how vLLM\u0026rsquo;s PagedAttention fixes it with OS-style paging: physical blocks, a block table, chained block hashes, and a walkthrough of how blocks are allocated, shared, freed and evicted.","title":"How PagedAttention Works: Paging the KV Cache Like an Operating System"},{"content":"In my last post I explained that a prompt cache stores the KV cache for your prompt. Several readers followed up with the obvious next question: how much physical GPU memory does that KV cache take up? And with memory chips as expensive as they are right now, how do you fit more context into the memory you have?\nThis post works out how big the KV cache is on a real inference server. We\u0026rsquo;ll start from the simplest possible attention and build the formula up one factor at a time.\nA quick recap: what the KV cache stores Inference in a Transformer revolves around attention, the mechanism that lets the model search the context for the tokens most relevant to the one it\u0026rsquo;s about to generate. The token embeddings are projected into three matrices, Q, K and V. Multiplying Q by K transposed gives a table of scores, softmax turns those scores into weights, and the weights are used to take a weighted sum of the rows of V. That sum is the output.\nDuring decode, each new token brings a fresh Query, which is compared against every earlier Key and then used to weight every earlier Value. The Query is new at every step and thrown away afterwards. K and V, on the other hand, are needed again for every token the model generates from then on, so they\u0026rsquo;re kept. That\u0026rsquo;s the KV cache.\nNow some numbers. Say the prompt is \\(n\\) tokens long and each token\u0026rsquo;s vector has \\(d\\) dimensions. Then K and V are each an \\(n \\times d\\) matrix, and the memory the KV cache needs is:\n$$ \\text{KV cache} = \\textcolor{#d97706}{2} \\times n \\times d \\times \\textcolor{#6b7280}{\\text{bytes per element}} $$ The 2 is there because we store two matrices, K and V. Bytes per element is how much space one number takes. With FP16, the most common format, that\u0026rsquo;s 2 bytes.\nMulti-head attention: every head stores its own K and V A single set of Q, K and V has a limitation: it can only judge how tokens relate from one angle. Language is full of different kinds of relationships. Which earlier word does \u0026ldquo;it\u0026rdquo; refer to? Who did something to whom? Is \u0026ldquo;Apple\u0026rdquo; the company or the fruit? One attention pattern struggles to capture all of these at once. What you tend to get is an average of them, which captures none of them precisely.\nMulti-head attention fixes this. Instead of computing attention once, it splits attention into several heads, each with its own Q, K and V, so each head can look at the same input from a different angle in parallel.\nHere\u0026rsquo;s how the computation goes. Each head projects the tokens into its own Q, K and V and computes attention on its own. In this example there are 4 heads, so we get 4 output matrices, each \\(n \\times d\\). We only want one output, though, so the 4 matrices are concatenated side by side into a single \\(n \\times 4d\\) matrix, which holds everything the 4 heads found. A \\(4d \\times d\\) projection matrix, \\(W^O\\), then turns it into the final output of multi-head attention.\n(To keep the picture simple, each head here works in the full d dimensions. In real models each head usually works in a smaller slice, so the heads\u0026rsquo; dimensions add up to the model\u0026rsquo;s width. Qwen3-8B, which we\u0026rsquo;ll meet at the end, has 32 heads of 128 dimensions each, for a width of 4,096. From here on, \\(d\\) in the formula means the dimension of one head.)\nThe important part for memory is that every head computes its own K and V, and all of them have to be kept. So the KV cache grows by a factor of \\(h\\), the number of attention heads:\n$$ \\text{KV cache} = \\textcolor{#d97706}{2} \\times n \\times d \\times \\textcolor{#d97706}{h} \\times \\textcolor{#6b7280}{\\text{bytes per element}} $$ Stacked decoder layers: different K and V at every layer Everything so far happens inside a single decoder layer. A real Transformer stacks many of these layers on top of each other.\nWhy stack them? In one attention layer, each token gets only one chance to mix in information from the context, and that\u0026rsquo;s not enough to build much understanding. Language has to be understood in layers of abstraction. It\u0026rsquo;s a bit like a convolutional network recognizing an image: the bottom layers see only pixels, the next layers assemble pixels into edges and textures, the layers above that combine those into eyes and ears, and only the top layers recognize the animal in the picture. Stacking many identical decoder layers lets a Transformer refine each token\u0026rsquo;s representation the same way, step by step, into something more abstract and complete.\nEvery layer has the same structure. First comes the multi-head attention we just covered, then a feed-forward network, and each of the two is followed by an Add \u0026amp; Norm step: a residual connection plus normalization. Those steps mainly keep a network dozens of layers deep stable during training. They don\u0026rsquo;t take part in attention and don\u0026rsquo;t produce any K or V, so they don\u0026rsquo;t affect the KV cache. The token embeddings enter the first layer, each layer\u0026rsquo;s output becomes the next layer\u0026rsquo;s input, and after the top layer the model uses the final output to predict the next token.\nBecause every layer has its own \\(h\\) sets of K and V, the formula picks up another factor, \\(L\\), the number of decoder layers:\n$$ \\text{KV cache} = \\textcolor{#d97706}{2} \\times n \\times d \\times \\textcolor{#d97706}{h} \\times \\textcolor{#d97706}{L} \\times \\textcolor{#6b7280}{\\text{bytes per element}} $$ With this many factors, it\u0026rsquo;s fair to ask whether every layer\u0026rsquo;s K and V really need to be kept. They do. Every new token passes through the whole stack starting from the first layer, computing attention at each one. If any layer\u0026rsquo;s K and V weren\u0026rsquo;t cached, that layer\u0026rsquo;s cost per decode step would jump from the order of \\(n\\) to the order of \\(n^2\\). However fast the other layers are, that one layer would hold up the whole decode, so the K and V of every layer have to stay.\nMQA and GQA: replacing h with h_kv Multi-head attention is the most expressive option, since every head can look at the context from its own angle. The downside is just as clear: with many heads, each one storing its own K and V puts enormous pressure on memory. Engineers have come up with several ways around this.\nThe most aggressive is multi-query attention (MQA). Every head keeps its own Q, so the model still looks from many angles, but all the heads share a single K and V. That cuts the K and V you need to store by a factor of \\(h\\). The memory savings are real, but so is the cost: with every head attending through the same K and V, much of the benefit of having multiple heads is lost, and output quality drops noticeably.\nThe compromise is grouped-query attention (GQA), which is what most new models use today. Instead of giving every head its own K and V like MHA, or making every head share one like MQA, it splits the heads into groups, and each group shares one K and V. Pair up 8 heads, for example, and you only need 4 sets of K and V. Seen this way, MHA and MQA are both special cases of GQA: MQA is GQA with one group, and MHA is GQA with as many groups as heads. GQA gets the best of both, striking a good balance between expressiveness and memory.\nThat gives us the final formula, where \\(h_{\\text{kv}}\\) is the number of K and V heads actually stored:\n$$ \\text{KV cache} = \\textcolor{#d97706}{2} \\times n \\times d \\times \\textcolor{#059669}{h_{\\text{kv}}} \\times \\textcolor{#d97706}{L} \\times \\textcolor{#6b7280}{\\text{bytes per element}} $$ Checking the formula against a real calculator We can check this against a real tool. The LMCache project has a KV cache calculator on its documentation site that works out the KV cache size for open models. With its default model, Qwen3-8B, the data type set to BF16 and the context set to 100,000 tokens, it reports that the KV cache for this one request takes 13.73 GB of GPU memory.\nQwen3-8B has 36 layers and 32 attention heads, but only 8 KV heads, and each head has 128 dimensions. BF16 takes 2 bytes per element. Plug those into the formula:\n$$ \\begin{aligned} \\text{KV cache} \u0026= \\textcolor{#d97706}{2} \\times \\underbrace{100{,}000}_{n} \\times \\underbrace{128}_{d} \\times \\underbrace{\\textcolor{#059669}{8}}_{h_{\\text{kv}}} \\times \\underbrace{\\textcolor{#d97706}{36}}_{L} \\times \\underbrace{\\textcolor{#6b7280}{2}}_{\\text{bytes}}\\\\ \u0026= 14{,}745{,}600{,}000\\ \\text{bytes} \\approx 13.73\\ \\text{GiB} \\end{aligned} $$ That\u0026rsquo;s about 7.37 billion numbers, or 14.7 billion bytes. The calculator labels the result GB, but it divides by 1024³, so strictly speaking it\u0026rsquo;s 13.73 GiB, or about 14.7 GB in decimal units. Either way, it matches the formula exactly.\n(This counts only the K and V tensors themselves. The total memory a GPU process uses also includes the model weights, activations and runtime workspace, so this formula can\u0026rsquo;t predict that total on its own.)\nReferences Vaswani et al. (@ashVaswani). “Attention Is All You Need.” NeurIPS 2017. Ainslie et al. “GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints.” EMNLP 2023. LMCache (@lmcache). “KV Cache Size Calculator.” LMCache documentation. ","permalink":"https://zhangchitc.com/posts/kv-cache-memory/","summary":"We build the KV cache size formula one factor at a time, from single-head attention to multi-head, stacked layers and MQA/GQA, then check it against a real calculator: 13.73 GiB for 100,000 tokens on Qwen3-8B.","title":"How Much GPU Memory Does the KV Cache Really Take?"},{"content":"I\u0026rsquo;ve covered prompt caching a few times now, and the same questions keep coming back. Does the server save the model\u0026rsquo;s previous answer? When my prompt prefix hits the cache, does the model skip running altogether and just hand back a stored response, like a database lookup?\nBoth come from common misconceptions about how LLMs work, and they boil down to the same question: what exactly is sitting in a prompt cache? Let\u0026rsquo;s answer that properly. Along the way we\u0026rsquo;ll meet its close cousin, the KV cache, and by the end the difference between the two will be clear.\nTwo phases: prefill and decode To get there, we need to start with how a Transformer generates a response. It happens in two phases.\nThe first is prefill. The model reads your entire prompt in one go, building up its understanding of the context as it reads, so it\u0026rsquo;s ready to start answering.\nThe second is decode, where the model writes the answer. Here\u0026rsquo;s the catch: even if your prompt is a thousand tokens long, prefill reads it all at once, but on the way out the model can only produce one token at a time. And before each new token, it has to look back over everything that came before: your whole prompt, plus every token it has generated so far. Generating text this way is called autoregressive.\nPrompt caching mainly speeds up the first phase, prefill.\nA quick refresher on attention To see why, we need to revisit attention, the core of the Transformer and the key to understanding the KV cache.\nAttention exists because, when the model predicts the next token, the earlier words aren\u0026rsquo;t equally important. It needs a way to tell which ones matter most right now.\nHere\u0026rsquo;s an example. The prompt is \u0026ldquo;The quick brown fox jumps\u0026rdquo;, and the model has to predict what comes next. In the diagram, thicker arrows mean more attention. The thickest ones land on \u0026ldquo;quick\u0026rdquo;, \u0026ldquo;brown\u0026rdquo; and \u0026ldquo;fox\u0026rdquo;, while \u0026ldquo;The\u0026rdquo; and \u0026ldquo;jumps\u0026rdquo; barely register. The model outputs \u0026ldquo;over\u0026rdquo;.\n(The weights are illustrative, not measured from a real model.)\nWhy \u0026ldquo;over\u0026rdquo;? Because \u0026ldquo;The quick brown fox jumps over the lazy dog\u0026rdquo;, the pangram that packs every letter of the alphabet into one sentence and is a staple of typing drills and font previews, appears in its training data more times than anyone can count. That co-occurrence is baked into its weights. Once it spots \u0026ldquo;quick brown fox\u0026rdquo; as the words that matter, \u0026ldquo;over\u0026rdquo; is the obvious next pick. That\u0026rsquo;s attention in a nutshell: instead of treating every word equally, it weighs them by how much they matter at this moment.\nSo how is it computed? Every token gets projected into three vectors: a Query, a Key and a Value.\nThe Query is what this token is looking for. The Key is what this token can offer. The Value is the information this token actually carries. The names come from information retrieval and databases. The Query is like what you type into a search box, the Key is like an index key in a database, and the Value is the data you get back for that key.\nThe current token takes its Query and scores it against the Key of every earlier token. A higher score means that token is more relevant. Those scores then become weights for a weighted sum of all the tokens\u0026rsquo; Values. That\u0026rsquo;s the whole attention computation, and the formula says exactly the same thing:\n$$ \\text{Attention}(Q,K,V)=\\text{softmax}\\!\\left(\\frac{QK^{\\top}}{\\sqrt{d_k}}\\right)V $$ \\(QK^{\\top}\\) is \u0026ldquo;score the current Query against every Key\u0026rdquo;. Dividing by \\(\\sqrt{d_k}\\) is a scaling trick from the original paper that keeps the scores from getting too large. The softmax normalizes the scores into weights that sum to 1. Multiplying by \\(V\\) is \u0026ldquo;take the weighted sum of every token\u0026rsquo;s Value\u0026rdquo;. What prefill and decode compute Back to the computation. Say your prompt has \\(n\\) tokens, and each token\u0026rsquo;s Q, K and V vectors have \\(d\\) dimensions. Stack the \\(n\\) Query vectors row by row and you get an \\(n \\times d\\) matrix. The same goes for K and V.\nHere\u0026rsquo;s what prefill does. It computes all three matrices for the whole prompt. Then it multiplies Q by K transposed in a single step, producing an \\(n \\times n\\) table of scores between every pair of tokens in the prompt. That table is used to weight the entire V matrix. The attention for the whole prompt is done in one shot.\nBecause that table is \\(n \\times n\\), it grows quickly as the prompt gets longer. The amount of computation is huge, which is why prefill is limited by the GPU\u0026rsquo;s raw compute.\nNow decode. Generating one new token adds just one row for that token: its own Q, K and V, each a \\(1 \\times d\\) vector. So the K and V matrices each grow by one row, to \\(n+1\\). The new token\u0026rsquo;s Query is compared against every row of K, and the result is used to take a weighted sum over V.\nThe second token, the third, and every one after that work the same way: K and V keep growing by one row at a time. And all the earlier rows get used again for every single new token.\nWhat the KV cache stores, and how much it saves Decoding one token at a time like this means recomputing K and V for every earlier token at every step. But once those K and V are computed, they never change. So engineers had the obvious idea: keep them around during decode. That\u0026rsquo;s the KV cache.\nWhy store K and V but not Q? A token\u0026rsquo;s Query is only used once, at the step that produces that token. At the next step there\u0026rsquo;s a new token with a new Query, and the old one is useless, so storing it would be a waste. K and V, on the other hand, never change once computed and get pulled out again at every decode step, so they\u0026rsquo;re worth keeping.\nThe KV cache exists to speed up every decode step. Let \\(N\\) be the current context length. Without a KV cache, every new token means recomputing everything for all \\(N\\) earlier tokens from scratch, which costs on the order of \\(N^2\\) per step. With a KV cache, the model only computes K and V for the one new token and reads the rest from the cache, bringing the cost per step down to the order of \\(N\\).\nThat\u0026rsquo;s a factor of \\(N\\) less work per step. With a few thousand tokens of context, each step does thousands of times less work, and with tens of thousands of tokens, tens of thousands of times less.\nPrefill fills the cache, decode appends to it With a KV cache in place, here\u0026rsquo;s what the full inference process looks like.\nDuring prefill, the model computes K and V for every token in the prompt at once and writes them into an empty KV cache. Prefill is where the KV cache gets initialized.\nDuring decode, each new token only needs its own K and V, which are appended to the end of the cache. Decode keeps adding one row at a time.\nIn the diagram below, the orange arrows are writes: prefill fills the cache from above, and each decode step appends one row from below (the highlighted last column of \\(K^{\\top}\\) and last row of V). The gray arrows on the right are reads: both the prefill and the decode attention take K and V straight from the cache.\nSo, back to the question we started with: what exactly is stored in a prompt cache? It\u0026rsquo;s that KV cache, the one prefill builds after your prompt has gone through the Transformer. It\u0026rsquo;s an intermediate result from halfway through the model\u0026rsquo;s computation. It\u0026rsquo;s not your last answer, and it\u0026rsquo;s certainly not a string that can be handed straight back to you.\nWhat changes with a prompt cache So far, the KV cache only removes repeated work within a single response. Inside one request, decode doesn\u0026rsquo;t recompute the earlier K, V or attention. A prompt cache extends that reuse across requests.\nWhat does that mean? Without a prompt cache, prefill starts from scratch for every new request. Your system prompt, your tool definitions, the earlier turns of the conversation: K and V get recomputed for every one of those tokens.\nWith a prompt cache, things change. Prefill notices that this request\u0026rsquo;s prefix is identical to last time, so it pulls out the K and V it stored back then and uses them directly, skipping that whole stretch of attention computation. The model only has to process the small part you added this time. Once that\u0026rsquo;s done, it joins up with the cached part and decode carries on as usual. (The blue marker in the diagram shows where the cached prefix ends, which Anthropic\u0026rsquo;s API sets with cache_control.)\nNote that a cache hit doesn\u0026rsquo;t mean you get a ready-made answer. What it saves is the prefill computation. The model still has to generate every token of its reply. That\u0026rsquo;s also why cached tokens are so much cheaper: you\u0026rsquo;re not making the GPU recompute them.\nKV cache vs prompt cache To wrap up, here\u0026rsquo;s how the two compare.\nIn one sentence: the KV cache saves repeated decode work within a request, and the prompt cache saves repeated prefill work across requests. Used together, one covers decode and the other covers prefill.\nReferences Vaswani et al. (@ashVaswani). “Attention Is All You Need.” NeurIPS 2017. Sankalp (@dejavucoder). “How prompt caching works: Paged Attention and Automatic Prefix Caching plus practical tips.” 2025. vLLM. “Automatic Prefix Caching.” vLLM documentation. Gim et al. “Prompt Cache: Modular Attention Reuse for Low-Latency Inference.” MLSys 2024. ","permalink":"https://zhangchitc.com/posts/kv-cache-vs-prompt-cache/","summary":"A prompt cache doesn\u0026rsquo;t store your last answer. We walk through prefill, decode and attention to see what the KV cache keeps, and how a prompt cache reuses it across requests.","title":"KV Cache vs. Prompt Cache: What's the Difference?"},{"content":"Hi, I\u0026rsquo;m Chi Zhang. I studied computer science at Peking University and have spent more than a decade building consumer products at Facebook and Airbnb. I started out on Facebook\u0026rsquo;s growth team in Kuala Lumpur, went on to work at both companies\u0026rsquo; headquarters in the Bay Area, and later joined the founding team of Airbnb\u0026rsquo;s Beijing office, where I helped build the engineering team from the ground up.\nThese days most of my curiosity goes into AI. Working with coding agents like Claude Code and Codex made me curious about what happens underneath, both in the agents themselves and in the LLMs that power them. So I started taking them apart. I capture the real requests, read the papers, redo the math step by step, and turn what I find into visual explainers.\nMost of this work first appears as short videos in Chinese, under the name 张司机在路上 (\u0026ldquo;Driver Zhang on the road\u0026rdquo;). This blog is where the English versions live, with the reasoning written out in full.\nOff the keyboard Canadian CASI Level 2 snowboard instructor Licensed sport pilot (fixed-wing aircraft) in China Hiker who has summited Kilimanjaro, Mount Fuji and Mount Kinabalu, Malaysia\u0026rsquo;s highest peak Scuba diver and freediver Foodie who once documented a week of breakfasts at Airbnb\u0026rsquo;s San Francisco HQ Get in touch The easiest way to reach me is on X or LinkedIn. Everything else is linked from the homepage.\n","permalink":"https://zhangchitc.com/about/","summary":"\u003cp\u003eHi, I\u0026rsquo;m Chi Zhang. I studied computer science at Peking University and have spent more than a decade building consumer products at Facebook and Airbnb. I started out on Facebook\u0026rsquo;s growth team in Kuala Lumpur, went on to work at both companies\u0026rsquo; headquarters in the Bay Area, and later joined the founding team of Airbnb\u0026rsquo;s Beijing office, where I helped build the engineering team from the ground up.\u003c/p\u003e\n\u003cp\u003eThese days most of my curiosity goes into AI. Working with coding agents like Claude Code and Codex made me curious about what happens underneath, both in the agents themselves and in the LLMs that power them. So I started taking them apart. I capture the real requests, read the papers, redo the math step by step, and turn what I find into visual explainers.\u003c/p\u003e","title":"About"}]