KV-cache size

Inference · Formula · Appears in 8 lessons

MKV=2⋅L⋅H⋅dh⋅S⋅B⋅bM_{\text{KV}} = 2 \cdot L \cdot H \cdot d_h \cdot S \cdot B \cdot b

Every generated token must keep its keys and values so later tokens can attend to them. The cache grows linearly with sequence length and batch size, and at long contexts it, not the weights, is what fills the GPU.

Symbols

LL
The number of layers.
HH
The number of key/value heads.
dhd_h
The dimension of each head.
SS
The sequence length so far.
BB
The batch size.
bb
Bytes per value — two for fp16.
Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark