This document describes the mathematical foundation for calculating the total memory requirements of Large Language Models (LLMs) in GGUF format during inference. The total memory consumption consists of two primary components: model weights and KV cache.
The total memory required for inference is given by:
where:
-
$M_{\text{total}}$ = Total memory requirement (in bytes) -
$M_{\text{model}}$ = Memory occupied by model weights (in bytes) -
$M_{\text{KV}}$ = Memory occupied by Key-Value cache (in bytes) -
$M_{\text{overhead}}$ = Runtime overhead for inference engine buffers (in bytes)
Important (MoE Architectures): For Mixture-of-Experts models (e.g., Mixtral), all memory calculations use the total parameter count, not the active parameter count. While computational cost scales with active parameters, memory footprint requires all expert weights to remain resident.
The overhead component models memory required for inference engine context, scratch buffers, and temporary activation tensors:
where:
-
$P$ = Total model parameter count (in billions) -
$\alpha \approx 0.02$ GB/B (per-parameter overhead) -
$\beta \approx 0.15$ GB (fixed engine overhead)
The model memory is determined by the size of the quantized weight file(s):
where:
-
$S_i$ = Size of the$i$ -th model shard file (in bytes) -
$n$ = Number of split/shard files
For single-file models:
For multi-shard models: The metadata field split.count indicates the total number of shards, and all shards must be summed.
The KV cache stores intermediate key and value tensors for each attention layer during inference. Its size depends on:
- Model Architecture Parameters
- Context Length
- Quantization Precision
The generalized formula using per-head dimensions is:
where:
-
$n_{\text{layers}}$ = Number of transformer layers (from GGUF metadata:*.block_count) -
$n_{\text{heads}}^{\text{KV}}$ = Number of key-value attention heads (from GGUF metadata:*.attention.head_count_kv) -
$d_{\text{head}}$ = Dimension per attention head =$d_{\text{model}} / n_{\text{heads}}$ -
$C$ = Context size in tokens (user-specified) -
$b_{\text{KV}}$ = Bytes per value for KV cache quantization (user-specified) - Factor of 2 accounts for both Key and Value tensors
Simplification Using Hidden Size
Using the relationship
where *.attention.head_count).
For models with Grouped Query Attention (GQA) or Multi-Query Attention (MQA), the KV heads may differ from the query heads. The effective KV dimension is:
Thus, the KV cache formula becomes:
Since *.embedding_length in GGUF metadata) is directly available, the implementation uses:
Note: This assumes the ratio
The precision of the KV cache significantly impacts memory usage. Supported quantization formats:
| Format | Precision | Bytes per Value | Bytes per KV-pair ( |
|---|---|---|---|
| FP32 | 32-bit floating point | 4.0 | 8.0 |
| FP16/BF16 | 16-bit floating point | 2.0 | 4.0 |
| INT8 | 8-bit integer | 1.0 | 2.0 |
| Q6 | 6-bit quantized | 0.75 | 1.5 |
| Q5 | 5-bit quantized | 0.625 | 1.25 |
| Q4 | 4-bit quantized | 0.5 | 1.0 |
Note: "Bytes per Value" is storage for a single scalar in one tensor (K or V). "Bytes per KV-pair" is the combined storage (K+V) at one position. The implementation uses Bytes per KV-pair directly, absorbing the factor of 2 from the formula.
The calculator extracts the following parameters from GGUF file metadata:
| Parameter | GGUF Metadata Key | Symbol | Description |
|---|---|---|---|
| Attention Heads | *.attention.head_count |
Total number of query heads | |
| KV Heads | *.attention.head_count_kv |
Number of key-value heads (for GQA/MQA) | |
| Hidden Layers | *.block_count |
Number of transformer blocks/layers | |
| Hidden Size | *.embedding_length |
Model embedding dimension | |
| Split Count | split.count |
Number of model shards (optional) |
Fallback Logic:
- If
*.attention.head_count_kvis not present, the calculator assumes$n_{\text{heads}}^{\text{KV}} = n_{\text{heads}}$ (standard MHA).
- Model file size:
$M_{\text{model}} = 15{,}000$ MB - Context size:
$C = 8{,}192$ tokens - Hidden layers:
$n_{\text{layers}} = 32$ - Hidden size:
$d_{\text{model}} = 4{,}096$ - KV cache quantization: FP16 (
$b_{\text{KV}} = 4.0$ bytes per KV-pair) - Parameter count:
$P = 13$ billion
Result: The model requires approximately 19.7 GB of memory for inference at 8K context.
The calculator uses the following conversion:
For models distributed across multiple files (shards):
- Parse split pattern from filename:
*-XXXXX-of-YYYYY.gguf - Read
split.countfrom metadata of the first shard - Sum file sizes:
$M_{\text{model}} = \sum_{i=1}^{n} S_i$ - Calculate KV cache using parameters from any shard (all shards share the same architecture)
For remote files (URLs), the calculator:
- Uses HTTP Range requests to read only the file header and metadata
- Extracts file size from
Content-LengthorContent-Rangeheaders - Minimizes data transfer by aborting after metadata extraction
To reduce
-
Decrease context size (
$C$ ): Linear relationship$$M_{\text{KV}} \propto C$$ -
Use aggressive KV quantization (
$b_{\text{KV}}$ ): Linear relationship- Switching from FP16 (4.0 bytes) to Q4 (1.0 bytes) reduces KV cache by 75%
-
Model architecture selection: Choose models with fewer layers (
$n_{\text{layers}}$ ) or smaller hidden size ($d_{\text{model}}$ )
| Optimization | Memory Savings | Quality Impact |
|---|---|---|
| Reduce context size | High | None (within capacity) |
| KV quantization (Q8) | Moderate | Minimal |
| KV quantization (Q4) | High | Moderate (precision loss) |
| Smaller model | High | Significant (capacity loss) |
- GGUF Format Specification: ggml/docs/gguf.md
- Transformer Architecture: Vaswani et al., "Attention Is All You Need" (2017)
- KV Cache Optimization: Pope et al., "Efficiently Scaling Transformer Inference" (2022)
- v1.0 (2025-10-02): Initial documentation with mathematical formulation and examples