File size: 11,389 Bytes
c8763bd
 
 
4cfc121
c8763bd
 
a894537
0f1bf97
 
 
c382b2a
0f1bf97
 
 
 
fbc8c87
0f1bf97
 
a894537
 
 
0f1bf97
c8763bd
 
 
d262fb3
708b21b
c8763bd
a894537
 
f41da95
 
a894537
dcfabfb
6db5c25
bf397e6
0f1bf97
804d27e
97058d0
a894537
ec9f1c7
 
dcfabfb
a894537
 
 
 
0f1bf97
 
 
 
ad86e2e
223c247
ec9f1c7
 
223c247
0f1bf97
5490c7c
efc3d5b
d262fb3
c8763bd
 
b3a1bf0
e2c5bda
 
 
223c247
0f1bf97
a894537
ec9f1c7
a894537
ec9f1c7
 
 
 
223c247
 
5490c7c
89517bf
bf397e6
f2d0515
223c247
 
f2d0515
223c247
 
 
f2d0515
223c247
f2d0515
223c247
f2d0515
75b2595
bf397e6
 
ec9f1c7
 
 
 
 
5490c7c
 
 
 
 
 
 
 
b3a1bf0
 
 
5490c7c
 
a600c79
a894537
a600c79
a894537
b3a1bf0
f41da95
fda0b51
a894537
 
0f1bf97
930b7c1
c8763bd
 
b3a1bf0
 
f2d0515
8e785e9
8e8c463
0f1bf97
 
a894537
0f1bf97
 
 
a894537
0f1bf97
 
bf397e6
0f1bf97
 
 
b3a1bf0
8e8c463
 
5236273
8e8c463
0f1bf97
 
 
 
 
8e8c463
b3a1bf0
fbbd324
 
8985298
d3abea5
5643bcb
4cfc121
5643bcb
0f1bf97
 
 
 
a894537
bf397e6
 
 
0f1bf97
a894537
0f1bf97
 
8e8c463
 
 
 
 
97058d0
c4dcfe7
 
 
 
 
 
 
97058d0
b3a1bf0
 
 
a894537
0f1bf97
 
e2d1670
 
 
 
 
 
 
 
 
 
 
a894537
c4dcfe7
b3a1bf0
 
 
 
 
 
 
 
 
c796580
 
 
8e8c463
 
c8763bd
 
8e8c463
c8763bd
8e8c463
 
5721994
c8763bd
67b4a03
 
5721994
271d809
 
 
67b4a03
 
 
a894537
 
67b4a03
44c696c
97058d0
5721994
 
271d809
5721994
 
67b4a03
 
 
 
 
28d58c8
97058d0
5721994
271d809
ec9f1c7
271d809
 
67b4a03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6db5c25
67b4a03
 
6db5c25
 
67b4a03
 
 
 
 
 
 
 
c796580
 
67b4a03
 
 
 
 
c796580
5721994
 
c382b2a
c796580
 
 
 
 
4cfc121
fbc8c87
 
 
 
 
 
 
 
 
 
c796580
fbc8c87
b3a1bf0
9dc4521
00642fb
 
 
 
 
 
d262fb3
c796580
d262fb3
c8763bd
0f1bf97
 
 
 
 
 
c8763bd
d262fb3
 
c8763bd
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import os
import gradio as gr
import pandas as pd
import plotly.express as px
from apscheduler.schedulers.background import BackgroundScheduler

from src.assets.css_html_js import custom_css, custom_js
from src.assets.text_content import (
    TITLE,
    INTRODUCTION_TEXT,
    ABOUT_TEXT,
    CITATION_BUTTON_LABEL,
    CITATION_BUTTON_TEXT,
)
from src.utils import (
    change_tab,
    restart_space,
    load_dataset_repo,
    process_model_name,
    process_model_type,
    process_weight_class,
)


LLM_PERF_LEADERBOARD_REPO = "optimum/llm-perf-leaderboard"
LLM_PERF_DATASET_REPO = "optimum/llm-perf-dataset"
OPTIMUM_TOKEN = os.environ.get("OPTIMUM_TOKEN", None)


ALL_COLUMNS_MAPPING = {
    "model_type": "Type πŸ€—",
    "weight_class": "Class πŸ‹οΈ",
    #
    "backend.name": "Backend 🏭",
    "backend.torch_dtype": "Dtype πŸ“₯",
    "optimizations": "Optimizations πŸ› οΈ",
    #
    "generate.throughput(tokens/s)": "Throughput (tokens/s) ⬆️",
    "forward.peak_memory(MB)": "Peak Memory (MB) ⬇️",
    #
    "best_scored_model": "Best Scored Model πŸ†",
    "best_score": "Best Score (%) ⬆️",
}
ALL_COLUMNS_DATATYPES = [
    "str",
    "str",
    #
    "str",
    "str",
    "str",
    #
    "number",
    "number",
    #
    "markdown",
    "number",
]
SORTING_COLUMN = ["tradeoff"]

