Skip to content

About

Measure GPU energy per generated token for LLM inference across batch sizes, power limits and clock locks (NVML energy counters, vLLM)

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

gpu-energy-bench: GPU energy per token for LLM inference

Here's the article describing my findings: I measured LLM energy per token on five GPUs.

Measure how much energy a GPU uses per generated token, not just how fast it is, across batch sizes, power limits and clock locks. Results for an NVIDIA L4, RTX 2080 Ti, RTX 3090, RTX 4090 and RTX 6000 Ada are in the article.

All energy readings come from the GPU's hardware energy counter through NVML (falling back to power sampling on older GPUs). Every setting runs several times in alternating order; results are medians with their spread, and the scripts say when a difference is within run-to-run noise.

Scripts

Script What it measures
gpu_bench.py Synthetic workloads: matrix multiply (compute-bound), memory copy (bandwidth-bound), and a simulated LLM decode step at several batch sizes
vllm_energy.py A real model served with vLLM: tokens/s, energy per token and speed per user for decode at several batch sizes, plus prompt processing
idle_power.py Power, clocks and P-state every 0.5 s through six states: empty GPU, CUDA context, model loaded, busy, idle after work, after shutdown
bench.sbatch Slurm batch file that runs any of the above on a cluster node

vllm_energy.py and idle_power.py import helpers from gpu_bench.py, so keep all three in one folder.

Requirements

  • An NVIDIA GPU and a recent driver
  • Python 3.10+
  • pip install torch nvidia-ml-py matplotlib (synthetic test)
  • pip install vllm nvidia-ml-py matplotlib (real-model and idle tests; vLLM brings its own PyTorch)
  • vLLM needs compute capability 7.0+ (RTX 20-series or newer); bf16 needs 8.0+ (RTX 30-series or newer). On older cards pass --dtype float16 to vllm_energy.py and idle_power.py; gpu_bench.py switches to fp16 automatically

Power limits and clock locks need root or admin rights. Without them the scripts say so and run at default settings. Most cloud containers don't allow them; Google Colab did when I tested.

Quick start

Synthetic test, default settings:

python gpu_bench.py

Synthetic test with power limits and clock locks (needs root):

sudo "$(which python)" gpu_bench.py --power-limits "100%,90%,80%" --clock-locks "1500,1200,1000"

Real model in vLLM (downloads Qwen2.5-1.5B-Instruct, about 3 GB):

python vllm_energy.py

Another model and batch sizes:

python vllm_energy.py --model Qwen/Qwen2.5-7B-Instruct --batch-sizes 1,8,32,64,128 --gpu-mem 0.9

Idle power over time (about 13 minutes):

python idle_power.py

gpu_bench.py and vllm_energy.py accept --repeats, --out, and --resume <csv> to continue an interrupted run with the same options; idle_power.py accepts --out and lets you change how long each phase lasts. Run any script with --help for the full list.

Output

Each run writes:

  • a raw CSV with every measurement,
  • a _summary.csv with medians and spreads,
  • a .png chart (if matplotlib is installed),

and prints summary tables. gpu_bench.py and vllm_energy.py also name the most energy-efficient setting within a slowdown tolerance (--tolerance, default 5%).

Slurm clusters

bench.sbatch runs the tests as a batch job. Edit the #SBATCH lines (partition, time, GPU type syntax) and the module load anaconda line for your cluster, create a conda environment named gpubench with the packages above, then:

sbatch bench.sbatch
sbatch --gpus=rtx_4090:1 --export='ALL,RUN=vllm,MODEL=Qwen/Qwen2.5-7B-Instruct,BATCHES=1 8 32 64 128' bench.sbatch

RUN can be both (default), vllm, synthetic or idle. Results are saved after every measurement, and a requeued (preempted) job resumes the vLLM and synthetic tests where they stopped (the idle test starts over). The scripts match the GPU Slurm assigned you by UUID or CUDA_VISIBLE_DEVICES, so on shared nodes they read power from your GPU, not someone else's.

Measurement notes

  • Idle power: after any GPU activity, the cards I tested stayed in a high-power state (P2) for about 17–20 seconds before dropping to idle (P8). The scripts wait 30 s before measuring idle. Measuring sooner can overstate idle power 3–4×.
  • Clock readings are sampled every 50 ms in the benchmarks and every 0.5 s in the idle logger. Under a power limit the clock changes quickly, so treat average clocks in power-limited runs as approximate.
  • Prompts are random token IDs and prefix caching is off, so results don't depend on prompt content. Each decode request generates a fixed number of tokens (end-of-text is ignored).
  • Settings apply to the whole GPU. Don't run on a GPU someone else is using. The scripts restore the original power limit and clocks when they finish or are interrupted. If a run is killed hard, reset with nvidia-smi -pl <watts> and nvidia-smi -rgc.

Limitations

Synthetic kernels are simplified. Real deployments have varying prompt and output lengths, and results differ between individual cards, drivers and software versions. Check any setting on your own workload before relying on it.

License

MIT, see LICENSE.

About

Measure GPU energy per generated token for LLM inference across batch sizes, power limits and clock locks (NVML energy counters, vLLM)

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages