File size: 5,973 Bytes
d1e3b68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import random
import subprocess
import gradio as gr
from ansi2html import Ansi2HTMLConverter
from optimum_benchmark.task_utils import (
    TASKS_TO_AUTOMODELS,
    infer_task_from_model_name_or_path,
)


def get_backend_config():
    return [
        # seed
        gr.Textbox(label="backend.seed", value=42),
        # inter_op_num_threads
        gr.Textbox(
            label="backend.inter_op_num_threads",
            value=None,
            placeholder=None,
        ),
        # intra_op_num_threads
        gr.Textbox(
            label="backend.intra_op_num_threads",
            value=None,
            placeholder=None,
        ),
        # initial_isolation_check
        gr.Checkbox(label="backend.initial_isolation_check", value=True),
        # continous_isolation_check
        gr.Checkbox(label="backend.continous_isolation_check", value=True),
        # delete_cache
        gr.Checkbox(label="backend.delete_cache", value=False),
    ]


def get_inference_config():
    return [
        # duration
        gr.Textbox(label="benchmark.duration", value=10),
        # warmup runs
        gr.Textbox(label="benchmark.warmup_runs", value=1),
    ]


def get_pytorch_config():
    return [
        # no_weights
        gr.Checkbox(label="backend.no_weights"),
        # device_map
        gr.Dropdown(["auto", "sequential"], label="backend.device_map"),
        # torch_dtype
        gr.Dropdown(
            ["bfloat16", "float16", "float32", "auto"],
            label="backend.torch_dtype",
        ),
        # disable_grad
        gr.Checkbox(label="backend.disable_grad"),
        # eval_mode
        gr.Checkbox(label="backend.eval_mode"),
        # amp_autocast
        gr.Checkbox(label="backend.amp_autocast"),
        # amp_dtype
        gr.Dropdown(["bfloat16", "float16"], label="backend.amp_dtype"),
        # torch_compile
        gr.Checkbox(label="backend.torch_compile"),
        # bettertransformer
        gr.Checkbox(label="backend.bettertransformer"),
        # quantization_scheme
        gr.Dropdown(["gptq", "bnb"], label="backend.quantization_scheme"),
        # use_ddp
        gr.Checkbox(label="backend.use_ddp"),
        # peft_strategy
        gr.Textbox(label="backend.peft_strategy"),
    ]


conv = Ansi2HTMLConverter()


def run_experiment(kwargs):
    arguments = [
        "optimum-benchmark",
        "--config-dir",
        "./",
        "--config-name",
        "base_config",
    ]
    for key, value in kwargs.items():
        arguments.append(f"{key.label}={value if value != '' else 'null'}")

    # 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, ""):
        # stream process output
        print(ansi_line, end="")
        # append line to ansi text
        ansi_text += ansi_line
        # convert ansi to html
        html_text = conv.convert(ansi_text)
        # extract style from html
        style = html_text.split('<style type="text/css">')[1].split("</style>")[0]
        # parse style into dict
        style_dict = {}
        for line in style.split("\n"):
            if line:
                key, value = line.split("{")
                key = key.replace(".", "").strip()
                value = value.split("}")[0].strip()
                style_dict[key] = value

        # replace style in html
        for key, value in style_dict.items():
            html_text = html_text.replace(f'class="{key}"', f'style="{value}"')

        yield html_text

    return html_text


with gr.Blocks() as demo:
    # title text
    gr.HTML("<h1 style='text-align: center'>🤗 Optimum Benchmark 🏋️</h1>")
    # explanation text
    gr.Markdown(
        "This is a demo space of [Optimum-Benchmark](https://github.com/huggingface/optimum-benchmark.git)."
    )

    model = gr.Textbox(
        label="model",
        value="bert-base-uncased",
    )
    task = gr.Dropdown(
        label="task",
        value="text-classification",
        choices=list(TASKS_TO_AUTOMODELS.keys()),
    )
    device = gr.Dropdown(
        value="cpu",
        choices=["cpu", "cuda"],
        label="device",
    )
    expetiment_name = gr.Textbox(
        label="experiment_name",
        value=f"experiment_{random.getrandbits(16)}",
    )

    model.submit(fn=infer_task_from_model_name_or_path, inputs=[model], outputs=[task])

    with gr.Row():
        with gr.Column(variant="panel"):
            backend = gr.Dropdown(
                ["pytorch", "onnxruntime", "openvino", "neural-compressor"],
                label="backend",
                value="pytorch",
                container=True,
            )

        with gr.Column(variant="panel"):
            with gr.Accordion(label="Backend Config", open=False):
                backend_config = get_backend_config() + get_pytorch_config()

    with gr.Row():
        with gr.Column(variant="panel"):
            benchmark = gr.Dropdown(
                choices=["inference", "training"],
                label="benchmark",
                value="inference",
                container=True,
            )

        with gr.Column(variant="panel"):
            with gr.Accordion(label="Benchmark Config", open=False):
                benchmark_config = get_inference_config()

    # run benchmark button
    run_benchmark = gr.Button(value="Run Benchmark", variant="primary")
    # accordion with output logs
    with gr.Accordion(label="Logs:", open=True):
        logs = gr.HTML()

    run_benchmark.click(
        fn=run_experiment,
        inputs={
            expetiment_name,
            model,
            task,
            device,
            backend,
            benchmark,
            *backend_config,
            *benchmark_config,
        },
        outputs=[logs],
        queue=True,
    )


if __name__ == "__main__":
    demo.queue().launch()