Home

Awesome

<p align="center"><img src="https://raw.githubusercontent.com/huggingface/optimum-benchmark/main/logo.png" alt="Optimum-Benchmark Logo" width="350" style="max-width: 100%;" /></p> <p align="center"><q>All benchmarks are wrong, some will cost you less than others.</q></p> <h1 align="center">Optimum-Benchmark ๐Ÿ‹๏ธ</h1>

PyPI - Python Version PyPI - Version PyPI - Downloads PyPI - Implementation PyPI - Format PyPI - License

Optimum-Benchmark is a unified multi-backend & multi-device utility for benchmarking Transformers, Diffusers, PEFT, TIMM and Optimum libraries, along with all their supported optimizations & quantization schemes, for inference & training, in distributed & non-distributed settings, in the most correct, efficient and scalable way possible.

News ๐Ÿ“ฐ

Motivations ๐ŸŽฏ

ย 

[!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 ๐Ÿ“ˆ

API_CPU API_CUDA API_MISC API_ROCM

CLI ๐Ÿ“ˆ

CLI_CPU_IPEX CLI_CPU_LLAMA_CPP CLI_CPU_ONNXRUNTIME CLI_CPU_OPENVINO CLI_CPU_PYTORCH CLI_CPU_PY_TXI CLI_CUDA_ONNXRUNTIME CLI_CUDA_PYTORCH CLI_CUDA_PY_TXI CLI_CUDA_TENSORRT_LLM CLI_CUDA_TORCH_ORT CLI_CUDA_VLLM CLI_MISC CLI_ROCM_PYTORCH

Quickstart ๐Ÿš€

Installation ๐Ÿ“ฅ

You can install the latest released version of optimum-benchmark on PyPI:

pip install optimum-benchmark

or you can install the latest version from the main branch on GitHub:

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:

git clone https://github.com/huggingface/optimum-benchmark.git
cd optimum-benchmark
pip install -e .
<details> <summary>Advanced install options</summary>

Depending on the backends you want to use, you can install optimum-benchmark with the following extras:

We also support the following extra extra dependencies:

</details>

Running benchmarks using the Python API ๐Ÿงช

You can run benchmarks from the Python API, using the Benchmark class and its launch method. It takes a BenchmarkConfig object as input, runs the benchmark in an isolated process and returns a BenchmarkReport object containing the benchmark results.

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.

from optimum_benchmark import Benchmark, BenchmarkConfig, TorchrunConfig, InferenceConfig, PyTorchConfig
from optimum_benchmark.logging_utils import setup_logging

setup_logging(level="INFO", handlers=["console"])

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)

    # convert artifacts to a dictionary or dataframe
    benchmark_config.to_dict() # or benchmark_config.to_dataframe()

    # save artifacts to disk as json or csv files
    benchmark_report.save_csv("benchmark_report.csv") # or benchmark_report.save_json("benchmark_report.json")

    # push artifacts to the hub
    benchmark_config.push_to_hub("IlyasMoutawwakil/pytorch_gpt2") # or benchmark_config.push_to_hub("IlyasMoutawwakil/pytorch_gpt2")

    # or merge them into a single artifact
    benchmark = Benchmark(config=benchmark_config, report=benchmark_report)
    benchmark.save_json("benchmark.json") # or benchmark.save_csv("benchmark.csv")
    benchmark.push_to_hub("IlyasMoutawwakil/pytorch_gpt2")

    # load artifacts from the hub
    benchmark = Benchmark.from_hub("IlyasMoutawwakil/pytorch_gpt2") # or Benchmark.from_hub("IlyasMoutawwakil/pytorch_gpt2")

    # or load them from disk
    benchmark = Benchmark.load_json("benchmark.json") # or Benchmark.load_csv("benchmark_report.csv")

If you're on VSCode, you can hover over the configuration classes to see the available parameters and their descriptions. You can also see the available parameters in the 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. --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.

optimum-benchmark --config-dir examples/ --config-name cuda_pytorch_bert

This will run the benchmark using the configuration in examples/cuda_pytorch_bert.yaml and store the results in runs/cuda_pytorch_bert.

The resulting files are :

<details> <summary>Advanced CLI options</summary>

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:

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).

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. They are heavily commented to help you understand the structure of the configuration files.

</details>

Features ๐ŸŽจ

optimum-benchmark allows you to run benchmarks with minimal configuration. A benchmark is defined by three main components:

Launchers ๐Ÿš€

<details> <summary>General Launcher features ๐Ÿงฐ</summary> </details>

Scenarios ๐Ÿ‹

<details> <summary>Inference scenario features ๐Ÿงฐ</summary>

See InferenceConfig for more information.

</details> <details> <summary>Training scenario features ๐Ÿงฐ</summary>

See TrainingConfig for more information.

</details>

Backends & Devices ๐Ÿ“ฑ

<details> <summary>General backend features ๐Ÿงฐ</summary> </details> <details> <summary>Backend specific features ๐Ÿงฐ</summary>

For more information on the features of each backend, you can check their respective configuration files:

</details>

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:

To get started, you can check the CONTRIBUTING.md file.