ml0x / calculators / llm memory calculator

LLM memory calculator

By Michael Lip. Model fields fetched and cross checked on 12 July 2026.

The head count most calculators get wrong

Most LLM memory calculators make two quiet mistakes. They ask you to type a parameter count you are guessing at, and they size the KV cache from num_attention_heads. The second one inflates the cache badly on modern models. Under grouped query attention the tensors that persist across decoding are the keys and values, and their count comes from num_key_value_heads, a different config field that is usually several times smaller. Pope et al. show that sharing key and value heads across query heads lowers memory requirements and lets inference scale to much longer contexts (arXiv 2211.05102).

Take Qwen2.5 7B. Its published config.json sets "num_attention_heads": 28 and "num_key_value_heads": 4. Open the file and check. A calculator that sizes the cache from the attention head count over-reports it by 7x for this model. ModelScope, an independent host run by a different operator, serves the same two values in its copy of the file, so this is not a typo on one hub.

The parameter counts here are fetched, not typed. Every preset carries the exact count from the safetensors.total field of that model's Hugging Face model-info record. Llama 3.1 8B, for example, reports 8030261248 parameters, which is 8.03 billion, a little over the name. Pick a preset and both numbers land in the calculator for you.

The calculator

Every result recomputes as you type, in your browser. Weights are the parameter count times the bytes per parameter you pick. The KV cache is per-token bytes times context length times batch, held at fp16, two bytes per value, doubled once because K and V are separate tensors (Pope et al., arXiv 2211.05102; Kwon et al., arXiv 2309.06180). Adam's optimizer states cost eight bytes per parameter, two fp32 moment estimates of four bytes each (Kingma and Ba, arXiv 1412.6980; Micikevicius et al., arXiv 1710.03740). The activation line is an estimate and says so, with its coefficient printed right next to it.

LineMemoryHow the number is computed

For a model outside the list, pick Custom and copy num_hidden_layers, num_key_value_heads and head_dim from its config.json. When head_dim is not published, divide hidden_size by num_attention_heads. Qwen2.5 7B, for instance, publishes "hidden_size": 3584 alongside its 28 attention heads, and the head dimension follows from that division.

Every preset, sized from its fetched count

One row per preset. The parameter column is the exact safetensors.total from the linked record, in billions. Weights columns use two bytes per parameter at fp16 and half a byte at int4. Adam states use eight bytes per parameter (Micikevicius et al., arXiv 1710.03740). The KV column assumes an fp16 cache, a single sequence, and a 128k context. And the over-report column is the reason this page exists. It shows how far a num_attention_heads calculator overstates that model's cache.

ModelParams (B)Q heads per KV headNaive KV over-reportWeights fp16 (GiB)Weights int4 (GiB)Adam states (GiB)KV cache at 128k (GiB)Count source
Llama 3.1 8B8.034300%14.963.7459.8316record
Llama 3.2 1B1.244300%2.30.589.214record
Qwen2.5 0.5B0.497600%0.920.233.681.5record
Qwen2.5 1.5B1.546500%2.880.7211.53.5record
Qwen2.5 3B3.098700%5.751.4422.994.5record
Qwen2.5 7B7.627600%14.193.5556.747record
Qwen2.5 14B14.775400%27.516.88110.0524record
Qwen2.5 32B32.765400%61.0315.26244.1132record
Qwen2.5 72B72.718700%135.4333.86541.740record
Qwen3 8B8.194300%15.263.8161.0318record
Qwen3 14B14.775400%27.516.88110.0320record
Qwen3 32B32.768700%61.0215.26244.132record
Mistral 7B v0.17.244300%13.493.3753.9616record
Mistral NeMo 12B12.254300%22.815.791.2520record
Mixtral 8x7B46.74300%86.9921.75347.9616record
Phi-3 mini 4k3.8210%7.121.7828.4748record
Phi-414.664300%27.316.83109.2225record
OLMo 2 7B7.310%13.593.454.3864record
SmolLM2 1.7B1.7110%3.190.812.7524record

