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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import subprocess
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from ansi2html import Ansi2HTMLConverter
|
| 5 |
+
from optimum_benchmark.task_utils import (
|
| 6 |
+
TASKS_TO_AUTOMODELS,
|
| 7 |
+
infer_task_from_model_name_or_path,
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def get_backend_config():
|
| 12 |
+
return [
|
| 13 |
+
# seed
|
| 14 |
+
gr.Textbox(label="backend.seed", value=42),
|
| 15 |
+
# inter_op_num_threads
|
| 16 |
+
gr.Textbox(
|
| 17 |
+
label="backend.inter_op_num_threads",
|
| 18 |
+
value=None,
|
| 19 |
+
placeholder=None,
|
| 20 |
+
),
|
| 21 |
+
# intra_op_num_threads
|
| 22 |
+
gr.Textbox(
|
| 23 |
+
label="backend.intra_op_num_threads",
|
| 24 |
+
value=None,
|
| 25 |
+
placeholder=None,
|
| 26 |
+
),
|
| 27 |
+
# initial_isolation_check
|
| 28 |
+
gr.Checkbox(label="backend.initial_isolation_check", value=True),
|
| 29 |
+
# continous_isolation_check
|
| 30 |
+
gr.Checkbox(label="backend.continous_isolation_check", value=True),
|
| 31 |
+
# delete_cache
|
| 32 |
+
gr.Checkbox(label="backend.delete_cache", value=False),
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def get_inference_config():
|
| 37 |
+
return [
|
| 38 |
+
# duration
|
| 39 |
+
gr.Textbox(label="benchmark.duration", value=10),
|
| 40 |
+
# warmup runs
|
| 41 |
+
gr.Textbox(label="benchmark.warmup_runs", value=1),
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def get_pytorch_config():
|
| 46 |
+
return [
|
| 47 |
+
# no_weights
|
| 48 |
+
gr.Checkbox(label="backend.no_weights"),
|
| 49 |
+
# device_map
|
| 50 |
+
gr.Dropdown(["auto", "sequential"], label="backend.device_map"),
|
| 51 |
+
# torch_dtype
|
| 52 |
+
gr.Dropdown(
|
| 53 |
+
["bfloat16", "float16", "float32", "auto"],
|
| 54 |
+
label="backend.torch_dtype",
|
| 55 |
+
),
|
| 56 |
+
# disable_grad
|
| 57 |
+
gr.Checkbox(label="backend.disable_grad"),
|
| 58 |
+
# eval_mode
|
| 59 |
+
gr.Checkbox(label="backend.eval_mode"),
|
| 60 |
+
# amp_autocast
|
| 61 |
+
gr.Checkbox(label="backend.amp_autocast"),
|
| 62 |
+
# amp_dtype
|
| 63 |
+
gr.Dropdown(["bfloat16", "float16"], label="backend.amp_dtype"),
|
| 64 |
+
# torch_compile
|
| 65 |
+
gr.Checkbox(label="backend.torch_compile"),
|
| 66 |
+
# bettertransformer
|
| 67 |
+
gr.Checkbox(label="backend.bettertransformer"),
|
| 68 |
+
# quantization_scheme
|
| 69 |
+
gr.Dropdown(["gptq", "bnb"], label="backend.quantization_scheme"),
|
| 70 |
+
# use_ddp
|
| 71 |
+
gr.Checkbox(label="backend.use_ddp"),
|
| 72 |
+
# peft_strategy
|
| 73 |
+
gr.Textbox(label="backend.peft_strategy"),
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
conv = Ansi2HTMLConverter()
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def run_experiment(kwargs):
|
| 81 |
+
arguments = [
|
| 82 |
+
"optimum-benchmark",
|
| 83 |
+
"--config-dir",
|
| 84 |
+
"./",
|
| 85 |
+
"--config-name",
|
| 86 |
+
"base_config",
|
| 87 |
+
]
|
| 88 |
+
for key, value in kwargs.items():
|
| 89 |
+
arguments.append(f"{key.label}={value if value != '' else 'null'}")
|
| 90 |
+
|
| 91 |
+
# stream subprocess output
|
| 92 |
+
process = subprocess.Popen(
|
| 93 |
+
arguments,
|
| 94 |
+
stdout=subprocess.PIPE,
|
| 95 |
+
stderr=subprocess.STDOUT,
|
| 96 |
+
universal_newlines=True,
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
ansi_text = ""
|
| 100 |
+
for ansi_line in iter(process.stdout.readline, ""):
|
| 101 |
+
# stream process output
|
| 102 |
+
print(ansi_line, end="")
|
| 103 |
+
# append line to ansi text
|
| 104 |
+
ansi_text += ansi_line
|
| 105 |
+
# convert ansi to html
|
| 106 |
+
html_text = conv.convert(ansi_text)
|
| 107 |
+
# extract style from html
|
| 108 |
+
style = html_text.split('<style type="text/css">')[1].split("</style>")[0]
|
| 109 |
+
# parse style into dict
|
| 110 |
+
style_dict = {}
|
| 111 |
+
for line in style.split("\n"):
|
| 112 |
+
if line:
|
| 113 |
+
key, value = line.split("{")
|
| 114 |
+
key = key.replace(".", "").strip()
|
| 115 |
+
value = value.split("}")[0].strip()
|
| 116 |
+
style_dict[key] = value
|
| 117 |
+
|
| 118 |
+
# replace style in html
|
| 119 |
+
for key, value in style_dict.items():
|
| 120 |
+
html_text = html_text.replace(f'class="{key}"', f'style="{value}"')
|
| 121 |
+
|
| 122 |
+
yield html_text
|
| 123 |
+
|
| 124 |
+
return html_text
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
with gr.Blocks() as demo:
|
| 128 |
+
# title text
|
| 129 |
+
gr.HTML("<h1 style='text-align: center'>🤗 Optimum Benchmark 🏋️</h1>")
|
| 130 |
+
# explanation text
|
| 131 |
+
gr.Markdown(
|
| 132 |
+
"This is a demo space of [Optimum-Benchmark](https://github.com/huggingface/optimum-benchmark.git)."
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
model = gr.Textbox(
|
| 136 |
+
label="model",
|
| 137 |
+
value="bert-base-uncased",
|
| 138 |
+
)
|
| 139 |
+
task = gr.Dropdown(
|
| 140 |
+
label="task",
|
| 141 |
+
value="text-classification",
|
| 142 |
+
choices=list(TASKS_TO_AUTOMODELS.keys()),
|
| 143 |
+
)
|
| 144 |
+
device = gr.Dropdown(
|
| 145 |
+
value="cpu",
|
| 146 |
+
choices=["cpu", "cuda"],
|
| 147 |
+
label="device",
|
| 148 |
+
)
|
| 149 |
+
expetiment_name = gr.Textbox(
|
| 150 |
+
label="experiment_name",
|
| 151 |
+
value=f"experiment_{random.getrandbits(16)}",
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
model.submit(fn=infer_task_from_model_name_or_path, inputs=[model], outputs=[task])
|
| 155 |
+
|
| 156 |
+
with gr.Row():
|
| 157 |
+
with gr.Column(variant="panel"):
|
| 158 |
+
backend = gr.Dropdown(
|
| 159 |
+
["pytorch", "onnxruntime", "openvino", "neural-compressor"],
|
| 160 |
+
label="backend",
|
| 161 |
+
value="pytorch",
|
| 162 |
+
container=True,
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
with gr.Column(variant="panel"):
|
| 166 |
+
with gr.Accordion(label="Backend Config", open=False):
|
| 167 |
+
backend_config = get_backend_config() + get_pytorch_config()
|
| 168 |
+
|
| 169 |
+
with gr.Row():
|
| 170 |
+
with gr.Column(variant="panel"):
|
| 171 |
+
benchmark = gr.Dropdown(
|
| 172 |
+
choices=["inference", "training"],
|
| 173 |
+
label="benchmark",
|
| 174 |
+
value="inference",
|
| 175 |
+
container=True,
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
with gr.Column(variant="panel"):
|
| 179 |
+
with gr.Accordion(label="Benchmark Config", open=False):
|
| 180 |
+
benchmark_config = get_inference_config()
|
| 181 |
+
|
| 182 |
+
# run benchmark button
|
| 183 |
+
run_benchmark = gr.Button(value="Run Benchmark", variant="primary")
|
| 184 |
+
# accordion with output logs
|
| 185 |
+
with gr.Accordion(label="Logs:", open=True):
|
| 186 |
+
logs = gr.HTML()
|
| 187 |
+
|
| 188 |
+
run_benchmark.click(
|
| 189 |
+
fn=run_experiment,
|
| 190 |
+
inputs={
|
| 191 |
+
expetiment_name,
|
| 192 |
+
model,
|
| 193 |
+
task,
|
| 194 |
+
device,
|
| 195 |
+
backend,
|
| 196 |
+
benchmark,
|
| 197 |
+
*backend_config,
|
| 198 |
+
*benchmark_config,
|
| 199 |
+
},
|
| 200 |
+
outputs=[logs],
|
| 201 |
+
queue=True,
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
if __name__ == "__main__":
|
| 206 |
+
demo.queue().launch()
|
base_config.yaml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defaults:
|
| 2 |
+
- backend: pytorch # default backend
|
| 3 |
+
- benchmark: inference # default benchmark
|
| 4 |
+
- experiment # inheriting experiment schema
|
| 5 |
+
- _self_ # for hydra 1.1 compatibility
|
| 6 |
+
- override hydra/job_logging: colorlog # colorful logging
|
| 7 |
+
- override hydra/hydra_logging: colorlog # colorful logging
|
| 8 |
+
|
| 9 |
+
hydra:
|
| 10 |
+
run:
|
| 11 |
+
dir: runs/${experiment_name}
|
| 12 |
+
job:
|
| 13 |
+
chdir: true
|
| 14 |
+
env_set:
|
| 15 |
+
CUDA_VISIBLE_DEVICES: 0
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
ansi2html
|
| 3 |
+
git[onnxruntime,openvino,neural-compressor]+https://github.com/huggingface/optimum-benchmark.git
|