Does the model fit in memory?
The sum behind our VRAM explorer: weights, KV cache per slot and an allowance for buffers, against usable memory.
Write memoryNeededGb(params, quant, kvBytesPerToken, contextTokens, slots), which returns the GB needed:
- weights:
params(billions)× BITS_PER_WEIGHT[quant] / 8; - KV cache:
kvBytesPerToken × contextTokens × slots / 1e9(a server reserves one cache per slot); - plus
computeAllowanceGb(params, slots), the allowance for compute buffers.
Also write fitStatus(neededGb, usableGb): return 'fits' when the need is <= 85 % of usable memory, 'tight' when it fits but goes past 85 %, and 'no' when it does not fit. Do not round anything: the results must match the explorer's.
Challenges 0/4
- Llama 3.1 8B at Q4 with 8192 tokens and one slot needs 6.48 GB
- Mistral Small 24B at Q4 with 8192 tokens × 8 slots: 26.24 GB (Ollama reported 25.64)
- fits, tight and no verdicts around the 85 % line
- On a DGX Spark (122 GB usable) the 70B does not fit in fp16 but fits in Q8
function memoryNeededGb(params, quant, kvBytesPerToken, contextTokens, slots) {
const weights = (params * BITS_PER_WEIGHT[quant]) / 8;
// add the KV cache (one per slot) and the compute-buffer allowance
return weights;
}
function fitStatus(neededGb, usableGb) {
// 'fits' up to 85 % of usableGb, 'tight' up to 100 %, otherwise 'no'
return neededGb <= usableGb ? 'fits' : 'no';
}
// Llama 3.1 8B at Q4, 8192 tokens of context, one slot.
// KV cache per token: 32 layers x 8 KV heads x 128 x 4 bytes = 131072 bytes
console.log(memoryNeededGb(8.03, 'q4', 131072, 8192, 1));Tab indents, Ctrl+Z undoes. To leave the editor with the keyboard: Esc, then Tab.
Why this matters
Before you download a model of several gigabytes it helps to know whether it fits. The sum has three terms: the weights, which depend on the parameters and the quantisation; the KV cache, which grows with the context and with the parallel slots; and an allowance for compute buffers. The third status, tight, is the most misleading: the model loads, but as the conversation grows it starts using shared memory and turns slow. It is the sum behind our VRAM explorer, which we checked against 13 real loads: there you can try it with your card and your model.
Go deeper: the related tool →
This in production, with your data? Let's talk for 15 minutes →