-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcpu_server.py
More file actions
525 lines (442 loc) · 17.1 KB
/
cpu_server.py
File metadata and controls
525 lines (442 loc) · 17.1 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import asyncio
import json
import logging
import os
import sys
import tempfile
import itertools
import re
from typing import Optional
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from hitl_agent.constants import CPU_SERVER_PORT
from hitl_agent.tools.analyze_profile import analyze_trace
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
app = FastAPI(title="CPU Code Execution Server", version="1.0.0")
# Semaphore to limit concurrent compilation requests
compilation_semaphore = asyncio.Semaphore(1)
correctness_semaphore = asyncio.Semaphore(1)
performance_semaphore = asyncio.Semaphore(1)
profile_semaphore = asyncio.Semaphore(1)
class CodeRequest(BaseModel):
code: str
timeout: Optional[int] = 30
class CodeResponse(BaseModel):
output: str
error: Optional[str] = None
exit_code: int
class AutotuneRequest(BaseModel):
code_template: str
search_space: dict[str, list]
timeout: Optional[int] = 30
class GetBackendVersionResponse(BaseModel):
backend_version: str
def get_cpu_env():
"""
Returns environment variables that force JAX to use CPU backend.
"""
env = os.environ.copy()
env["JAX_PLATFORMS"] = "cpu"
env["JAX_PLATFORM_NAME"] = "cpu"
# Disable GPU visibility to ensure CPU-only execution
env["CUDA_VISIBLE_DEVICES"] = ""
return env
@app.get("/health")
async def health_check():
return {"status": "healthy", "backend": "cpu"}
@app.post("/compilation_test", response_model=CodeResponse)
async def compilation_test(request: CodeRequest):
"""
Try to execute kernel safely in a subprocess with CPU backend and return the output.
"""
logging.info("Starting compilation test on CPU backend")
async with compilation_semaphore:
try:
# Extract code from markdown format if present
code_content = request.code.strip()
if code_content.startswith("```python") and code_content.endswith("```"):
# Remove the markdown code block markers
lines = code_content.split("\n")
if lines[0].strip() == "```python":
lines = lines[1:]
if lines[-1].strip() == "```":
lines = lines[:-1]
code_content = "\n".join(lines)
elif code_content.startswith("```") and code_content.endswith("```"):
# Handle generic code blocks
lines = code_content.split("\n")
if lines[0].strip().startswith("```"):
lines = lines[1:]
if lines[-1].strip() == "```":
lines = lines[:-1]
code_content = "\n".join(lines)
request.code = code_content
# Create a temporary file to store the code
with tempfile.NamedTemporaryFile(
mode="w", suffix=".py", delete=False
) as temp_file:
temp_file.write(request.code)
temp_file_path = temp_file.name
# Execute the code in a subprocess with CPU-only environment
process = await asyncio.create_subprocess_exec(
sys.executable,
temp_file_path,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=tempfile.gettempdir(),
env=get_cpu_env(), # Force CPU backend
)
try:
stdout, stderr = await asyncio.wait_for(
process.communicate(), timeout=request.timeout
)
output = stdout.decode("utf-8") if stdout else ""
error = stderr.decode("utf-8") if stderr else None
exit_code = process.returncode
logging.info(
f"Compilation test completed successfully on CPU with exit_code: {exit_code}"
)
return CodeResponse(output=output, error=error, exit_code=exit_code)
except asyncio.TimeoutError:
process.kill()
await process.wait()
logging.error(f"Compilation test timed out after {request.timeout}s")
raise HTTPException(status_code=408, detail="Code execution timed out")
except HTTPException:
# Re-raise HTTPExceptions to avoid logging them twice
raise
except Exception as e:
logging.error(f"Compilation test failed with error: {str(e)}")
raise HTTPException(status_code=500, detail=f"Execution error: {str(e)}")
finally:
# Clean up the temporary file
if "temp_file_path" in locals():
try:
os.unlink(temp_file_path)
except OSError:
pass
logging.info("Compilation test finished")
@app.post("/correctness_test", response_model=CodeResponse)
async def correctness_test(request: CodeRequest):
"""
Test the correctness of the kernel code by executing it on CPU and comparing the output.
"""
logging.info("Starting correctness test on CPU backend")
async with correctness_semaphore:
try:
# Extract code from markdown format if present
code_content = request.code.strip()
if code_content.startswith("```python") and code_content.endswith("```"):
# Remove the markdown code block markers
lines = code_content.split("\n")
if lines[0].strip() == "```python":
lines = lines[1:]
if lines[-1].strip() == "```":
lines = lines[:-1]
code_content = "\n".join(lines)
elif code_content.startswith("```") and code_content.endswith("```"):
# Handle generic code blocks
lines = code_content.split("\n")
if lines[0].strip().startswith("```"):
lines = lines[1:]
if lines[-1].strip() == "```":
lines = lines[:-1]
code_content = "\n".join(lines)
request.code = code_content
# Create a temporary file to store the code
with tempfile.NamedTemporaryFile(
mode="w", suffix=".py", delete=False
) as temp_file:
temp_file.write(request.code)
temp_file_path = temp_file.name
# Execute the code in a subprocess with CPU-only environment
process = await asyncio.create_subprocess_exec(
sys.executable,
temp_file_path,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=tempfile.gettempdir(),
env=get_cpu_env(), # Force CPU backend
)
try:
stdout, stderr = await asyncio.wait_for(
process.communicate(), timeout=request.timeout
)
output = stdout.decode("utf-8") if stdout else ""
error = stderr.decode("utf-8") if stderr else None
exit_code = process.returncode
logging.info(
f"Correctness test completed successfully on CPU with exit_code: {exit_code}"
)
return CodeResponse(output=output, error=error, exit_code=exit_code)
except asyncio.TimeoutError:
process.kill()
await process.wait()
logging.error(f"Correctness test timed out after {request.timeout}s")
raise HTTPException(status_code=408, detail="Code execution timed out")
except HTTPException:
# Re-raise HTTPExceptions to avoid logging them twice
raise
except Exception as e:
logging.error(f"Correctness test failed with error: {str(e)}")
raise HTTPException(status_code=500, detail=f"Execution error: {str(e)}")
finally:
# Clean up the temporary file
if "temp_file_path" in locals():
try:
os.unlink(temp_file_path)
except OSError:
pass
logging.info("Correctness test finished")
@app.post("/performance_test", response_model=CodeResponse)
async def performance_test(request: CodeRequest):
"""
Test the performance of the kernel code by executing it on CPU and measuring the execution time.
"""
logging.info("Starting performance test on CPU backend")
async with performance_semaphore:
try:
# Extract code from markdown format if present
code_content = request.code.strip()
if code_content.startswith("```python") and code_content.endswith("```"):
# Remove the markdown code block markers
lines = code_content.split("\n")
if lines[0].strip() == "```python":
lines = lines[1:]
if lines[-1].strip() == "```":
lines = lines[:-1]
code_content = "\n".join(lines)
elif code_content.startswith("```") and code_content.endswith("```"):
# Handle generic code blocks
lines = code_content.split("\n")
if lines[0].strip().startswith("```"):
lines = lines[1:]
if lines[-1].strip() == "```":
lines = lines[:-1]
code_content = "\n".join(lines)
request.code = code_content
# Create a temporary file to store the code
with tempfile.NamedTemporaryFile(
mode="w", suffix=".py", delete=False
) as temp_file:
temp_file.write(request.code)
temp_file_path = temp_file.name
# Execute the code in a subprocess with CPU-only environment
process = await asyncio.create_subprocess_exec(
sys.executable,
temp_file_path,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=tempfile.gettempdir(),
env=get_cpu_env(), # Force CPU backend
)
try:
stdout, stderr = await asyncio.wait_for(
process.communicate(), timeout=request.timeout
)
output = stdout.decode("utf-8") if stdout else ""
error = stderr.decode("utf-8") if stderr else None
exit_code = process.returncode
logging.info(
f"Performance test completed successfully on CPU with exit_code: {exit_code}"
)
return CodeResponse(output=output, error=error, exit_code=exit_code)
except asyncio.TimeoutError:
process.kill()
await process.wait()
logging.error(f"Performance test timed out after {request.timeout}s")
raise HTTPException(status_code=408, detail="Code execution timed out")
except HTTPException:
# Re-raise HTTPExceptions to avoid logging them twice
raise
except Exception as e:
logging.error(f"Performance test failed with error: {str(e)}")
raise HTTPException(status_code=500, detail=f"Execution error: {str(e)}")
finally:
# Clean up the temporary file
if "temp_file_path" in locals():
try:
os.unlink(temp_file_path)
except OSError:
pass
logging.info("Performance test finished")
@app.post("/autotune", response_model=CodeResponse)
async def autotune(request: AutotuneRequest):
logging.info("Starting autotune on CPU backend")
async with performance_semaphore:
try:
# Generate all combinations
keys = list(request.search_space.keys())
values = list(request.search_space.values())
combinations = list(itertools.product(*values))
best_time = float("inf")
best_cfg = None
best_output = ""
for combo in combinations:
cfg = dict(zip(keys, combo))
try:
code_content = request.code_template.format(**cfg)
except KeyError as e:
logging.error(f"KeyError during template formatting: {e}. Config: {cfg}")
continue
# Execute the code
with tempfile.NamedTemporaryFile(
mode="w", suffix=".py", delete=False
) as temp_file:
temp_file.write(code_content)
temp_file_path = temp_file.name
try:
process = await asyncio.create_subprocess_exec(
sys.executable,
temp_file_path,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=tempfile.gettempdir(),
env=get_cpu_env(), # Force CPU backend
)
stdout, stderr = await asyncio.wait_for(
process.communicate(), timeout=request.timeout
)
output = stdout.decode("utf-8") if stdout else ""
error = stderr.decode("utf-8") if stderr else ""
exit_code = process.returncode
if exit_code == 0:
# Parse RESULT_TIME
match = re.search(r"RESULT_TIME:\s*([0-9.]+)", output)
if match:
time_taken = float(match.group(1))
if time_taken < best_time:
best_time = time_taken
best_cfg = cfg
best_output = output
else:
logging.warning(f"No RESULT_TIME found in output for config {cfg}")
else:
logging.warning(f"Config {cfg} failed with exit code {exit_code}. Stderr: {error}")
except asyncio.TimeoutError:
logging.warning(f"Config {cfg} timed out")
process.kill()
await process.wait()
except Exception as e:
logging.error(f"Error running config {cfg}: {e}")
finally:
try:
os.unlink(temp_file_path)
except OSError:
pass
if best_cfg is None:
return CodeResponse(
output="",
error="No successful configuration found during autotune.",
exit_code=-1,
)
output_data = {
"best_cfg": best_cfg,
"best_time": best_time,
"best_output": best_output,
}
logging.info("Autotune finished on CPU backend")
return CodeResponse(output=json.dumps(output_data), error=None, exit_code=0)
except Exception as e:
logging.error(f"Autotune failed with error: {str(e)}")
raise HTTPException(status_code=500, detail=f"Autotune error: {str(e)}")
@app.post("/profile", response_model=CodeResponse)
async def profile(request: CodeRequest):
logging.info("Starting profile on CPU backend")
async with profile_semaphore:
try:
# Extract code from markdown format if present
code_content = request.code.strip()
if code_content.startswith("```python") and code_content.endswith("```"):
# Remove the markdown code block markers
lines = code_content.split("\n")
if lines[0].strip() == "```python":
lines = lines[1:]
if lines[-1].strip() == "```":
lines = lines[:-1]
code_content = "\n".join(lines)
elif code_content.startswith("```") and code_content.endswith("```"):
# Handle generic code blocks
lines = code_content.split("\n")
if lines[0].strip().startswith("```"):
lines = lines[1:]
if lines[-1].strip() == "```":
lines = lines[:-1]
code_content = "\n".join(lines)
request.code = code_content
# Create a temporary directory to store the code and any generated files
temp_dir = tempfile.mkdtemp()
logging.info("temp_dir: " + str(temp_dir))
# Create a temporary file to store the code within temp_dir
temp_file_path = os.path.join(temp_dir, "profile_code.py")
with open(temp_file_path, "w") as temp_file:
temp_file.write(request.code)
# Execute the code in a subprocess with CPU-only environment
process = await asyncio.create_subprocess_exec(
sys.executable,
temp_file_path,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=temp_dir,
env=get_cpu_env(), # Force CPU backend
)
try:
stdout, stderr = await asyncio.wait_for(
process.communicate(), timeout=request.timeout
)
output = stdout.decode("utf-8") if stdout else ""
error = stderr.decode("utf-8") if stderr else None
exit_code = process.returncode
logging.info("Profile code executed, now analyzing trace.")
# Recursively search for .xplane.pb file under temp_file_path directory
xplane_pb_file = None
for root, _, files in os.walk(temp_dir):
for fname in files:
if fname.endswith(".xplane.pb"):
xplane_pb_file = os.path.join(root, fname)
break
if xplane_pb_file:
break
logging.info("Found xplane file at: " + str(xplane_pb_file))
ratio = analyze_trace(xplane_pb_file)
logging.info(
f"Profile analysis completed successfully on CPU with exit_code: {exit_code}"
)
return CodeResponse(
output=json.dumps({"ratio": ratio, "xplane_path": xplane_pb_file}),
error=error,
exit_code=exit_code,
)
except asyncio.TimeoutError:
process.kill()
await process.wait()
logging.error(f"Profile analysis timed out after {request.timeout}s")
raise HTTPException(status_code=408, detail="Code execution timed out")
except HTTPException:
# Re-raise HTTPExceptions to avoid logging them twice
raise
except Exception as e:
logging.error(f"Profile analysis failed with error: {str(e)}")
raise HTTPException(status_code=500, detail=f"Execution error: {str(e)}")
finally:
# Clean up the temporary directory
# try:
# shutil.rmtree(temp_dir)
# except Exception:
# pass
logging.info("Profile analysis finished")
@app.post("/get_backend_version", response_model=GetBackendVersionResponse)
async def get_backend_version() -> str:
"""
Returns the backend version for CPU execution.
Returns:
A string indicating CPU backend.
"""
return GetBackendVersionResponse(backend_version="CPU")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=CPU_SERVER_PORT)