|
| 1 | +from collections.abc import Callable |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import fastapi |
| 5 | +import httpx |
| 6 | +from fastapi import Request |
| 7 | +from fastapi.responses import JSONResponse, Response |
| 8 | +from loguru import logger |
| 9 | +from pydantic import BaseModel |
| 10 | + |
| 11 | +from checkpoint_engine.ps import ParameterServer |
| 12 | + |
| 13 | + |
| 14 | +def request_inference_to_update( |
| 15 | + url: str, |
| 16 | + socket_paths: dict[str, str], |
| 17 | + timeout: float = 300.0, |
| 18 | + uds: str | None = None, |
| 19 | +): |
| 20 | + """Send an inference update request to inference server via HTTP or Unix socket. |
| 21 | +
|
| 22 | + Args: |
| 23 | + url (str): The HTTP URL or request path (e.g., "http://localhost:19730/inference") to send the request to. |
| 24 | + socket_paths (dict[str, str]): A dictionary containing device uuid and IPC socket paths for updating weights. |
| 25 | + timeout (float, optional): Request timeout in seconds. Defaults to 300.0. |
| 26 | + uds (str, optional): Path to a Unix domain socket. If provided, the request |
| 27 | + will be sent via the Unix socket instead of HTTP. Defaults to None. |
| 28 | +
|
| 29 | + Raises: |
| 30 | + httpx.HTTPStatusError: If the response contains an HTTP error status. |
| 31 | + httpx.RequestError: If there was an issue while making the request. |
| 32 | + """ |
| 33 | + resp = httpx.Client(transport=httpx.HTTPTransport(uds=uds)).post( |
| 34 | + url, |
| 35 | + json={ |
| 36 | + "method": "update_weights_from_ipc", |
| 37 | + "args": [socket_paths], |
| 38 | + "timeout": timeout, |
| 39 | + }, |
| 40 | + timeout=timeout, |
| 41 | + ) |
| 42 | + resp.raise_for_status() |
| 43 | + |
| 44 | + |
| 45 | +def _init_api(ps: ParameterServer) -> Any: |
| 46 | + app = fastapi.FastAPI() |
| 47 | + |
| 48 | + class RegisterRequest(BaseModel): |
| 49 | + files: list[str] |
| 50 | + |
| 51 | + class UpdateRequest(BaseModel): |
| 52 | + ranks: list[int] = [] |
| 53 | + update_url: str | None = None |
| 54 | + inference_group_ranks: list[int] = [] |
| 55 | + timeout: float = 300.0 |
| 56 | + uds: str | None = None |
| 57 | + |
| 58 | + def wrap_exception(func: Callable[[], None]) -> Response: |
| 59 | + try: |
| 60 | + func() |
| 61 | + except Exception as e: # noqa: BLE001 |
| 62 | + logger.exception(f"wrap exception {func} failed") |
| 63 | + return JSONResponse(content=str(e), status_code=500) |
| 64 | + return Response(status_code=200) |
| 65 | + |
| 66 | + @app.post("/v1/checkpoints/{checkpoint_name}/files") |
| 67 | + async def register_files(checkpoint_name: str, req: RegisterRequest, raw: Request) -> Response: |
| 68 | + return wrap_exception(lambda: ps.register_checkpoint(checkpoint_name, files=req.files)) |
| 69 | + |
| 70 | + @app.delete("/v1/checkpoints/{checkpoint_name}") |
| 71 | + async def unregister_checkpoint(checkpoint_name: str) -> Response: |
| 72 | + return wrap_exception(lambda: ps.unregister_checkpoint(checkpoint_name)) |
| 73 | + |
| 74 | + @app.get("/v1/healthz") |
| 75 | + async def healthz() -> Response: |
| 76 | + return Response(status_code=200) |
| 77 | + |
| 78 | + @app.post("/v1/checkpoints/{checkpoint_name}/gather-metas") |
| 79 | + async def gather_metas(checkpoint_name: str) -> Response: |
| 80 | + return wrap_exception(lambda: ps.gather_metas(checkpoint_name)) |
| 81 | + |
| 82 | + @app.post("/v1/checkpoints/{checkpoint_name}/update") |
| 83 | + async def update(checkpoint_name: str, req: UpdateRequest) -> Response: |
| 84 | + def update_func(socket_paths: list[tuple[str, str]]): |
| 85 | + if req.update_url is None: |
| 86 | + return |
| 87 | + if req.inference_group_ranks: |
| 88 | + socket_paths = [socket_paths[i] for i in req.inference_group_ranks] |
| 89 | + request_inference_to_update( |
| 90 | + req.update_url, dict(socket_paths), timeout=req.timeout, uds=req.uds |
| 91 | + ) |
| 92 | + |
| 93 | + return wrap_exception(lambda: ps.update(checkpoint_name, update_func, ranks=req.ranks)) |
| 94 | + |
| 95 | + return app |
0 commit comments