MODULE 10  ยท  GPU & Hardware

VRAM Math: How to Calculate If a Model Fits

Every AI engineer asks: will this model fit on my GPU? Here's how to calculate it yourself โ€” in your head, in 30 seconds.

๐Ÿ“… Mar 2026
โฑ 10 min read
๐ŸŽฏ Episode 65 of 98
VRAMMemoryGPUCalculation
In this episode

"Can I run Llama 3 70B on my GPU?"

Every AI engineer asks this question. And most just Google it and hope someone posted a benchmark. But what if you could calculate it yourself โ€” in your head, in 30 seconds?

That's what this episode gives you. A practical formula for VRAM requirements that works for any model, any precision, any batch size. Once you learn this, you'll never wonder "will it fit?" again. You'll know.

The Formula

Here's the master formula:

Total VRAM = Model Weights + KV Cache + Activations + Overhead

Where:

Model Weights = Parameters ร— Bytes per Parameter

KV Cache = 2 ร— Layers ร— Hidden_dim ร— Seq_len ร— Batch_size ร— Bytes

snippet
code
Activations   = ~10-20% of model weights (during inference)
Overhead      = CUDA context + framework buffers (~500MB-1GB)

Let's break each piece down.

โšก

Part 1: Model Weights

This is the big one โ€” and the simplest to calculate.

Bytes per Parameter

Precision Bytes per Param Formula

FP32 4 bytes params ร— 4

FP16 / BF16 2 bytes params ร— 2

FP8 1 byte params ร— 1

INT8 1 byte params ร— 1

INT4 (Q4) 0.5 bytes params ร— 0.5

GPTQ/AWQ 4-bit ~0.55 bytes params ร— 0.55 (quantization overhead)

Quick Calculations

Llama 3 8B:

FP16: 8B ร— 2 bytes = 16 GB

INT8: 8B ร— 1 byte = 8 GB

INT4: 8B ร— 0.5 = 4 GB

Llama 3 70B:

FP16: 70B ร— 2 bytes = 140 GB

INT8: 70B ร— 1 byte = 70 GB

INT4: 70B ร— 0.5 = 35 GB

Llama 3 405B:

FP16: 405B ร— 2 = 810 GB

INT4: 405B ร— 0.5 = 202.5 GB

snippet
code
The mental shortcut: For FP16, the VRAM in GB is roughly 2ร— the parameter count in billions. For INT4, it's roughly half the parameter count.

FP16: VRAM (GB) โ‰ˆ Params (B) ร— 2

INT4: VRAM (GB) โ‰ˆ Params (B) ร— 0.5

INT8: VRAM (GB) โ‰ˆ Params (B) ร— 1

Does It Fit?

ModelFP16INT8INT4RTX 4090 (24GB)A100 (80GB)H100 (80GB)
8B16 GB8 GB4 GBโœ… FP16โœ…โœ…
13B26 GB13 GB6.5 GBโœ… INT8โœ…โœ…
34B68 GB34 GB17 GBโœ… INT4โœ…โœ…
70B140 GB70 GB35 GBโŒโœ… INT8โœ… INT8
405B810 GB405 GB202 GBโŒโŒโŒ (need multi)

๐Ÿ”ง

Part 2: KV Cache Memory

The KV Cache stores the key and value vectors for every token in the context window, for every layer, for every attention head. During generation, this cache grows with each token and can consume more VRAM than the model weights for long contexts.

The KV Cache Formula

KV Cache (bytes) = 2 ร— num_layers ร— hidden_dim ร— seq_length ร— batch_size ร— bytes_per_value

The "2" is for K (keys) and V (values) โ€” both are cached.

Worked Example: Llama 3 70B

Architecture:

Layers: 80

Hidden dim: 8192

Attention heads: 64

KV heads: 8 (GQA โ€” Grouped Query Attention)

Head dim: 128

With GQA, the effective KV hidden dim = num_kv_heads ร— head_dim = 8 ร— 128 = 1024

KV Cache for 1 sequence of 4K tokens (FP16):

