Spaces:
Sleeping
Sleeping
File size: 2,336 Bytes
7f9a235 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import pprint
import subprocess
import gradio as gr
from ansi2html import Ansi2HTMLConverter
ansi2html_converter = Ansi2HTMLConverter(inline=True)
def run_benchmark(kwargs):
for key, value in kwargs.copy().items():
if 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
arguments = [
"optimum-benchmark",
"--config-dir",
"./",
"--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}")
pprint.pprint(arguments)
# stream subprocess output
process = subprocess.Popen(
arguments,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
)
ansi_text = ""
for ansi_line in iter(process.stdout.readline, ""):
if "torch.distributed.nn.jit.instantiator" in ansi_line:
continue
# stream process output
print(ansi_line, end="")
# append line to ansi text
ansi_text += ansi_line
# convert ansi to html
html_text = ansi2html_converter.convert(ansi_text)
# stream html output
yield html_text
return html_text
|