[diffusion] chore: warm the image writer and the HTTP route models at startup (#41833)

Co-authored-by: Mick Qian <mickqian@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Mick
2026-10-05 21:58:00 +08:00
committed by GitHub
co-authored by Mick Qian Claude Opus 5.5
parent c83c84b422
commit ad3e80d4de
5 changed files with 77 additions and 0 deletions
@@ -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
@@ -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,
@@ -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."
)
@@ -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
@@ -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 = []