1 00:00:01,000 --> 00:00:09,300 [Hal Turing] Alrighty! Thanks for tuning in! Hello AI world! I am your host, Hal Turing, and my co-host is Dr. Ada Shannon. 2 00:00:09,300 --> 00:00:36,975 [Hal Turing] Today we're looking at Hydragen: High-Throughput LLM Inference with Shared Prefixes. The first author is Jordan Juravsky, with Bradley Brown, Ryan Ehrlich, Daniel Fu, Christopher Ré and Azalia Mirhoseini. It comes from Stanford, with Oxford and Waterloo co-authors, and it's on arXiv from 2024. The claim is up to 32 times higher end-to-end throughput, and the question is how much of that is the idea and how much is the baseline. 3 00:00:36,975 --> 00:00:54,174 [Dr. Ada Shannon] The stakes are simple. When many requests share a long prompt, decode attention is where large-batch throughput goes to die. The paper asks something narrow. If the system already stores the shared prefix once, can you also stop reading it once per sequence, and what does that buy end to end? 4 00:00:54,174 --> 00:00:57,349 [Hal Turing] Start me at the bottom, then. What's decode? 5 00:00:57,349 --> 00:01:18,949 [Dr. Ada Shannon] Generation has two phases. Prefill processes the whole prompt in parallel, so attention there is matrix times matrix. Decode then emits one token per step. Each step, a sequence has a single new query that must attend to the keys and values of every earlier token. Those are stored in the KV cache, so they aren't recomputed. One query against a long cache is a matrix-vector product. 6 00:01:18,949 --> 00:01:21,249 [Hal Turing] And that's bad because? 7 00:01:21,249 --> 00:01:47,899 [Dr. Ada Shannon] Arithmetic intensity: operations per byte moved from memory. In a matrix-vector product, every matrix element is read from GPU memory, used for one multiply-add, and then discarded. That's memory-bound, so the arithmetic units mostly wait. Matrix-matrix work reuses each byte many times. It also runs on tensor cores, the matrix-multiply units on modern GPUs, which have over ten times the FLOPS of the rest of the chip. They only help when the work is shaped as a matrix-matrix product. 8 00:01:47,899 --> 00:01:54,424 [Hal Turing] But we batch requests precisely so the GPU stays busy. Why doesn't batching fix it? 9 00:01:54,424 --> 00:02:15,299 [Dr. Ada Shannon] For the dense layers it does. A batch of B tokens times one weight matrix is a real matrix-matrix product, and the weights are read once for everyone. Attention is the exception, because every sequence has its own KV cache. Batching more sequences means reading more caches, so attention grows to dominate decode as batch size and context length grow. 10 00:02:15,299 --> 00:02:19,549 [Hal Turing] Unless the sequences share a cache. Which they often do. 11 00:02:19,549 --> 00:02:53,199 [Dr. Ada Shannon] Yes. A shared prefix is a run of identical tokens at the start of many sequences: a chatbot system prompt, few-shot examples, a long document. Because attention is causal, a token's keys and values depend only on earlier tokens. So identical prefixes give identical keys and values in every sequence. Sharing generalizes to a tree. Take a coding system sampling 128 solutions per problem. A few-shot prompt sits at the root, each problem statement is a branch, and the sampled solutions are the leaves. Each node holds tokens shared by everything below it. 12 00:02:53,199 --> 00:02:58,324 [Hal Turing] I thought this was already solved. vLLM shares prefixes. 13 00:02:58,324 --> 00:03:31,549 [Dr. Ada Shannon] It solved half of it, and this distinction is the heart of the paper. PagedAttention, from Kwon and colleagues at Berkeley, SOSP 2023, is the memory manager inside vLLM. It stores the KV cache in pages and lets sequences point to the same pages for a shared prefix. That fixes capacity, since the prefix is stored once. But the attention kernel still treats each sequence independently. Each one streams the whole cache, prefix included, from memory. So storage is deduplicated and reads aren't. With B sequences and a prefix of length P, you read P tokens' worth of keys and values B times. 14 00:03:31,549 --> 00:03:39,124 [Hal Turing] So redundant storage and redundant reads are different bills, and PagedAttention only paid one of them. 15 00:03:39,124 --> 00:03:51,974 [Dr. Ada Shannon] Yes. Decode attention is bandwidth-bound, so the read bill is what you pay in time. Hydragen goes after the reads. It also changes the shape of the prefix work so tensor cores can handle it. 16 00:03:51,974 --> 00:03:55,549 [Hal Turing] Does it need a new kind of attention kernel? 17 00:03:55,549 --> 00:04:18,649 [Dr. Ada Shannon] Not a new kind. It builds on the FlashAttention family: the original paper by Tri Dao, Daniel Fu, Stefano Ermon, Atri Rudra and Chris Ré, NeurIPS 2022, and FlashAttention-2 by Dao in 2023. Those compute exact attention in blocks, never materializing the full score matrix, which makes attention efficient in memory traffic. Hydragen uses that machinery and the same blocked-softmax bookkeeping. I'll hold the details for the next part. 18 00:04:18,649 --> 00:04:23,924 [Hal Turing] Is the output approximate, then? Some of these tricks quietly change the answer. 19 00:04:23,924 --> 00:04:36,199 [Dr. Ada Shannon] No. The paper's claim is exact attention. The model, weights and outputs are the same, up to floating-point ordering. It's a change to how attention is computed, not to what is computed. 20 00:04:36,199 --> 00:04:39,174 [Hal Turing] What's the hardware argument behind it? 21 00:04:39,174 --> 00:04:59,124 [Dr. Ada Shannon] The authors' motivating point is that GPU compute has grown faster than memory bandwidth, and a growing share of peak FLOPS is reachable only through matrix-matrix operations. A matrix-vector workload leaves more and more of the chip idle. That's the framing in their first figure, and it's a trend claim, not something the experiments test directly. 22 00:04:59,124 --> 00:05:08,849 [Hal Turing] So here's what I want to track. That 32x is the headline, and I want to know how much belongs to the idea and how much to the baseline. 23 00:05:08,849 --> 00:05:36,974 [Dr. Ada Shannon] The paper tests against vLLM, and the answer to that split is only partly in the paper. Better to know the mechanism before arguing about the number. Hydragen is two techniques stacked. The first is attention decomposition. Split the keys and values into a prefix part and a suffix part. Compute attention separately over each, and also record each part's log-sum-exp, the log of its softmax denominator. Then combine the two outputs, weighting each by the exponential of its LSE and renormalizing. 24 00:05:36,974 --> 00:05:44,474 [Hal Turing] Why isn't that an approximation? Softmax normalizes over everything, and you've chopped the sequence in two. 25 00:05:44,474 --> 00:06:11,399 [Dr. Ada Shannon] Because each half's denominator is just a partial sum of the full one. Say the prefix has total exponentiated score 6 and the suffix has 2. The full denominator is 8. If the prefix half returns an output normalized by 6 and the suffix half returns one normalized by 2, you multiply them back by 6 and 2, add, and divide by 8. You've undone the local normalization and applied the global one. It's exact, and it's the same rescaling FlashAttention uses between blocks. Hydragen applies it at the prefix and suffix boundary. 26 00:06:11,399 --> 00:06:14,699 [Hal Turing] So on its own that's strictly more work. 27 00:06:14,699 --> 00:06:37,199 [Dr. Ada Shannon] Yes, the paper says so. You do two attentions and a merge. The payoff is the second technique, inter-sequence batching. Every sequence's prefix keys and values are identical. So the single query from each of, say, a thousand sequences can be stacked into one thousand-row query matrix and run against the prefix once. The prefix is read from memory once instead of a thousand times, and the operation is now matrix times matrix, so tensor cores can run it. 28 00:06:37,199 --> 00:06:39,099 [Hal Turing] And the suffixes? 29 00:06:39,099 --> 00:06:52,299 [Dr. Ada Shannon] They can't be batched, because each sequence's suffix differs. They stay matrix-vector and memory-bound. That's the main limit. The longer completions get, the more of the cache is suffix, and the less Hydragen helps. 30 00:06:52,299 --> 00:06:55,074 [Hal Turing] What about the tree-shaped sharing case? 31 00:06:55,074 --> 00:07:12,074 [Dr. Ada Shannon] Apply the same decomposition at every node. Take the paper's coding setup: a few-shot prompt shared by everything, then a problem description shared by that problem's 128 samples, then the samples. Queries batch across all descendants at each level. 32 00:07:12,074 --> 00:07:15,274 [Hal Turing] Is that a big engineering lift? 33 00:07:15,274 --> 00:07:34,324 [Dr. Ada Shannon] It isn't. It's PyTorch plus flash-attn 2.3.6 for the prefix, since it returns the LSE, and an xformers Triton kernel for the suffix, which handles changing suffix lengths under CUDA graphs. No custom CUDA. But the user has to declare where sharing happens, and it's only evaluated on Llama-family models. 34 00:07:34,324 --> 00:07:36,574 [Hal Turing] When does it actually help? 35 00:07:36,574 --> 00:07:57,424 [Dr. Ada Shannon] When attention dominates decode time. At small batches, reading model weights dominates and nothing changes. Models with grouped-query or multi-query attention have smaller caches, so there's less to save, though smaller caches also allow bigger batches. The paper includes a no-attention baseline, which deletes attention entirely as a throughput ceiling. 36 00:07:57,424 --> 00:08:02,674 [Hal Turing] Then let's get to the number I flagged. What is the 32x measuring? 37 00:08:02,674 --> 00:08:37,299 [Dr. Ada Shannon] Setup first, since it matters later. CodeLlama-13b on eight A100-40GBs with tensor parallelism, batch of 1024, 128 generated tokens each. Higher throughput is better. The baseline is vLLM version 0.2.7, and timing counts decoding only, with prefill subtracted. Now grow the shared prefix from 1K to 16K tokens. vLLM falls from just under five thousand tokens a second to under five hundred. Hydragen stays close to the no-attention ceiling. The 32x is the best point on this sweep. 38 00:08:37,299 --> 00:08:40,724 [Hal Turing] Is that the same shape on the smaller model? 39 00:08:40,724 --> 00:08:51,249 [Dr. Ada Shannon] Yes. In the 7B table, going from 1K to 16K costs Hydragen about a tenth of its throughput and costs vLLM about nine tenths. 40 00:08:51,249 --> 00:08:53,449 [Hal Turing] And at small batches? 41 00:08:53,449 --> 00:08:57,799 [Dr. Ada Shannon] All methods are about the same. The gap is a large-batch result. 42 00:08:57,799 --> 00:09:01,599 [Hal Turing] Does the kernel on its own show the same thing? 43 00:09:01,599 --> 00:09:31,699 [Dr. Ada Shannon] On one A100 with the per-GPU shape of a 34B model, Hydragen attention is up to about 16 times faster than a FlashAttention baseline. It's biggest with large batches, long prefixes and short suffixes, and it fades as suffixes grow. One setup detail: this microbenchmark flushes the L2 cache every iteration, and the end-to-end runs don't. Gains were larger on an L40S, which has more compute per byte of bandwidth. On an H100 they were similar to the A100, because that flash-attn version isn't Hopper-optimized. 44 00:09:31,699 --> 00:09:37,624 [Hal Turing] Fine, so the tree case. Does the two-level version earn its keep on the coding task? 45 00:09:37,624 --> 00:10:08,299 [Dr. Ada Shannon] On 120 APPS problems with a 2,400-token two-shot prompt, two-level sharing cuts total inference time by 18% at the same batch size. Lower time is better. Then the saved memory allows a larger batch, and the total reduction is about 55%. The text calls that an additional 45%, which reconciles if it applies to the already-reduced time. So most of the total gain is a memory effect, not faster attention. The authors separate the two, which is the right accounting. There are no repeated runs reported for this experiment. 46 00:10:08,299 --> 00:10:11,549 [Hal Turing] Is there a long-context result too? 47 00:10:11,549 --> 00:10:27,799 [Dr. Ada Shannon] One, as supporting evidence. On a roughly 20,000-token document with Yi-6B-200k on four A100s, Hydragen answers 256 questions faster than the FlashAttention baseline answers 64. 48 00:10:27,799 --> 00:10:38,749 [Hal Turing] So take the 32x apart. How much of it survives against something newer than vLLM 0.2.7, and what does the paper actually support? 49 00:10:38,749 --> 00:10:59,724 [Dr. Ada Shannon] Three candidate sources, and the paper separates none of them. First, old-vLLM overhead. The appendix says vLLM 0.2.7 detokenizes the prompt once per sequence, which costs minutes at large batch. The authors subtract a one-token run and patch out a line in one baseline. But they say the cause only 'appears' to be this, and there's no profiler breakdown. 50 00:10:59,724 --> 00:11:01,349 [Hal Turing] Second one? 51 00:11:01,349 --> 00:11:21,624 [Dr. Ada Shannon] Kernel shape. PagedAttention does a per-sequence matrix-vector read of the prefix. That's exactly what Hydragen is built to fix, so this slice of the gap is the real contribution. Third, scheduling. vLLM 0.2.7 has no automatic prefix caching, and the paper tests none. So the gap can't be apportioned from the text. 52 00:11:21,624 --> 00:11:26,749 [Hal Turing] That's uncomfortable for a headline number. Do newer engines close it? 53 00:11:26,749 --> 00:11:48,774 [Dr. Ada Shannon] Not measured here, so I won't give a number. Later vLLM releases, FlashInfer, FlashAttention-3 backends: none are in the paper. What would settle it is rerunning the batch-1024, prefix 1K-to-16K sweep on a current engine with a shared-prefix-aware attention backend. As background, outside this source: cascade-style attention in later serving stacks adopts the same prefix and suffix decomposition idea. 54 00:11:48,774 --> 00:12:01,524 [Hal Turing] Now the cache question. The paper admits vLLM's single copy of the prefix can get better GPU cache hit rates. Doesn't that absorb some of the baseline's redundant reads? 55 00:12:01,524 --> 00:12:32,874 [Dr. Ada Shannon] Partly, yes. And it's why the two benchmarks answer different questions. The microbenchmark flushes L2 every iteration, so it's a cold-cache bound on kernel gains. The end-to-end runs don't flush, so they're closer to serving. My reasoning, and it's inference, not a result: a 1K-to-16K-token prefix across many heads and layers per GPU is far more than survives between layers. So reuse across a full decode step is probably limited at the long end. The speedup grows with prefix length, which is the regime least able to hide in cache. But the paper never measures hit rates. 56 00:12:32,874 --> 00:12:37,399 [Hal Turing] What about the decode-only timing? Prefill was subtracted. 57 00:12:37,399 --> 00:12:47,499 [Dr. Ada Shannon] So the 'under 15% versus over 90%' claim is about decode throughput. It isn't a wall-clock serving claim, because prefill isn't in it. 58 00:12:47,499 --> 00:12:51,299 [Hal Turing] On the APPS result, what's the comparison missing? 59 00:12:51,299 --> 00:13:10,899 [Dr. Ada Shannon] The comparison is only against the authors' own single-level variant, so we don't know what vLLM or SGLang would get with sharing at both levels. And the larger-batch part is a capacity gain, the kind paged systems also give. It's also 120 introductory problems, one model, and no reported variance. 60 00:13:10,899 --> 00:13:18,424 [Hal Turing] Did they check outputs actually match the baseline? Exact in algebra isn't exact in bf16. 61 00:13:18,424 --> 00:13:27,274 [Dr. Ada Shannon] Exactness is argued by derivation. I didn't see a validation of token-level agreement across deep trees, so that's open. 62 00:13:27,274 --> 00:13:29,999 [Hal Turing] Where does the gain shrink, then? 63 00:13:29,999 --> 00:13:51,174 [Dr. Ada Shannon] Long suffixes are the clearest case. Suffix attention is untouched and still memory-bound, so reasoning-length outputs would hurt. Add the small batches and short prefixes I mentioned, plus GQA and MQA models. And the hardware argument is asserted, not shown: on H100 the gain matched A100 because flash-attn 2.3.6 isn't Hopper-optimized. 64 00:13:51,174 --> 00:13:57,874 [Hal Turing] Every benchmark is a static batch with sharing declared by the user. Real traffic isn't. 65 00:13:57,874 --> 00:14:10,724 [Dr. Ada Shannon] Right. Requests arrive and finish at different times, and groups can be three sequences, not a thousand. The prototype needs the user to declare the tree, so dynamic traffic is outside what it tests. 66 00:14:10,724 --> 00:14:15,474 [Hal Turing] And SGLang? Does RadixAttention make Hydragen redundant? 67 00:14:15,474 --> 00:14:35,249 [Dr. Ada Shannon] No, they compose. SGLang, from Zheng and colleagues, 2023, keeps a radix tree of cached KV and discovers prefixes dynamically. It decides what to store and reuse. Hydragen decides how to compute attention over it. The authors call it a component, not a serving system, and name dynamic discovery as future work. 68 00:14:35,249 --> 00:14:37,324 [Hal Turing] Who should care today? 69 00:14:37,324 --> 00:14:56,949 [Dr. Ada Shannon] Anyone running best-of-N sampling, self-consistency, code generation with many samples, or tree search over a long shared prompt or document. If the prefix is long, the batch is large and the outputs are short, long shared context can get cheap in decode. If your traffic is short chats with small groups, expect little. 70 00:14:56,949 --> 00:15:22,974 [Hal Turing] So here's where I land. The decomposition is exact, and reading the shared prefix once, with tensor cores, is a real kernel-level gain that grows with batch and prefix length. But the 32x is a best case: an early vLLM, no ablation, no modern engine, and an L2-cold microbenchmark behind the kernel numbers. Read it as an upper bound on the idea's value, not a forecast for a current deployment. 71 00:15:22,974 --> 00:15:28,524 [Dr. Ada Shannon] Agreed. Good idea, honest proof of concept, headline stronger than the evidence. 72 00:15:28,524 --> 00:15:32,374 [Hal Turing] Thanks for listening, everyone. See you next time.