= 2 ร— 80 layers ร— 1024 ร— 4096 tokens ร— 2 bytes

= 2 ร— 80 ร— 1024 ร— 4096 ร— 2

= 1,342,177,280 bytes

โ‰ˆ 1.25 GB

KV Cache for 1 sequence of 128K tokens (FP16):

= 2 ร— 80 ร— 1024 ร— 131072 ร— 2

โ‰ˆ 40 GB โ† Half the VRAM just for one sequence's KV cache!

KV Cache Scaling

Sequence LengthLlama 3 70B KV Cache (FP16)Llama 3 8B KV Cache (FP16)
2K tokens0.625 GB0.125 GB
4K tokens1.25 GB0.25 GB
8K tokens2.5 GB0.5 GB
32K tokens10 GB2 GB
128K tokens40 GB8 GB

Key insight: GQA (Grouped Query Attention) saves enormous KV cache memory. Without GQA, Llama 3 70B would use 8x more KV cache (64 KV heads instead of 8). That's why GQA is standard in modern architectures.

Batch Size Impact

Each request in a batch needs its own KV cache. This multiplies linearly:

snippet
code
Batch size 1:  1 ร— 1.25 GB = 1.25 GB
Batch size 8:  8 ร— 1.25 GB = 10 GB
Batch size 32: 32 ร— 1.25 GB = 40 GB
Batch size 64: 64 ร— 1.25 GB = 80 GB  โ† Exceeds the entire VRAM!

This is why serving AI at high throughput requires careful VRAM budgeting. The model weights are fixed, but KV cache scales with both sequence length AND concurrent users.

๐Ÿ“Š

Part 3: Activations

Activations are the intermediate values computed during the forward pass โ€” the output of each layer before it feeds into the next.

During Inference

Activations are relatively small during inference because you only need the current layer's output:

Activation memory โ‰ˆ batch_size ร— seq_len ร— hidden_dim ร— bytes

For Llama 3 70B, 1 sequence, 4K tokens:

โ‰ˆ 1 ร— 4096 ร— 8192 ร— 2 bytes โ‰ˆ 64 MB (small!)

During Training

Activations during training are massive because you need to store all intermediate values for backpropagation:

Training activation memory โ‰ˆ num_layers ร— batch_size ร— seq_len ร— hidden_dim ร— bytes ร— factor

The "factor" accounts for multiple intermediate values per layer:

For Llama 3 70B training:

โ‰ˆ 80 ร— 1 ร— 4096 ร— 8192 ร— 2 ร— 10 โ‰ˆ 50+ GB per sample

This is why training requires far more VRAM than inference and why techniques like gradient checkpointing (recomputing activations instead of storing them) are essential.

Part 4: Putting It All Together

Inference VRAM Budget

snippet
code
Llama 3 70B, INT4, batch_size=1, 4K context:
Model weights:     70B ร— 0.5 = 35.0 GB

KV cache: 1.25 GB (FP16, 4K tokens)

Activations: ~0.1 GB

Overhead: ~1.0 GB

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

snippet
code
Total:             ~37.4 GB    โ†’ Fits on A100 80GB โœ…
โ†’ Fits on 2ร— RTX 4090 48GB โœ…
Llama 3 70B, FP16, batch_size=16, 8K context:
Model weights:     70B ร— 2 = 140.0 GB

KV cache: 16 ร— 2.5 = 40.0 GB

Activations: ~2.0 GB

Overhead: ~1.0 GB

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

snippet
code
Total:             ~183 GB     โ†’ Needs 3ร— A100 80GB
โ†’ Or 2ร— H200 141GB

Training VRAM Budget

Training requires far more โ€” you need:

Model weights (in FP32 for master copy)

Optimizer states (Adam stores 2 extra copies: momentum + variance)

snippet
code
Gradients (same size as weights)
Activations (stored for backpropagation)

Training VRAM (rough estimate):

โ‰ˆ 4 ร— model_weights_FP32 + activations

For a 7B model:

