Part 2 of 12
How Much Memory Does a Model Need? A Data Brief
Data briefBy Bot Tutorials · Published · Facts last verified Nov 3, 2026
Part 2: estimate memory from parameter count and precision with a tested script, use Hugging Face's training figures, and let a bot sanity-check it.
Before you choose a GPU, rent a cloud machine or download a large model, answer one question: will it fit in memory? The rough answer comes from two numbers, the model's parameter count and the precision you load it in. This brief shows the arithmetic, a small tested estimator, and how to get a bot to check your working instead of guessing.
What the bot can and can't do here
It can:
- Do the arithmetic and explain each term.
- Read a model card and find the parameter count and stored precision.
- Point out what an estimate leaves out, like activations or caches.
It can't:
- Tell you the exact memory a run will use. That depends on your code, batch size and library version.
- Benchmark hardware. Any speed figure it gives you without a source is a guess.
- Know a model's size unless you paste the model card or file list.
Check first, buy second
Estimate on paper, then test small on the hardware you already have, and only then spend money. A model that needs 14 GB for weights won't fit in 12 GB, and no setting will change that.
The arithmetic
Weights take parameters × bytes per parameter. Common sizes are 4 bytes for fp32, 2 for fp16 or bf16, 1 for int8 and half a byte for 4-bit formats.
For training, Hugging Face's "model training anatomy" page breaks memory down for mixed-precision training with Adam:
- Weights: 6 bytes per parameter, because mixed precision keeps an fp16 copy and an fp32 main copy.
- Optimizer states: 8 bytes per parameter for Adam's momentum and variance in fp32.
- Gradients: 4 bytes per parameter in fp32.
- Activations: extra, and they grow with batch size, sequence length, depth and hidden size.
That's 18 bytes per parameter before activations. The same page notes that a quantized Adam from bitsandbytes can cut optimizer states to 2 bytes per parameter.
A tested estimator
"""Rough memory estimates from parameter counts. Weights only for inference;
training uses the Hugging Face 'model memory anatomy' figures for mixed-precision AdamW:
6 (weights) + 8 (optimizer) + 4 (gradients) = 18 bytes per parameter, before activations."""
BYTES = {"fp32": 4, "fp16": 2, "bf16": 2, "int8": 1, "int4": 0.5}
TRAIN_MIXED_ADAMW = 18
GB = 1e9
def inference_gb(params, dtype="fp16"):
return params * BYTES[dtype] / GB
def training_gb(params):
return params * TRAIN_MIXED_ADAMW / GB
def count_params(model):
return sum(p.numel() for p in model.parameters())
if __name__ == "__main__":
for n in (125e6, 1e9, 7e9):
row = ", ".join(f"{d} {inference_gb(n, d):.2f}" for d in BYTES)
print(f"{n/1e9:g}B params | weights GB: {row} | train (mixed AdamW, no activations) {training_gb(n):.1f}")
import torch.nn as nn
m = nn.Sequential(nn.Linear(784, 256), nn.ReLU(), nn.Linear(256, 10))
n = count_params(m)
print("tiny MLP params:", n, "| fp32 weights MB:", round(n * 4 / 1e6, 2))
Real output from our box:
0.125B params | weights GB: fp32 0.50, fp16 0.25, bf16 0.25, int8 0.12, int4 0.06 | train (mixed AdamW, no activations) 2.2
1B params | weights GB: fp32 4.00, fp16 2.00, bf16 2.00, int8 1.00, int4 0.50 | train (mixed AdamW, no activations) 18.0
7B params | weights GB: fp32 28.00, fp16 14.00, bf16 14.00, int8 7.00, int4 3.50 | train (mixed AdamW, no activations) 126.0
tiny MLP params: 203530 | fp32 weights MB: 0.81
The last line counts the parameters of a real PyTorch model on our machine: a two-layer network has 203,530 parameters, which is about 0.81 MB in fp32. You can check that by hand: 784 × 256 + 256 + 256 × 10 + 10 = 203,530.
The tests:
from mem_estimate import inference_gb, training_gb, count_params
import torch.nn as nn
def test_inference():
assert inference_gb(1e9, "fp32") == 4
assert inference_gb(1e9, "fp16") == 2
assert inference_gb(1e9, "int4") == 0.5
def test_training():
assert training_gb(1e9) == 18
def test_count():
assert count_params(nn.Linear(10, 5)) == 55
Result: 3 passed in 0.97s.
Reading the numbers
A 7-billion-parameter model needs about 14 GB just for fp16 weights, and about 126 GB for mixed-precision Adam training before activations. That's why people fine-tune large models with methods that train far fewer parameters, or use quantized optimizers. For a 125-million-parameter model, the weights are a quarter of a gigabyte in fp16, and full training fits on far more machines.
These are floors. On a GPU, PyTorch's CUDA semantics notes explain that its caching allocator holds on to memory, so tools like nvidia-smi can show more in use than your tensors need. PyTorch's "Understanding CUDA Memory Usage" page shows how to record and inspect memory snapshots when you want real numbers.
The prompt
Here's the model card and file list for a model (pasted). Find the parameter count and stored precision, quoting the lines you used. Then estimate weight memory in fp16 and int8, and training memory using 18 bytes per parameter from Hugging Face's training anatomy page. List what the estimate leaves out. Don't give speed figures.
Picking hardware from the estimate
- Fits with headroom: if the weights take under roughly two-thirds of your memory, try a small run and measure.
- Barely fits: expect out-of-memory errors once activations arrive. Lower the batch size or sequence length first.
- Doesn't fit: use a smaller model, lower precision for inference, or a machine with more memory. Check the GPU's real memory on the manufacturer's spec page, not from a bot's memory.
Tested vs documented
- Tested by us: the estimator, its three tests, and the parameter count of a real PyTorch model, all on a CPU-only box.
- Documented, not tested by us: the training memory breakdown, from Hugging Face's page, and GPU memory behaviour, from PyTorch's docs.
- Not claimed: any speed or benchmark result.
Where to go next
The last part today pins your library versions so this week's working setup still works next month.
Safety, licensing and privacy: read this before you run anything
This series is about learning and small personal projects with machine learning libraries. A bot is a helper, not an authority.
- Check bot advice against official docs. Version numbers, install commands and compatibility tables change often. Compare every answer with the PyTorch, TensorFlow, JAX, scikit-learn, NVIDIA, Hugging Face or ONNX Runtime pages we link. If the bot and the docs disagree, the docs win.
- Work in a virtual environment. Don't install or remove packages in your system Python, and don't run
sudo pip. - Run untrusted files carefully. Pickle-based model files can run code when loaded. Only load models from sources you trust, and prefer safer formats like safetensors where the library supports them.
- Respect licenses. Datasets and model weights carry their own licenses and terms. Read the model card and license before you use, share or sell anything built on them. We don't give legal advice; ask a qualified person if it matters.
- Protect personal data. Don't paste private, customer or medical data into a bot or a training set unless you have the right to, and check the bot's own privacy settings first.
- No hardware claims you didn't measure. Treat any speed figure from a bot as a guess until you've timed it on your own machine.
- No medical, legal or financial decisions from a toy model. These guides are for learning only.
FAQ
Why 18 bytes per parameter for training?
Hugging Face's model training anatomy page gives 6 bytes for weights in mixed precision, 8 bytes for Adam's optimizer states and 4 bytes for gradients. Activations come on top and depend on batch size and sequence length.
Is the inference estimate exact?
No. It covers the weights only. Real use adds activations, caches and framework overhead, so leave headroom and measure.
Did you benchmark any GPUs?
No. We ran the estimator and its tests on a CPU-only box. We make no speed claims.
Sources
- Hugging Face Transformers: Model training anatomy
- PyTorch docs: Understanding CUDA Memory Usage
- PyTorch docs: CUDA semantics
- Hugging Face Hub: Model Cards
- NVIDIA: CUDA GPU Compute Capability
- pytest documentation
More in this series
- Part 1: GPU, Driver and CUDA Compatibility: A Bot-Assisted Check · Guide
- Part 2: How Much Memory Does a Model Need? A Data Brief (you are here) · Data brief
- Part 3: Pin Versions and Build Reproducible ML Environments · Walkthrough
- Part 4: NumPy and pandas Basics with a Bot: Cheat Sheet · Cheat sheet
- Part 5: Your First scikit-learn Model, Built with a Bot · Walkthrough
- Part 6: PyTorch on the CPU: First Steps with a Bot · Guide
- Part 7: TensorFlow and Keras Install Requirements, Explained · Explainer
- Part 8: JAX on the CPU: Field Notes with a Bot · Field notes
- Part 9: Read Hugging Face Model Cards and Licenses with a Bot · Guide
- Part 10: Export a Model to ONNX and Run It: A Deep Dive · Deep dive
- Part 11: Read ML Error Messages and Profile Code: Cheat Sheet · Cheat sheet
- Part 12: Review Your Training Code with a Bot: Checklist · Checklist
See the ML Libraries with a Bot series hub · Latest on Bot Tutorials
Bot advice can be wrong. We cite official docs; check them yourself before acting, especially for anything involving money, safety or your data.