<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ayush Garg on Medium]]></title>
        <description><![CDATA[Stories by Ayush Garg on Medium]]></description>
        <link>https://medium.com/@ayushgarg70?source=rss-fd252d0be937------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*oVGS9TikBwXjrvI4pQimUw.jpeg</url>
            <title>Stories by Ayush Garg on Medium</title>
            <link>https://medium.com/@ayushgarg70?source=rss-fd252d0be937------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 14:50:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ayushgarg70/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Twice the bandwidth, not twice the speed: decode on Blackwell]]></title>
            <link>https://medium.com/@ayushgarg70/twice-the-bandwidth-not-twice-the-speed-decode-on-blackwell-916ece024c40?source=rss-fd252d0be937------2</link>
            <guid isPermaLink="false">https://medium.com/p/916ece024c40</guid>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[gpu]]></category>
            <category><![CDATA[cuda]]></category>
            <dc:creator><![CDATA[Ayush Garg]]></dc:creator>
            <pubDate>Fri, 20 Nov 2026 17:06:37 GMT</pubDate>
            <atom:updated>2026-11-20T17:06:37.541Z</atom:updated>
            <content:encoded><![CDATA[<h4>Profiling Llama-3.1-8B decode on an NVIDIA B200 vs an H100: the bandwidth floor halves, but a few microseconds of fixed cost per kernel don’t.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*v4q91cCZN-c_ghw_.png" /><figcaption>The post in one picture: A decode step is a long row of GPU kernels (557 at batch 1; 12 are drawn here). Each one pays a fixed cost to start, get its memory requests in flight and finish (grey), plus the time to move its bytes (blue). The B200’s faster memory halves the blue parts but not the grey ones, so a step made mostly of small kernels gets well under 2× faster.</figcaption></figure><h3>TL;DR</h3><p>In <a href="https://medium.com/@ayushgarg70/where-does-decode-time-go-profiling-llama-3-1-8b-on-an-h100-token-by-token-c3afbe77a459">post 1</a>, I worked out what one generated token of Llama-3.1-8B <em>should</em> cost on an H100 (the time to read the weights and the KV cache from memory) and then traced where every millisecond of the gap went. For this post I took the same code to an NVIDIA B200, which has 2.1× the memory bandwidth, wrote down my predictions before renting it, and checked them. The short version:</p><ul><li><strong>The floor follows the byte math:</strong> 15.0 GB of weights ÷ the 6.63 TB/s I measured on this B200 =<strong> 2.27 ms per token,</strong> 2.1× lower than the H100&#39;s. (I&#39;d guessed 2.0–2.1 ms, assuming the B200 would reach as much of its datasheet bandwidth as the H100 does; it reaches 86%, not 94%.)</li><li><strong>The real speed doesn’t follow.</strong> My best batch-1 decode goes from 6.63 ms to <strong>3.86 ms: 1.72× faster, and only 59% of the new floor</strong> (the H100 reached 72%).</li><li><strong>The reason is a fixed cost of a few microseconds per kernel</strong> that doesn’t shrink with bandwidth. A decode step is 557 kernels, and on a faster GPU that fixed cost is a bigger share of each one. The gap above the floor barely moved: <strong>1.89 ms on the H100, 1.60 ms on the B200.</strong> It isn’t my code: vLLM drops from 76% of the floor on the H100 to 61% on the B200, too.</li><li><strong>Batching hides it:</strong> at batch 128 the B200 reaches 76% of its floor and <strong>2× the H100’s throughput</strong> (12,989 tokens/s).</li><li><strong>Blackwell’s headline low-precision formats didn’t speed up batch-1 decode at all</strong> with the PyTorch stack I used: FP8 was 3% <em>slower</em> than BF16, NVFP4 only 8% faster, and NVFP4 weight-only 4× slower. Each for a different reason, and none of them is the hardware.</li></ul><h3>Same code, new GPU</h3><p>Everything is the same as in post 1: Llama-3.1-8B in BF16, my ~300-line PyTorch decode loop with a static KV cache, CUDA-event timing with a throwaway warm-up process before every configuration, and profiling runs kept separate from timing runs. Only the GPU changed: one <strong>NVIDIA B200</strong> (148 SMs, 179 GiB of HBM3e as reported by the driver), rented as a preemptible VM, with PyTorch 2.15 (CUDA 13.2) and torchao 0.18.</p><p>On paper the B200 has 7.7 TB/s of memory bandwidth and 2.25 PFLOPS of dense BF16 compute, both about 2.3× the H100. Since decode is a memory problem, the whole post is really one question: <strong>if the memory gets 2× faster, does decode get 2× faster?</strong></p><h3>Predictions, written before renting</h3><p>I wrote these down before the VM existed (from the same byte math as post 1):</p><ul><li><strong>Floor at batch 1:</strong> H100 4.76 ms · my B200 prediction: 2.0–2.1 ms</li><li><strong>Eager PyTorch:</strong> H100 14.5–16.4 ms · prediction: <strong>about the same</strong>: it’s CPU-bound, and the CPU didn’t get faster</li><li><strong>CUDA graphs + fusion:</strong> H100 6.63 ms (72% of floor) · prediction: <strong>2.4–2.9 ms (70–85%)</strong></li><li><strong>Cost per token of context:</strong> H100 41 ns · prediction: 17–19 ns</li><li><strong>NVFP4 (4-bit) weights at batch 1:</strong> prediction: faster than BF16, but well short of the 3.6× byte ratio</li></ul><p><strong>Spoiler:</strong> eager held, and NVFP4 held in direction only. The floor came out about 10% higher than I guessed (next section), and the two that mattered most, CUDA graphs + fusion and the cost of context, missed.</p><h3>The roof (and a correction to post 1)</h3><p>As in post 1, I measured the bandwidth this particular GPU actually reaches before comparing anything to it. And the B200 taught me something about my own method: one big sum() over a 4 GiB tensor read at only 5.97 TB/s, while a copy of the same tensor reached <strong>6.63 TB/s (86% of the datasheet)</strong>. (The B200&#39;s L2 cache is 126.5 MiB, 2.5× the H100&#39;s 50 MiB, but the 4 GiB test tensor is still 32× bigger, and the 15 GB of weights a decode step reads are 120× bigger, so neither the roof nor decode gets any help from it.) One kernel tells you how fast that kernel is, not how fast the memory is. So the roof is now the best of several simple streaming kernels, and I went back and re-measured the H100 the same way (its roof moved from 3.08 to 3.15 TB/s; post 1 already uses the corrected value).</p><p>The measured ratio between the two GPUs is therefore <strong>2.10×</strong> (6.63 vs 3.15 TB/s), and that’s the number to beat.</p><h3>Batch 1: 1.72×, not 2.10×</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aB3lYHuOvEWNXOui.png" /><figcaption>The same decode step launched four ways on each GPU (batch 1, 512-token context). Dashed lines: each GPU’s floor, the weights and KV cache read once at its measured roof. The B200 bars shrink less than the floors do.</figcaption></figure><ul><li><strong>Eager PyTorch:</strong> H100 14.5–16.4 ms · B200 13.9 ms · ~1.1×</li><li><strong>+ CUDA graph:</strong> 8.38 ms → 5.93 ms · 1.41×</li><li><strong>torch.compile (fusion):</strong> 7.05 ms → 4.50 ms · 1.57×</li><li><strong>Fusion + CUDA graph:</strong> <strong>6.63 ms → 3.86 ms · 1.72×</strong></li></ul><p><strong>Eager is CPU-bound, exactly as predicted.</strong> A GPU that’s 2× faster doesn’t help when it spends more than half of every step waiting for the CPU to launch the next kernel. Same lesson as post 1, just more extreme.</p><p><strong>The surprise is the best mode.</strong> With launch overhead gone, I expected the B200 to land at 70–85% of its floor, like the H100. It reached 59%. So I traced one step on both GPUs and compared them kernel by kernel:</p><ul><li><strong>lm_head matmul</strong> (reads 1.05 GB): 350 µs on the H100 → 154 µs on the B200 · 2.27×</li><li><strong>MLP matmuls</strong> (gate, up, down; 117 MB each): 40.3 µs → 21.2 µs · 1.90×</li><li><strong>K/V projections</strong> (8 MB each): 5.60 µs → 5.58 µs · 1.00×</li><li><strong>Attention, 512 tokens</strong> (3 MB): 6.8 µs → 8.7 µs · 0.78×</li></ul><p>The big kernel gets the full bandwidth advantage. The medium one gets most of it. The small ones get none at all.</p><p>Here’s why. Take the 117 MB MLP matmuls: at each GPU’s roof they would take 37.2 µs on the H100 and 17.7 µs on the B200. Both actually take <strong>about 3 µs longer than that</strong>: the time to start the kernel, get enough memory requests in flight to reach full bandwidth, and finish the last ones. That fixed cost is the same on both GPUs. On the H100 it’s 8% of this kernel; on the B200 it’s 16%. And for a kernel that moves only a few MB, it’s almost the whole kernel.</p><p>At batch 1, a decode step is 557 kernels. So the faster memory shrinks the part of each kernel that moves bytes and leaves the fixed part alone. You can see it in one number: <strong>the time above the floor (at a 2k context) is 1.89 ms on the H100 and 1.60 ms on the B200.</strong> The floor halved; the overhead barely moved.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_wcoEFOEAcxygmz2.png" /><figcaption>Four kernels from the same traced step on both GPUs. The dark part of each bar is the kernel’s bytes read at its GPU’s measured roof; the light part is everything else. The dark parts halve on the B200; the light parts stay at a few µs or grow.</figcaption></figure><p>To check that this fixed cost is real and not an artifact of reading kernels out of a trace, I measured it on its own: batch-1 matmuls of 11 sizes from 0.5 MB to 1 GB, hundreds of each back to back in a CUDA graph, each call reading its own copy of the weight from memory. A matmul that reads almost nothing still takes <strong>3.7 µs</strong>, and the shapes a decode step uses sit 2–5 µs above the time their bytes need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dg7QH_Uwa1OrRKnM.png" /><figcaption>Each point is one matmul size on the B200: the time per call against the weight bytes it reads (both axes logarithmic). The dashed line is bytes ÷ the measured roof. Big matmuls sit on it; below about 10 MB the time stays between 3.7 and 5.6 µs however few bytes are read. The k and v projections are in that flat part, and q and o are still mostly fixed cost.</figcaption></figure><p>Is the 59% my loop’s fault? vLLM, measured the same way as in post 1, gets <strong>3.80 ms (61% of the floor)</strong> on the B200 at a 2k context, against my 3.90 ms, after 76% on the H100. It speeds up 1.68× from the H100 to the B200; my loop 1.73×. A production engine hits the same wall.</p><p>One detail I only found because the numbers didn’t add up: on the B200, the kernel times in a step sum to <em>more</em> than the step itself (4.18 ms of kernels in a 3.86 ms step). Three quarters of the kernels start before the previous one ends, by 0.65 µs on average: the next kernel is launched early and waits for its inputs (NVIDIA calls this programmatic dependent launch). It’s already one of the ways the libraries chip away at the fixed cost. On the H100 trace, no two kernels overlap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*o7SeHRPerfPkhc-8.png" /><figcaption>About 33 µs of one B200 decode step in Nsight Systems. Nsight draws a kernel on a second row when it starts before the previous one has finished; here most of them do.</figcaption></figure><h3>Batching closes most of the gap</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RB7WR5iZA_8vFEpJ.png" /><figcaption>Tokens per second over all requests, batch 1 to 128, each request with a 2,048-token context. The B200 pulls away as the batch grows: 1.73× at batch 1, 1.99× at batch 128. Hollow points: vLLM, a few percent faster than my loop on both GPUs (13,774 tokens/s on the B200 at batch 128).</figcaption></figure><ul><li><strong>Batch 1:</strong> 6.74 ms on the H100 → 3.90 ms on the B200 · 256 tokens/s · 59% of floor · 1.73×</li><li><strong>Batch 8:</strong> 7.50 ms → 4.20 ms · 1,904 tokens/s · 62% · 1.78×</li><li><strong>Batch 32:</strong> 9.54 ms → 5.40 ms · 5,929 tokens/s · 66% · 1.77×</li><li><strong>Batch 128:</strong> 19.60 ms → 9.85 ms · <strong>12,989 tokens/s · 76% · 1.99×</strong></li></ul><p>(2k-token context per request.) More requests mean more KV cache per step, so the attention kernels move more bytes and the fixed cost becomes a smaller share. At batch 128 the B200 finally delivers what the bandwidth promises: <strong>2× the H100’s throughput.</strong> If you serve many users at once, the B200 is the 2× GPU on the box. If you serve one user at a time, it’s closer to 1.7×.</p><h3>Long context: a newer attention kernel, a smaller share of the roof</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ryNEfJwKOWHBlxkO.png" /><figcaption>Step time at batch 1 as the context grows. Filled points: my loop; solid lines: straight-line fits to them; dashed: the slope each GPU’s measured roof predicts. The H100 sits on its prediction; the B200 grows faster than its own (24.3 vs 19.8 ns per token). Hollow points: vLLM, which does much better on the B200 at 128k.</figcaption></figure><p>From 512 to 128k tokens of context, a batch-1 step on the B200 grows along a straight line: <strong>3.85 ms + 24.3 ns for every token of context</strong> (the H100: 41.1 ns). I predicted about 20 ns at the measured roof. Each token of context adds 128 KiB of KV cache, and 128 KiB in 24.3 ns means the B200’s attention kernel reads the cache at <strong>5.4 TB/s, 82% of the roof.</strong> On the H100 it read it at the full roof.</p><p>That’s a bit ironic, because the B200’s attention kernel is the more modern one. On the H100, cuDNN picked an Ampere-era flash-attention kernel (the older HMMA tensor-core instructions with LDGSTS asynchronous copies). On the B200 it picked a Blackwell-native kernel that uses the new 5th-generation tensor-core instruction (UTCHMMA) and Blackwell&#39;s new <em>tensor memory</em>, a dedicated on-chip store for tensor-core operands (LDTM/STTM move data in and out). But attention at decode does 4 FLOPs per byte, so the tensor cores barely matter; what matters is streaming the cache, and the old kernel was better at that. Nsight Compute shows one reason. At 64k context the H100&#39;s kernel splits the work into 256 thread blocks, about two for each of its 132 SMs, so every SM is reading from memory. The B200&#39;s kernel launches only 72 blocks for 148 SMs: about half the GPU has nothing to do, and the half that works can&#39;t keep enough reads in flight to reach full bandwidth. It&#39;s the same small-grid problem as the FP4 matmuls below, in a milder form.</p><p>It’s the kernel, not the GPU. vLLM, whose attention on Blackwell comes from FlashInfer’s TensorRT-LLM kernels, reaches <strong>81% of the floor at 128k</strong> against my loop’s 69%, and is 17% faster there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Iwsa3KT29kRXlofI.png" /><figcaption>The B200’s attention kernel (64k context) in Nsight Compute’s instruction view, two crops of the same kernel. Top: LDTM loads values from tensor memory into registers, FMUL multiplies them, STTM stores the results back. Bottom: a run of eight UTCHMMA tensor-core instructions.</figcaption></figure><h3>Low precision: fewer bytes, not faster</h3><p>This is the part I was most curious about. Blackwell’s big new feature is <strong>NVFP4</strong>: 4-bit floating-point numbers with one 8-bit scale per block of 16 values, multiplied directly by new FP4 tensor cores. For decode, 4-bit weights should mean reading 3.6× fewer bytes (4.22 GB instead of 15.0 GB per step). I tried four versions with torchao, PyTorch’s quantization library (the NVFP4 configs in it are marked as prototypes):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qnnmVp9NPg7BIp_S.png" /><figcaption>Step time for four weight formats at batch 1 and batch 32 (2,048-token context), with each format’s perplexity. Black tick: the bandwidth floor for the bytes each format stores. Only NVFP4 at batch 1 is meaningfully faster than BF16 (8%), and the fewer the bytes, the further the bar sits from its tick: at batch 1, BF16 takes 1.7× its floor, FP8 3.4× and NVFP4 5.3×.</figcaption></figure><ul><li><strong>BF16</strong> (15.0 GB per step): batch 1: 3.90 ms · batch 32: 5.40 ms · perplexity 6.31</li><li><strong>FP8, weights and activations</strong> (7.5 GB): batch 1: 4.03 ms · batch 32: 5.39 ms · perplexity 6.37</li><li><strong>NVFP4, weights and activations</strong> (4.2 GB): batch 1: 3.60 ms · batch 32: 5.93 ms · perplexity 7.13</li><li><strong>NVFP4, weights only</strong> (4.2 GB): batch 1: 14.99 ms · batch 32: 16.80 ms · perplexity 6.74</li></ul><p>(Perplexity on WikiText-2, the same 141 windows as post 1.)</p><p>None of them made batch-1 decode meaningfully faster. The traces show three different reasons:</p><ul><li><strong>FP8: too few thread blocks, again.</strong> The FP8 matmuls read their 7.5 GB at <strong>2.8 TB/s, 42% of the roof</strong> (the BF16 ones manage 4.4 TB/s), for the same reason FP8 was slow on the H100 in post 1. PyTorch’s FP8 matmul kernel (from CUTLASS) gives each thread block a large slice of the output and never splits the long input dimension, so the q, o and down projections launch 32 blocks and k and v launch 16, on a GPU with 148 SMs. cuBLAS gives the same BF16 layers 64–128 blocks. Those five matmuls take about as long in FP8 as in BF16, with half the bytes; only the wide ones (gate, up and lm_head) actually get faster. On top of that, every linear layer first has to quantize its input to FP8 (0.49 ms per step, about 0.2 ms more than the norms it absorbs), and these kernels don&#39;t use the early-launch trick, so the gaps between kernels come back. Net: 3% slower than BF16.</li><li><strong>NVFP4: most of the chip sits idle.</strong> The FP4 matmuls really do read only 4.2 GB, but at <strong>2.2 TB/s, a third of the roof</strong> (1.5 TB/s counting the kernels that quantize the activations). Nsight Compute shows why: the kernel splits the output into 128-column tiles and gives each tile one thread block, so a 4096-wide layer launches <strong>32 thread blocks for 148 SMs</strong>, and the K/V projections launch 8. These kernels are built for big batches (prefill, training), where each tile has lots of rows and lots of math. At batch 1 there’s one row, and three quarters of the GPU has nothing to do. Quantizing the activations to FP4 also adds a small kernel in front of every matmul: 1,129 kernels per step instead of 557. Net: 8% faster, for 13% worse perplexity.</li><li><strong>NVFP4 weights only: the weights are unpacked on every step.</strong> There’s no fused kernel for this combination here, so torchao turns every 4-bit weight back into BF16 in separate kernels (11.5 ms per step), writes that BF16 copy to memory, and then runs the ordinary BF16 matmul on it. That’s more memory traffic than plain BF16, not less. Net: 4× slower.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VriZ_tiAwhv1NAnT.png" /><figcaption>Nsight Compute’s launch statistics for the FP4 matmul of a 4096-wide layer: a grid of 32 thread blocks on a GPU with 148 SMs (0.22 waves). Nsight raises the “Small Grid” warning on its own.</figcaption></figure><p>And at batch 32, NVFP4 was <strong>slower than BF16</strong> (5.93 vs 5.40 ms): the matmuls cost the same as at batch 1, attention over 32 long KV caches adds 1.7 ms, and the extra quantization kernels add more.</p><p>Getting that batch-32 number took three tries: torch.compile crashed while compiling the NVFP4 model (“Cycle detected in memory planning graph”). Turning off the pass that crashed just moved the crash one step later. What worked was disabling the kernel fusion behind it (TORCHINDUCTOR_NESTED_REDUCTION=0); a BF16 run with the same setting was unchanged, so it doesn&#39;t distort the comparison. Prototype features are prototypes.</p><p><strong>What it means.</strong> Low precision only helps decode if the kernel that reads the smaller weights is as good at streaming memory as the BF16 one. With these libraries at batch 1, it isn’t: the FP8 and FP4 kernels are tuned for the large-batch, compute-heavy case, where Blackwell’s new tensor cores shine. That’s a software result, not a hardware limit; kernels written for small-batch FP4 decode would tell a different story, and that’s the experiment I’d run next. Post 1 found a milder version of the same thing on the H100.</p><h3>What the new tensor cores look like</h3><p>One more thing I wanted to see: what Blackwell actually runs. In Nsight Compute’s instruction view, the H100’s matmuls use HGMMA (Hopper&#39;s tensor-core instruction). On the B200 every matmul and the attention kernel use the new <strong>UTCHMMA</strong> (BF16) or <strong>UTCOMMA</strong> (FP4) instructions, results come out of tensor memory with <strong>LDTM</strong>, and the FP4 block scales are copied into tensor memory with <strong>UTCCP</strong>. The libraries are fully on Blackwell&#39;s new path. At batch 1 it just doesn&#39;t matter: averaged over the whole GPU, the tensor pipes are under 8% busy in every matmul.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iyYitWTlJ9hf31RO.png" /><figcaption>Where the tensor cores’ results go. On both GPUs the TMA copy engine moves whole tiles from GPU memory into on-chip shared memory, which feeds the tensor cores. On the H100 the results land directly in registers. On the B200 they land in tensor memory, a new on-chip store next to the tensor cores, and LDTM copies them into registers: that&#39;s the LDTM in the instruction listings below.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ma4voV_QQGK4GdIv.png" /><figcaption>The B200’s MLP gate matmul, three crops of the same kernel. Top: UTMALDG loads tiles of weights and inputs with TMA. Middle: UTCHMMA multiplies them into tensor memory. Bottom: LDTM reads the results out of tensor memory, and FFMA2/F2FP finish them and convert them to BF16.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*P8hwi_iOuR5oy5O7.png" /><figcaption>The FP4 matmul: UTCCP copies the block scales into tensor memory, and UTCOMMA multiplies the 4-bit values, with two thread blocks sharing one tile (2CTA).</figcaption></figure><p>For comparison, the H100’s matmul from post 1, with Hopper’s HGMMA:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5BLWeLd_29LM0rov.png" /><figcaption>The H100’s lm_head matmul from post 1, two crops: Hopper&#39;s HGMMA tensor-core instruction (top) and UTMALDG TMA loads (bottom). Same TMA loads as on the B200, but results stay in registers: there&#39;s no tensor memory and no LDTM.</figcaption></figure><h3>Takeaways</h3><ol><li><strong>The byte math still sets the floor</strong>, and decode is still memory-bound on Blackwell. The floor dropped 2.1×, exactly as much as the measured bandwidth rose.</li><li><strong>Faster memory exposes everything that isn’t memory.</strong> A few microseconds of fixed cost per kernel (3.7 µs for a matmul that reads almost nothing), times 557 kernels, kept batch-1 decode at 1.72× instead of 2.1×. The gap above the floor barely changed between the two GPUs, and vLLM pays the same price.</li><li><strong>Big kernels get the full upgrade.</strong> Batching (2× at batch 128) and long context bring the B200 much closer to its promise.</li><li><strong>New number formats need kernels built for the job.</strong> NVFP4 cut the bytes 3.6× and the time 8%, because the FP4 kernel left most of the GPU idle at batch 1.</li></ol><p><strong>If I did it again</strong> I’d run the fixed-cost sweep on the H100 as well (I only thought of it after deleting that VM), and try a decode-specific FP4 kernel to see how much of the 3.6× is really available.</p><h3>Appendix: reproduce it</h3><ul><li>Repo: <a href="https://github.com/ayushgarg70/llm-decode-profiling">github.com/ayushgarg70/llm-decode-profiling</a>. On any machine, no GPU needed: uv sync &amp;&amp; uv run pytest, then uv run python -m analysis.plots_blackwell regenerates every chart in this post and uv run python -m analysis.results_table --gpu B200 --out notes/RESULTS_B200.md regenerates notes/RESULTS_B200.md (every number with its run ID) from the data in data/. notes/REPORT_B200.md is the long version, including what failed. To rerun the experiments you need a 1×B200 VM with Nsight Systems and Nsight Compute, and access to Llama-3.1‑8B on Hugging Face; set it up as in the README, then run runs/b1.sh (B0–B4) and runs/b2.sh (the fixed-cost sweep, more context lengths, repeats, Nsight Compute with unlocked clocks, vLLM) one block at a time, e.g. runs/b1.sh b0.</li><li>Versions: PyTorch 2.15.0 (CUDA 13.2), driver 580.173.02, torchao 0.18.0, vLLM 0.34.0 (with PyTorch 2.14.0), Nsight Systems / Nsight Compute 2025.3.</li><li>Caveat: Nsight Compute locks GPU clocks by default, and on the B200 its “% of peak bandwidth” readings came out far below the timing runs. So I used it for instructions and launch shapes; a second pass with --clock-control none gives bandwidths in line with the timing runs (lm_head at 98% of the roof).</li><li>Credits: the same as post 1 (gpt-fast, torchao, vLLM and FlashInfer, NVIDIA’s Nsight documentation). Concept illustrations generated with OpenAI Codex; every chart is generated from the measured data.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=916ece024c40" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Where does decode time go? Profiling Llama-3.1-8B on an H100, token by token]]></title>
            <link>https://medium.com/@ayushgarg70/where-does-decode-time-go-profiling-llama-3-1-8b-on-an-h100-token-by-token-c3afbe77a459?source=rss-fd252d0be937------2</link>
            <guid isPermaLink="false">https://medium.com/p/c3afbe77a459</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[gpu]]></category>
            <category><![CDATA[cuda]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[pytorch]]></category>
            <dc:creator><![CDATA[Ayush Garg]]></dc:creator>
            <pubDate>Thu, 19 Nov 2026 10:35:43 GMT</pubDate>
            <atom:updated>2026-11-20T17:10:46.864Z</atom:updated>
            <content:encoded><![CDATA[<h4>Every token should cost 4.76 ms. Plain PyTorch takes 16.4. Here’s where the other 11.6 go, and how to get most of them back.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SRnlpNfkzIyYVIT3.png" /><figcaption>One decode step as a picture. To produce one token, the GPU has to stream every weight of the model (blue) and the KV cache (orange) out of its memory (HBM) and through the memory bus to its compute cores. The bus is the narrow part: the cores could do far more math than the arriving bytes give them, so most of them sit idle. That’s why the time for a token is roughly bytes ÷ bandwidth.</figcaption></figure><h3>TL;DR</h3><p>I wanted to know where the time actually goes when a large language model generates text, so I took Llama-3.1-8B on a single H100, worked out from first principles what one generated token <em>should</em> cost, and then measured, traced and profiled every millisecond of the gap. The short version:</p><ul><li><strong>The physics:</strong> every new token has to read all 15.0 GB of the model’s weights from GPU memory, which at the 3.15 TB/s I measured on this H100 sets a floor of <strong>4.76 ms per token</strong>.</li><li><strong>Plain PyTorch takes 16.4 ms</strong>, about 29% of that speed, and the GPU sits idle for <strong>half</strong> of every step, waiting for the CPU to launch its next tiny kernel (1,583 of them per token!).</li><li><strong>CUDA graphs plus torch.compile bring it to 6.63 ms (151 tokens/s), 72% of the floor</strong>, and within 6% of vLLM (6.37 ms).</li><li><strong>Long context and batching behave exactly as the byte math predicts:</strong> +41 ns for every token of context, and 44× more throughput at batch 128, because all requests share one read of the weights.</li><li><strong>Quantization is where the simple story breaks:</strong> INT4 weights read 3.8× fewer bytes but decode only 2.0× faster (with +12% perplexity), and at batch 32 two of the three formats I tried got <em>slower</em> than plain BF16.</li></ul><h3>The question and the setup</h3><p>Generating text with an LLM (large language model) happens in two phases: <em>prefill</em>, where the whole prompt goes through the model at once, and <em>decode</em>, where the model produces one token at a time, each one depending on the last. Decode is the part you feel as a user (it is the “typing speed” of the model), and it is a strange workload: for every single token, the GPU has to run the entire network, but only on one row of input.</p><p>My setup, kept deliberately small so that every number can be traced:</p><ul><li><strong>Model:</strong> Llama-3.1-8B (base), BF16 (bfloat16) weights.</li><li><strong>GPU:</strong> one NVIDIA H100 80GB SXM, rented as a preemptible VM.</li><li><strong>Software:</strong> PyTorch 2.15, CUDA 13.2, torchao 0.18, vLLM 0.34, Nsight Systems and Nsight Compute for profiling.</li><li><strong>My own decode loop:</strong> about 300 lines of plain PyTorch with a static KV cache (more on that below), checked against Hugging Face: the first 64 greedy tokens are identical.</li><li><strong>Timing:</strong> CUDA events around each decode step, 20 warm-up steps, 256 timed steps, median reported. Profiling runs are always separate from timing runs, because profilers change what they measure.</li></ul><p>All the code, raw CSVs and run IDs are in the repo, and every number in this post comes from a script-generated results file.</p><h3>What should a token cost?</h3><p>Before measuring anything I wanted a number to measure <em>against</em>. Two quantities matter for one decode step: how many bytes have to come out of memory, and how much arithmetic has to happen.</p><p><strong>Bytes.</strong> The model has 8.03 billion parameters, but the input embedding is only <em>looked up</em> (one row per token), so a decode step reads 7.5 billion weights at 2 bytes each: <strong>15.0 GB per step</strong>. On top of that comes the KV cache (the keys and values of every previous token, kept so they don’t have to be recomputed): with grouped-query attention (GQA), 32 layers × 8 KV heads × 128 dimensions × 2 (K and V) × 2 bytes = <strong>128 KiB per token of context</strong>. The KV cache only catches up with the weights at around 114k tokens.</p><p><strong>FLOPs.</strong> Each weight is used in one multiply-add per token, so about <strong>15 GFLOPs per token</strong> for the matrix multiplies, plus a little for attention.</p><p><strong>Which one is the bottleneck?</strong> Divide FLOPs by bytes and you get the <em>arithmetic intensity</em>: about <strong>1 FLOP per byte</strong> for the matmuls at batch 1. The H100 can do 989 TFLOPS of BF16 math but only move 3.35 TB/s from memory, so it needs about 295 FLOPs for every byte to keep its math units busy (the <em>ridge point</em>). At 1 FLOP per byte, decode is memory-bound by a factor of ~300: the math is basically free, and the time is however long it takes to stream the weights.</p><p><strong>The real ceilings.</strong> Datasheet numbers are upper limits, so I measured this particular GPU first: the best simple read kernel reaches <strong>3.15 TB/s</strong> (94% of the datasheet) and a big BF16 matmul reaches <strong>797 TFLOPS</strong> (81%). The read test uses a 4 GiB tensor, about 80× the H100’s 50 MiB L2 cache, so it measures the memory itself rather than the cache. (The cache can’t help decode either: the 15 GB of weights are 300× its size, so by the time the next token starts, nothing from the last one is still in it.) That puts the measured ridge at 253 FLOPs/byte, and the floor for one token at:</p><blockquote><em>15.0 GB ÷ 3.15 TB/s = </em><strong><em>4.76 ms per token</em></strong><em>, or about 210 tokens per second.</em></blockquote><p>(Honestly, my first roof was wrong. I measured it with a single big sum, which gave 3.08 TB/s, and only noticed when the context sweep below streamed the KV cache <em>faster </em>than that “roof”. A sum over rows of the same tensor reaches 3.15 TB/s; one kernel tells you how fast that kernel is, not how fast the memory is. So the roof is now the best of a few simple kernels, and every percentage in this post uses it.)</p><p>That’s the number every other result in this post is compared against.</p><h3>The baseline: what actually happens</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*583sS5rdpOuEBsC_.png" /><figcaption>A real eager decode step on the GPU, from an nsys trace. Each colored sliver is one kernel (blue: weight matmuls, orange: attention, green: norms, RoPE and other small operations, yellow: KV-cache writes and argmax); the light grey background is time the GPU did nothing. Top: the whole step, 1,583 kernels. Bottom: 0.4 ms of the same step, where you can see the problem: each green kernel runs for a microsecond or two, and then the GPU waits for the CPU to launch the next one. (The profiler slows the CPU, so this traced step takes 24.6 ms with the GPU busy a third of the time; in the clean timing runs it takes 16.4 ms and the GPU is idle about half of it.)</figcaption></figure><p>Hugging Face’s generate() takes <strong>18.5 ms per token</strong> (54 tokens/s). My own eager loop is a bit faster at <strong>16.4 ms</strong>, but that&#39;s still only <strong>29% of the floor</strong>. (Eager timings move around between sessions by about 10%, between 14.5 and 16.4 ms, because they depend on the CPU; the GPU-bound numbers later in the post repeat to within 0.05%.)</p><p>The timeline above shows why. One decode step is <strong>1,583 separate GPU kernels</strong>: a few hundred matrix multiplies and well over a thousand tiny ones (norms, rotary embeddings, additions, copies, etc.). In eager mode, Python and PyTorch launch each of these one by one, and launching a kernel takes the CPU several microseconds, which is longer than many of these kernels take to run. So the GPU finishes a kernel and then waits for the next one, over and over: <strong>8.3 ms of every 16.4 ms step, the GPU is idle.</strong> The GPU isn’t slow here; it’s starving.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fjePqOnmwC4P6R8R.png" /><figcaption>Why the GPU waits. Top: in eager mode the CPU launches every kernel separately, and launching one takes longer than many kernels take to run, so the GPU sits idle in between. Bottom: the fix measured later in this post, a CUDA graph: the step is recorded once and then launched as a single unit, so the same kernels run back to back. A real step has 1,583 kernels, not six.</figcaption></figure><h3>Anatomy of one decode step</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ms1DS8vEgALDyDt2.png" /><figcaption>Where each step’s time goes, for the four combinations. Each bar is one clean step, split into kernel time per group (from the traces) and idle time (clean step time minus kernel time). The dashed line is the bandwidth floor. The weight matmuls (blue) take about 5.7 ms in every mode, so the four modes differ only in the small operations (green) and the idle time (grey). With both fixes, the step is almost all matmuls.</figcaption></figure><p>Grouping the kernels by what they do gives the first bar above. The weight matmuls take <strong>5.76 ms</strong>, and since they read 15.0 GB, they are streaming the weights at <strong>2.60 TB/s, 83% of the measured bandwidth</strong>. So the part of the step that does the “real” work is already close to the hardware limit. The problem is everything around it: 1.8 ms of small elementwise kernels and 8.3 ms of nothing.</p><p>Nsight Compute confirms the memory-bound picture for the single biggest kernel, the final lm_head projection (1.05 GB of weights): its DRAM throughput is <strong>87% of peak</strong>, while the SMs&#39; compute throughput is just <strong>4.5%</strong> (and the tensor cores are busy only 3% of the time).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fxdR9KbCS7Ep9VIb.png" /><figcaption>Nsight Compute’s “Speed of Light” section for the lm_head matmul. Each row is how busy one part of the GPU was, as a percentage of its peak: memory (DRAM) at 87%, the SMs&#39; compute at 4.5%; the bars at the bottom show the same two numbers. A kernel near the memory peak and far from the compute peak is memory-bound: it waits for bytes, not for math. (ncu runs the kernel at a locked base clock, so we don&#39;t use its 360 µs duration.)</figcaption></figure><p>The memory chart shows where those bytes go: 1.05 GB come out of DRAM (exactly the size of the lm_head weights), through L2, into the SMs, and the loads are issued by the TMA rather than by individual threads.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3QqjrmcS7K2kNt6G.png" /><figcaption>Nsight Compute’s memory chart for the same kernel: where its bytes travel. Read it from right to left: 1.05 GB come out of device memory (DRAM, the “Device Memory” box on the right), pass through the L2 cache (hit rate 2.9%: each weight is read once per step, and the 15 GB of weights are 300× the 50 MiB L2, so nothing is still cached when the next step comes around), and reach the SMs. On the left, the kernel issues only 101 ordinary global-load instructions but 72 thousand TMA instructions: the TMA copy engine moves whole tiles of weights, so the threads never load them one by one.</figcaption></figure><p>It also showed me something about the hardware I didn’t expect. Counting the actual machine instructions each kernel executes, the cuBLAS matmuls use Hopper’s newest paths, <strong>WGMMA</strong> tensor-core instructions (HGMMA in the assembly: 2.05 million of them in lm_head) fed by the <strong>TMA</strong> (Tensor Memory Accelerator), a copy engine that moves whole tiles from memory into the SM&#39;s shared memory in one go. PyTorch&#39;s attention kernel at decode, on the other hand, comes from cuDNN&#39;s sm80 (Ampere-generation) code: older <strong>HMMA</strong> tensor-core instructions fed by many threads doing small asynchronous copies, with <strong>zero</strong> WGMMA and zero TMA. For decode that&#39;s fine, since both are waiting on memory anyway, but it was a nice reminder that &quot;running on an H100&quot; doesn&#39;t mean &quot;using everything an H100 has&quot;.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*z_NrbvunOs21kkpS.png" /><figcaption>Two excerpts of the lm_head kernel&#39;s machine code (SASS) in Nsight Compute&#39;s Source view; each row is one instruction, and the highlighted rows are the ones that matter. Top: HGMMA.64x8x16.F32.BF16 is Hopper&#39;s warpgroup tensor-core instruction (WGMMA): each instruction multiplies a 64 × 16 block of weights by a 16 × 8 block of inputs (our one real token, padded to 8, the smallest width the instruction supports), with the running sums kept in FP32. Bottom, from another part of the kernel: each UTMALDG asks the TMA to load one tile from memory into shared memory, and the SYNCS instructions wait for tiles to arrive. The columns on the right show how often each instruction ran.</figcaption></figure><p>Two small details in that listing made me smile. The instruction is HGMMA.64x8x16: the 8 is the batch dimension, our single row of input padded up to the smallest tile the instruction supports. And the kernel executes 909k barrier-wait instructions (SYNCS) next to its 2.05 million HGMMAs: the tensor-core warps spend a lot of their time polling, waiting for the TMA to deliver the next tile of weights. That is what memory-bound looks like from the inside!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sTQA85fCWnfgwN9U.png" /><figcaption>The same view for cuDNN’s attention kernel. Top: HMMA.16816.F32.BF16, the older (Ampere) tensor-core instruction, where each warp multiplies its own small 16×8×16 tile. Bottom: LDGSTS (&quot;load global, store shared&quot;), Ampere&#39;s asynchronous copy, where each thread copies a 16-byte piece of the KV cache into shared memory. This kernel contains no HGMMA and no TMA instructions at all.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xQ5rDDD5V4_KON1a.png" /><figcaption>The two paths side by side. Left: the weights reach the tensor cores through the TMA, which copies whole tiles into on-chip shared memory, and four warps (a warpgroup, one in each quarter of the SM) multiply one big tile together (WGMMA), so all four of the SM’s tensor cores work on the same tile. Right: the KV cache is loaded by many threads, each copying a small piece (LDGSTS), and each warp issues its own small multiply (HMMA) on its own small tile. For decode both paths end up waiting on memory, so the older one costs little here.</figcaption></figure><h3>Closing the gaps with CUDA graphs</h3><p>If the problem is launching, the fix is to launch less. Two tools attack it from different sides, so I measured them in a 2×2:</p><ul><li><strong>Eager, no CUDA graph</strong> (1,583 kernels per step): 14.75 ms*</li><li><strong>Eager + CUDA graph:</strong> 8.38 ms</li><li><strong>Fusion with torch.compile, no graph</strong> (652 kernels per step): 7.05 ms</li><li><strong>Fusion + CUDA graph:</strong> <strong>6.63 ms</strong></li></ul><p>*Eager measured in the same session as the other three cells; see the note on eager variance above.</p><ul><li>A <strong>CUDA graph</strong> records the whole step once and then replays it with a single launch, so the CPU no longer sits between kernels. On its own, it takes eager from 14.75 to 8.38 ms.</li><li><strong>Fusion</strong> (torch.compile) merges the chains of small elementwise kernels into fewer, bigger ones: 1,583 kernels become 652, and many small round trips to memory disappear. On its own: 7.05 ms.</li><li><strong>Both together: 6.63 ms, or 151 tokens per second, 72% of the floor.</strong> The GPU is now busy 94% of the step, and idle time has gone from 8.3 ms to 0.26 ms.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*s8_6My9XQqCQpyhH.png" /><figcaption>Real GPU timelines of one decode step for each of the four combinations, from nsys traces (same colors as the eager timeline: each sliver is a kernel, grey background = GPU idle). With a CUDA graph (rows 2 and 4) the grey gaps almost disappear; with fusion (rows 3 and 4) there are far fewer kernels and almost no green ones. The bottom panel zooms in on 0.4 ms. The two rows without a graph are stretched by the profiler (24.6 and 11.3 ms traced, against 16.4 and 7.05 ms in clean runs); the graph rows are not, because the CPU does nothing during a replay.</figcaption></figure><p>CUDA graphs come with rules, and in hindsight they explain most of the design of the decode loop: shapes and memory addresses have to be identical on every replay, so the KV cache is allocated once at full size (a <em>static</em> cache) and updated in place, the position counter lives on the GPU and is updated in place too, and nothing in the step may ask the CPU for a value.</p><p>So where do the remaining 1.85 ms between 6.63 ms and the 4.78 ms floor (at this 512-token context) go? Mostly to the matmuls themselves: they take 5.71 ms instead of the ideal 4.76, because they reach 83% of the bandwidth rather than 100%. Attention adds 0.34 ms, the small ops 0.2 ms and the leftover gaps 0.26 ms.</p><h3>Long context: when KV reads take over</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7uJyUH1Jkx_9M9bI.png" /><figcaption>Time per decode step as the context grows (batch 1, CUDA graphs + fusion). Dots: measurements. Dashed line: the bandwidth floor, (weights + KV-cache bytes) ÷ 3.15 TB/s. The measurements lie on a straight line (light blue) that rises 41.1 ns per token of context, almost exactly the floor’s slope: each extra token of context costs the time it takes to read its 128 KiB of KV cache. The dotted vertical line marks where the KV cache becomes as big as the weights (114.5k tokens).</figcaption></figure><p>The KV cache grows with every token of context, and every decode step has to read all of it. So the prediction is a straight line: 15.0 GB plus 128 KiB per token of context, divided by 3.15 TB/s, which means <strong>+41.6 ns for every token of context</strong>.</p><p>The measurement, from 512 tokens all the way to the model’s 128k limit: <strong>6.65 ms + 41.1 ns per token of context</strong>, a straight line that fits every point to within 0.05 ms. The KV cache is being read at <strong>3.19 TB/s, right at the measured roof.</strong> At ~128k context, one request’s KV cache is 16 GiB, as big as the whole model, and a token takes <strong>12.0 ms</strong>. Interestingly, the step gets <em>closer</em> to its floor as context grows (72% at short context, 85% at 128k), because the attention kernel streams the KV cache more efficiently than the matmuls stream the weights.</p><p>I had a prediction here that turned out wrong, and I like it more than the ones that were right. At 512 tokens, the attention kernel launches only 32 thread blocks (one per query head) on a GPU with 132 SMs (streaming multiprocessors), so I expected it to leave most of the GPU idle at long context. Nsight Compute at 64k context said otherwise: cuDNN splits each head’s KV sequence into 8 chunks, launches <strong>256 blocks</strong>, and merges the partial results in a small second kernel (the idea known as <em>flash-decoding</em>). It reads exactly the predicted bytes (269.8 MB per layer, against 269.6 MB predicted) at <strong>85% of the peak bandwidth</strong>. Nothing to fix!</p><h3>Batching: sharing the weights</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6IlpVA_7Y6FcdRHF.png" /><figcaption>Batching, with 2,048 tokens of context per request. Left: total tokens per second; right: time per step; dots are measurements, dashed lines the bandwidth floor. Up to about batch 16 the step barely gets slower (right), so throughput grows almost in proportion to the batch (left): all requests share one read of the weights. The dotted vertical line (B = 49) is where the requests’ KV caches together become as big as the weights; past it, step time grows with the batch. The shaded area marks batch sizes whose KV cache wouldn’t fit in the GPU’s 80 GB.</figcaption></figure><p>All requests in a batch share one read of the 15 GB of weights, but each brings its own KV cache. So adding requests should be nearly free until the KV bytes catch up with the weight bytes, which at 2k context per request happens around batch 49.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qY4Ox6XEFFgIsbTs.png" /><figcaption>Why batching is nearly free: the weights (blue) are read once per step and used for every request in the batch, while each request brings its own KV cache (orange), which has to be read separately. Eight requests are drawn; we measured up to 128.</figcaption></figure><p>And that’s what happens: <strong>8 requests cost 11% more time than 1 and give 7.2× the throughput</strong>, and 128 requests give <strong>6,530 tokens/s, 44× batch 1</strong>, for 2.9× the step time. Past batch ~49 the step time grows almost in proportion to the batch, because now the KV cache dominates the bytes.</p><p>Nsight Compute shows why the first part is free: cuBLAS’s smallest tile already covers 8 rows, so at batch 1, seven-eighths of the tensor-core work is multiplying padding, and batch 8 runs <em>the same kernel in the same 44 µs</em>. At batch 64 the tensor cores are 8× busier (22% instead of 2.8%), and the matmul still takes the same time, because it’s still waiting on memory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ex8n3L-l8f-kgnAf.png" /><figcaption>Nsight Compute comparing the same MLP matmul at batch 1 (blue, “Current”) and batch 64 (green, “Baseline 1”). The kernel names at the top show cuBLAS picked a bigger tile for batch 64 (112×64 instead of 64×8). Each bar is a pipe’s utilization, the share of cycles that kind of execution unit was busy: the tensor pipes go from about 3% to about 22%. The kernel takes the same 44 µs at both batch sizes, because it’s waiting on memory either way.</figcaption></figure><p>Does batching ever make decode compute-bound? The matmuls alone would need about 253 requests to reach the ridge point, but there are two problems. First, at 2k context, the KV caches of more than about 228 requests don’t fit in 80 GB next to the weights. Second, and more fundamentally, the KV bytes grow with the batch too, so <strong>the arithmetic intensity of the whole step can never go above 53 FLOPs per byte at this context length, whatever the batch size</strong>. With BF16 weights on one H100, decode is memory-bound all the way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dQkObN8FuTdeKwmj.png" /><figcaption>The roofline. Each dot is a whole decode step (2k context, CUDA graphs + fusion). Horizontal axis: arithmetic intensity, the FLOPs done per byte read from memory; vertical axis: FLOPs per second achieved; both axes are logarithmic. The slanted black line is the memory roof (3.15 TB/s) and the flat one the compute roof (797 TFLOP/s); they meet at the ridge, 253 FLOPs per byte. Blue dots are BF16 from batch 1 to 128, climbing along the memory roof as the batch grows; the other colors are the quantized formats at batch 1 and 32. Every dot is far left of the ridge, and the blue vertical line shows why: because each request’s KV cache grows the bytes along with the FLOPs, no batch size at this context can get past 53 FLOPs per byte.</figcaption></figure><h3>Shrinking the bytes: quantization</h3><p>If time is bytes, then storing weights in fewer bits should make decode faster in proportion. I tried three formats with torchao, quantizing every linear layer including lm_head:</p><ul><li><strong>INT8 weight-only</strong> (7.51 GB per step): weights stored as 8-bit integers, converted back to BF16 on the fly.</li><li><strong>INT4 weight-only</strong> (3.99 GB): 4-bit integers with a scale and zero point per group of 128, using the <em>tinygemm</em> kernel that gpt-fast made popular. (The scales are why it’s 0.53 bytes per weight rather than 0.5.)</li><li><strong>FP8</strong> (7.51 GB): 8-bit floats for the weights <em>and</em> the activations, so the matmul runs on Hopper’s FP8 tensor cores.</li></ul><p>And since faster isn’t useful if the model gets dumber, every format also gets a quality check: perplexity on the WikiText-2 test set (289k tokens; lower is better).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ro1Ne-5FQowZtkS4.png" /><figcaption>Decode time per weight format at 2k context; left batch 1, right batch 32. Bars: measured time per step. Black tick: that format’s bandwidth floor (the bytes it reads ÷ 3.15 TB/s). The labels on the left give each format’s perplexity on WikiText-2 (lower is better). At batch 1 every format beats BF16, but none gets near its own floor: INT4’s bar is 2.5× its tick. At batch 32 only FP8 beats BF16; INT8’s bar is cut off at the edge (549 ms), because its kernel falls back to a slow path for more than one row.</figcaption></figure><ul><li><strong>BF16:</strong> perplexity 6.31 · batch 1: 6.74 ms · batch 32: 9.54 ms</li><li><strong>INT8:</strong> perplexity 6.32 · batch 1: <strong>4.02 ms</strong> (1.7× faster) · batch 32: 549 ms (!)</li><li><strong>INT4:</strong> perplexity 7.07 (+12%) · batch 1: <strong>3.4 ms</strong> (2.0× faster) · batch 32: 21.0 ms</li><li><strong>FP8:</strong> perplexity 6.37 · batch 1: 5.9 ms (1.1× faster) · batch 32: <strong>8.75 ms</strong> (1.09× faster)</li></ul><p><strong>Why INT4 isn’t 3.8× faster.</strong> The bytes went down 3.8×, but the kernels read them much less efficiently: the weight matmuls stream at <strong>2.6 TB/s in BF16, 2.0 in INT8, 1.56 in INT4 and 1.50 in FP8</strong>. Each format pays for something: tinygemm has to unpack 4-bit values and apply scales before the tensor cores can use them, and with so few bytes left, that work is no longer hidden behind the memory reads. FP8 uses cuBLAS’s real FP8 tensor-core path, but with 128 × 128 tiles: at batch 1, 127 of the 128 input rows are padding, and the smaller layers split into very few tiles. The k and v projections launch just 8 blocks on 132 SMs, and q, o and down 32, so those few SMs end up compute-bound, mostly multiplying padding, while the rest of the GPU sits idle. On top of that, small extra kernels convert the activations to FP8. And everything quantization doesn’t touch (attention, norms, the KV cache) stays exactly the same size.</p><p><strong>The batch-32 surprise.</strong> INT8 at batch 32 took <strong>549 ms per step</strong>, 58× slower than BF16, and that’s not physics, it’s software. Sweeping the batch size showed INT8 is fast <em>only</em> at batch 1 (4.0 ms), then 60 ms at batch 2, rising steadily from there. The trace explained it: at batch 1, torch.compile fuses “read INT8, convert, multiply” into one Triton kernel per matmul, but for more than one row it falls back to a generic PyTorch kernel that works in FP32 with no tensor cores. INT4’s tinygemm is designed for tiny batches and also degrades (21.0 ms at batch 32). <strong>Only FP8 stays ahead of BF16 at batch 32</strong>, because it runs on cuBLAS’s normal Hopper matmul path.</p><p>Honestly, this was the most useful finding of the whole project for me: “fewer bits” is a promise about bytes, and whether you get the speed depends entirely on the kernel and on the batch size you actually serve.</p><h3>A production reference: vLLM</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Mr93b63MgmfOXchf.png" /><figcaption>Left: time per decode step for our loop (blue), vLLM with CUDA graphs (orange) and vLLM in eager mode (green), at batch 1, 8 and 32 with 2k context. vLLM is 2.5–5.5% faster than our loop; without graphs it takes 12.2 ms at every batch size, the sign of a step limited by the CPU. Right: vLLM at batch 1 with a BF16 KV cache (blue) and an FP8 KV cache (orange), measured at 32,768 and 130,000 tokens of context; each line joins the two points. Halving the bytes per cached token halves the cost of each token of context (41.6 → 19.6 ns).</figcaption></figure><p>Finally, how does my ~300-line loop compare to a real inference engine? vLLM on the same model and GPU, measured the same way:</p><ul><li><strong>With CUDA graphs, vLLM takes 6.37 ms per token at batch 1</strong> (157 tokens/s), against my 6.74 ms at the same 2k context, so it’s 2.5–5.5% faster across batch sizes 1, 8 and 32. I didn’t trace vLLM’s kernels, so I can’t say exactly where its edge comes from.</li><li><strong>Without CUDA graphs (</strong><strong>--enforce-eager) it takes 12.2 ms at every batch size</strong>, the flat line that gives away a CPU-bound loop.</li><li><strong>Context costs it the same as mine: 41.6 ns per token.</strong> With its <strong>FP8 KV cache</strong>, that halves to <strong>19.6 ns per token</strong>, making a step at 130k context 23% faster (9.04 ms instead of 11.73). I didn’t measure the quality cost of the FP8 KV cache.</li></ul><h3>Takeaways</h3><ol><li><strong>Decode is a memory-bandwidth problem.</strong> Once launch overhead is gone, a token costs about as long as it takes to read the weights and the KV cache, and the byte math predicted every sweep in this post to within a few percent.</li><li><strong>In plain PyTorch, the CPU is the bottleneck, not the GPU.</strong> Half of every eager step was idle time; CUDA graphs and fusion took Llama-3.1-8B from 16.4 to 6.63 ms per token, 72% of the bandwidth floor and within 6% of vLLM.</li><li><strong>Context and batch size just move bytes around.</strong> +41 ns per token of context; batching is nearly free until the KV cache outweighs the weights.</li><li><strong>Quantization only pays off if the kernel does.</strong> INT4 was 2.0× faster at batch 1 (with a real quality cost), and at batch 32 only FP8 still beat BF16.</li></ol><p><strong>What’s next.</strong> The obvious follow-ups attack the bytes in different ways. Some cut the bytes per token: speculative decoding (verify several tokens per read of the weights) and better 4-bit methods like GPTQ or AWQ. Tensor parallelism splits the weights across GPUs, so each GPU reads only its share. And paged attention doesn’t reduce what a step reads; it stops the KV cache from reserving memory a request may never use, so more requests fit and batches can grow.</p><p>Next: I ran the same code on an NVIDIA B200: <a href="https://medium.com/@ayushgarg70/twice-the-bandwidth-not-twice-the-speed-decode-on-blackwell-916ece024c40?postPublishedType=initial">Twice the bandwidth, not twice the speed: decode on Blackwell.</a></p><h3>Appendix: reproduce it</h3><ul><li>Repo: <a href="https://github.com/ayushgarg70/llm-decode-profiling">github.com/ayushgarg70/llm-decode-profiling</a>. On any machine, no GPU needed: uv sync &amp;&amp; uv run pytest, then uv run python -m analysis.plots regenerates every chart and uv run python -m analysis.results_tableregenerates notes/RESULTS.md (every number with its run ID) from the data in data/. To rerun the experiments you need a 1×H100 VM with Nsight Systems and Nsight Compute, and access to Llama-3.1-8B on Hugging Face; the README lists the steps (scripts/bootstrap_gpu.sh, then the run lists runs/s1.sh to runs/s3.sh, one block at a time, e.g. runs/s1.sh e0).</li><li>Versions: PyTorch 2.15.0 (CUDA 13.2), driver 580.173.02, transformers 5.23.0, torchao 0.18.0, vLLM 0.34.0 (with PyTorch 2.14.0), Nsight Systems / Nsight Compute 2025.3.</li><li>Credits: gpt-fast (the static-cache + torch.compile recipe), torchao (quantization), vLLM, and NVIDIA’s Nsight documentation. Concept illustrations generated with OpenAI Codex; every chart is generated from the measured data.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c3afbe77a459" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>