Bot TutorialsGrok BotChatGPTClaudeGeminiCopilotPerplexityMore botsGuidesArticlesPapers
Topics
CookingHome automationFitnessCreative writingMusic productionCodingIndustrialPros (Professional track)
Search

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

ML libraries series complete. "Review Your Training Code with a Bot: Checklist" was the finale. See every part in the series hub.

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.

Video: How Much Memory Does a Model Need? A Data Brief (4:32). Narrated from this guide, with the real code and real output we ran. Captions included (caption file). Prefer reading? Everything in the video is in the guide below.

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:

It can't:

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:

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

Tested vs documented

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.

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

  1. Hugging Face Transformers: Model training anatomy
  2. PyTorch docs: Understanding CUDA Memory Usage
  3. PyTorch docs: CUDA semantics
  4. Hugging Face Hub: Model Cards
  5. NVIDIA: CUDA GPU Compute Capability
  6. pytest documentation

More in this series

  1. Part 1: GPU, Driver and CUDA Compatibility: A Bot-Assisted Check · Guide
  2. Part 2: How Much Memory Does a Model Need? A Data Brief (you are here) · Data brief
  3. Part 3: Pin Versions and Build Reproducible ML Environments · Walkthrough
  4. Part 4: NumPy and pandas Basics with a Bot: Cheat Sheet · Cheat sheet
  5. Part 5: Your First scikit-learn Model, Built with a Bot · Walkthrough
  6. Part 6: PyTorch on the CPU: First Steps with a Bot · Guide
  7. Part 7: TensorFlow and Keras Install Requirements, Explained · Explainer
  8. Part 8: JAX on the CPU: Field Notes with a Bot · Field notes
  9. Part 9: Read Hugging Face Model Cards and Licenses with a Bot · Guide
  10. Part 10: Export a Model to ONNX and Run It: A Deep Dive · Deep dive
  11. Part 11: Read ML Error Messages and Profile Code: Cheat Sheet · Cheat sheet
  12. 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.