|
|
import os, tempfile, time |
|
|
import gradio as gr |
|
|
from tool.test import run_autotune_pipeline, DATA_DIR |
|
|
|
|
|
|
|
|
|
|
|
def get_test_text(test_file, test_data_input): |
|
|
if test_file is not None: |
|
|
if hasattr(test_file, "read"): |
|
|
return test_file.read().decode("utf-8") |
|
|
elif hasattr(test_file, "data"): |
|
|
return test_file.data if isinstance(test_file.data, str) else test_file.data.decode("utf-8") |
|
|
elif hasattr(test_file, "name") and os.path.exists(test_file.name): |
|
|
with open(test_file.name, "r", encoding="utf-8") as f: |
|
|
return f.read() |
|
|
|
|
|
return test_data_input or "" |
|
|
|
|
|
def generate_kernel(text_input, test_data_input, test_file, n_iters, progress=gr.Progress()): |
|
|
""" |
|
|
text_input : string from textbox (NL description or base CUDA code) |
|
|
test_data_input: test data (variable name, data) |
|
|
file_input : gr.File upload object (or None) |
|
|
Returns : (kernel_code_str, downloadable_file_path) |
|
|
""" |
|
|
progress((0, n_iters), desc="Initializing...") |
|
|
|
|
|
|
|
|
if not text_input.strip(): |
|
|
return "β οΈ Please paste a description or baseline CUDA code." |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_text = get_test_text(test_file, test_data_input) |
|
|
|
|
|
if not test_text.strip(): |
|
|
return "β οΈ Test data required." |
|
|
|
|
|
|
|
|
best_code = "" |
|
|
for info in run_autotune_pipeline( |
|
|
input_code=text_input, |
|
|
test_data_input=test_text, |
|
|
test_file=None, |
|
|
bin_dir=DATA_DIR, |
|
|
max_iterations=int(n_iters) |
|
|
): |
|
|
|
|
|
if info["iteration"] is not None: |
|
|
|
|
|
progress((info["iteration"], n_iters), desc=info["message"]) |
|
|
|
|
|
|
|
|
if info["code"]: |
|
|
best_code = info["code"] |
|
|
|
|
|
|
|
|
return best_code |
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks( |
|
|
title="KernelPilot", |
|
|
theme=gr.themes.Soft( |
|
|
text_size="lg", |
|
|
font=[ |
|
|
"system-ui", |
|
|
"-apple-system", |
|
|
"BlinkMacSystemFont", |
|
|
"Segoe UI", |
|
|
"Roboto", |
|
|
"Helvetica Neue", |
|
|
"Arial", |
|
|
"Noto Sans", |
|
|
"sans-serif" |
|
|
])) as demo: |
|
|
gr.Markdown( |
|
|
"""# π KernelPilot Optimizer |
|
|
Enter a code, test data, then click **Generate** to obtain the optimized kernel function.""" |
|
|
) |
|
|
|
|
|
with gr.Row(): |
|
|
txt_input = gr.Textbox( |
|
|
label="π Input", |
|
|
lines=10, |
|
|
placeholder="Enter the code", |
|
|
scale=3 |
|
|
) |
|
|
level = gr.Number( |
|
|
label="Optimazation Level", |
|
|
minimum=1, |
|
|
maximum=5, |
|
|
value=5, |
|
|
step=1, |
|
|
scale=1 |
|
|
) |
|
|
|
|
|
with gr.Row(): |
|
|
test_data_input = gr.Textbox( |
|
|
label="Test Data Input", |
|
|
lines=10, |
|
|
placeholder="<number_of_test_cases>\n<number_of_variables>\n\n<variable_1_name>\n<variable_1_testcase_1_data>\n<variable_1_testcase_2_data>\n...\n<variable_1_testcase_N_data>\n\n<variable_2_name>\n<variable_2_testcase_1_data>\n...\n<variable_2_testcase_N_data>\n\n...", |
|
|
scale=2 |
|
|
) |
|
|
test_file = gr.File( |
|
|
label="Upload Test Data (.txt)", |
|
|
file_types=["text"], |
|
|
scale=1 |
|
|
) |
|
|
|
|
|
gen_btn = gr.Button("β‘ Generate") |
|
|
|
|
|
kernel_output = gr.Code( |
|
|
label="π― Tuned CUDA Kernel", |
|
|
language="cpp" |
|
|
) |
|
|
|
|
|
gen_btn.click( |
|
|
fn=generate_kernel, |
|
|
inputs=[txt_input, test_data_input, test_file, level], |
|
|
outputs=[kernel_output], |
|
|
queue=True, |
|
|
show_progress=True, |
|
|
show_progress_on=kernel_output |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.queue(default_concurrency_limit=1, max_size=50) |
|
|
demo.launch(server_name="0.0.0.0", server_port=7860) |