Contents

Foundation Model Engineering

Why a seventh token left the KV cache with a smaller buffer

Published by Seongeun So ·

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?

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 [1, 2, 3, 8] 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.

You can reproduce the arithmetic with PyTorch alone. This example follows one key tensor through a decode step.

import torch

kept = torch.zeros(1, 2, 6, 8, dtype=torch.float32)[:, :, -3:, :]
print(kept.nbytes, kept.untyped_storage().nbytes())       # 192 384
next_key = torch.ones(1, 2, 1, 8, dtype=kept.dtype)
kept = torch.cat([kept, next_key], dim=2)[:, :, -3:, :]
print(kept.nbytes, kept.untyped_storage().nbytes())       # 192 256

The full experiment uses Hugging Face’s Inkling2LoopTextModel 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 20 × 2 × 128 = 5,120 bytes. The four full-attention layers each add one key and one value, using 4 × 2 × 64 = 512 bytes. Subtract the growth from the savings and you get the missing 4,608 bytes. The sum of the visible tensor sizes actually grew from 10.5 to 11 KiB.

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.

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. Inspect the counting code on GitHub

Run the complete CPU experiment or inspect the measured bytes at one, three and five passes. The script compares the last hidden state from cached decoding with a fresh run on all seven tokens. The book section connects the result to two different approaches to layer reuse, including Z.ai’s description of GLM-5.4 and the Thinking Machines Lab research checkpoint.

Run the measurement on your CPU

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.

python3.12 -m venv .venv
. .venv/bin/activate
python -m pip install --index-url https://download.pytorch.org/whl/cpu torch==2.15.0
python -m pip install transformers==5.24.1
curl -fLo loop_cache.py \
  https://raw.githubusercontent.com/sungeuns/foundation-model-engineering/9c2cdca7b6eb33707aed43535afa0d132eff74db/public/labs/loop_cache.py
HF_HUB_OFFLINE=1 python loop_cache.py > loop-run.json

Find the row with passes equal to 1 in loop-run.json. Its backing_bytes should fall from 18,432 to 13,824 while logical_bytes rises from 10,752 to 11,264. A message about causal_conv1d means the CPU run is using the reference PyTorch implementation.

An AI coding assistant wrote this article and ran the CPU experiment with PyTorch 2.15.0 and Transformers 5.24.1.