(ps: ParameterServer)
| 44 | |
| 45 | |
| 46 | def _init_api(ps: ParameterServer) -> Any: |
| 47 | app = fastapi.FastAPI() |
| 48 | |
| 49 | class RegisterRequest(BaseModel): |
| 50 | files: list[str] |
| 51 | |
| 52 | class UpdateRequest(BaseModel): |
| 53 | ranks: list[int] = [] |
| 54 | update_url: str | None = None |
| 55 | inference_group_ranks: list[int] = [] |
| 56 | timeout: float = 300.0 |
| 57 | uds: str | None = None |
| 58 | |
| 59 | def wrap_exception(func: Callable[[], None]) -> Response: |
| 60 | try: |
| 61 | func() |
| 62 | except Exception as e: # noqa: BLE001 |
| 63 | logger.exception(f"wrap exception {func} failed") |
| 64 | return JSONResponse(content=str(e), status_code=500) |
| 65 | return Response(status_code=200) |
| 66 | |
| 67 | @app.post("/v1/checkpoints/{checkpoint_name}/files") |
| 68 | async def register_files(checkpoint_name: str, req: RegisterRequest, raw: Request) -> Response: |
| 69 | return wrap_exception(lambda: ps.register_checkpoint(checkpoint_name, files=req.files)) |
| 70 | |
| 71 | @app.delete("/v1/checkpoints/{checkpoint_name}") |
| 72 | async def unregister_checkpoint(checkpoint_name: str) -> Response: |
| 73 | return wrap_exception(lambda: ps.unregister_checkpoint(checkpoint_name)) |
| 74 | |
| 75 | @app.get("/v1/healthz") |
| 76 | async def healthz() -> Response: |
| 77 | return Response(status_code=200) |
| 78 | |
| 79 | @app.post("/v1/checkpoints/{checkpoint_name}/gather-metas") |
| 80 | async def gather_metas(checkpoint_name: str) -> Response: |
| 81 | return wrap_exception(lambda: ps.gather_metas(checkpoint_name)) |
| 82 | |
| 83 | @app.get("/v1/metas") |
| 84 | async def get_metas() -> dict[int, MemoryBufferMetaList]: |
| 85 | try: |
| 86 | return ps.get_metas() |
| 87 | except Exception as e: |
| 88 | logger.exception("get_metas failed") |
| 89 | raise HTTPException(status_code=500, detail=str(e)) from e |
| 90 | |
| 91 | @app.post("/v1/metas") |
| 92 | async def load_metas(metas: dict[int, MemoryBufferMetaList]) -> Response: |
| 93 | return wrap_exception(lambda: ps.load_metas(metas)) |
| 94 | |
| 95 | @app.post("/v1/checkpoints/{checkpoint_name}/update") |
| 96 | async def update(checkpoint_name: str, req: UpdateRequest) -> Response: |
| 97 | def update_func(socket_paths: list[tuple[str, str]]): |
| 98 | if req.update_url is None: |
| 99 | return |
| 100 | if req.inference_group_ranks: |
| 101 | socket_paths = [socket_paths[i] for i in req.inference_group_ranks] |
| 102 | request_inference_to_update( |
| 103 | req.update_url, dict(socket_paths), timeout=req.timeout, uds=req.uds |
no outgoing calls