import subprocess
import gradio as gr
import pandas as pd
from ansi2html import Ansi2HTMLConverter
ansi2html_converter = Ansi2HTMLConverter(inline=True)
def run_benchmark(kwargs):
for key, value in kwargs.copy().items():
if key.label == "Compare to Baseline":
baseline = value
kwargs.pop(key)
elif key.label == "experiment_name":
experiment_name = value
kwargs.pop(key)
elif key.label == "model":
model = value
kwargs.pop(key)
elif key.label == "task":
task = value
kwargs.pop(key)
elif key.label == "device":
device = value
kwargs.pop(key)
elif key.label == "backend":
backend = value
kwargs.pop(key)
elif key.label == "benchmark":
benchmark = value
kwargs.pop(key)
else:
continue
if baseline:
baseline_arguments = [
"optimum-benchmark",
"--config-dir",
"./configs",
"--config-name",
"base_config",
f"backend=pytorch",
f"task={task}",
f"model={model}",
f"device={device}",
f"benchmark={benchmark}",
f"experiment_name=baseline",
]
for component, value in kwargs.items():
if f"{benchmark}." in component.label:
label = component.label.replace(f"{benchmark}.", "benchmark.")
if isinstance(component, gr.Dataframe):
for sub_key, sub_value in zip(component.headers, value[0]):
baseline_arguments.append(f"++{label}.{sub_key}={sub_value}")
else:
baseline_arguments.append(f"{label}={value}")
# yield from run_experiment(baseline_arguments) but get the return code
baseline_return_code, html_text = yield from run_experiment(baseline_arguments, "")
if baseline_return_code is not None and baseline_return_code != 0:
yield gr.update(value=html_text), gr.update(interactive=True), gr.update(visible=False)
return
else:
html_text = ""
arguments = [
"optimum-benchmark",
"--config-dir",
"./configs",
"--config-name",
"base_config",
f"task={task}",
f"model={model}",
f"device={device}",
f"backend={backend}",
f"benchmark={benchmark}",
f"experiment_name={experiment_name}",
]
for component, value in kwargs.items():
if f"{backend}." in component.label or f"{benchmark}." in component.label:
label = component.label.replace(f"{backend}.", "backend.").replace(f"{benchmark}.", "benchmark.")
if isinstance(component, gr.Dataframe):
for sub_key, sub_value in zip(component.headers, value[0]):
arguments.append(f"++{label}.{sub_key}={sub_value}")
else:
arguments.append(f"{label}={value}")
return_code, html_text = yield from run_experiment(arguments, html_text)
if return_code is not None and return_code != 0:
yield gr.update(value=html_text), gr.update(interactive=True), gr.update(visible=False)
return
if baseline:
baseline_table = pd.read_csv(f"runs/baseline/{benchmark}_results.csv", index_col=0)
table = pd.read_csv(f"runs/{experiment_name}/{benchmark}_results.csv", index_col=0)
# concat tables
table = pd.concat([baseline_table, table], axis=0)
table = postprocess_table(table, experiment_name)
else:
table = pd.read_csv(f"runs/{experiment_name}/{benchmark}_results.csv", index_col=0)
table_update = gr.update(visible=True, value={"headers": list(table.columns), "data": table.values.tolist()})
yield gr.update(value=html_text), gr.update(interactive=True), table_update
return
def run_experiment(args, html_text=""):
command = "
".join(args)
html_text += f"