Transformer memory arithmetic

What a model costs in bytes: weights, KV cache, activations and optimiser state, and how to tell in advance whether it fits on the card you have.

Weights, by dtype

Bytes per parameterfp32 4 · bf16/fp16 2 · fp8/int8 1 · int4 0.5bf16 is the default for training and serving. fp16 has the same size but a much narrower exponent range, which is why loss spikes and overflow are an fp16 problem more than a bf16 one.
Weight memoryparameters (billions) × bytes per parameter = GBThe whole rule. 7B in bf16 is 14 GB; 70B in bf16 is 140 GB; 70B in int4 is about 35 GB.
Usable card memoryabout 90-95% of the nameplate figureThe CUDA context, the allocator and fragmentation take the rest. Plan against 72 GB on an 80 GB card, not 80.
Serving footprintweights + KV cache + 1-2 GB runtime overheadLeave 10-20% headroom on top, or the first long request evicts something and latency becomes erratic.

KV cache

Bytes per token2 × layers × kv_heads × head_dim × bytes_per_elementThe 2 is key and value. kv_heads is the grouped-query figure, which is often 8 while attention heads number 32 or more — that difference is most of why modern models serve long contexts affordably.
8B-class model, GQA, bf16about 128 KiB per token32 layers, 8 kv heads, head_dim 128: 2 × 32 × 8 × 128 × 2 = 131,072 bytes. An 8k-token sequence is 1 GiB of cache.
70B-class model, GQA, bf16about 320 KiB per token80 layers, 8 kv heads, head_dim 128. A single 32k conversation costs roughly 10 GiB of cache on top of the weights.
Same model without GQA4× larger, at 32 kv heads instead of 8512 KiB per token for the 8B shape. Older multi-head models are the reason long context used to be prohibitive.
Total cachebytes_per_token × sequence_length × concurrent_sequencesThis is what actually caps your batch size in production, not the weights. Batch 32 at 8k tokens on the 8B shape is 32 GiB.
KV cache quantizationfp8 halves it; int4 quarters itQuality loss is usually smaller than for weight quantization, but it lands on long-context retrieval first — test on your longest inputs, not your average ones.

Training memory

Mixed precision with Adamabout 16 bytes per parameterbf16 weights (2) + fp32 master copy (4) + fp32 first moment (4) + fp32 second moment (4) + gradients (2). A 1B model needs roughly 16 GB before a single activation.
Full fine-tune of 7Babout 112 GB of state, plus activationsWhich is why a full tune of a 7B model does not fit on one 80 GB card, and why sharding or LoRA is the usual answer.
SGD with momentumabout 4-6 bytes per parameter less than AdamOne moment instead of two. Rarely used for transformers, because Adam's per-parameter scaling is doing real work there.
LoRAoptimiser state on adapters only, typically under 1% of parametersThe frozen base can also be 4-bit (QLoRA), so a 7B tune fits in about 6-10 GB of weights plus activations — one 24 GB card.
Activationsroughly batch × sequence × hidden × layers × a constant of a few tensThe constant depends on how many intermediate tensors the implementation keeps. Treat it as measured, not derived: run one step and read the allocator.
Gradient checkpointingactivation memory down to roughly the square root of layer count, for about 20-30% more computeStore activations at a few boundaries and recompute the rest in the backward pass. Almost always worth it when it is the difference between fitting and not.

What fits

24 GB (4090, A10G, 3090)7-8B bf16 inference at short context · 13B at 4-bit · QLoRA tune of 7BContext length is what runs you out, not the weights. 14 GB of weights leaves under 10 GB of cache.
48 GB (L40S, A6000)13B bf16 · 34B at 4-bit · comfortable QLoRA of 13BThe common single-card serving size. One card holds a mid-size model with enough cache for real concurrency.
80 GB (A100, H100)70B at 4-bit · 34B bf16 · full fine-tune of a model up to roughly 3B70B bf16 needs two of these for weights alone, and realistically four once you want batch and context.
8 × 80 GB node70B bf16 served with real batch and long context · full fine-tune of 7-13B with shardingAbove this you are in multi-node territory, where interconnect bandwidth starts deciding the design.

Figures last checked 2026-09-13. Several of these age; check anything you are about to rely on.

Where these numbers come from