From ad3e80d4de6808a77428eb651b30d831fe41b67b Mon Sep 17 00:00:00 2001 From: Mick Date: Mon, 5 Oct 2026 21:58:00 +0800 Subject: [PATCH] [diffusion] chore: warm the image writer and the HTTP route models at startup (#41833) Co-authored-by: Mick Qian Co-authored-by: Claude Opus 5.5 --- .../runtime/entrypoints/http_server.py | 10 ++++++ .../runtime/entrypoints/utils.py | 11 +++++++ .../runtime/managers/gpu_worker.py | 7 +++++ .../test/unit/test_health_warmup_gate.py | 31 +++++++++++++++++++ .../test/unit/test_output_saving.py | 18 +++++++++++ 5 files changed, 77 insertions(+) diff --git a/python/sglang/multimodal_gen/runtime/entrypoints/http_server.py b/python/sglang/multimodal_gen/runtime/entrypoints/http_server.py index e8226e5260..f67de09570 100644 --- a/python/sglang/multimodal_gen/runtime/entrypoints/http_server.py +++ b/python/sglang/multimodal_gen/runtime/entrypoints/http_server.py @@ -112,6 +112,15 @@ async def _run_server_warmup_after_http_live( os.kill(os.getpid(), signal.SIGTERM) +def build_route_request_models(app: FastAPI) -> None: + """Build the routes' pydantic models now; FastAPI otherwise builds them on the + first matching request (~17 ms). The OpenAPI schema resolves the same models.""" + try: + app.openapi() + except Exception: + logger.debug("Route model prebuild failed", exc_info=True) + + @asynccontextmanager async def lifespan(app: FastAPI): from sglang.multimodal_gen.runtime.entrypoints.openai.video_api import ( @@ -124,6 +133,7 @@ async def lifespan(app: FastAPI): # 1. Initialize the singleton client that connects to the backend Scheduler server_args = app.state.server_args + build_route_request_models(app) async_scheduler_client.initialize(server_args) warmup_done = asyncio.Event() app.state.server_warmup_done = warmup_done diff --git a/python/sglang/multimodal_gen/runtime/entrypoints/utils.py b/python/sglang/multimodal_gen/runtime/entrypoints/utils.py index 520af1b047..807f42b46c 100644 --- a/python/sglang/multimodal_gen/runtime/entrypoints/utils.py +++ b/python/sglang/multimodal_gen/runtime/entrypoints/utils.py @@ -1024,6 +1024,17 @@ def _save_image_frame( imageio.imwrite(path, frame, quality=quality) +def warm_image_writer() -> None: + """Run imageio's one-time plugin setup (~30 ms) before the first request saves.""" + try: + with tempfile.TemporaryDirectory() as tmp: + _save_image_frame( + os.path.join(tmp, "warm.jpg"), np.zeros((8, 8, 3), np.uint8), 75, None + ) + except Exception: + logger.debug("Image writer warmup failed", exc_info=True) + + def save_outputs( outputs: Sequence[Any], data_type: DataType, diff --git a/python/sglang/multimodal_gen/runtime/managers/gpu_worker.py b/python/sglang/multimodal_gen/runtime/managers/gpu_worker.py index 5f98bef67e..598108554d 100644 --- a/python/sglang/multimodal_gen/runtime/managers/gpu_worker.py +++ b/python/sglang/multimodal_gen/runtime/managers/gpu_worker.py @@ -52,6 +52,7 @@ from sglang.multimodal_gen.runtime.entrypoints.utils import ( materialize_output_sample, post_process_sample, save_outputs, + warm_image_writer, ) from sglang.multimodal_gen.runtime.managers.memory_managers.auto_residency import ( WarmupMemoryRecord, @@ -461,6 +462,12 @@ class GPUWorker(GPUWorkerPostTrainingMixin): ), ) + if ( + self.is_output_rank + and self.server_args.pipeline_config.task_type.is_image_gen() + ): + warm_image_writer() + logger.info( f"Worker {self.rank}: Initialized device, model, and distributed environment." ) diff --git a/python/sglang/multimodal_gen/test/unit/test_health_warmup_gate.py b/python/sglang/multimodal_gen/test/unit/test_health_warmup_gate.py index a3898bad83..8dbb95f4b2 100644 --- a/python/sglang/multimodal_gen/test/unit/test_health_warmup_gate.py +++ b/python/sglang/multimodal_gen/test/unit/test_health_warmup_gate.py @@ -53,6 +53,37 @@ class TestHealthWarmupGate(unittest.IsolatedAsyncioTestCase): self.assertEqual(resp, {"status": "ok"}) +class TestRouteModelPrebuild(unittest.TestCase): + def _app(self): + from fastapi import APIRouter, FastAPI + from pydantic import BaseModel + + class Body(BaseModel): + value: int + + router = APIRouter() + + @router.post("/thing") + async def thing(body: Body) -> Body: + return body + + app = FastAPI() + app.include_router(router) + return app + + def test_prebuild_resolves_route_models_before_the_first_request(self): + app = self._app() + self.assertIsNone(app.openapi_schema) + http_server.build_route_request_models(app) + self.assertIn("/thing", app.openapi_schema["paths"]) + + def test_prebuild_failure_does_not_break_startup(self): + app = self._app() + with mock.patch.object(app, "openapi", side_effect=RuntimeError("bad schema")): + http_server.build_route_request_models(app) + self.assertIsNone(app.openapi_schema) + + class _FakeResponse: def __init__(self, status_code: int): self.status_code = status_code diff --git a/python/sglang/multimodal_gen/test/unit/test_output_saving.py b/python/sglang/multimodal_gen/test/unit/test_output_saving.py index 27150d883d..ee5bc9178e 100644 --- a/python/sglang/multimodal_gen/test/unit/test_output_saving.py +++ b/python/sglang/multimodal_gen/test/unit/test_output_saving.py @@ -73,6 +73,24 @@ def test_png_output_saving_uses_fast_pillow_path( assert save_calls == [("PNG", expected_compress_level)] +def test_warm_image_writer_takes_the_default_jpeg_path(tmp_path, monkeypatch): + written = [] + imwrite = output_utils.imageio.imwrite + + def imwrite_spy(path, frame, **kwargs): + written.append((path, kwargs)) + return imwrite(path, frame, **kwargs) + + monkeypatch.setattr(output_utils.imageio, "imwrite", imwrite_spy) + monkeypatch.setattr(output_utils.tempfile, "tempdir", str(tmp_path)) + + output_utils.warm_image_writer() + + assert len(written) == 1 + assert written[0][0].endswith(".jpg") and written[0][1] == {"quality": 75} + assert list(tmp_path.iterdir()) == [] + + def test_video_with_audio_uses_single_pass_encoder(tmp_path, monkeypatch): output_path = tmp_path / "sample.mp4" calls = []