https://github.com/huggingface/optimum-benchmark
๐๏ธ A unified multi-backend utility for benchmarking Transformers, Timm, PEFT, Diffusers and Sentence-Transformers with full support of Optimum's hardware optimizations & quantization schemes.
https://github.com/huggingface/optimum-benchmark
benchmark neural-compressor onnxruntime openvino pytorch tensorrt-llm text-generation-inference
Last synced: 2 months ago
JSON representation
๐๏ธ A unified multi-backend utility for benchmarking Transformers, Timm, PEFT, Diffusers and Sentence-Transformers with full support of Optimum's hardware optimizations & quantization schemes.
- Host: GitHub
- URL: https://github.com/huggingface/optimum-benchmark
- Owner: huggingface
- License: apache-2.0
- Created: 2023-04-26T17:19:02.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-09-24T07:07:21.000Z (3 months ago)
- Last Synced: 2025-09-24T08:21:51.401Z (3 months ago)
- Topics: benchmark, neural-compressor, onnxruntime, openvino, pytorch, tensorrt-llm, text-generation-inference
- Language: Python
- Homepage:
- Size: 8.91 MB
- Stars: 315
- Watchers: 4
- Forks: 59
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-production-machine-learning - Optimum-Benchmark - benchmark.svg?style=social) - A unified multi-backend utility for benchmarking Transformers and Diffusers with support for Optimum's arsenal of hardware optimizations/quantization schemes. (Evaluation and Monitoring)
README