The ratio is not one number across model families. Mistral 7B v0.1 publishes "num_key_value_heads": 8 against "num_attention_heads": 32 in its config.json. phi-4 publishes "num_key_value_heads": 10 against "num_attention_heads": 40 in its file. And OLMo 2 7B keeps full multi head attention, with "num_key_value_heads": 32 equal to its "num_attention_heads": 32 in its config, so the naive method happens to be right there. You cannot assume the correction away. Check the config, or use a calculator that already did.

A worked example with Llama 3.1 8B

Llama 3.1 8B reports 8030261248 parameters in safetensors.total (the model-info record). Serve it at fp16 and the weights take 14.96 GiB, the count times two bytes. The cache then adds 131072 bytes for every token in flight, 128 KiB each, so one full 128k-token sequence costs 16 GiB on top of the weights. Fine-tune the same model with Adam and the bill changes shape. Gradients at fp16 match the fp16 weights, another 14.96 GiB, and the optimizer's two fp32 moment tensors add 59.83 GiB at eight bytes per parameter (Micikevicius et al., arXiv 1710.03740). Mixed precision training also maintains a single-precision master copy of the weights that accumulates the gradients after each optimizer step, so budget four more bytes per parameter if your framework keeps one. Even with that, the same paper reports mixed precision training can reduce the memory consumption of deep learning models by nearly 2x against full fp32.

The KV cache, briefly

Kwon et al. put it plainly in the vLLM paper. The KV cache memory for each request is huge, and it grows and shrinks dynamically (arXiv 2309.06180). So at long contexts the context length input moves your total more than any other field on this page. Sizing the cache precisely, per model and per quantization, is its own topic, and this site already covers it in depth. The KV cache size calculator goes further than the summary here.

Common questions

Why another calculator shows a bigger KV cache

The other calculator probably multiplied by num_attention_heads. Grouped query attention stores keys and values for num_key_value_heads only, and the query heads share them (Pope et al., arXiv 2211.05102). The over-report column in the table shows the gap per model.

What Adam stores, and why it outweighs fp16 weights

Adam keeps adaptive estimates of lower-order moments of the gradients, two state values per parameter (Kingma and Ba, arXiv 1412.6980). Held in fp32 at four bytes each, that comes to eight bytes per parameter, four times what the fp16 weights themselves cost. The optimizer, not the model, dominates a training budget.

Whether int4 shrinks the whole budget

No, only the weights line. Quantizing weights to half a byte per parameter leaves this page's fp16 KV cache and the activation estimate untouched. At long contexts the cache can outweigh the quantized weights, and the table's 128k column makes that easy to spot.

Why the activation line is only an estimate

Runtimes differ in what they keep live. Fused kernels, activation recomputation, memory pooling and batching strategy all change the real footprint, so the calculator states its coefficient (two live values of d_model per layer per token, at two bytes each) instead of pretending to measure. Trust a profiler over any formula, this one included.

What mixed precision actually saves

Micikevicius et al. report that mixed precision training can reduce the memory consumption of deep learning models by nearly 2x, with weights, activations and gradients stored in IEEE half-precision format, sixteen bits per value (arXiv 1710.03740). The savings land in the weights and gradients lines here. The fp32 master copy and the Adam states stay full width.

Where these numbers come from

Nothing on this page is typed from memory. Parameter counts are read from each model's Hugging Face model-info record, the safetensors.total field, then cross checked against the model's safetensors byte footprint divided by its stored dtype width, and against the same footprint on ModelScope, an independent host. Per-token KV figures come from each model's own config.json, recomputed in code as two bytes at fp16, times two for K and V, times num_key_value_heads, times head_dim, times num_hidden_layers, following Pope et al. (arXiv 2211.05102) and Kwon et al. (arXiv 2309.06180). A value the two hosts disagree on is held back, and nothing gets published for it. Models whose attention layout the formula does not describe are excluded rather than approximated, so no preset carries a silently wrong number. Weights, gradient and optimizer results are pure multiples of the fetched count by the bytes-per-parameter constant shown beside each one, recomputed in your browser so you can reproduce every figure.

Every ml0x tool runs client side, and I check that the way you would, by watching the network panel while the page works. I ran that inspection across the live ml0x calculator pages on 10 July 2026 and the computation stayed on the machine. This page is built the same way.

Sources

Assumptions and limits