<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Foundation Model Engineering — Selected articles</title>
    <link>https://sungeuns.github.io/foundation-model-engineering/#short-reads</link>
    <description>Standalone articles from Foundation Model Engineering, with worked examples and reproducible experiments.</description>
    <language>en</language>
    <atom:link href="https://sungeuns.github.io/foundation-model-engineering/articles.xml" rel="self" type="application/rss+xml" />
    <atom:link href="https://pubsubhubbub.appspot.com/" rel="hub" />
    <item>
      <title>Why did 4/5 accuracy become 4/6 on two ranks?</title>
      <link>https://sungeuns.github.io/foundation-model-engineering/articles/ddp-sampler-audit/</link>
      <guid isPermaLink="true">https://sungeuns.github.io/foundation-model-engineering/articles/ddp-sampler-audit/</guid>
      <pubDate>Wed, 25 Nov 2026 05:28:12 GMT</pubDate>
      <description>Trace five example IDs through PyTorch’s DistributedSampler and DataLoader. One repeated mistake turns 4/5 into 4/6. Dropping a batch loses a different example.</description>
      <category>python</category>
      <category>pytorch</category>
      <category>machinelearning</category>
      <content:encoded>&lt;p&gt;Five validation examples have IDs 0 through 4. The prediction for ID 0 is wrong and the other four are right. Accuracy is &lt;strong&gt;4/5&lt;/strong&gt;. Now give the same examples and predictions to a two-rank evaluation loop. Each rank adds up its correct predictions and its number of examples. The combined result is &lt;strong&gt;4/6&lt;/strong&gt;. Which example got a second vote?&lt;/p&gt;
