KV Cache Memory › Worked Example: Hand-Calculating LLaMA 7B Cache

Michael BrenndoerferJanuary 7, 20263 min read
Language AI HandbookAll chapters
Part 42 of 67Inference Optimization
Chapter 314KV Cache Memory › Worked Example: Hand-Calculating LLaMA 7B Cache
Reading section 4 of 14 · Worked Example: Hand-Calculating LLaMA 7B Cache · 3 minSection 4 of 14 · 3 min

Book contents

Section 4 of 14 · 3 min read

KV Cache Memory › Worked Example: Hand-Calculating LLaMA 7B CacheOne section · Sections in this chapter4 / 14 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Working through a concrete numerical example cements the formula and builds the intuition you need to estimate cache sizes quickly. Let us trace through the LLaMA 7B calculation step by step, so that every multiplication corresponds to a specific architectural decision.

LLaMA 7B has the following relevant specifications:

  • L=32L = 32 transformer layers
  • Hkv=32H_{\text{kv}} = 32 key-value heads (standard MHA, no GQA in the original LLaMA 7B)
  • Dh=128D_h = 128 head dimension
  • Inference in FP16: bytes=2\text{bytes} = 2

Step 1: Memory per element. Each floating-point value in the cache occupies 2 bytes in FP16.

Step 2: Elements per head per token. Each head stores one key vector of length Dh=128D_h = 128 and one value vector of length Dh=128D_h = 128. That gives 128+128=256128 + 128 = 256 elements per head per token.

Step 3: Elements across all heads per token per layer. With Hkv=32H_{\text{kv}} = 32 heads, one layer stores 32×256=8,19232 \times 256 = 8{,}192 elements per token.

Step 4: Elements across all layers per token. With L=32L = 32 layers, each token contributes 32×8,192=262,14432 \times 8{,}192 = 262{,}144 elements to the cache in total.

Step 5: Bytes per token across all layers. Multiplying by 2 bytes per element:

Bytes per token=2×L×Hkv×Dh×bytes=2×32×32×128×2=524,288 bytes=512 KB≈0.5 MB\begin{aligned} \text{Bytes per token} &= 2 \times L \times H_{\text{kv}} \times D_h \times \text{bytes} \\ &= 2 \times 32 \times 32 \times 128 \times 2 \\ &= 524{,}288 \text{ bytes} \\ &= 512 \text{ KB} \\ &\approx 0.5 \text{ MB} \end{aligned}

Step 6: Total cache for a sequence of 4,096 tokens. Multiplying by the sequence length:

Total Cache=T×bytes per token=4,096×524,288=2,147,483,648 bytes=2 GB\begin{aligned} \text{Total Cache} &= T \times \text{bytes per token} \\ &= 4{,}096 \times 524{,}288 \\ &= 2{,}147{,}483{,}648 \text{ bytes} \\ &= 2 \text{ GB} \end{aligned}

Step 7: Sanity check using the simplified MHA formula. Recall that for MHA, Hkv×Dh=DmodelH_{\text{kv}} \times D_h = D_{\text{model}}. For LLaMA 7B, Dmodel=32×128=4,096D_{\text{model}} = 32 \times 128 = 4{,}096. Plugging into the simplified formula:

Cache=2×L×B×T×Dmodel×bytes=2×32×1×4,096×4,096×2=2 GB\begin{aligned} \text{Cache} &= 2 \times L \times B \times T \times D_{\text{model}} \times \text{bytes} \\ &= 2 \times 32 \times 1 \times 4{,}096 \times 4{,}096 \times 2 \\ &= 2 \text{ GB} \end{aligned}

Both paths give the same answer, which is reassuring. At 4K tokens with a batch size of 1 in FP16, LLaMA 7B requires exactly 2 GB for its KV cache. This number is small enough to coexist with the model weights (roughly 14 GB in FP16) on a 24 GB GPU. But it doubles with every doubling of context length, and it multiplies linearly with batch size.

Step 8: Scaling to a different batch size. If we want to serve 8 concurrent requests (batch size 8):

Cache for batch 8=8×2 GB=16 GB\text{Cache for batch 8} = 8 \times 2 \text{ GB} = 16 \text{ GB}

Adding 14 GB for model weights and roughly 2 GB for other overhead, this already approaches the 32 GB limit of an A100 40 GB GPU, leaving only a small safety margin. This calculation, which takes under a minute once you know the formula, determines whether a deployment plan is feasible before you ever touch the hardware.