llm_perf_dataset_repo = load_dataset_repo(LLM_PERF_DATASET_REPO, OPTIMUM_TOKEN)


def get_benchmark_df(benchmark="1xA100-80GB"):
    if llm_perf_dataset_repo:
        llm_perf_dataset_repo.git_pull()

    # load and merge
    bench_df = pd.read_csv(f"./llm-perf-dataset/reports/{benchmark}.csv")
    scores_df = pd.read_csv(
        "./llm-perf-dataset/reports/Weighted+Classed-Open-LLM-Leaderboard.csv"
    )

    bench_df["merge_id"] = bench_df.experiment_name.str.split("_1_1000_").str[-1]
    scores_df["merge_id"] = scores_df.weight_class + "_" + scores_df.model_type
    merged_df = bench_df.merge(scores_df, on="merge_id")

    # add optimizations
    merged_df["optimizations"] = merged_df[
        ["backend.bettertransformer", "backend.load_in_8bit", "backend.load_in_4bit"]
    ].apply(
        lambda x: ", ".join(
            filter(
                lambda x: x != "",
                [
                    "BetterTransformer" if x[0] == True else "",
                    "LLM.int8" if x[1] == True else "",
                    "LLM.fp4" if x[2] == True else "",
                ],
            ),
        )
        if any([x[0] == True, x[1] == True, x[2] == True])
        else "None",
        axis=1,
    )

    # remove score for quantized models
    merged_df.loc[
        merged_df["optimizations"].str.contains("LLM.int8|LLM.fp4"), "best_score"
    ] = "Not Evaluated"

    # create composite score
    score_distance = 100 - merged_df["best_score"]
    # normalize latency between 0 and 100
    latency_distance = merged_df["generate.latency(s)"]
    merged_df["tradeoff"] = (score_distance**2 + latency_distance**2) ** 0.5
    merged_df["tradeoff"] = merged_df["tradeoff"].round(2)

    return merged_df


def get_benchmark_table(bench_df):
    # sort
    bench_df.sort_values(by=SORTING_COLUMN, ascending=True, inplace=True)
    # filter
    bench_df = bench_df[list(ALL_COLUMNS_MAPPING.keys())]
    # rename
    bench_df.rename(columns=ALL_COLUMNS_MAPPING, inplace=True)
    # transform
    bench_df["Type πŸ€—"] = bench_df["Type πŸ€—"].apply(process_model_type)
    bench_df["Class πŸ‹οΈ"] = bench_df["Class πŸ‹οΈ"].apply(process_weight_class)
    bench_df["Best Scored Model πŸ†"] = bench_df["Best Scored Model πŸ†"].apply(
        process_model_name
    )
    return bench_df


def get_benchmark_plot(bench_df):
    # untill falcon gets fixed / natively supported
    bench_df = bench_df[bench_df["generate.latency(s)"] < 150]

    fig = px.scatter(
        bench_df,
        x="generate.latency(s)",
        y="best_score",
        color="model_type",
        size="forward.peak_memory(MB)",
        custom_data=[
            "best_scored_model",
            "backend.name",
            "backend.torch_dtype",
            "optimizations",
            "forward.peak_memory(MB)",
            "generate.throughput(tokens/s)",
        ],
        color_discrete_sequence=px.colors.qualitative.Light24,
    )

    fig.update_layout(
        title={
            "text": "Model Score vs. Latency vs. Memory",
            "y": 0.95,
            "x": 0.5,
            "xanchor": "center",
            "yanchor": "top",
        },
        xaxis_title="Per 1000 Tokens Latency (s)",
        yaxis_title="Open LLM Score (%)",
        legend_title="Model Type",
        width=1200,
        height=600,
    )

    fig.update_traces(
        hovertemplate="<br>".join(
            [
                "Model: %{customdata[0]}",
                "Backend: %{customdata[1]}",
                "Load Datatype: %{customdata[2]}",
                "Optimizations: %{customdata[3]}",
                "Peak Memory (MB): %{customdata[4]}",
                "Throughput (tokens/s): %{customdata[5]}",
                "Per 1000 Tokens Latency (s): %{x}",
                "Open LLM Score (%): %{y}",
            ]
        )
    )

    return fig


def filter_query(
    text,
    backends,
    datatypes,
    optimizations,
    score,
    memory,
    benchmark="1xA100-80GB",
):
    raw_df = get_benchmark_df(benchmark=benchmark)

    filtered_df = raw_df[
        raw_df["best_scored_model"].str.lower().str.contains(text.lower())
        & raw_df["backend.name"].isin(backends)
        & raw_df["backend.torch_dtype"].isin(datatypes)
        & (
            pd.concat(
                [
                    raw_df["optimizations"].str.contains(optimization)
                    for optimization in optimizations
                ],
                axis=1,
            ).any(axis="columns")
            if len(optimizations) > 0
            else True
        )
        & (raw_df["best_score"] >= score)
        & (raw_df["forward.peak_memory(MB)"] <= memory)
    ]

    filtered_table = get_benchmark_table(filtered_df)
    filtered_plot = get_benchmark_plot(filtered_df)

    return filtered_table, filtered_plot


