keepcalculator

06 Numbers and Powers

Benchmark AI Inference Performance in the Cloud

Card No. 06·62869 · Updated · Standard formula, shown below

This guide walks you through benchmarking AI inference performance across cloud providers or instance types. You’ll learn how to design a reproducible test, collect latency and throughput metrics, and compare results fairly. By the end, you’ll have a clear method to evaluate which cloud setup best meets your model’s speed and cost requirements.

What You’ll Need

  • An AI model (e.g., PyTorch, TensorFlow, ONNX) that you want to benchmark.
  • Access to at least one cloud instance (GPU or CPU) with the necessary drivers and runtime installed.
  • A representative dataset or input samples (e.g., images, text sequences) that match your real workload.
  • A benchmarking script or tool (e.g., a custom Python script, or a library like onnxruntime or triton).
  • Basic familiarity with command-line and Python.

Step-by-Step

  1. Define your performance metrics. Decide what you care about: latency (time per inference), throughput (inferences per second), or both. Also consider p99 latency (the 99th percentile) for tail behavior.
  2. Prepare your model and inputs. Export your model to a format that is optimized for inference (e.g., ONNX, TorchScript, or TensorRT). Create a set of input tensors that match your real data’s shape and data type. Use a fixed batch size (e.g., 1, 8, 32) that you’ll test.
  3. Write a benchmarking script. Your script should:
    • Load the model once.
    • Warm up the model with a few inferences (e.g., 10) to trigger lazy initialization and GPU warm-up.
    • Run a large number of inferences (e.g., 1000) while recording start/end times.
    • Calculate average latency, throughput, and percentile latencies.
    • Optionally, measure CPU/GPU utilization and memory usage.
  4. Run the benchmark on each cloud setup. For each instance type or cloud provider, launch an instance with the same OS and runtime versions. Run your script and save the output to a file.
  5. Repeat multiple times. Run the benchmark at least 3 times per setup to account for variance. Use the median or mean of the runs for comparison.
  6. Collect cost data. Note the hourly price of each instance (from the cloud provider’s pricing page). This will let you compute cost per 1000 inferences.
  7. Analyze and compare. Create a table with metrics (latency, throughput, cost) for each setup. Decide which trade-off is best for your use case—e.g., lowest p99 latency for real-time apps, highest throughput for batch processing.

Common Mistakes

  • Not warming up. Skipping warm-up leads to inflated latency numbers due to initial model loading and GPU initialization.
  • Using different input sizes. Comparing instances with different batch sizes or input resolutions invalidates the comparison.
  • Ignoring network variability. If you’re calling a remote inference endpoint, network latency can dominate. Benchmark the inference itself, not the network.
  • Overlooking CPU vs. GPU. Some models run fine on CPU; don’t assume GPU is always faster. Test both if applicable.
  • Not checking driver/runtime versions. Different CUDA or TensorRT versions can change performance significantly. Keep them consistent across tests.

Tips

  • Use a standard benchmark tool like benchmark from ONNX Runtime or perf_analyzer from Triton if your model supports it—they handle warm-up and statistics automatically.
  • Monitor resource usage with nvidia-smi (GPU) or top (CPU) to see if you’re bottlenecked on compute, memory, or I/O.
  • Test with different batch sizes—the optimal batch size can vary by instance.
  • Consider using a fixed random seed for input generation to ensure identical inputs across runs.
  • Run benchmarks at different times of day to check for noisy neighbors (especially on shared instances).

Troubleshooting

  • Benchmark crashes with out-of-memory. Reduce batch size or use a smaller model variant. Check that your instance has enough GPU/CPU RAM.
  • Results vary wildly between runs. This is common on shared hardware. Run more iterations (e.g., 5000) and report median, not mean. Also ensure no other processes are running.
  • Model loads slowly on first inference. That’s normal—warm-up is essential. If the first inference is always slow, consider using a model server that keeps the model loaded.
  • GPU utilization is low. Your batch size might be too small, or your data loading is a bottleneck. Use asynchronous data loading or increase batch size.
  • Cannot install a dependency. Check the cloud provider’s supported OS images—some have pre-installed CUDA and cuDNN. Otherwise, use a container image (e.g., NVIDIA NGC).

Frequently Asked Questions

  1. What is the difference between latency and throughput? Latency is the time for one inference (usually in milliseconds). Throughput is the number of inferences per second. They are related: throughput = batch_size / latency, but in practice, larger batches increase throughput while also increasing latency per request.
  2. How many iterations should I run? At least 1000 after warm-up. More iterations reduce noise. For very slow models, 100 may suffice, but use a fixed count for fair comparison.
  3. Should I benchmark with a batch size of 1? It depends on your use case. For real-time APIs, batch size 1 is typical. For offline processing, larger batches (e.g., 32 or 64) are common. Test multiple sizes.
  4. Can I compare different cloud providers with different GPUs? Yes, but note that results are not directly comparable if the hardware differs. The benchmark helps you decide which provider gives the best performance per dollar for your specific model.
  5. What is p99 latency and why does it matter? p99 latency is the value below which 99% of inferences fall. It indicates tail performance—important for user-facing applications where slow outliers are noticeable.
  6. Do I need to benchmark on every instance type? No. Start with a few representative ones (e.g., a small, medium, and large GPU instance) and then narrow down based on your cost/performance targets.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →