LearnQuestion bank

A document-QA product re-sends the same 60k-token contract across many sessions spread over hours, so GPU prefix-cache hits are rare. Would you offload KV cache to CPU memory or SSD and reload it instead of recomputing? Show the arithmetic.

Applied LLMTechnicalHard

Answer it yourself first, out loud or on paper, then compare.

Answer

Model answer, written in a candidate's voice

In short

  • Cache size: 2 x 32 layers x 8 KV heads x 128 x 2 B = 128 KiB/token; x 60k tokens ≈ 7.3 GiB (7.9 GB) in bf16 for an 8B-class GQA model
  • Recompute cost: 2 x params x tokens ≈ 9.6e14 FLOPs plus a causal attention term of similar size at 60k (≈ 9.4e14) → ≈ 10–19 s at 100–200 effective TFLOP/s
  • Reload cost: 7.9 GB ÷ tier bandwidth — host memory over PCIe (~25 GB/s) ≈ 0.3 s; network store (~10 GB/s) ≈ 0.8 s; local NVMe (~5 GB/s) ≈ 1.6 s
  • Verdict: reload wins when transfer is faster than prefill and the tier has capacity — likelier for long prompts, large models and fast links; pipelining transfer with compute narrows the gap further
  • System pieces: KV layer such as LMCache or a Mooncake-style distributed store, cache-aware routing, eviction policy and TTLs
  • Risks: stale caches after model, tokenizer or template changes (include them in the cache key), tenant isolation, storage cost, measure hit rate and TTFT before/after

In full

Yes, for this workload offloading and reloading is likely to win, but the arithmetic should decide it. For an 8B-class GQA model (32 layers, 8 KV heads of dimension 128) the KV cache is about 128 KiB per token in bf16, and a 60k-token contract needs about 7.3 GiB.

KV per token = 2 (K,V) x 32 layers x 8 heads x 128 dim x 2 B = 131,072 B = 128 KiB KV for prompt = 60,000 x 131,072 B = 7.86e9 B ≈ 7.3 GiB (7.9 GB) Recompute (prefill) weights term ≈ 2 x params x tokens = 2 x 8e9 x 6e4 = 9.6e14 FLOPs attention term ≈ 2 x layers x tokens^2 x d_model (causal) = 2 x 32 x (6e4)^2 x 4096 ≈ 9.4e14 FLOPs total ≈ 1.9e15 FLOPs -> ≈ 9.5 s at 200 TFLOP/s effective, ≈ 19 s at 100 TFLOP/s Reload 7.9 GB host memory over PCIe (~25 GB/s) ≈ 0.3 s network store (~10 GB/s) ≈ 0.8 s local NVMe (~5 GB/s) ≈ 1.6 s

At this length the attention term roughly doubles the simple 2 x params x tokens estimate, which strengthens the case. Reload wins whenever the transfer is faster than prefill and the tier has capacity, which is likelier for long prompts, large models and fast links, and pipelining the transfer layer by layer with compute narrows the gap further, so even NVMe is competitive here. I would put a KV layer such as LMCache or a Mooncake-style distributed store in front of the GPUs, with cache-aware routing so requests land where the cache lives, plus an eviction policy and TTLs so stale entries do not linger. The risks are stale caches after a model, tokenizer or chat-template change (the cache key must include them), tenant isolation of cached content, storage cost (7.9 GB per contract adds up quickly), and the need to measure hit rate and TTFT before and after rollout rather than assume the win.

Corrected by an independent AI reviewer.

What a strong answer covers

  • Computes cache size from the model config, e.g. 128 KiB per token × 60k tokens ≈ 7.5 GiB in bf16 for an 8B-class GQA model
  • Compares reload time (size ÷ effective bandwidth of the tier: tens of GB/s for host memory over PCIe, a few GB/s for local NVMe, network for remote stores) with recompute time (≈ 2 × params × tokens ÷ effective FLOP/s)
  • Concludes that reloading wins when the transfer is faster than prefill and the tier has capacity, which is likelier for long prompts, large models and fast links; pipelining transfer with compute narrows the gap further
  • Names system pieces: a KV layer such as LMCache or a Mooncake-style distributed store, cache-aware routing so requests land where the cache is, eviction policy and TTLs
  • Covers risks: stale caches after model or prompt changes, tenant isolation of cached content, storage cost, and measuring hit rate and TTFT before and after

Common mistakes

  • Assuming any cache hit is faster without comparing transfer and recompute time
  • Forgetting that a different model version or chat template invalidates the cache

Follow-ups

  • How does the answer change for a hybrid linear-attention model?