KV Cache Memory › Key Parameters

Michael BrenndoerferJanuary 7, 20261 min read
Language AI HandbookAll chapters
Part 42 of 67Inference Optimization
Chapter 314KV Cache Memory › Key Parameters
Reading section 12 of 14 · Key Parameters · 1 minSection 12 of 14 · 1 min

Book contents

Section 12 of 14 · 1 min read

KV Cache Memory › Key ParametersOne section · Sections in this chapter12 / 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

The following parameters are essential for KV cache memory calculation:

  • num_layers: Number of transformer layers in the model. Each layer maintains its own key and value cache, so cache memory scales linearly with the number of layers.
  • num_kv_heads: Number of key-value attention heads, also written as HkvH_{\text{kv}}. In standard Multi-Head Attention, this equals the number of query heads. Grouped Query Attention uses fewer KV heads to reduce memory.
  • head_dim: Dimension of each attention head, also written as DhD_h. Typically 64, 96, or 128. Combined with number of heads, this determines the model dimension.
  • batch_size: Number of sequences processed simultaneously. Each sequence maintains its own independent cache, so memory scales linearly with batch size.
  • seq_len: Context length in tokens. Cache memory grows linearly as more tokens are processed and cached.
  • dtype_bytes: Bytes per element based on data type. FP16/BF16 use 2 bytes, FP32 uses 4 bytes, INT8 uses 1 byte, INT4 uses 0.5 bytes.