Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Choose GPU Memory and Instance Sizes for AI

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide helps you pick the right GPU memory (VRAM) and cloud instance size for training and running AI models. You'll learn how to estimate memory needs, match instance types to workloads, and avoid common pitfalls that lead to overspending or out-of-memory errors.

What You'll Need

  • Your model's parameter count (e.g., 7B, 13B, 70B) and precision (FP32, FP16, INT8, etc.)
  • Training dataset size and batch size you plan to use
  • Context length (for LLMs) or input/output dimensions (for other models)
  • Access to cloud provider pricing pages (AWS, GCP, Azure, or others)

Step-by-Step

  1. Determine your model's memory footprint for inference. For a transformer model, approximate VRAM needed = (number of parameters × bytes per parameter) + overhead. For FP16, bytes per parameter ≈ 2; for INT8, ≈ 1; for FP32, ≈ 4. Add ~20% overhead for activations and framework buffers. Example: a 7B model in FP16 needs ~14 GB + ~3 GB overhead ≈ 17 GB VRAM.
  2. Calculate training memory needs. Training requires more memory because you store gradients and optimizer states. A rough rule: for Adam optimizer, multiply inference memory by ~4–6×. For a 7B model in FP16, training might need ~60–100 GB VRAM. If that's too high, use mixed precision (FP16/BF16) or gradient checkpointing to reduce memory.
  3. Account for context length and batch size. For LLMs, memory grows with sequence length and batch size. Use the formula: additional memory ≈ (batch size × sequence length × hidden size × layers × bytes per element). For example, a 7B model with 32 layers, hidden size 4096, batch size 1, sequence length 2048, FP16 (2 bytes) gives ~1.7 GB extra. Increase batch size to 8 → ~13.6 GB extra.
  4. Choose the smallest GPU that fits your peak memory. Look up GPU specs (e.g., NVIDIA A10G 24GB, A100 40/80GB, H100 80GB, L4 24GB, T4 16GB). Pick one where your estimated peak memory is ≤ 80% of VRAM to leave headroom. For inference of a 7B FP16 model, a 24GB GPU works; for 70B, you need 80GB+ or quantization.
  5. Select instance type based on GPU count and price. Cloud providers offer instances with 1, 4, or 8 GPUs. For training, you might need multiple GPUs. Use this rule: if your model fits on one GPU, start with a single-GPU instance to test; scale to multi-GPU only if training time is too long. Compare price per GPU-hour across instance sizes; sometimes 8 smaller GPUs are cheaper than 2 large ones.
  6. Consider CPU RAM and storage. Ensure your instance has enough CPU RAM to hold the model in system memory before loading to GPU (for inference, at least 1.5× model size). For training, have fast SSD storage for checkpoints and dataset loading.
  7. Test with a small run before committing. Launch a small instance with a tiny batch size, run a few steps, and monitor memory usage via nvidia-smi. If memory usage is near 100%, increase GPU memory or reduce batch size. If it's under 60%, consider a smaller GPU to save cost.

Common Mistakes

  • Ignoring precision: Using FP32 when FP16 or BF16 suffices doubles memory. Many models can be quantized to INT8 with minimal accuracy loss.
  • Forgetting activation memory: During training, activations can consume more memory than weights, especially with long sequences. Use gradient checkpointing to trade compute for memory.
  • Overestimating batch size: A batch size of 32 on a 24GB GPU may cause OOM; start with 1 and double until memory is ~90%.
  • Choosing a single huge GPU when multiple smaller ones are cheaper: For training, 4×A10G (24GB each) may be more cost-effective than 1×A100 80GB, depending on workload.

Tips

  • Use the nvidia-smi command to monitor real-time VRAM usage; it's your best friend.
  • For inference, consider using vLLM or TensorRT to reduce memory overhead and increase throughput.
  • Always reserve ~10–20% headroom for spikes (e.g., during evaluation or when processing longer inputs).
  • If you're on a budget, look for spot/preemptible instances for non-critical training runs.
  • For very large models (70B+), use model parallelism (e.g., DeepSpeed ZeRO) to split across GPUs, but be aware of communication overhead.

Troubleshooting

  • Out of Memory (OOM) error: Reduce batch size, use gradient accumulation, enable mixed precision, or switch to a smaller GPU instance. If you're already on the smallest, consider quantization.
  • GPU utilization is low: This often means a bottleneck in data loading (CPU) or communication. Increase number of dataloader workers, use larger batch size, or check if your code is using the GPU properly.
  • Training is slow: If memory usage is low but time is high, you may be using too many GPUs for a small model; reduce instance size. Also check if you're using FP32 instead of mixed precision.
  • Model doesn't fit even after quantization: Try offloading to CPU (for inference) or use parameter-efficient fine-tuning (LoRA) for training, which drastically reduces memory.

Frequently Asked Questions

How much VRAM do I need for a 7B model?

For inference in FP16, you need at least 14 GB plus overhead, so a 16 GB GPU works with careful settings. For training, expect to need 40–60 GB, so a 48GB or 80GB GPU is safer.

What is the difference between GPU memory and instance size?

GPU memory is the VRAM on the graphics card; instance size refers to the entire virtual machine (CPU, RAM, storage, network) that includes one or more GPUs. You need both to match your workload.

Can I use multiple GPUs to fit a model that doesn't fit on one?

Yes, using model parallelism (e.g., tensor parallelism or pipeline parallelism) splits the model across GPUs, but you need fast inter-GPU communication (NVLink or high-speed network). This is common for large models.

Is it better to buy a bigger GPU or use more smaller GPUs?

For training, multiple smaller GPUs can be more cost-effective and scalable, but they add communication overhead. For inference, a single large GPU is simpler and often cheaper per token.

How do I estimate memory for a custom model?

Use the formula: memory = (parameters × bytes per param) + (activations). For activations, multiply batch size, sequence length, hidden size, and layers by bytes per element. Tools like PyTorch's torch.cuda.max_memory_allocated() can help after a test run.

What is the role of quantization in memory reduction?

Quantization reduces the bytes per parameter (e.g., from 2 to 1 for INT8), cutting memory roughly in half. It can cause slight accuracy loss, but for many tasks it's negligible.