&lt;h2 id=&quot;follow-the-ids&quot;&gt;Follow the IDs&lt;/h2&gt;
&lt;p&gt;The CPU experiment runs PyTorch 2.15.0’s real &lt;code&gt;DistributedSampler&lt;/code&gt; and &lt;code&gt;DataLoader&lt;/code&gt; for each rank in one process. The answers are fixed on purpose. There is no model to change its prediction. Shuffling is off, and each rank batches two examples at a time. Each pair of brackets below is one batch delivered by the loader.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Run&lt;/th&gt;&lt;th&gt;Rank 0 batches&lt;/th&gt;&lt;th&gt;Rank 1 batches&lt;/th&gt;&lt;th&gt;Correct / visits&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Single rank, unsharded&lt;/td&gt;&lt;td&gt;&lt;code&gt;[0,1] [2,3] [4]&lt;/code&gt;&lt;/td&gt;&lt;td&gt;—&lt;/td&gt;&lt;td&gt;4/5&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Two ranks, keep the tails&lt;/td&gt;&lt;td&gt;&lt;code&gt;[0,2] [4]&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;[1,3] [0]&lt;/code&gt;&lt;/td&gt;&lt;td&gt;4/6&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Drop the sampler tail only&lt;/td&gt;&lt;td&gt;&lt;code&gt;[0,2]&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;[1,3]&lt;/code&gt;&lt;/td&gt;&lt;td&gt;3/4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Drop each loader’s last partial batch only&lt;/td&gt;&lt;td&gt;&lt;code&gt;[0,2]&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;[1,3]&lt;/code&gt;&lt;/td&gt;&lt;td&gt;3/4&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Five indices cannot split evenly across two ranks. With &lt;code&gt;DistributedSampler(drop_last=False)&lt;/code&gt;, PyTorch extends the sequence to &lt;code&gt;0, 1, 2, 3, 4, 0&lt;/code&gt; for this unshuffled run. Rank 0 takes every other index starting at the first. Rank 1 takes the rest. The extra ID is the one wrong answer. Summing both ranks’ counts gives exactly the accuracy over those six visits. &lt;a href=&quot;https://github.com/pytorch/pytorch/blob/d0f2af8b66b20da4baa50644c0cd3f887a2eae2e/torch/utils/data/distributed.py#L116-L134&quot;&gt;Sampler implementation from the CPU build&lt;/a&gt; · &lt;a href=&quot;https://docs.pytorch.org/docs/2.15/data.html#torch.utils.data.distributed.DistributedSampler&quot;&gt;PyTorch 2.15 sampler documentation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The last two rows reach 3/4 by different routes. With &lt;code&gt;drop_last=True&lt;/code&gt; on the &lt;strong&gt;sampler&lt;/strong&gt;, ID 4 is trimmed before the ranks get their indices. With &lt;code&gt;drop_last=True&lt;/code&gt; only on the &lt;strong&gt;loader&lt;/strong&gt;, the sampler assigns all six visits as before. Each rank then discards its one-example final batch. Rank 0 drops the real ID 4 while rank 1 drops the second copy of ID 0. &lt;a href=&quot;https://docs.pytorch.org/docs/2.15/data.html#torch.utils.data.DataLoader&quot;&gt;PyTorch 2.15 DataLoader documentation&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;move-the-mistake&quot;&gt;Move the mistake&lt;/h2&gt;
&lt;p&gt;What if ID 4 were the only mistake? The batches would stay the same. Download the &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/labs/2026-11/ddp-sampler-audit.json&quot;&gt;recorded CPU run (JSON)&lt;/a&gt;, then run this snippet beside it. It uses the standard library and re-scores the IDs that PyTorch actually delivered.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pathlib &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Path&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;runs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.loads(Path(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ddp-sampler-audit.json&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).read_text())[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;runs&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;unsharded&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;padded&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sampler_drop_last&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;loader_drop_last&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ids &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [i &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rank &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; runs[name][&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ranks&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; batch &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rank[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;batches&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; batch]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    correct &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; sum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(i &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ids)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(name, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;correct&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ids)&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now the full dataset still scores 4/5. Counting the repeated &lt;strong&gt;correct&lt;/strong&gt; answer produces &lt;strong&gt;5/6&lt;/strong&gt;. Either drop setting produces &lt;strong&gt;4/4&lt;/strong&gt; because the only error never reaches the scorer.&lt;/p&gt;
&lt;p&gt;For an accuracy report that gives every example equal weight, keep a stable example ID with its prediction and label. Check that the observed IDs cover exactly the evaluation manifest and that repeated IDs agree before counting one result per ID. This recovers 4/5 in the padded run. Both truncated runs are missing ID 4. To report the full-dataset score from either, evaluate the missing example under the same settings.&lt;/p&gt;
&lt;p&gt;To reproduce the original four runs, download the &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/labs/ddp_sampler_audit.py&quot;&gt;CPU script&lt;/a&gt; and run &lt;code&gt;python ddp_sampler_audit.py&lt;/code&gt; with PyTorch 2.15.0 installed. The JSON records the indices assigned to each rank as well as the batches actually scored. &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/chapter-7/data-parallelism/#ddp-sampler-audit&quot;&gt;Continue to the data-parallelism chapter&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;An AI coding assistant ran the CPU experiment and wrote this article.&lt;/em&gt;&lt;/p&gt;</content:encoded>
    </item>
    <item>
      <title>Why did a vision model read a missing receipt total as 1?</title>
      <link>https://sungeuns.github.io/foundation-model-engineering/articles/receipt-grounding/</link>
      <guid isPermaLink="true">https://sungeuns.github.io/foundation-model-engineering/articles/receipt-grounding/</guid>
      <pubDate>Wed, 25 Nov 2026 04:54:26 GMT</pubDate>
      <description>Change two quantities on a receipt with no prices and predict SmolVLM’s answer. Swap the item rows and inspect all eight model outputs.</description>
      <category>python</category>
      <category>machinelearning</category>
      <category>computervision</category>
      <content:encoded>&lt;p&gt;SmolVLM-256M-Instruct read &lt;code&gt;31.50&lt;/code&gt; from a receipt marked &lt;code&gt;Total 31.50&lt;/code&gt;. Change the line to &lt;code&gt;Total 42.00&lt;/code&gt; and the model answers &lt;code&gt;42.00&lt;/code&gt;. Erase it and the model answers &lt;strong&gt;&lt;code&gt;1&lt;/code&gt;&lt;/strong&gt;. Where could that digit have come from?&lt;/p&gt;
&lt;img src=&quot;https://raw.githubusercontent.com/sungeuns/foundation-model-engineering/6295c5cf5d9056f33795003a0c6ddaf2f569a49b/src/assets/images/chapter-11/receipt-missing.png&quot; alt=&quot;The receipt lists Drawing paper, 1 pack, and Pencils, 1 box. Below the rule the total is blank. Neither item has a price.&quot; width=&quot;512&quot; height=&quot;384&quot;&gt;
&lt;p&gt;This is the actual 512 × 384 input, drawn with Pillow. The prompt asked the model to copy the printed Total line exactly and answer &lt;code&gt;MISSING&lt;/code&gt; if the line was absent. A pack of paper and a box of pencils each supply a plausible source for the digit. Neither has a price.&lt;/p&gt;
&lt;h2 id=&quot;predict-the-next-answer&quot;&gt;Predict the next answer&lt;/h2&gt;
&lt;p&gt;Keep the total blank. First change only the paper quantity to 7. Then start again and change only the pencil quantity to 8. What would you expect if the model were copying the paper quantity? What if it were copying the pencil quantity? The last case changes both digits. Read the third column first. Then predict what would happen if the pencil entry, including its quantity and unit, moved above the paper entry.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;right&quot;&gt;Paper packs&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Pencil boxes&lt;/th&gt;&lt;th&gt;Paper above pencils&lt;/th&gt;&lt;th&gt;Pencils above paper&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;1&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;1&lt;/td&gt;&lt;td&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;7&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;1&lt;/td&gt;&lt;td&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;7&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;1&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;8&lt;/td&gt;&lt;td&gt;&lt;code&gt;8 box&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;7&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;8&lt;/td&gt;&lt;td&gt;&lt;code&gt;8 box&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;7.&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;For the last column we swapped two 48-pixel-high strips of the original image. The words and quantities traveled together. The prompt, checkpoint, CPU and greedy decoding settings stayed fixed. The table trims the leading space from each answer. The period in &lt;code&gt;7.&lt;/code&gt; came from the model.&lt;/p&gt;
&lt;p&gt;When pencils were last, changing the paper quantity never changed the answer. That makes it tempting to conclude that the model picked pencils. Yet on the 7-pack, 8-box receipt, moving paper to the bottom switched the answer from &lt;code&gt;8 box&lt;/code&gt; to &lt;code&gt;7.&lt;/code&gt;. &lt;strong&gt;On all six images with different quantities, the first digit matched the lower row.&lt;/strong&gt; The two images with a 1 on both rows cannot distinguish the two explanations. This isolates a position effect in this layout and prompt. It leaves open what would happen with more items or a different layout.&lt;/p&gt;
&lt;h2 id=&quot;how-a-scoring-rule-hides-the-error&quot;&gt;How a scoring rule hides the error&lt;/h2&gt;
&lt;p&gt;An amount extractor would pass &lt;strong&gt;2 of 2&lt;/strong&gt; receipts with visible totals. A verifier asking for the &lt;strong&gt;whole printed line&lt;/strong&gt; would pass &lt;strong&gt;0 of 2&lt;/strong&gt; because both answers omit &lt;code&gt;Total&lt;/code&gt;. Across the eight distinct blank-total images, the model gave the requested &lt;code&gt;MISSING&lt;/code&gt; response &lt;strong&gt;0 of 8&lt;/strong&gt; times. Keeping these three counts separate shows what would happen if an expense tool accepted any numeric answer as a charge.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/labs/2026-11/receipt-grounding.zip&quot;&gt;Download the receipt images, CPU code and results (ZIP)&lt;/a&gt;. The ZIP includes the eight PNGs in &lt;code&gt;order-inputs/&lt;/code&gt;. Run this Python snippet in the directory containing the unopened ZIP to compare each original answer with the answer after the row swap. It uses only the standard library and preserves the spaces and punctuation.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; zipfile &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ZipFile&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;with&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ZipFile(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;receipt-grounding.zip&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; bundle:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    original &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.loads(bundle.read(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;receipt-grounding-cpu.json&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;cases&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    edits &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.loads(bundle.read(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;receipt-quantity-outputs.json&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;cases&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.loads(bundle.read(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;receipt-order-outputs.json&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;cases&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;blank &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; next&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(row &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; row &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; original &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; row[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;file&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;receipt-missing.png&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;answers &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;): blank[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;raw_text&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; row &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; edits:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    answers[row[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;paper_packs&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], row[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pencil_boxes&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; row[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;raw_text&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; row &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; row[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;row_order&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;pencils-first&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        counts &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (row[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;paper_packs&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], row[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pencil_boxes&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(counts, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;repr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(answers[counts]), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;repr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(row[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;raw_text&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To run the model yourself, extract the ZIP. In that directory, install the pinned packages listed in &lt;code&gt;receipt_grounding.py&lt;/code&gt;, then run &lt;code&gt;python receipt_grounding.py .&lt;/code&gt;, &lt;code&gt;python receipt_quantity_probe.py&lt;/code&gt; and &lt;code&gt;python receipt_order_probe.py&lt;/code&gt;. The first run downloads the pinned model weights (about 513 MB). &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/chapter-11/vision-language-bridges/#receipt-grounding&quot;&gt;Compare the original images in the vision-language chapter&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;An AI coding assistant wrote this article from the book’s recorded CPU experiments.&lt;/em&gt;&lt;/p&gt;</content:encoded>
    </item>
    <item>
      <title>Why a seventh token left the KV cache with a smaller buffer</title>
      <link>https://sungeuns.github.io/foundation-model-engineering/articles/loop-cache-storage/</link>
      <guid isPermaLink="true">https://sungeuns.github.io/foundation-model-engineering/chapter-4/various-llm-architectures/#loop-cache-storage</guid>
      <pubDate>Wed, 25 Nov 2026 03:26:00 GMT</pubDate>
      <description>Trace a 24-layer looped decoder on CPU. After a seventh token, the logical KV payload grew but the buffers behind it shrank.</description>
      <category>python</category>
      <category>machinelearning</category>
      <category>pytorch</category>
      <content:encoded>&lt;p&gt;Six tokens have passed through a small decoder. Its keys and values occupy 18,432 bytes of backing storage. Send it one more token and the figure drops to 13,824 bytes. Where did 4,608 bytes go?&lt;/p&gt;
&lt;p&gt;The clue is a single sliding-window attention layer. Its window covers the current token and three earlier ones. After processing six tokens, the keys it keeps for the next query have shape &lt;code&gt;[1, 2, 3, 8]&lt;/code&gt; in this experiment. Two heads, three positions and eight FP32 coordinates make 192 bytes. Look underneath that tensor and you find a 384-byte allocation. The last three positions are a view into the original six.&lt;/p&gt;
&lt;p&gt;You can reproduce the arithmetic with PyTorch alone. This example follows one key tensor through a decode step.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; torch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;kept &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; torch.zeros(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;torch.float32)[:, :, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:, :]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(kept.nbytes, kept.untyped_storage().nbytes())       &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# 192 384&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;next_key &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; torch.ones(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;kept.dtype)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;kept &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; torch.cat([kept, next_key], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;dim&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)[:, :, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:, :]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(kept.nbytes, kept.untyped_storage().nbytes())       &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# 192 256&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The full experiment uses Hugging Face’s &lt;code&gt;Inkling2LoopTextModel&lt;/code&gt; with 24 layers, of which 20 have a short window. Each of those 20 layers has both a key and a value buffer. Moving from six positions to four in their backing allocations saves &lt;code&gt;20 × 2 × 128 = 5,120&lt;/code&gt; bytes. The four full-attention layers each add one key and one value, using &lt;code&gt;4 × 2 × 64 = 512&lt;/code&gt; bytes. Subtract the growth from the savings and you get the missing 4,608 bytes. The sum of the &lt;em&gt;visible tensor sizes&lt;/em&gt; actually grew from 10.5 to 11 KiB.&lt;/p&gt;
&lt;p&gt;What if the middle 12 layers run five times? The same 24 layer objects now receive 72 calls per forward pass. This implementation keeps a separate cache for each pass. After the six-token prefill the KV storage is 54 KiB, and after one more token it is 40.5 KiB. The experiment uses narrow, randomly initialized FP32 weights on CPU. These numbers describe the backing storage reached from the KV tensors and exclude convolution state, allocator reservations and the rest of the process. They cannot predict the memory or answer quality of a trained checkpoint.&lt;/p&gt;
&lt;p&gt;Several tensor views can share one allocation. Counting the bytes behind each view would count that allocation more than once. The experiment groups allocations by their storage address and sums each size once. &lt;a href=&quot;https://github.com/sungeuns/foundation-model-engineering/blob/9c2cdca7b6eb33707aed43535afa0d132eff74db/public/labs/loop_cache.py#L17-L29&quot;&gt;Inspect the counting code on GitHub&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Run the &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/labs/loop_cache.py&quot;&gt;complete CPU experiment&lt;/a&gt; or inspect &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/labs/2026-11/loop-cache.json&quot;&gt;the measured bytes at one, three and five passes&lt;/a&gt;. The script compares the last hidden state from cached decoding with a fresh run on all seven tokens. The &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/chapter-4/various-llm-architectures/#loop-cache-storage&quot;&gt;book section&lt;/a&gt; connects the result to two different approaches to layer reuse, including &lt;a href=&quot;https://docs.z.ai/guides/llm/glm-5.4&quot;&gt;Z.ai’s description of GLM-5.4&lt;/a&gt; and the &lt;a href=&quot;https://thinkingmachines.ai/model-card/tml-d24-varloop-research/&quot;&gt;Thinking Machines Lab research checkpoint&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;run-the-measurement-on-your-cpu&quot;&gt;Run the measurement on your CPU&lt;/h2&gt;
&lt;p&gt;These commands use Python 3.12 and curl on Linux x86-64. The initial setup downloads packages and the script. The experiment creates a small model with random weights, so the model run works offline even with an empty model cache. The install selects the CPU build of PyTorch.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;python3.12&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -m&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; venv&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; .venv&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; .venv/bin/activate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;python&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -m&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pip&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --index-url&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; https://download.pytorch.org/whl/cpu&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; torch==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2.15.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;python&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -m&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pip&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; transformers==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5.24.1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;curl&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -fLo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; loop_cache.py&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  https://raw.githubusercontent.com/sungeuns/foundation-model-engineering/9c2cdca7b6eb33707aed43535afa0d132eff74db/public/labs/loop_cache.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;HF_HUB_OFFLINE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; python&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; loop_cache.py&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; loop-run.json&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Find the row with &lt;code&gt;passes&lt;/code&gt; equal to 1 in &lt;code&gt;loop-run.json&lt;/code&gt;. Its &lt;code&gt;backing_bytes&lt;/code&gt; should fall from 18,432 to 13,824 while &lt;code&gt;logical_bytes&lt;/code&gt; rises from 10,752 to 11,264. A message about &lt;code&gt;causal_conv1d&lt;/code&gt; means the CPU run is using the reference PyTorch implementation.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;An AI coding assistant wrote this article and ran the CPU experiment with PyTorch 2.15.0 and Transformers 5.24.1.&lt;/em&gt;&lt;/p&gt;</content:encoded>
    </item>
    <item>
      <title>How a sample-rate label turns 11 seconds into 30 for Whisper</title>
      <link>https://sungeuns.github.io/foundation-model-engineering/articles/whisper-sample-rate/</link>
      <guid isPermaLink="true">https://sungeuns.github.io/foundation-model-engineering/chapter-11/audio-and-speech-integration/#whisper-sample-rate</guid>
      <pubDate>Wed, 25 Nov 2026 02:00:59 GMT</pubDate>
      <description>Hear the same samples at two playback rates, then reproduce a Whisper transcription error that a tensor shape check misses.</description>
      <category>python</category>
      <category>audio</category>
      <category>machinelearning</category>
      <content:encoded>&lt;p&gt;At 44,100 Hz, two notes an octave apart fit into one second. At 16,000 Hz, both notes sound lower, and the second note is still playing two seconds in.&lt;/p&gt;
&lt;p&gt;Both files contain the same 44,100 synthesized samples. The WAV header tells your player how quickly to consume them. At 16,000 samples per second, playback takes exactly 2.75625 seconds. You can open the &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/labs/audio/sample-clock-44100.wav&quot; download=&quot;sample-clock-44100.wav&quot;&gt;44,100 Hz WAV&lt;/a&gt; and the &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/labs/audio/sample-clock-16000.wav&quot; download=&quot;sample-clock-16000.wav&quot;&gt;16,000 Hz WAV&lt;/a&gt; files separately. This Python example fetches both into memory, calculates their durations from their headers, and checks that the PCM audio bytes match. It uses only the standard library and needs an internet connection.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wave&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; io &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; BytesIO&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; urllib.request &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; urlopen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;audio_url &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;https://sungeuns.github.io/foundation-model-engineering/labs/audio/&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; read_pcm&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(file_rate):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    with&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; urlopen(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;audio_url&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;sample-clock-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file_rate&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.wav&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; response:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        data &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; response.read()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    with&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wave.open(BytesIO(data), &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;rb&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wav:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        frames &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wav.getnframes()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        rate &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wav.getframerate()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;frames&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; / &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;rate&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; = &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;frames &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rate&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:g&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; s&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wav.readframes(frames)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;fast &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; read_pcm(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;44100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;slow &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; read_pcm(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;assert&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fast &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; slow&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The two lines read &lt;code&gt;44100 / 44100 = 1 s&lt;/code&gt; and &lt;code&gt;44100 / 16000 = 2.75625 s&lt;/code&gt;. Only the playback clock changed.&lt;/p&gt;
&lt;h2 id=&quot;why-the-shape-check-passed&quot;&gt;Why the shape check passed&lt;/h2&gt;
&lt;p&gt;In a separate CPU experiment, Whisper transcribed an 11-second excerpt of John F. Kennedy’s inaugural address. Mixing it to mono and resampling to 16 kHz produced 176,000 samples. Keeping the original rate after the mono mix produced 485,100. Passing that array to the Transformers feature extractor with &lt;code&gt;sampling_rate=16000&lt;/code&gt; made the excerpt appear 30.31875 seconds long. The extractor padded the shorter input and trimmed the longer one to the same 30-second window. Both produced a feature tensor of shape &lt;code&gt;[1, 80, 3000]&lt;/code&gt;. The resampled recording yielded all three phrases checked by Whisper’s upstream test. The mislabeled one yielded none.&lt;/p&gt;
&lt;p&gt;The resampled transcript began “And so my fellow Americans”. With the wrong rate, it began “Ah, oh, oh, oh, oh,”. The three phrase checks look for lowercase substrings in the decoded text. You can inspect the first five words and matches for each input before downloading a model. &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/labs/2026-11/whisper-sample-rate.json&quot;&gt;Recorded output (JSON)&lt;/a&gt; · &lt;a href=&quot;https://github.com/sungeuns/foundation-model-engineering/blob/70f930a14cc225a0a69547514846a14e889c061c/public/labs/whisper_sample_rate.py#L99-L102&quot;&gt;Scoring code on GitHub&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Whisper’s own &lt;code&gt;whisper.load_audio&lt;/code&gt; resamples a file automatically. When you supply a waveform array yourself, divide its unpadded sample count by the rate you declare and compare that duration with the source. The phrase counts describe this one recording. &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/labs/whisper_sample_rate.py&quot;&gt;Reproduce the CPU run&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;An AI coding assistant wrote this adaptation from the textbook’s &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/chapter-11/audio-and-speech-integration/#whisper-sample-rate&quot;&gt;worked example and measurements&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;</content:encoded>
    </item>
    <item>
      <title>Why length bucketing cut padding by two-thirds and left peak KV memory unchanged</title>
      <link>https://sungeuns.github.io/foundation-model-engineering/articles/length-bucketing/</link>
      <guid isPermaLink="true">https://sungeuns.github.io/foundation-model-engineering/chapter-12/length-bucketing-lab/</guid>
      <pubDate>Wed, 25 Nov 2026 00:27:23 GMT</pubDate>
      <description>Pair six prompts two ways, count the padding and reproduce the peak key/value cache size with a few lines of Python.</description>
      <category>python</category>
      <category>machinelearning</category>
      <content:encoded>&lt;p&gt;Six prompts arrive with lengths of 3, 11, 4, 10, 5 and 9 tokens. An inference worker reads them two at a time. If it groups prompts of similar length, how much less memory will its largest key/value (KV) cache need?&lt;/p&gt;
&lt;p&gt;In a CPU experiment with SmolLM2-135M, padding fell from 18 positions to 6. The largest cache held &lt;strong&gt;990 KiB&lt;/strong&gt; either way. To see where the saving went, follow the 11-token prompt.&lt;/p&gt;
&lt;h2 id=&quot;the-longest-prompt-picks-the-width&quot;&gt;The longest prompt picks the width&lt;/h2&gt;
&lt;p&gt;In arrival order, the first pair has lengths 3 and 11. Left-padding the short prompt gives a rectangle 2 rows tall and 11 positions wide. Fourteen of its 22 positions contain real tokens. The attention mask hides the eight padding positions, but the dense key and value tensors still have room for all 22.&lt;/p&gt;
&lt;p&gt;Now sort the prompts. The 11-token prompt gets a 10-token partner. Twenty-one positions hold real tokens, and one holds padding. The rectangle still has 22 positions.&lt;/p&gt;
&lt;p&gt;In this model, keeping the keys and values for one position in FP32 costs 45 KiB. Multiply 2 tensors × 30 layers × 3 KV heads × 64 values per head × 4 bytes to get 46,080 bytes. Both pairs therefore use 22 × 45 = 990 KiB.&lt;/p&gt;
&lt;h2 id=&quot;a-sum-and-a-peak-answer-different-questions&quot;&gt;A sum and a peak answer different questions&lt;/h2&gt;
&lt;p&gt;The experiment released each pair’s cache before running the next pair. These were the measured cache sizes immediately after the model finished reading each pair of prompts.&lt;/p&gt;























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Pairing&lt;/th&gt;&lt;th&gt;Cache sizes in execution order&lt;/th&gt;&lt;th&gt;Sum&lt;/th&gt;&lt;th&gt;Peak&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Arrival order&lt;/td&gt;&lt;td&gt;990, 900, 810 KiB&lt;/td&gt;&lt;td&gt;2,700 KiB&lt;/td&gt;&lt;td&gt;990 KiB&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sorted by length&lt;/td&gt;&lt;td&gt;360, 810, 990 KiB&lt;/td&gt;&lt;td&gt;2,160 KiB&lt;/td&gt;&lt;td&gt;990 KiB&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The 540 KiB difference in the sums corresponds exactly to twelve saved padding positions. Those sums span three moments in time. To find out how much KV data was live at once, take the maximum.&lt;/p&gt;
&lt;p&gt;This Python calculation runs without a model. Try the original six lengths, then remove the final 9 and run it again. The last batch can contain a single request.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;lengths &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; order &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (lengths, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;sorted&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(lengths)):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    batches &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [order[start:start &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; start &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(order), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    slots &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(batch) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; max&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(batch) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; batch &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; batches]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    sizes_kib &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;45&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; count &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; count &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; slots]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;sum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(slots) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; sum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(order), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;sum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(sizes_kib), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;max&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(sizes_kib))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# padding positions, summed KiB, peak KiB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# 18 2700 990&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#  6 2160 990&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With five requests, the arrival-order batches hold 990, 900 and 225 KiB. Sorted batches hold 360, 900 and 495 KiB. The longest prompt now runs alone and needs only 11 × 45 = 495 KiB. The preceding pair of lengths 5 and 10 sets the peak at &lt;strong&gt;900 KiB&lt;/strong&gt;, down from 990. Counting the actual key and value tensors on CPU for these five prompts reproduced all six batch sizes.&lt;/p&gt;
&lt;h2 id=&quot;what-to-measure-on-your-worker&quot;&gt;What to measure on your worker&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/labs/kv_batch_bucketing.py&quot;&gt;CPU experiment&lt;/a&gt; counts bytes in the actual tensors. The &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/labs/2026-11/kv-batch-bucketing.json&quot;&gt;recorded run&lt;/a&gt; includes every pair and each prompt’s greedy next-token ID. All six prompts kept the same next-token ID when run alone and under both batching policies. For each prompt the script also checked the full vector of next-token logits against its solo run, with absolute and relative tolerances of &lt;code&gt;1e-4&lt;/code&gt;. &lt;a href=&quot;https://github.com/sungeuns/foundation-model-engineering/blob/d4d45703a0941543c63afc406bcaf21e0b99a036/public/labs/kv_batch_bucketing.py#L100-L109&quot;&gt;Inspect the comparison on GitHub&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For a service that waits to find similar-length partners, measure the time spent in the queue as well as peak cache size. For a paged engine, count the live blocks it actually allocates. This experiment used dense batches and measured the KV tensor payload. Process memory, allocator reservations and request latency require their own measurements.&lt;/p&gt;
&lt;h2 id=&quot;run-the-measurement-on-your-cpu&quot;&gt;Run the measurement on your CPU&lt;/h2&gt;
&lt;p&gt;These commands were tested with Python 3.12 and curl on Linux x86-64. The first model run downloads about 269 MB of public SmolLM2 weights plus the tokenizer files. To try the arithmetic without that download, run the short Python example above.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;python3.12&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -m&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; venv&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; .venv&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; .venv/bin/activate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;python&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -m&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pip&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --index-url&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; https://download.pytorch.org/whl/cpu&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; torch==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2.15.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;python&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -m&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pip&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; transformers==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5.24.1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;curl&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -fLo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; kv_batch_bucketing.py&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  https://raw.githubusercontent.com/sungeuns/foundation-model-engineering/d4d45703a0941543c63afc406bcaf21e0b99a036/public/labs/kv_batch_bucketing.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;python&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; kv_batch_bucketing.py&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; bucketing-run.json&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The JSON report contains token IDs and measurements for the six original prompts. Look for &lt;code&gt;largest_batch_prefill_kv_bytes&lt;/code&gt; under both policies. Each should read 1,013,760 bytes, or 990 KiB. Once the model files are cached, &lt;code&gt;HF_HUB_OFFLINE=1 python kv_batch_bucketing.py&lt;/code&gt; reruns the experiment offline in that environment.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;An AI coding assistant wrote this adaptation from the book’s &lt;a href=&quot;https://sungeuns.github.io/foundation-model-engineering/chapter-12/length-bucketing-lab/&quot;&gt;length-bucketing experiment&lt;/a&gt; and its recorded measurements.&lt;/em&gt;&lt;/p&gt;</content:encoded>
    </item>
  </channel>
</rss>
