Curriculum / Part 3 · How AI workloads use storage
Lesson 14

Inference and KV cache

Model weights at startup, many replicas loading at once, and the KV cache: how much memory it needs and when it spills to storage.

⏱ 35 min5-question quiz

The simple version

A new cashier can't start until they've memorised the price list (the model weights). During the shift they jot notes about each customer's order so they don't ask twice (the KV cache). If the notepad fills up, older notes go to a filing cabinet, which is slower to check.

Loading weights

load time ≈ weights size ÷ read bandwidth available to that replica

70B parameters in bf16 = 140 GB. At 2 GB/s that is 70 s; at 14 GB/s, 10 s.
But if 20 replicas start together against a NAS that delivers 30 GB/s in total, each gets ~1.5 GB/s → about 93 s each. Autoscaling events create exactly this "thundering herd".

Common fixes: cache weights on local NVMe after the first pull, stagger startups, increase client-side parallelism (Lab 6.1), or distribute weights peer-to-peer between nodes.

How big is the KV cache?

Transformers keep the attention keys and values for every token in the context so they don't recompute them.

KV bytes per token = 2 × layers × KV heads × head dim × bytes per value

A typical 70B configuration with grouped-query attention: 80 layers, 8 KV heads, head dim 128, bf16 (2 bytes) → 2 × 80 × 8 × 128 × 2 = 327,680 B ≈ 320 KiB per token.
A 32,768-token context → ~10 GiB for one sequence.

GPU memory fills fast, so serving systems evict, compress or offload KV blocks. Offloading to CPU memory or NVMe lets a server reuse a long shared prefix (a system prompt, a document) instead of recomputing it.

Why latency matters more here

Training cares about sustained throughput. Inference cares about time to first token and per-token latency for each user. Fetching an offloaded KV block from NVMe must beat recomputing it, so the storage path's latency, not just its bandwidth, decides whether offload helps.

Tools

fio --rw=read --bs=1M on the weights path
Measure the read rate a replica will see.
nfsstat -m
Check NFS mount options (nconnect, rsize) used to load weights.
ls -lh /models/...
Know the weight file sizes you're planning around.

Quiz

Lab

Related lab

Lab 6.1 · One TCP stream

Loading a 140 GiB model from the NAS (/nas/models/llm-70b/weights.safetensors) runs at about 2.2 GB/s. Someone already tried mount -o remount,nconnect=8 /nas and saw no change.

Start lab 6.1 →

Try it now in the simulator (opens with the command ready; press Enter):

$ ls -lh /nas/models/llm-70b$ nfsstat -m