AI Compass
Compass

Memory and bandwidth

The calculation that precedes every hardware purchase: how much memory a model needs, what the KV cache costs, and where the real limit sits.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

The idea

A model must fit entirely into the graphics card's memory. If it does not, data is swapped, and access over the system bus is about fifty times slower. The practical consequence is not "a bit slower" but "unusable".

The rule of thumb

FormatBytes per parameter7 bn13 bn70 bn
float32428 GB52 GB280 GB
float16214 GB26 GB140 GB
int817 GB13 GB70 GB
int40.53.5 GB6.5 GB35 GB

Add 10 to 20 percent runtime overhead plus the context memory.

The KV cache

This is the item routinely missing from plans.

Size of the KV cache

KV = 2 · L · s · h_kv · b · n

For every layer, a key and a value are stored per token, times the number of concurrent requests.

L
number of layers
s
sequence length in tokens
h_kv
width of key and value vectors per layer
b
bytes per value, 2 for float16
n
number of concurrent requests

Worked through for a model with 32 layers, h_kv = 4096, float16:

Context1 request8 requests32 requests
4,096 tokens2.1 GB17.2 GB68.7 GB
32,768 tokens17.2 GB137 GB550 GB

At 32,000 tokens of context the cache is already larger than the 7-billion model itself. Which is exactly why modern architectures use grouped queries: with eight query heads sharing one key-value head, h_kv falls by eight and the cache with it.

  • Budget model, cache and a safety margin separately.
  • Cap the maximum context length in production, or a single user determines capacity.
  • With tight memory, quantise the cache first; it costs less quality than int4 weights.

The roofline model

Achievable performance

P = min( P_max , B · I )

Achievable performance is the minimum of peak compute and what bandwidth allows at that intensity.

P
actually achievable compute
P_max
peak compute of the card
B
memory bandwidth
I
arithmetic intensity in FLOPs per byte

For a card with P_max = 1,000 TFLOP/s and B = 3,000 GB/s the knee sits at I = 333 FLOPs per byte. Typical values:

OperationArithmetic intensityLimited by
Generating a single token~2Bandwidth
Generation with batch 64~128Bandwidth
Generation with batch 512~1,000Compute
Prompt processing (prefill)~500Compute
Training~2,000Compute

The table explains an often surprising observation: processing a long prompt runs at full compute while the subsequent token-by-token generation barely loads the card. The two phases have different bottlenecks and are planned separately in modern systems.

A complete sizing calculation

Requirement: a 13-billion-parameter model, 8,000 tokens of context, 16 concurrent requests, float16 for weights and cache.

ItemCalculationMemory
Weights13e9 × 2 B26.0 GB
KV cache2 × 40 × 8,000 × 5,120 × 2 B × 16104.9 GB
Activations and buffers~10 %13.1 GB
Total144.0 GB

That does not fit an 80 GB card. Three ways out, in order of effect:

  • Grouped queries in the model, factor 8 on the cache: from 104.9 down to 13.1 GB.
  • Cache in int8, factor 2: halved again.
  • Cap context at 4,000 tokens, factor 2 on the cache.
  • Weights in int8, factor 2: from 26.0 down to 13.0 GB.

With the first two measures the total lands around 46 GB and fits comfortably. That order is typical: the cache is nearly always a bigger lever than the weights.

Related courses and sources

PaperFreeEN

FlashAttention

Computing attention without ever materialising the quadratic matrix. The precondition for long contexts.

For anyone running long contexts who needs to know what memory hangs on.

ToolFreeEN

vLLM

A high-throughput server with continuous batching and paged attention cache. The reason self-hosting becomes economical at volume.

For self-hosting at volume; the reason it can pay off at all.

Was this page helpful?
Memory and bandwidth