"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
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
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?
| Model | FP16 | INT8 | INT4 | RTX 4090 (24GB) | A100 (80GB) | H100 (80GB) |
|---|---|---|---|---|---|---|
| 8B | 16 GB | 8 GB | 4 GB | โ FP16 | โ | โ |
| 13B | 26 GB | 13 GB | 6.5 GB | โ INT8 | โ | โ |
| 34B | 68 GB | 34 GB | 17 GB | โ INT4 | โ | โ |
| 70B | 140 GB | 70 GB | 35 GB | โ | โ INT8 | โ INT8 |
| 405B | 810 GB | 405 GB | 202 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 Length | Llama 3 70B KV Cache (FP16) | Llama 3 8B KV Cache (FP16) |
|---|---|---|
| 2K tokens | 0.625 GB | 0.125 GB |
| 4K tokens | 1.25 GB | 0.25 GB |
| 8K tokens | 2.5 GB | 0.5 GB |
| 32K tokens | 10 GB | 2 GB |
| 128K tokens | 40 GB | 8 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:
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:
- Attention scores
- QKV projections
- FFN intermediate activations
- Layer norm values
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
Llama 3 70B, INT4, batch_size=1, 4K context:
Model weights: 70B ร 0.5 = 35.0 GBKV cache: 1.25 GB (FP16, 4K tokens)
Activations: ~0.1 GB
Overhead: ~1.0 GB
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
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 GBKV cache: 16 ร 2.5 = 40.0 GB
Activations: ~2.0 GB
Overhead: ~1.0 GB
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Total: ~183 GB โ Needs 3ร A100 80GB
โ Or 2ร H200 141GBTraining VRAM Budget
Training requires far more โ you need:
Model weights (in FP32 for master copy)
Optimizer states (Adam stores 2 extra copies: momentum + variance)
Gradients (same size as weights)
Activations (stored for backpropagation)Training VRAM (rough estimate):
โ 4 ร model_weights_FP32 + activations
For a 7B model:
Weights (FP32): 7B ร 4 = 28 GB
Optimizer state: 7B ร 8 = 56 GB (Adam: 2 ร FP32 copies)
Gradients: 7B ร 4 = 28 GBActivations: ~30 GB
โโโโโโโโโโโโโโโโโโโโโโโโโ
Total: ~142 GB โ Needs 2ร A100 80GBRule 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
| 1B | 0.5 GB | 1 GB | 2 GB |
|---|---|---|---|
| 3B | 1.5 GB | 3 GB | 6 GB |
| 7B | 3.5 GB | 7 GB | 14 GB |
| 8B | 4 GB | 8 GB | 16 GB |
| 13B | 6.5 GB | 13 GB | 26 GB |
| 34B | 17 GB | 34 GB | 68 GB |
| 70B | 35 GB | 70 GB | 140 GB |
| 405B | 202 GB | 405 GB | 810 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
| A100 | 80 GB | 2,039 GB/s | Up to ~130B |
|---|---|---|---|
| H100 | 80 GB | 3,350 GB/s | Up to ~130B |
| H200 | 141 GB | 4,800 GB/s | Up to ~250B |
| Mac M4 Max | 128 GB* | 546 GB/s | Up to ~220B |
| Mac M4 Ultra | 512 GB* | 819 GB/s | Up 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
"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
"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:
Theoretical INT4: 70B ร 0.5 = 35.0 GBActual 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
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.