Spaces:
Running
Running
import os | |
import gradio as gr | |
import pandas as pd | |
from apscheduler.schedulers.background import BackgroundScheduler | |
from src.assets.text_content import TITLE, INTRODUCTION_TEXT, SINGLE_A100_TEXT, CITATION_BUTTON_LABEL, CITATION_BUTTON_TEXT | |
from src.utils import restart_space, load_dataset_repo, make_clickable_model, make_clickable_score, extract_score_from_clickable | |
from src.assets.css_html_js import custom_css | |
LLM_PERF_LEADERBOARD_REPO = "optimum/llm-perf-leaderboard" | |
LLM_PERF_DATASET_REPO = "optimum/llm-perf-dataset" | |
OPTIMUM_TOKEN = os.environ.get("OPTIMUM_TOKEN", None) | |
COLUMNS_MAPPING = { | |
"model": "Model π€", | |
"backend.name": "Backend π", | |
"backend.torch_dtype": "Datatype π₯", | |
"average": "Average H4 Score β¬οΈ", | |
"forward.peak_memory(MB)": "Peak Memory (MB) β¬οΈ", | |
"generate.throughput(tokens/s)": "Throughput (tokens/s) β¬οΈ", | |
} | |
COLUMNS_DATATYPES = ["markdown", "str", "str", "markdown", "number", "number"] | |
SORTING_COLUMN = ["Throughput (tokens/s) β¬οΈ"] | |
llm_perf_dataset_repo = load_dataset_repo(LLM_PERF_DATASET_REPO, OPTIMUM_TOKEN) | |
def get_benchmark_df(benchmark): | |
if llm_perf_dataset_repo: | |
llm_perf_dataset_repo.git_pull() | |
# load | |
bench_df = pd.read_csv( | |
f"./llm-perf-dataset/reports/{benchmark}/inference_report.csv") | |
scores_df = pd.read_csv( | |
f"./llm-perf-dataset/reports/average_scores.csv") | |
bench_df = bench_df.merge(scores_df, on="model", how="left") | |
bench_df["average"] = bench_df["average"].apply( | |
make_clickable_score) | |
# preprocess | |
bench_df["model"] = bench_df["model"].apply(make_clickable_model) | |
# filter | |
bench_df = bench_df[list(COLUMNS_MAPPING.keys())] | |
# rename | |
bench_df.rename(columns=COLUMNS_MAPPING, inplace=True) | |
# sort | |
bench_df.sort_values(by=SORTING_COLUMN, ascending=False, inplace=True) | |
return bench_df | |
def submit_query(text, backends, datatypes, threshold, raw_df): | |
# extract the average score (float) from the clickable score (clickable markdown) | |
raw_df["Average H4 Score β¬οΈ"] = raw_df["Average H4 Score β¬οΈ"].apply( | |
extract_score_from_clickable) | |
filtered_df = raw_df[ | |
raw_df["Model π€"].str.lower().str.contains(text.lower()) & | |
raw_df["Backend π"].isin(backends) & | |
raw_df["Datatype π₯"].isin(datatypes) & | |
(raw_df["Average H4 Score β¬οΈ"] >= threshold) | |
] | |
filtered_df["Average H4 Score β¬οΈ"] = filtered_df["Average H4 Score β¬οΈ"].apply( | |
make_clickable_score) | |
return filtered_df | |
# Define demo interface | |
demo = gr.Blocks(css=custom_css) | |
with demo: | |
gr.HTML(TITLE) | |
gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text") | |
# controls | |
with gr.Row(): | |
search_bar = gr.Textbox( | |
label="Model π€", | |
info="Search for a model name", | |
elem_id="search-bar", | |
) | |
backend_checkboxes = gr.CheckboxGroup( | |
label="Backends π", | |
choices=["pytorch", "onnxruntime"], | |
value=["pytorch", "onnxruntime"], | |
info="Select the backends", | |
elem_id="backend-checkboxes", | |
) | |
datatype_checkboxes = gr.CheckboxGroup( | |
label="Datatypes π₯", | |
choices=["float32", "float16"], | |
value=["float32", "float16"], | |
info="Select the load datatypes", | |
elem_id="datatype-checkboxes", | |
) | |
with gr.Row(): | |
threshold_slider = gr.Slider( | |
label="Average H4 Score π", | |
info="Filter by minimum average H4 score", | |
value=0.0, | |
elem_id="threshold-slider", | |
) | |
with gr.Row(): | |
submit_button = gr.Button( | |
value="Submit π", | |
info="Submit the filters", | |
elem_id="submit-button", | |
) | |
# leaderboard tabs | |
with gr.Tabs(elem_classes="tab-buttons") as tabs: | |
with gr.TabItem("π₯οΈ A100-80GB Benchmark ποΈ", elem_id="A100-benchmark", id=0): | |
gr.HTML(SINGLE_A100_TEXT) | |
single_A100_df = get_benchmark_df(benchmark="1xA100-80GB") | |
# Original leaderboard table | |
single_A100_leaderboard = gr.components.Dataframe( | |
value=single_A100_df, | |
datatype=COLUMNS_DATATYPES, | |
headers=list(COLUMNS_MAPPING.values()), | |
elem_id="1xA100-table", | |
) | |
# Dummy Leaderboard table for handling the case when the user uses backspace key | |
single_A100_for_search = gr.components.Dataframe( | |
value=single_A100_df, | |
datatype=COLUMNS_DATATYPES, | |
headers=list(COLUMNS_MAPPING.values()), | |
max_rows=None, | |
visible=False, | |
) | |
# Callbacks | |
submit_button.click( | |
submit_query, | |
[ | |
search_bar, backend_checkboxes, datatype_checkboxes, threshold_slider, | |
single_A100_for_search | |
], | |
[single_A100_leaderboard] | |
) | |
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() | |