snippet
code
Weights (FP32): 7B ร— 4 = 28 GB
Optimizer state: 7B ร— 8 = 56 GB (Adam: 2 ร— FP32 copies)
Gradients: 7B ร— 4 = 28 GB

Activations: ~30 GB

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

snippet
code
Total: ~142 GB โ†’ Needs 2ร— A100 80GB

Rule of thumb for training: You need roughly 18-20 bytes per parameter for full fine-tuning with Adam optimizer. So a 7B model needs ~130-140 GB.

๐Ÿ’ก

The Cheat Sheet

Print this. Tattoo it. Whatever works:

Quick VRAM Estimates (Inference)

Params INT4 INT8 FP16

1B0.5 GB1 GB2 GB
3B1.5 GB3 GB6 GB
7B3.5 GB7 GB14 GB
8B4 GB8 GB16 GB
13B6.5 GB13 GB26 GB
34B17 GB34 GB68 GB
70B35 GB70 GB140 GB
405B202 GB405 GB810 GB

Add 10-30% for KV cache and overhead at typical batch sizes.

GPU Memory Quick Reference

GPU VRAM Bandwidth Fits (INT4, batch=1)

RTX 3060 12 GB 360 GB/s Up to ~20B

RTX 3090 24 GB 936 GB/s Up to ~40B

RTX 4090 24 GB 1,008 GB/s Up to ~40B

A10080 GB2,039 GB/sUp to ~130B
H10080 GB3,350 GB/sUp to ~130B
H200141 GB4,800 GB/sUp to ~250B
Mac M4 Max128 GB*546 GB/sUp to ~220B
Mac M4 Ultra512 GB*819 GB/sUp to ~900B

*Mac uses unified memory (shared CPU/GPU), which means the full amount is available for models.

๐Ÿ”ฌ

Common Mistakes

Mistake 1: Forgetting KV Cache

snippet
code
"70B in INT4 is 35 GB, fits on my A100 80GB!"

Yes, but with batch_size=32 and 8K context:

KV cache = 32 ร— 2.5 GB = 80 GB โ† EXCEEDS TOTAL VRAM

Your model fits but you can't serve any traffic.

Mistake 2: Confusing INT4 with FP16

snippet
code
"I downloaded the Llama 3 70B FP16 model, it should be 35 GB..."
No. FP16 = 2 bytes per param = 140 GB.
INT4 = 0.5 bytes per param = 35 GB.

You downloaded 140 GB. It won't fit.

Mistake 3: Ignoring Quantization Overhead

Real INT4 models (GPTQ, AWQ) store per-group quantization scales and zeros, adding ~10% overhead:

snippet
code
Theoretical INT4: 70B ร— 0.5 = 35.0 GB

Actual GPTQ-4bit: 70B ร— 0.55 โ‰ˆ 38.5 GB

๐Ÿ›ก๏ธ

Practical Takeaways

FP16 VRAM โ‰ˆ params ร— 2 โ€” the simplest formula in AI engineering

KV cache can exceed model weights โ€” especially with long contexts and large batches

GQA saves KV cache memory โ€” 8 KV heads vs 64 can mean 8x less cache

Training needs ~18-20 bytes per parameter โ€” 10x more than inference

Always add 10-30% for overhead โ€” CUDA context, framework buffers, fragmentation

Batch size ร— sequence length determines serving cost โ€” not just model size

๐Ÿ“ฆ

What's Next?

Episode 66: Multi-GPU Setups โ€” When one GPU isn't enough, how do you split a model across multiple GPUs? Tensor parallelism, pipeline parallelism, data parallelism โ€” and when you need each one.

โ† Previous

Ep 64: GPU Architecture Basics

Next โ†’

Ep 66: Multi-GPU Setups

Next: Episode 66 โ€” Multi-GPU Setups

snippet
code
Llama 3 405B in FP16 is 810 GB. The biggest GPU has 192 GB. You need at least 5 GPUs. Here's how model parallelism works.

This is part of a 98-episode series covering AI engineering from tokens to production deployment.

โ† Previous Ep 64: GPU Architecture Basics: Why GPUs Are AI's Engine