This repository was archived by the owner on May 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
184 lines (148 loc) · 5.62 KB
/
api.py
File metadata and controls
184 lines (148 loc) · 5.62 KB
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
import json
import sys
from threading import Thread
from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse
import modal
from pathlib import Path
import utils
import runner
from problem import Problem
import os
DEVEL_IMAGE_NAME = "nvidia/cuda:12.8.0-devel-ubuntu22.04"
RUNTIME_IMAGE_NAME = "nvidia/cuda:12.8.0-runtime-ubuntu22.04"
CURR_DIR = Path(__file__).parent
PIP_PACKAGES = ["torch", "numpy", "fastapi[standard]", "triton"]
LOCAL_SOURCE = ["utils", "runner", "problem"]
devel_image = (
modal.Image.from_registry(DEVEL_IMAGE_NAME, add_python="3.11")
.apt_install(["build-essential", "gcc", "g++"])
.env({"CC": "gcc"})
.pip_install(PIP_PACKAGES)
.add_local_python_source(*LOCAL_SOURCE)
)
runtime_image = (
modal.Image.from_registry(RUNTIME_IMAGE_NAME, add_python="3.11")
.apt_install(["build-essential", "gcc", "g++"])
.env({"CC": "gcc"})
.pip_install(PIP_PACKAGES)
.add_local_python_source(*LOCAL_SOURCE)
)
app = modal.App("tensara-cli-engine", image=devel_image)
web_app = FastAPI()
def binary_runner(type: str, compiled_lib: bytes, solution_code: str, problem_name: str, problem_def: str, dtype: str, language: str):
gen = None
if type == "checker":
gen = runner.run_checker(problem_name, problem_def, compiled_lib, solution_code, dtype, language)
elif type == "benchmark":
gen = runner.run_benchmark(problem_name, problem_def, compiled_lib, solution_code, dtype, language)
else:
raise ValueError(f"Unknown binary type: {type}")
for event in gen:
yield event
gpu_runners = {
gpu: app.function(
image=runtime_image,
name=f"runner_{gpu}",
gpu=gpu,
enable_memory_snapshot=True,
)(binary_runner)
for gpu in utils.GPU_COMPUTE_CAPABILITIES.keys()
}
for gpu in gpu_runners:
globals()[f"runner_{gpu}"] = gpu_runners[gpu]
def gen_wrapper(gen):
for event in gen:
yield "data: " + json.dumps(event, allow_nan=False) + "\n\n"
@web_app.post("/checker-{gpu}")
async def checker(gpu: str, request: Request):
req = await request.json()
if gpu not in gpu_runners:
return 404
solution_code = req["solution_code"]
problem_def = req["problem_def"]
dtype = req["dtype"]
language = req["language"]
problem_name = utils.convert_slug_to_module_name(req["problem"])
def create_stream():
yield {"status": "compiling"}
def compile_benchmark():
try:
utils.run_nvcc_and_return_bytes(gpu, solution_code, "solution")
except Exception:
pass
if language == "cuda":
bench_thr = Thread(target=compile_benchmark)
bench_thr.start()
try:
checker_compiled = utils.run_nvcc_and_return_bytes(gpu, solution_code, "checker")
except utils.NVCCError as e:
yield {
"status": "error",
"error": "Compilation failed",
"details": e.args[0],
"test_results": [],
"passed_tests": 0,
"total_tests": 0,
}
return
bench_thr.join()
else:
checker_compiled = None
runner = gpu_runners[gpu]
stream = runner.remote_gen("checker", checker_compiled, solution_code, problem_name, problem_def, dtype, language)
for event in stream:
yield event
stream = gen_wrapper(create_stream())
return StreamingResponse(stream, media_type="text/event-stream")
@web_app.post("/benchmark-{gpu}")
async def benchmark(gpu: str, request: Request):
req = await request.json()
if gpu not in gpu_runners:
return 404
solution_code = req["solution_code"]
problem_def = req["problem_def"]
dtype = req["dtype"]
language = req["language"]
problem_name = utils.convert_slug_to_module_name(req["problem"])
def create_stream():
yield {"status": "compiling"}
if language == "cuda":
try:
benchmark_compiled = utils.run_nvcc_and_return_bytes(gpu, solution_code, "benchmark")
except utils.NVCCError as e:
yield {
"status": "error",
"error": "Compilation failed",
"details": e.args[0],
}
return
else:
benchmark_compiled = None
runner = gpu_runners[gpu]
first_test_passed = False
checker_stream = runner.remote_gen("checker", benchmark_compiled, solution_code, problem_name, problem_def, dtype, language)
for event in checker_stream:
if event["status"] == "test_result":
first_test_passed = event["result"]["status"] == "PASSED"
break
elif event["status"] == "error":
yield event
return
if not first_test_passed:
yield {
"status": "error",
"error": "Sanity check failed",
"details": "Solution failed the sanity check. Benchmark aborted. Please fix the solution.",
}
return
yield {"status": "sanity_check", "message": "Sanity check passed, starting benchmark..."}
stream = runner.remote_gen("benchmark", benchmark_compiled, solution_code, problem_name, problem_def, dtype, language)
for event in stream:
yield event
stream = gen_wrapper(create_stream())
return StreamingResponse(stream, media_type="text/event-stream")
@app.function()
@modal.asgi_app()
def fastapi_app():
return web_app