All benchmarks are wrong, some will cost you less than others.
Optimum-Benchmark ๐๏ธ
[](https://pypi.org/project/optimum-benchmark/)
[](https://pypi.org/project/optimum-benchmark/)
[](https://pypi.org/project/optimum-benchmark/)
[](https://pypi.org/project/optimum-benchmark/)
[](https://pypi.org/project/optimum-benchmark/)
Optimum-Benchmark is a unified [multi-backend & multi-device](#backends--devices-) utility for benchmarking [Transformers](https://github.com/huggingface/transformers), [Diffusers](https://github.com/huggingface/diffusers), [PEFT](https://github.com/huggingface/peft), [TIMM](https://github.com/huggingface/pytorch-image-models) and [Optimum](https://github.com/huggingface/optimum) libraries, along with all their supported [optimizations & quantization schemes](#backends--devices-), for [inference & training](#scenarios-), in [distributed & non-distributed settings](#launchers-), in the most correct, efficient and scalable way possible.
*News* ๐ฐ
- LlamaCpp backend for benchmarking [`llama-cpp-python`](https://github.com/abetlen/llama-cpp-python) bindings with all its supported devices ๐
- ๐ฅณ PyPI package is now available for installation: `pip install optimum-benchmark` ๐ [check it out](https://pypi.org/project/optimum-benchmark/) !
- Model loading latency/memory/energy tracking for all backends in the inference scenario ๐
- numactl support for Process and Torchrun launchers to control the NUMA nodes on which the benchmark runs.
- 4 minimal docker images (`cpu`, `cuda`, `rocm`) in [packages](https://github.com/huggingface/optimum-benchmark/pkgs/container/optimum-benchmark) for testing, benchmarking and reproducibility ๐ณ
- vLLM backend for benchmarking [vLLM](https://github.com/vllm-project/vllm)'s inference engine ๐
- Hosting the codebase of the [LLM-Perf Leaderboard](https://huggingface.co/spaces/optimum/llm-perf-leaderboard) ๐ฅ
- Py-TXI backend for benchmarking [Py-TXI](https://github.com/IlyasMoutawwakil/py-txi/tree/main) ๐
- Python API for running isolated and distributed benchmarks with Python scripts ๐
- Simpler CLI interface for running benchmarks (runs and sweeps) using the Hydra ๐งช
*Motivations* ๐ฏ
- HuggingFace hardware partners wanting to know how their hardware performs compared to another hardware on the same models.
- HuggingFace ecosystem users wanting to know how their chosen model performs in terms of latency, throughput, memory usage, energy consumption, etc compared to another model.
- Benchmarking hardware & backend specific optimizations & quantization schemes that can be applied to models and improve their computational/memory/energy efficiency.
ย
> \[!Note\]
> Optimum-Benchmark is a work in progress and is not yet ready for production use, but we're working hard to make it so. Please keep an eye on the project and help us improve it and make it more useful for the community. We're looking forward to your feedback and contributions. ๐
ย
## CI Status ๐ฆ
Optimum-Benchmark is continuously and intensively tested on a variety of devices, backends, scenarios and launchers to ensure its stability with over 300 tests running on every PR (you can request more tests if you want to).
### API ๐
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_api_cpu.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_api_cuda.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_api_misc.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_api_rocm.yaml)
### CLI ๐
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_cli_cpu_ipex.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_cli_cpu_llama_cpp.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_cli_cpu_onnxruntime.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_cli_cpu_openvino.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_cli_cpu_pytorch.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_cli_cpu_py_txi.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_cli_cuda_onnxruntime.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_cli_cuda_pytorch.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_cli_cuda_py_txi.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_cli_cuda_tensorrt_llm.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_cli_cuda_vllm.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_cli_misc.yaml)
[](https://github.com/huggingface/optimum-benchmark/actions/workflows/test_cli_rocm_pytorch.yaml)
## Quickstart ๐
### Installation ๐ฅ
#### Using uv (Recommended)
[uv](https://docs.astral.sh/uv/) is a fast Python package manager that we recommend for installing and managing dependencies:
```bash
# Install uv if you haven't already
pip install uv
# Add optimum-benchmark to your uv project
uv add optimum-benchmark
# Or run optimum-benchmark with uv as a command without installing it
# (automaticlly installs and runs it in an isolated virtual environment)
uv run optimum-benchmark --config-dir examples/ --config-name cuda_pytorch_bert
# Or run optimum-benchmark with uv script without installing it
# (automaticlly installs and runs it in an isolated virtual environment)
uv run script.py
script.py:
# /// script
# dependencies = [
# "optimum-benchmark",
# ]
# ///
# [...]
# Or clone the repository and create an environment with optimum-benchmark installed for development
git clone https://github.com/huggingface/optimum-benchmark.git
cd optimum-benchmark
uv sync
```
#### Using pip
You can also install using traditional pip:
```bash
pip install optimum-benchmark
```
or install the latest version from the main branch:
```bash
pip install git+https://github.com/huggingface/optimum-benchmark.git
```
or if you want to tinker with the code, you can clone the repository and install it in editable mode:
```bash
git clone https://github.com/huggingface/optimum-benchmark.git
cd optimum-benchmark
pip install -e .
```
Advanced install options
Depending on the backends you want to use, you can install `optimum-benchmark` with the following extras:
#### With uv (Recommended):
- PyTorch (default): `uv add optimum-benchmark`
- OpenVINO: `uv add optimum-benchmark --extra openvino`
- ONNXRuntime: `uv add optimum-benchmark --extra onnxruntime`
- TensorRT-LLM: `uv add optimum-benchmark --extra tensorrt-llm`
- ONNXRuntime-GPU: `uv add optimum-benchmark --extra onnxruntime-gpu`
- Py-TXI (TGI & TEI): `uv add optimum-benchmark --extra py-txi`
- vLLM: `uv add optimum-benchmark --extra vllm`
- IPEX: `uv add optimum-benchmark --extra ipex`
#### With pip:
- PyTorch (default): `pip install optimum-benchmark`
- OpenVINO: `pip install optimum-benchmark[openvino]`
- ONNXRuntime: `pip install optimum-benchmark[onnxruntime]`
- TensorRT-LLM: `pip install optimum-benchmark[tensorrt-llm]`
- ONNXRuntime-GPU: `pip install optimum-benchmark[onnxruntime-gpu]`
- Py-TXI (TGI & TEI): `pip install optimum-benchmark[py-txi]`
- vLLM: `pip install optimum-benchmark[vllm]`
- IPEX: `pip install optimum-benchmark[ipex]`
We also support the following extra dependencies:
- gptqmodel
- sentence-transformers
- bitsandbytes
- codecarbon
- flash-attn
- deepspeed
- diffusers
- timm
- peft
### Running benchmarks using the Python API ๐งช
You can run benchmarks from the Python API as simple python scripts. This is especially cool when used with [`uv` scripts](https://docs.astral.sh/uv/guides/scripts/).
Here's an example of how to run an isolated benchmark using the `pytorch` backend, `torchrun` launcher and `inference` scenario with latency and memory tracking enabled.
```python
# /// script
# dependencies = [
# "optimum-benchmark",
# ]
# ///
from optimum_benchmark import Benchmark, BenchmarkConfig, TorchrunConfig, InferenceConfig, PyTorchConfig
from optimum_benchmark.logging_utils import setup_logging
setup_logging(level="INFO")
if __name__ == "__main__":
launcher_config = TorchrunConfig(nproc_per_node=2)
scenario_config = InferenceConfig(latency=True, memory=True)
backend_config = PyTorchConfig(model="gpt2", device="cuda", device_ids="0,1", no_weights=True)
benchmark_config = BenchmarkConfig(
name="pytorch_gpt2",
scenario=scenario_config,
launcher=launcher_config,
backend=backend_config,
)
benchmark_report = Benchmark.launch(benchmark_config)
# push artifacts to the hub
benchmark_config.push_to_hub("IlyasMoutawwakil/pytorch_gpt2") # or benchmark_config.save_json("pytorch_gpt2/benchmark_config.json")
benchmark_report.push_to_hub("IlyasMoutawwakil/pytorch_gpt2") # or benchmark_report.save_json("pytorch_gpt2/benchmark_report.json")
# merge them into a single artifact and push to the hub
benchmark = Benchmark(config=benchmark_config, report=benchmark_report)
benchmark.push_to_hub("IlyasMoutawwakil/pytorch_gpt2") # or benchmark.save_json("pytorch_gpt2/benchmark.json")
```
Running the above with `uv` will automatically runs it in an isolated virtual environment with `optimum-benchmark` installed, and it's as simple as:
```bash
uv run script.py
```
You can also see the available parameters of different configuration classes in the [Features](#features-) section below.
### Running benchmarks using the Hydra CLI ๐งช
You can also run a benchmark using the command line by specifying the configuration directory and the configuration name. Both arguments are mandatory for [`hydra`](https://hydra.cc/). `--config-dir` is the directory where the configuration files are stored and `--config-name` is the name of the configuration file without its `.yaml` extension.
```bash
optimum-benchmark --config-dir examples/ --config-name cuda_pytorch_bert
```
This will run the benchmark using the configuration in [`examples/cuda_pytorch_bert.yaml`](examples/cuda_pytorch_bert.yaml) and store the results in `runs/cuda_pytorch_bert`.
The resulting files are :
- `benchmark_config.json` which contains the configuration used for the benchmark, including the backend, launcher, scenario and the environment in which the benchmark was run.
- `benchmark_report.json` which contains a full report of the benchmark's results, like latency measurements, memory usage, energy consumption, etc.
- `benchmark_report.txt` which contains a detailed report of the benchmark's results, in the same format they were logged.
- `benchmark_report.md` which contains a detailed report of the benchmark's results, in markdown format.
- `benchmark.json` contains both the report and the configuration in a single file.
- `benchmark.log` contains the logs of the benchmark run.
Advanced CLI options
#### Configuration overrides ๐๏ธ
It's easy to override the default behavior of a benchmark from the command line of an already existing configuration file. For example, to run the same benchmark on a different device, you can use the following command:
```bash
optimum-benchmark --config-dir examples/ --config-name pytorch_bert backend.model=gpt2 backend.device=cuda
```
#### Configuration sweeps ๐งน
You can easily run configuration sweeps using the `--multirun` option. By default, configurations will be executed serially but other kinds of executions are supported with hydra's launcher plugins (e.g. `hydra/launcher=joblib`).
```bash
optimum-benchmark --config-dir examples --config-name pytorch_bert -m backend.device=cpu,cuda
```
### Configurations structure ๐
You can create custom and more complex configuration files following these [examples]([examples](https://github.com/IlyasMoutawwakil/optimum-benchmark-examples)). They are heavily commented to help you understand the structure of the configuration files.
## Features ๐จ
`optimum-benchmark` allows you to run benchmarks with minimal configuration. A benchmark is defined by three main components:
- The launcher to use (e.g. `process`)
- The scenario to follow (e.g. `training`)
- The backend to run on (e.g. `onnxruntime`)
### Launchers ๐
- [x] Process launcher (`launcher=process`); Launches the benchmark in an isolated process.
- [x] Torchrun launcher (`launcher=torchrun`); Launches the benchmark in multiples processes using `torch.distributed`.
- [x] Inline launcher (`launcher=inline`), not recommended for benchmarking, only for debugging purposes.
General Launcher features ๐งฐ
- [x] Assert GPU devices (NVIDIA & AMD) isolation (`launcher.device_isolation=true`). This feature makes sure no other processes are running on the targeted GPU devices other than the benchmark. Espepecially useful when running benchmarks on shared resources.
### Scenarios ๐
- [x] Training scenario (`scenario=training`) which benchmarks the model using the trainer class with a randomly generated dataset.
- [x] Inference scenario (`scenario=inference`) which benchmakrs the model's inference method (forward/call/generate) with randomly generated inputs.
Inference scenario features ๐งฐ
- [x] Memory tracking (`scenario.memory=true`)
- [x] Energy and efficiency tracking (`scenario.energy=true`)
- [x] Latency and throughput tracking (`scenario.latency=true`)
- [x] Warm up runs before inference (`scenario.warmup_runs=20`)
- [x] Inputs shapes control (e.g. `scenario.input_shapes.sequence_length=128`)
- [x] Forward, Call and Generate kwargs (e.g. for an LLM `scenario.generate_kwargs.max_new_tokens=100`, for a diffusion model `scenario.call_kwargs.num_images_per_prompt=4`)
See [InferenceConfig](optimum_benchmark/scenarios/inference/config.py) for more information.
Training scenario features ๐งฐ
- [x] Memory tracking (`scenario.memory=true`)
- [x] Energy and efficiency tracking (`scenario.energy=true`)
- [x] Latency and throughput tracking (`scenario.latency=true`)
- [x] Warm up steps before training (`scenario.warmup_steps=20`)
- [x] Dataset shapes control (e.g. `scenario.dataset_shapes.sequence_length=128`)
- [x] Training arguments control (e.g. `scenario.training_args.per_device_train_batch_size=4`)
See [TrainingConfig](optimum_benchmark/scenarios/training/config.py) for more information.
### Backends & Devices ๐ฑ
- [x] PyTorch backend for CPU (`backend=pytorch`, `backend.device=cpu`)
- [x] PyTorch backend for CUDA (`backend=pytorch`, `backend.device=cuda`, `backend.device_ids=0,1`)
- [ ] PyTorch backend for Habana Gaudi Processor (`backend=pytorch`, `backend.device=hpu`, `backend.device_ids=0,1`)
- [x] ONNXRuntime backend for CPUExecutionProvider (`backend=onnxruntime`, `backend.device=cpu`)
- [x] ONNXRuntime backend for CUDAExecutionProvider (`backend=onnxruntime`, `backend.device=cuda`)
- [x] ONNXRuntime backend for ROCMExecutionProvider (`backend=onnxruntime`, `backend.device=cuda`, `backend.provider=ROCMExecutionProvider`)
- [x] ONNXRuntime backend for TensorrtExecutionProvider (`backend=onnxruntime`, `backend.device=cuda`, `backend.provider=TensorrtExecutionProvider`)
- [x] Py-TXI backend for CPU and GPU (`backend=py-txi`, `backend.device=cpu` or `backend.device=cuda`)
- [x] Neural Compressor backend for CPU (`backend=neural-compressor`, `backend.device=cpu`)
- [x] TensorRT-LLM backend for CUDA (`backend=tensorrt-llm`, `backend.device=cuda`)
- [x] OpenVINO backend for CPU (`backend=openvino`, `backend.device=cpu`)
- [x] OpenVINO backend for GPU (`backend=openvino`, `backend.device=gpu`)
- [x] vLLM backend for CUDA (`backend=vllm`, `backend.device=cuda`)
- [x] vLLM backend for ROCM (`backend=vllm`, `backend.device=rocm`)
- [x] vLLM backend for CPU (`backend=vllm`, `backend.device=cpu`)
- [x] IPEX backend for CPU (`backend=ipex`, `backend.device=cpu`)
- [x] IPEX backend for XPU (`backend=ipex`, `backend.device=xpu`)
General backend features ๐งฐ
- [x] Device selection (`backend.device=cuda`), can be `cpu`, `cuda`, `mps`, etc.
- [x] Device ids selection (`backend.device_ids=0,1`), can be a list of device ids to run the benchmark on multiple devices.
- [x] Model selection (`backend.model=gpt2`), can be a model id from the HuggingFace model hub or an **absolute path** to a model folder.
- [x] "No weights" feature, to benchmark models without downloading their weights, using randomly initialized weights (`backend.no_weights=true`)
Backend specific features ๐งฐ
For more information on the features of each backend, you can check their respective configuration files:
- [VLLMConfig](optimum_benchmark/backends/vllm/config.py)
- [IPEXConfig](optimum_benchmark/backends/ipex/config.py)
- [OpenVINOConfig](optimum_benchmark/backends/openvino/config.py)
- [PyTXIConfig](optimum_benchmark/backends/py_txi/config.py)
- [PyTorchConfig](optimum_benchmark/backends/pytorch/config.py)
- [ONNXRuntimeConfig](optimum_benchmark/backends/onnxruntime/config.py)
- [TRTLLMConfig](optimum_benchmark/backends/tensorrt_llm/config.py)
## Contributing ๐ค
Contributions are welcome! And we're happy to help you get started. Feel free to open an issue or a pull request.
Things that we'd like to see:
- More backends (Tensorflow, TFLite, Jax, etc).
- More tests (for optimizations and quantization schemes).
- More hardware support (Habana Gaudi Processor (HPU), Apple M series, etc).
- Task evaluators for the most common tasks (would be great for output regression).
To get started, you can check the [CONTRIBUTING.md](CONTRIBUTING.md) file.