Smart Systems for Home

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

How to Benchmark CPU for AI Inference & Data Center Workloads

· 6 min read · Home Tech

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

Benchmarking a CPU for AI inference and data center workloads goes beyond simple clock speeds and core counts. It measures how well a processor handles the specific mix of matrix math, memory bandwidth, and latency that machine learning models demand. This guide walks you through a practical, repeatable benchmarking process—from choosing the right tools to interpreting results—so you can make informed hardware decisions or validate system performance.

What You'll Need

  • A Linux-based server or workstation (Ubuntu 22.04 LTS or similar) with root/sudo access.
  • Python 3.8+ installed, along with pip.
  • Basic familiarity with the command line.
  • Optional but recommended: a second machine to monitor system metrics without interfering with the benchmark.

Step-by-Step

  1. Define your workload profile. AI inference workloads vary: image classification (CNN), natural language processing (Transformer), or recommendation systems. Each stresses the CPU differently—CNNs are compute-bound, Transformers are memory-bandwidth-heavy. Write down the models you care about (e.g., ResNet-50, BERT-base) and the batch size you typically use.
  2. Prepare the system. Update the OS and install essential tools: sudo apt update && sudo apt install -y git build-essential python3-pip. Ensure CPU frequency scaling is set to "performance" mode: sudo cpupower frequency-set -g performance (install linux-tools-common if needed). Disable turbo boost for more stable results? Actually, keep it enabled if you want to measure real-world peak performance; just document the setting.
  3. Install a benchmarking framework. The two most common are MLPerf Inference (official, heavy) and ONNX Runtime (lighter, flexible). For most users, ONNX Runtime is easier to start with: pip install onnxruntime onnxruntime-extensions. For MLPerf, clone the repo and follow the official setup—it requires Docker and more dependencies.
  4. Set up a representative model. Download a pre-trained ONNX model (e.g., ResNet-50 from the ONNX Model Zoo). Use a script to preprocess a sample image or generate dummy input tensors. For NLP, use a tokenizer from Hugging Face to create input IDs.
  5. Write a simple benchmark script. In Python, load the model, warm up with a few inferences (e.g., 10), then run a timed loop (e.g., 100 inferences) and record latency per inference and throughput (inferences per second). Use time.perf_counter() for accuracy. Example snippet:
    import onnxruntime as ort
    import time
    import numpy as np
    
    sess = ort.InferenceSession("resnet50.onnx")
    input_name = sess.get_inputs()[0].name
    input_shape = sess.get_inputs()[0].shape
    

    Warm up

    for _ in range(10): sess.run(None, {input_name: np.random.randn(*input_shape).astype(np.float32)})

    Benchmark

    n = 100 start = time.perf_counter() for _ in range(n): sess.run(None, {input_name: np.random.randn(*input_shape).astype(np.float32)}) end = time.perf_counter() latency_ms = (end - start) / n * 1000 throughput = n / (end - start) print(f"Latency: {latency_ms:.2f} ms, Throughput: {throughput:.2f} inf/s")
  6. Measure system-level metrics. While the benchmark runs, monitor CPU utilization, memory bandwidth, and power consumption. Use perf stat for cache misses and IPC, turbostat for frequency and power, and mpstat for per-core utilization. Log these to a file for later analysis.
  7. Run multiple iterations. Run the benchmark at least 5 times, taking the median or mean. Record the standard deviation. This accounts for thermal throttling and background noise.
  8. Test different batch sizes. AI inference often uses batch sizes of 1 (online) or larger (offline). Repeat the benchmark with batch sizes 1, 8, 32, and 64 to see how the CPU scales. For ONNX Runtime, adjust the input tensor's first dimension accordingly.
  9. Compare with a baseline. If you're evaluating a new CPU, run the same benchmark on a known reference system (or the old CPU) with identical software versions. This isolates hardware differences.
  10. Document everything. Record the CPU model, BIOS version, OS kernel, Python version, ONNX Runtime version, model name, input shape, and all settings. This ensures reproducibility.

Common Mistakes

  • Not warming up. Skipping warm-up yields artificially high latency due to lazy initialization and cache cold starts.
  • Ignoring NUMA. On multi-socket systems, memory access across sockets is slower. Pin the process to a single socket using numactl --cpunodebind=0 --membind=0 for consistent results.
  • Using the wrong data type. FP32 vs. INT8 dramatically changes performance. Ensure you're benchmarking the same precision you'll use in production.
  • Not disabling background services. Cron jobs, logging daemons, or even a web browser can skew results. Run benchmarks in a clean environment.
  • Overlooking thermal throttling. On air-cooled servers, sustained load can cause the CPU to downclock. Monitor frequency during the run to ensure it stays constant.

Tips

  • Use the official MLPerf Inference suite if you need industry-standard, comparable numbers. It includes predefined models and scenarios, but it's complex to set up.
  • For data center workloads, also test multi-stream scenarios. Many real-world systems handle multiple inference requests concurrently. Use a tool like wrk or a custom async script to simulate concurrency.
  • Check for AVX-512 support. CPUs with AVX-512 can significantly accelerate certain AI ops. Use lscpu | grep avx512 to see if your CPU supports it, and ensure your software is compiled to use it.
  • Run benchmarks in a container. Docker or Singularity containers ensure consistent software environments across systems.
  • Look at latency percentiles (P99), not just averages. Tail latency matters for user-facing inference. Record the 95th and 99th percentiles.

Troubleshooting

  • Benchmark crashes with an out-of-memory error. Reduce batch size or input dimensions. Large models like BERT can consume several GB of RAM.
  • Results vary wildly between runs. Check if the CPU frequency is fluctuating. Re-enable turbo boost if you disabled it; alternatively, use taskset to pin the process to specific cores to reduce scheduling jitter.
  • ONNX Runtime fails to load the model. Ensure you downloaded the correct ONNX version for your runtime. Use onnxruntime-gpu if you have a GPU, but for CPU benchmarking, stick to the CPU package.
  • perf stat gives "permission denied" errors. Run with sudo, or set kernel.perf_event_paranoid to 2 or lower: sudo sysctl kernel.perf_event_paranoid=2.
  • Numbers don't match published benchmarks. Published results often use specific compiler flags, model optimizations, and hardware configurations. Your setup will differ; focus on relative comparisons between your own systems.

Frequently Asked Questions

Q: What is the difference between latency and throughput?
A: Latency is the time to process one inference request; throughput is the number of inferences completed per second. They are inversely related—higher batch sizes increase throughput but may increase latency per request.

Q: Should I benchmark with real data or random data?
A: Real data is better because it exercises the actual input distribution and may trigger different code paths (e.g., sparse operations). If real data is unavailable, random data is acceptable for relative comparisons.

Q: Can I use the same benchmark for GPU comparison?
A: Yes, but you'd need to adjust the software (e.g., use onnxruntime-gpu) and account for GPU-specific metrics like VRAM usage. The methodology is similar.

Q: How long should a benchmark run?
A> At least 100 inferences or 30 seconds, whichever is longer. For stable measurements, aim for 1-2 minutes per configuration.

Q: Do I need to disable hyper-threading?
A: Not necessarily. Hyper-threading can help throughput but may hurt latency. Test both settings if your workload is sensitive.

Q: What is the best way to compare two CPUs?
A: Run the exact same benchmark script on both, with identical software stacks and power settings. Use the median of 5 runs and compare both latency and throughput.