Calculate VRAM Requirements Before Deploying LLMs Locally

    This article explains why GPU memory (VRAM) is the primary bottleneck for running large language models locally, how to estimate weight memory and KV cache usage, what additional overheads exist in practice, and which model sizes fit into 8 GB, 16 GB, and larger GPUs, with a simple Python script for quick calculation.

    Why VRAM Comes First

    When running large language models locally, VRAM determines three things: whether the model can fit, how many tokens can be generated per second, and how long a context can be handled. CPU, motherboard, and hard drive also matter, but they only come into play once the VRAM requirement is satisfied. If VRAM is insufficient, some data gets swapped to system memory, generation speed drops noticeably, and in severe cases, the program exits with an error.

    VRAM is the primary bottleneck, but generation speed is also affected by memory bandwidth and GPU compute capacity. When choosing hardware, first use VRAM to narrow down the models that can run, then compare bandwidth and price.

    How to Calculate Weight Memory

    Weight usage can be estimated with the following formula:

    Weight VRAM (GB) = Number of parameters (in billions) × Bits per weight ÷ 8

    Dividing the bits per weight by 8 gives the number of bytes per parameter. FP16 uses 16 bits, i.e., 2 bytes; INT8 uses 8 bits, i.e., 1 byte; INT4 uses 4 bits, i.e., 0.5 bytes.

    • FP16: A 7B model takes about 14 GB, a 9B model about 18 GB.
    • INT8: A 7B model takes about 7 GB, a 9B model about 9 GB.
    • INT4: A 7B model takes about 3.5 GB, a 9B model about 4.5 GB, a 27B model about 13.5 GB.

    Here GB is in decimal, meaning 1 GB equals 10^9 bytes. The next section on KV Cache uses binary units MiB and GiB, where 1 GiB is approximately 1.074 GB. These units should not be treated as identical when converting.

    The numbers above include only the weights. Actual runtime also requires additional memory for KV Cache, framework overhead, and a safety margin.

    KV Cache: The Part That Grows with Context

    During inference, the model caches the Key and Value of every processed token. This cache is called the KV Cache. Its size depends on three factors: context length, batch size (the number of concurrent requests), and the model architecture.

    The KV Cache bytes per token can be calculated with:

    KV bytes per token = 2 × number of layers × number of KV heads × head dimension × bytes per value

    Where:

    • 2: One copy for Key and one for Value.
    • Layers: Number of Transformer layers. This is found in the model’s config.json, usually under num_hidden_layers.
    • KV heads: The number of Key/Value heads actually stored per layer, usually under num_key_value_heads.
    • Head dimension: The vector length of each attention head, usually under head_dim. If not present, it can be derived by dividing hidden_size by num_attention_heads.
    • Bytes per value: 2 for FP16, 1 for INT8.

    For a hypothetical architecture with 32 layers, 8 KV heads, head dimension 128, and FP16 storage, each token requires 131072 bytes, or 128 KiB. With a 4096-token context, this is about 512 MiB; with 8192 tokens, about 1 GiB. If batch size is 4, the same 4096-token context requires about 2 GiB.

    KV Cache scales linearly with context length and batch size. Doubling the context doubles the KV Cache. Increasing batch from 1 to 4 multiplies the KV Cache by 4 as well.

    How Grouped Query Attention (GQA) Reduces KV Cache

    In standard multi-head attention (MHA), the number of KV heads equals the number of attention heads. Grouped Query Attention (GQA) lets multiple query heads share the same set of Keys and Values, so the number of KV heads is far smaller than the number of attention heads. With a smaller KV head count in the formula, the KV Cache shrinks accordingly.

    Using 32 layers, head dimension 128, and FP16:

    • MHA (32 KV heads): each token takes 2 × 32 × 32 × 128 × 2 = 524288 bytes, or 512 KiB.
    • GQA (8 KV heads): each token takes 2 × 32 × 8 × 128 × 2 = 131072 bytes, or 128 KiB.

    The difference is 4×. When selecting a model, you can compare the attention head count and KV head count in config.json. The larger the gap, the more VRAM is saved in long-context scenarios.

    Additionally, some frameworks support quantizing the KV Cache. Reducing the bytes per value from 2 to 1 cuts the KV portion in half, but this requires framework support and may affect output quality.

    Ready-to-Run VRAM Estimation Script

    Requirements: Python 3.x. This script only performs arithmetic; it does not require a GPU or third-party libraries. Save the following as vram_estimate.py and run it with python vram_estimate.py.

    1. def estimate_vram_gb(params_b, weight_bits, layers, kv_heads, head_dim,
    2. ctx_len, batch=1, kv_bytes=2,
    3. fixed_overhead_gb=1.0, margin=0.10):
    4. """Estimate the VRAM needed for inference, in decimal GB.
    5. params_b: Number of parameters in billions (e.g., 7 for a 7B model)
    6. weight_bits: Weight quantization bits, e.g., 16, 8, 4
    7. kv_bytes: Bytes per value in KV Cache; 2 for FP16, 1 for INT8
    8. fixed_overhead_gb: Fixed overhead, including CUDA context and framework usage
    9. margin: Proportional buffer to cover activations and memory fragmentation
    10. """
    11. weight_gb = params_b * weight_bits / 8
    12. kv_per_token = 2 * layers * kv_heads * head_dim * kv_bytes
    13. kv_gb = kv_per_token * ctx_len * batch / 1e9
    14. total_gb = (weight_gb + kv_gb) * (1 + margin) + fixed_overhead_gb
    15. return {"weight_gb": weight_gb, "kv_gb": kv_gb, "total_gb": total_gb}
    16. if __name__ == "__main__":
    17. r = estimate_vram_gb(params_b=7, weight_bits=4, layers=32,
    18. kv_heads=8, head_dim=128, ctx_len=4096, batch=1)
    19. print(f"Weights: {r['weight_gb']:.2f} GB")
    20. print(f"KV Cache: {r['kv_gb']:.2f} GB")
    21. print(f"Estimated total VRAM: {r['total_gb']:.2f} GB")

    Expected output:

    1. Weights: 3.50 GB
    2. KV Cache: 0.54 GB
    3. Estimated total VRAM: 5.44 GB

    The example values (32 layers, 8 KV heads, head dimension 128) are for demonstration only. When using a real model, replace them with the values from that model’s config.json. The result above shows that a 7B model with INT4 quantization and a 4K context needs about 5.4 GB of VRAM, which fits on an 8 GB GPU.

    Other Overheads in Practice

    The formula gives a lower bound and should not be treated as a purchasing standard. At runtime, the following overheads also exist:

    • Framework and CUDA context: PyTorch, CUDA libraries, or the inference engine consumes a fixed amount of VRAM after startup, typically around 0.5 to 1.5 GB.
    • Activations: Intermediate results from computation grow with input length and batch size, especially noticeable with long prompts.
    • Memory fragmentation: After repeated allocation and deallocation, free space becomes fragmented, so the usable amount is less than the reported total.
    • Desktop usage: If the same GPU is also driving a display, the graphical interface also occupies some VRAM.
    • Safety margin: It is recommended to add 10–20% on top of the estimated total.

    In the script, fixed_overhead_gb and margin correspond to the first two and the last two items respectively. If the inference engine uses a different pre-allocation strategy, rely on measured results.

    Selection Guide for 8 GB, 16 GB, and Higher GPUs

    The table below assumes a 4K context, batch size of 1, FP16 KV Cache, and the default overhead from the script. These are reference ranges, not guaranteed outcomes.

    GPU VRAM Model size (reference) Recommended quantization Context & batch reference limit Recommended system RAM
    8 GB 7B–9B INT4 4K context, batch 1 16 GB or more
    8 GB 3B–4B INT8 4K context, batch 1 16 GB or more
    16 GB 14B INT4 8K context, batch 1–2 32 GB