Smart Systems for Home

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

AI Hardware Estimator: Training & Inference

· 4 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 walks you through estimating the hardware needed to train and run inference for an AI model. You'll learn to calculate compute requirements (FLOPs), memory needs, and the number of GPUs or TPUs, based on your model size, dataset, and target latency. Start by gathering a few key numbers about your model and workload.

What You'll Need

  • Model details: number of parameters (e.g., 7 billion) and architecture type (Transformer, CNN, etc.)
  • Training dataset size: total number of tokens (for LLMs) or images (for vision models)
  • Target training time: how many days you want training to take
  • Target inference latency: acceptable time per prediction (e.g., 100 ms)
  • Hardware specs you're considering: GPU/TPU memory, FLOPs, and memory bandwidth

Step-by-Step

  1. Estimate training FLOPs. For a Transformer model, the total training FLOPs are approximately 6 * N * D, where N is the number of parameters and D is the number of training tokens. For example, a 7B model trained on 1 trillion tokens requires about 6 * 7e9 * 1e12 = 4.2e22 FLOPs.
  2. Convert FLOPs to hardware-days. Divide total FLOPs by the peak FLOPs of a single accelerator (e.g., an A100 has ~312 TFLOPS for FP16 with sparsity). Then account for real-world utilization (often 30-50% of peak). Formula: accelerator-days = total_FLOPs / (peak_FLOPs_per_second * utilization * 86400).
  3. Determine number of accelerators. Divide accelerator-days by your target training time in days. For instance, if you need 1000 accelerator-days and want training in 10 days, you need 100 GPUs.
  4. Estimate training memory. For each parameter, you need memory for the model weights, gradients, and optimizer states. A common rule: for Adam optimizer, about 16 bytes per parameter. So a 7B model needs ~112 GB just for optimizer states. Add activations and batch size; typically you'll need 2-4x the model size in memory.
  5. Estimate inference FLOPs per token. For autoregressive generation, each token requires about 2 * N FLOPs (forward pass). For a 7B model, that's ~14 GFLOPs per token.
  6. Estimate inference memory. You need to load the model weights. With FP16, that's 2 bytes * N. For a 7B model, ~14 GB. Add KV cache for long sequences: 2 * layers * hidden_size * sequence_length * batch_size bytes.
  7. Calculate throughput and latency. Divide accelerator FLOPs by per-token FLOPs to get tokens per second. For a given latency target, you may need to batch multiple requests or use a smaller model.
  8. Choose hardware. Match your memory and FLOPs requirements to available GPUs/TPUs. Consider memory bandwidth for inference (often the bottleneck) and interconnect speed for training parallelism.

Common Mistakes

  • Ignoring utilization: Real-world training rarely hits 100% of peak FLOPs. Use 30-50% for training, 50-70% for inference.
  • Underestimating memory: Forgetting optimizer states and activations leads to out-of-memory errors. Always add a safety margin.
  • Mixing FLOPs and FLOPS: FLOPs (operations) vs FLOPS (operations per second) – a common unit confusion.
  • Overlooking power and cooling: High-end GPUs consume 300-700W each; cluster power and cooling are often underestimated.

Tips

  • Use mixed precision (FP16/BF16) to reduce memory and double throughput on modern GPUs.
  • For inference, consider quantization (INT8) to shrink memory and increase speed, but be aware of accuracy loss.
  • Start with a small-scale test on a single GPU to measure actual utilization and memory usage before scaling.
  • Use gradient checkpointing to trade compute for memory when training large models.

Troubleshooting

  • Out-of-memory during training: Reduce batch size, use gradient accumulation, or enable mixed precision. If still OOM, consider model parallelism.
  • Training too slow: Check if you're hitting peak FLOPs. If not, increase batch size, use data parallelism, or upgrade to faster interconnects (NVLink, InfiniBand).
  • Inference latency too high: Try smaller batch sizes, use a more efficient model (distillation), or use a GPU with higher memory bandwidth.
  • Estimates far off: Recheck your model size and token count. Use profiling tools like PyTorch Profiler to get real FLOPs and memory.

Frequently Asked Questions

Q: How accurate are these estimates?
A: They give a ballpark within 2-3x. Actual performance depends on implementation, library efficiency, and hardware specifics.

Q: Can I use CPU-only for training?
A: Yes for small models, but for anything above ~100M parameters, GPUs are 10-100x faster and often more cost-effective.

Q: What is the difference between training and inference hardware?
A: Training needs high FLOPs and memory for gradients; inference needs low latency and high throughput, often favoring memory bandwidth over raw compute.

Q: How do I estimate for a fine-tuning task?
A: Use the same formulas but with a smaller dataset (e.g., 1-10% of the original training data).

Q: Should I use cloud or on-premises?
A: For short-term projects, cloud is flexible; for long-term, on-prem may be cheaper. Use your estimates to compare costs.

Q: What about TPUs?
A: TPUs offer high FLOPs but limited flexibility. Use the same FLOPs calculations; TPU memory is often 16-32 GB per chip.