# Dataframes
A100_df = get_benchmark_df(benchmark="1xA100-80GB")
A100_table = get_benchmark_table(A100_df)
A100_plot = get_benchmark_plot(A100_df)

# Demo interface
demo = gr.Blocks(css=custom_css)
with demo:
    # leaderboard title
    gr.HTML(TITLE)

    # introduction text
    gr.Markdown(INTRODUCTION_TEXT, elem_classes="descriptive-text")

    # leaderboard tabs
    with gr.Tabs(elem_classes="A100-tabs") as A100_tabs:
        with gr.TabItem("πŸ–₯️ A100-80GB Benchmark πŸ†", id=0):
            gr.HTML(
                "πŸ‘‰ Scroll to the right πŸ‘‰ for more columns.", elem_id="descriptive-text"
            )
            # Original leaderboard table
            A100_leaderboard = gr.components.Dataframe(
                value=A100_table,
                datatype=ALL_COLUMNS_DATATYPES,
                headers=list(ALL_COLUMNS_MAPPING.values()),
                elem_id="1xA100-table",
            )

        with gr.TabItem("πŸ–₯️ A100-80GB Plot πŸ“Š", id=1):
            gr.HTML(
                "πŸ‘† Hover over the points πŸ‘† for additional information.",
                elem_id="descriptive-text",
            )
            # Original leaderboard plot
            A100_plotly = gr.components.Plot(
                value=A100_plot,
                elem_id="1xA100-plot",
                show_label=False,
            )

        with gr.TabItem("Control Panel πŸŽ›οΈ", id=2):
            gr.HTML(
                "Use this control panel to filter the leaderboard's table and plot.",
                elem_id="descriptive-text",
            )
            # control panel interface
            with gr.Row():
                with gr.Column(scale=1):
                    search_bar = gr.Textbox(
                        label="Model πŸ€—",
                        info="πŸ” Search for a model name",
                        elem_id="search-bar",
                    )
                with gr.Column(scale=1):
                    with gr.Box():
                        score_slider = gr.Slider(
                            label="Open LLM Score πŸ“ˆ",
                            info="🎚️ Slide to minimum Open LLM score",
                            value=0,
                            elem_id="threshold-slider",
                        )
                with gr.Column(scale=1):
                    with gr.Box():
                        memory_slider = gr.Slider(
                            label="Peak Memory (MB) πŸ“ˆ",
                            info="🎚️ Slide to maximum Peak Memory",
                            minimum=0,
                            maximum=80 * 1024,
                            value=80 * 1024,
                            elem_id="memory-slider",
                        )

            with gr.Row():
                with gr.Column(scale=1):
                    backend_checkboxes = gr.CheckboxGroup(
                        label="Backends 🏭",
                        choices=["pytorch", "onnxruntime"],
                        value=["pytorch", "onnxruntime"],
                        info="β˜‘οΈ Select the backends",
                        elem_id="backend-checkboxes",
                    )
                with gr.Column(scale=1):
                    datatype_checkboxes = gr.CheckboxGroup(
                        label="Dtypes πŸ“₯",
                        choices=["float32", "float16"],
                        value=["float32", "float16"],
                        info="β˜‘οΈ Select the load dtypes",
                        elem_id="dtype-checkboxes",
                    )
                with gr.Column(scale=2):
                    optimizations_checkboxes = gr.CheckboxGroup(
                        label="Optimizations πŸ› οΈ",
                        choices=["None", "BetterTransformer", "LLM.int8", "LLM.fp4"],
                        value=["None", "BetterTransformer", "LLM.int8", "LLM.fp4"],
                        info="β˜‘οΈ Select the optimizations",
                        elem_id="optimizations-checkboxes",
                    )

            with gr.Row():
                filter_button = gr.Button(
                    value="Filter πŸš€",
                    elem_id="filter-button",
                )

        with gr.TabItem("About πŸ“–", id=3):
            gr.HTML(ABOUT_TEXT, elem_classes="descriptive-text")

    demo.load(
        change_tab,
        A100_tabs,
        _js=custom_js,
    )

    filter_button.click(
        filter_query,
        [
            search_bar,
            backend_checkboxes,
            datatype_checkboxes,
            optimizations_checkboxes,
            score_slider,
            memory_slider,
        ],
        [A100_leaderboard, A100_plotly],
    )

    with gr.Row():
        with gr.Accordion("πŸ“™ Citation", open=False):
            citation_button = gr.Textbox(
                value=CITATION_BUTTON_TEXT,
                label=CITATION_BUTTON_LABEL,
                elem_id="citation-button",
            ).style(show_copy_button=True)


# Restart space every hour
scheduler = BackgroundScheduler()
scheduler.add_job(
    restart_space,
    "interval",
    seconds=3600,
    args=[LLM_PERF_LEADERBOARD_REPO, OPTIMUM_TOKEN],
)
scheduler.start()

# Launch demo
demo.queue(concurrency_count=40).launch()