transformers/benchmark
Luc Georges 9a94dfe123
feat: add benchmarks_entrypoint.py (#34495)
* feat: add `benchmarks_entrypoint.py`

Adding `benchmarks_entrypoint.py` file, which will be run from the
benchmarks CI.

This python script will list all python files from the `benchmark/`
folder and run the included `run_benchmark` function, allowing people to
add new benchmarks scripts.

* feat: add `MetricsRecorder`

* feat: update dashboard

* fix: add missing arguments to `MetricsRecorder`

* feat: update dash & add datasource + `default.yml`

* fix: move responsibility to create `MetricsRecorder` in bench script

* fix: update incorrect datasource UID

* fix: incorrect variable values

* debug: benchmark entrypoint script

* refactor: update log level

* fix: update broken import

* feat: add debug log in `MetricsRecorder`

* debug: set log level to debug

* fix: set connection `autocommit` to `True`
2024-12-18 18:59:07 +01:00
..
config [Benchmark] Reuse optimum-benchmark (#30615) 2024-05-21 15:15:19 +02:00
__init__.py [Benchmark] Reuse optimum-benchmark (#30615) 2024-05-21 15:15:19 +02:00
benchmark.py Fix benchmark script (#32635) 2024-08-22 16:07:47 +02:00
benchmarks_entrypoint.py feat: add benchmarks_entrypoint.py (#34495) 2024-12-18 18:59:07 +01:00
default.yml feat: add benchmarks_entrypoint.py (#34495) 2024-12-18 18:59:07 +01:00
grafana_dashboard.json feat: add benchmarks_entrypoint.py (#34495) 2024-12-18 18:59:07 +01:00
grafana_datasource.yaml feat: add benchmarks_entrypoint.py (#34495) 2024-12-18 18:59:07 +01:00
init_db.sql feat: add benchmarks_entrypoint.py (#34495) 2024-12-18 18:59:07 +01:00
llama.py feat: add benchmarks_entrypoint.py (#34495) 2024-12-18 18:59:07 +01:00
optimum_benchmark_wrapper.py [Benchmark] Reuse optimum-benchmark (#30615) 2024-05-21 15:15:19 +02:00
README.md feat: add benchmarks_entrypoint.py (#34495) 2024-12-18 18:59:07 +01:00
requirements.txt refactor: benchmarks (#33896) 2024-10-11 18:03:29 +02:00

Benchmarks

You might want to add new benchmarks.

You will need to define a python function named run_benchmark in your python file and the file must be located in this benchmark/ directory.

The expected function signature is the following:

def run_benchmark(logger: Logger, branch: str, commit_id: str, commit_msg: str, num_tokens_to_generate=100):

Writing metrics to the database

MetricRecorder is thread-safe, in the sense of the python Thread. This means you can start a background thread to do the readings on the device measurements while not blocking the main thread to execute the model measurements.

cf llama.py to see an example of this in practice.

from benchmarks_entrypoint import MetricsRecorder
import psycopg2

def run_benchmark(logger: Logger, branch: str, commit_id: str, commit_msg: str, num_tokens_to_generate=100):
  metrics_recorder = MetricsRecorder(psycopg2.connect("dbname=metrics"), logger, branch, commit_id, commit_msg)
  benchmark_id = metrics_recorder.initialise_benchmark({"gpu_name": gpu_name, "model_id": model_id})
    # To collect device measurements
    metrics_recorder.collect_device_measurements(
        benchmark_id, cpu_util, mem_megabytes, gpu_util, gpu_mem_megabytes
    )
    # To collect your model measurements
    metrics_recorder.collect_model_measurements(
        benchmark_id,
        {
            "model_load_time": model_load_time,
            "first_eager_forward_pass_time_secs": first_eager_fwd_pass_time,
            "second_eager_forward_pass_time_secs": second_eager_fwd_pass_time,
            "first_eager_generate_time_secs": first_eager_generate_time,
            "second_eager_generate_time_secs": second_eager_generate_time,
            "time_to_first_token_secs": time_to_first_token,
            "time_to_second_token_secs": time_to_second_token,
            "time_to_third_token_secs": time_to_third_token,
            "time_to_next_token_mean_secs": mean_time_to_next_token,
            "first_compile_generate_time_secs": first_compile_generate_time,
            "second_compile_generate_time_secs": second_compile_generate_time,
            "third_compile_generate_time_secs": third_compile_generate_time,
            "fourth_compile_generate_time_secs": fourth_compile_generate_time,
        },
    )