Skip to content

Commit 0d5ec10

Browse files
Create worker_gpu.py
1 parent 92571ba commit 0d5ec10

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

prototype/worker_gpu.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# prototype/worker_gpu.py (NEW) - Enhanced worker with GPU support
2+
from fastapi import FastAPI, HTTPException
3+
import torch
4+
import uvicorn
5+
import numpy as np
6+
from typing import Dict, Optional
7+
import base64
8+
from vllm import LLM
9+
10+
app = FastAPI()
11+
12+
class GPUSlice:
13+
"""GPU-accelerated slice for distributed inference."""
14+
15+
def __init__(self, model_slice: torch.nn.Module):
16+
self.model = model_slice
17+
self.device = torch.cuda.current_device()
18+
19+
def forward(self, x: np.ndarray) -> np.ndarray:
20+
"""GPU-accelerated forward pass with CUDA kernel fusion."""
21+
x_tensor = torch.from_numpy(x).float().to(self.device)
22+
23+
# Forward pass on GPU
24+
with torch.no_grad():
25+
out_tensor = self.model(x_tensor)
26+
27+
return out_tensor.cpu().numpy()
28+
29+
# Global model registry
30+
model_slices: Dict[str, GPUSlice] = {}
31+
32+
@app.post("/execute-gpu")
33+
async def execute_gpu(req: ExecRequest):
34+
"""GPU-accelerated inference with batch support."""
35+
36+
if req.slice_id not in model_slices:
37+
raise HTTPException(status_code=404, detail="slice not found")
38+
39+
slice_model = model_slices[req.slice_id]
40+
x = np.ascontiguousarray(req.input_blob) # Ensure contiguous
41+
42+
with torch.cuda.amp.autocast(): # Mixed precision
43+
out = slice_model.forward(x)
44+
45+
return {
46+
"output_b64": base64.b64encode(out).decode('ascii')
47+
}

0 commit comments

Comments
 (0)