[Perf] diffusion: Serve image responses from in-memory encoded bytes (#45987)
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:
co-authored by
Mick Qian
Claude Opus 5.5
parent
2fbd094a8c
commit
3e5f5505ed
@@ -461,6 +461,8 @@ class SamplingParams:
|
||||
|
||||
# return output file paths directly to client
|
||||
return_file_paths_only: bool = True
|
||||
# also return each image's encoded bytes, which are written to disk only when save_output
|
||||
return_image_bytes: bool = False
|
||||
enable_sequence_shard: bool | None = None
|
||||
diffusers_kwargs: dict | None = None
|
||||
max_sequence_length: int | None = None
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import asyncio
|
||||
import base64
|
||||
import contextlib
|
||||
import io
|
||||
import os
|
||||
import time
|
||||
from typing import List, Optional
|
||||
@@ -73,13 +74,8 @@ def _resolve_image_output_format(
|
||||
return sampling_params_cls.default_image_output_format()
|
||||
|
||||
|
||||
def _read_b64_for_paths(paths: list[str]) -> list[str]:
|
||||
"""Read and base64-encode each file. Must be called before cloud upload deletes them."""
|
||||
result = []
|
||||
for path in paths:
|
||||
with open(path, "rb") as f:
|
||||
result.append(base64.b64encode(f.read()).decode("utf-8"))
|
||||
return result
|
||||
def _b64_encode_images(images: list[bytes]) -> list[str]:
|
||||
return [base64.b64encode(image).decode("utf-8") for image in images]
|
||||
|
||||
|
||||
async def _upload_and_cleanup_images(paths: list[str]) -> list[str | None]:
|
||||
@@ -181,7 +177,7 @@ def _build_image_response_kwargs(
|
||||
) -> dict:
|
||||
"""Build ImageResponse data list.
|
||||
|
||||
For b64_json: uses pre-read b64_list (call _read_b64_for_paths first).
|
||||
For b64_json: uses b64_list, the base64 of the returned image bytes.
|
||||
For url: uses cloud_url or fallback_url.
|
||||
file_path is omitted when is_persistent=False to avoid exposing stale temp paths.
|
||||
"""
|
||||
@@ -246,15 +242,15 @@ def _build_image_response_kwargs(
|
||||
|
||||
|
||||
def _get_response_resize(
|
||||
sampling_params: SamplingParams, output_path: str | None = None
|
||||
sampling_params: SamplingParams, output_image_bytes: bytes | None = None
|
||||
) -> str | None:
|
||||
"""Return a generated GLM-Image output's actual size as WIDTHxHEIGHT."""
|
||||
if not isinstance(sampling_params, GlmImageSamplingParams):
|
||||
return None
|
||||
|
||||
if output_path is not None:
|
||||
if output_image_bytes is not None:
|
||||
try:
|
||||
with Image.open(output_path) as output_image:
|
||||
with Image.open(io.BytesIO(output_image_bytes)) as output_image:
|
||||
width, height = output_image.size
|
||||
return f"{width}x{height}"
|
||||
except (OSError, ValueError):
|
||||
@@ -303,6 +299,10 @@ async def generations(
|
||||
raw_request, request.prompt, enabled=request.enhance_prompt, task="image"
|
||||
)
|
||||
|
||||
# responses carry the encoded bytes; files are written only to persist or upload them
|
||||
write_output_files = (
|
||||
server_args.output_path is not None or cloud_storage.is_enabled()
|
||||
)
|
||||
with temp_dir_if_disabled(server_args.output_path) as output_dir:
|
||||
sampling = _build_request_sampling_params(
|
||||
request_id,
|
||||
@@ -315,6 +315,8 @@ async def generations(
|
||||
num_outputs_per_prompt=max(1, min(int(request.n or 1), 10)),
|
||||
output_file_name=f"{request_id}.{ext}",
|
||||
output_path=output_dir,
|
||||
save_output=write_output_files,
|
||||
return_image_bytes=True,
|
||||
num_frames=1,
|
||||
seed=request.seed,
|
||||
generator_device=request.generator_device,
|
||||
@@ -362,11 +364,10 @@ async def generations(
|
||||
async_scheduler_client, batch
|
||||
)
|
||||
save_file_path = save_file_path_list[0]
|
||||
response_resize = _get_response_resize(sampling, save_file_path)
|
||||
response_resize = _get_response_resize(sampling, result.output_image_bytes[0])
|
||||
|
||||
# read b64 before cloud upload may delete the local file
|
||||
b64_list = (
|
||||
_read_b64_for_paths(save_file_path_list)
|
||||
_b64_encode_images(result.output_image_bytes)
|
||||
if resp_format == "b64_json"
|
||||
else None
|
||||
)
|
||||
@@ -496,6 +497,10 @@ async def edits(
|
||||
)
|
||||
|
||||
ext = choose_output_image_ext(output_format, background)
|
||||
# responses carry the encoded bytes; files are written only to persist or upload them
|
||||
write_output_files = (
|
||||
server_args.output_path is not None or cloud_storage.is_enabled()
|
||||
)
|
||||
prompt = await maybe_enhance_prompt(
|
||||
raw_request,
|
||||
prompt,
|
||||
@@ -512,6 +517,8 @@ async def edits(
|
||||
num_outputs_per_prompt=max(1, min(int(n or 1), 10)),
|
||||
output_file_name=f"{request_id}.{ext}",
|
||||
output_path=output_dir,
|
||||
save_output=write_output_files,
|
||||
return_image_bytes=True,
|
||||
image_path=input_paths,
|
||||
seed=seed,
|
||||
generator_device=generator_device,
|
||||
@@ -544,11 +551,10 @@ async def edits(
|
||||
async_scheduler_client, batch
|
||||
)
|
||||
save_file_path = save_file_path_list[0]
|
||||
response_resize = _get_response_resize(sampling, save_file_path)
|
||||
response_resize = _get_response_resize(sampling, result.output_image_bytes[0])
|
||||
|
||||
# read b64 before cloud upload may delete the local file
|
||||
b64_list = (
|
||||
_read_b64_for_paths(save_file_path_list)
|
||||
_b64_encode_images(result.output_image_bytes)
|
||||
if resp_format == "b64_json"
|
||||
else None
|
||||
)
|
||||
|
||||
@@ -590,6 +590,7 @@ async def process_generation_batch(
|
||||
save_file_path_list = result.output_file_paths
|
||||
elif result.output is not None:
|
||||
num_outputs = len(result.output)
|
||||
image_bytes = [] if batch.return_image_bytes else None
|
||||
save_file_path_list = save_outputs(
|
||||
result.output,
|
||||
batch.data_type,
|
||||
@@ -598,6 +599,7 @@ async def process_generation_batch(
|
||||
lambda idx: str(batch.output_file_path(num_outputs, idx)),
|
||||
audio=result.audio,
|
||||
audio_sample_rate=result.audio_sample_rate,
|
||||
image_bytes_out=image_bytes,
|
||||
output_compression=batch.output_compression,
|
||||
x264_preset=batch.x264_preset,
|
||||
enable_frame_interpolation=batch.enable_frame_interpolation,
|
||||
@@ -608,6 +610,7 @@ async def process_generation_batch(
|
||||
upscaling_model_path=batch.upscaling_model_path,
|
||||
upscaling_scale=batch.upscaling_scale,
|
||||
)
|
||||
result.output_image_bytes = image_bytes
|
||||
|
||||
total_time = time.perf_counter() - total_start_time
|
||||
if get_global_server_args().batching_max_size > 1:
|
||||
|
||||
@@ -9,6 +9,7 @@ diffusion models.
|
||||
"""
|
||||
|
||||
import atexit
|
||||
import io
|
||||
import json
|
||||
import mmap
|
||||
import os
|
||||
@@ -1192,15 +1193,30 @@ def save_materialized_output(
|
||||
save_file_path: Optional[str],
|
||||
*,
|
||||
save_output: bool = True,
|
||||
return_image_bytes: bool = False,
|
||||
audio_sample_rate: Optional[int] = None,
|
||||
output_compression: Optional[int] = None,
|
||||
x264_preset: Optional[str] = None,
|
||||
) -> None:
|
||||
) -> bytes | None:
|
||||
"""save the output when save_output; return a single-frame image's encoded bytes
|
||||
|
||||
return_image_bytes encodes the image even when it is not saved; a saved file holds
|
||||
the same bytes
|
||||
"""
|
||||
image_bytes = None
|
||||
if (
|
||||
return_image_bytes
|
||||
and data_type != DataType.VIDEO
|
||||
and len(materialized.frames) == 1
|
||||
):
|
||||
image_bytes = _encode_image_frame(
|
||||
materialized.frames[0], save_file_path, output_compression
|
||||
)
|
||||
if not save_output:
|
||||
return
|
||||
return image_bytes
|
||||
if not save_file_path:
|
||||
logger.info("No output path provided, output not saved")
|
||||
return
|
||||
return image_bytes
|
||||
|
||||
os.makedirs(os.path.dirname(save_file_path), exist_ok=True)
|
||||
if data_type == DataType.VIDEO:
|
||||
@@ -1230,53 +1246,57 @@ def save_materialized_output(
|
||||
fps=materialized.fps,
|
||||
audio_sample_rate=audio_sample_rate,
|
||||
)
|
||||
elif len(materialized.frames) > 1:
|
||||
for i, image in enumerate(materialized.frames):
|
||||
parts = save_file_path.rsplit(".", 1)
|
||||
if len(parts) == 2:
|
||||
indexed_path = f"{parts[0]}_{i}.{parts[1]}"
|
||||
else:
|
||||
indexed_path = f"{save_file_path}_{i}"
|
||||
with open(indexed_path, "wb") as f:
|
||||
f.write(_encode_image_frame(image, indexed_path, output_compression))
|
||||
else:
|
||||
quality = output_compression if output_compression is not None else 75
|
||||
if len(materialized.frames) > 1:
|
||||
for i, image in enumerate(materialized.frames):
|
||||
parts = save_file_path.rsplit(".", 1)
|
||||
if len(parts) == 2:
|
||||
indexed_path = f"{parts[0]}_{i}.{parts[1]}"
|
||||
else:
|
||||
indexed_path = f"{save_file_path}_{i}"
|
||||
_save_image_frame(indexed_path, image, quality, output_compression)
|
||||
else:
|
||||
_save_image_frame(
|
||||
save_file_path, materialized.frames[0], quality, output_compression
|
||||
if image_bytes is None:
|
||||
image_bytes = _encode_image_frame(
|
||||
materialized.frames[0], save_file_path, output_compression
|
||||
)
|
||||
with open(save_file_path, "wb") as f:
|
||||
f.write(image_bytes)
|
||||
logger.info(f"Output saved to {CYAN}{save_file_path}{RESET}")
|
||||
return image_bytes
|
||||
|
||||
|
||||
def _save_image_frame(
|
||||
path: str, frame: np.ndarray, quality: int | None, output_compression: int | None
|
||||
) -> None:
|
||||
def _encode_image_frame(
|
||||
frame: np.ndarray, path: str, output_compression: int | None
|
||||
) -> bytes:
|
||||
"""encode a frame in the image format named by path's extension"""
|
||||
ext = os.path.splitext(path)[1].lower()
|
||||
if ext == ".png":
|
||||
compress_level = 1
|
||||
# zlib level 0 skips compression: several times faster, ~3x the bytes, same pixels
|
||||
compress_level = 0
|
||||
if output_compression is not None and output_compression != 75:
|
||||
compress_level = max(0, min(9, round(output_compression / 100 * 9)))
|
||||
if frame.ndim == 3 and frame.shape[-1] == 1:
|
||||
frame = frame[..., 0]
|
||||
Image.fromarray(frame).save(path, format="PNG", compress_level=compress_level)
|
||||
else:
|
||||
if ext in (".jpg", ".jpeg") and frame.ndim == 3 and frame.shape[-1] == 4:
|
||||
# Models such as Qwen-Image 2.1 produce RGBA even for opaque scenes.
|
||||
# JPEG needs RGB; composite on white instead of dropping alpha and
|
||||
# exposing arbitrary colors from transparent pixels.
|
||||
rgba = Image.fromarray(frame)
|
||||
background = Image.new("RGB", rgba.size, (255, 255, 255))
|
||||
background.paste(rgba, mask=rgba.getchannel("A"))
|
||||
frame = np.asarray(background)
|
||||
imageio.imwrite(path, frame, quality=quality)
|
||||
buffer = io.BytesIO()
|
||||
Image.fromarray(frame).save(buffer, format="PNG", compress_level=compress_level)
|
||||
return buffer.getvalue()
|
||||
if ext in (".jpg", ".jpeg") and frame.ndim == 3 and frame.shape[-1] == 4:
|
||||
# Models such as Qwen-Image 2.1 produce RGBA even for opaque scenes.
|
||||
# JPEG needs RGB; composite on white instead of dropping alpha and
|
||||
# exposing arbitrary colors from transparent pixels.
|
||||
rgba = Image.fromarray(frame)
|
||||
background = Image.new("RGB", rgba.size, (255, 255, 255))
|
||||
background.paste(rgba, mask=rgba.getchannel("A"))
|
||||
frame = np.asarray(background)
|
||||
quality = output_compression if output_compression is not None else 75
|
||||
return imageio.imwrite("<bytes>", frame, format=ext, 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
|
||||
)
|
||||
_encode_image_frame(np.zeros((8, 8, 3), np.uint8), "warm.jpg", None)
|
||||
except Exception:
|
||||
logger.debug("Image writer warmup failed", exc_info=True)
|
||||
|
||||
@@ -1293,6 +1313,7 @@ def save_outputs(
|
||||
samples_out: Optional[list[Any]] = None,
|
||||
audios_out: Optional[list[Any]] = None,
|
||||
frames_out: Optional[list[Any]] = None,
|
||||
image_bytes_out: Optional[list[bytes | None]] = None,
|
||||
output_compression: Optional[int] = None,
|
||||
x264_preset: Optional[str] = None,
|
||||
enable_frame_interpolation: bool = False,
|
||||
@@ -1386,6 +1407,7 @@ def save_outputs(
|
||||
audio_sample_rate=audio_sample_rate,
|
||||
output_compression=output_compression,
|
||||
x264_preset=x264_preset,
|
||||
image_bytes_out=image_bytes_out,
|
||||
enable_frame_interpolation=enable_frame_interpolation,
|
||||
frame_interpolation_exp=frame_interpolation_exp,
|
||||
frame_interpolation_scale=frame_interpolation_scale,
|
||||
@@ -1424,8 +1446,9 @@ def post_process_sample(
|
||||
upscaling_model_path: Optional[str] = None,
|
||||
upscaling_scale: int = 4,
|
||||
x264_preset: Optional[str] = None,
|
||||
image_bytes_out: Optional[list[bytes | None]] = None,
|
||||
) -> list[Any]:
|
||||
"""materialize frames and save outputs (optional)"""
|
||||
"""materialize frames and save outputs (optional); image_bytes_out collects the encoded image"""
|
||||
if data_type == DataType.ACTION:
|
||||
return []
|
||||
|
||||
@@ -1441,13 +1464,16 @@ def post_process_sample(
|
||||
upscaling_model_path=upscaling_model_path,
|
||||
upscaling_scale=upscaling_scale,
|
||||
)
|
||||
save_materialized_output(
|
||||
image_bytes = save_materialized_output(
|
||||
materialized,
|
||||
data_type,
|
||||
save_file_path,
|
||||
save_output=save_output,
|
||||
return_image_bytes=image_bytes_out is not None,
|
||||
audio_sample_rate=audio_sample_rate,
|
||||
output_compression=output_compression,
|
||||
x264_preset=x264_preset,
|
||||
)
|
||||
if image_bytes_out is not None:
|
||||
image_bytes_out.append(image_bytes)
|
||||
return materialized.frames
|
||||
|
||||
@@ -889,7 +889,7 @@ class GPUWorker(GPUWorkerPostTrainingMixin):
|
||||
self._async_output_save
|
||||
and not req.is_warmup
|
||||
and not req.return_raw_frames
|
||||
and bool(req.save_output and req.return_file_paths_only)
|
||||
and self._encodes_on_output_rank(req)
|
||||
and req.perf_dump_path is None
|
||||
and not envs.SGLANG_DIFFUSION_STAGE_LOGGING
|
||||
)
|
||||
@@ -981,11 +981,18 @@ class GPUWorker(GPUWorkerPostTrainingMixin):
|
||||
) -> None:
|
||||
if req.return_raw_frames:
|
||||
self._materialize_raw_frame_transport(output_batch, req)
|
||||
elif req.save_output and req.return_file_paths_only:
|
||||
elif self._encodes_on_output_rank(req):
|
||||
self._materialize_file_path_transport(output_batch, save_output_paths)
|
||||
elif getattr(req, "return_frames", False):
|
||||
self._materialize_frame_outputs_for_return(output_batch, req)
|
||||
|
||||
@staticmethod
|
||||
def _encodes_on_output_rank(req: Req) -> bool:
|
||||
# the output rank encodes the outputs to save them or to return the image bytes
|
||||
return req.return_file_paths_only and (
|
||||
req.save_output or req.return_image_bytes
|
||||
)
|
||||
|
||||
def _materialize_raw_frame_transport(
|
||||
self, output_batch: OutputBatch, req: Req
|
||||
) -> None:
|
||||
@@ -1291,14 +1298,16 @@ class GPUWorker(GPUWorkerPostTrainingMixin):
|
||||
def build_output_path(idx: int) -> str:
|
||||
return req.output_file_path(num_outputs, idx)
|
||||
|
||||
image_bytes = [] if req.return_image_bytes else None
|
||||
output_batch.output_file_paths = save_outputs(
|
||||
output_batch.output,
|
||||
req.data_type,
|
||||
req.fps,
|
||||
True,
|
||||
req.save_output,
|
||||
build_output_path,
|
||||
audio=output_batch.audio,
|
||||
audio_sample_rate=output_batch.audio_sample_rate,
|
||||
image_bytes_out=image_bytes,
|
||||
output_compression=req.output_compression,
|
||||
x264_preset=req.x264_preset,
|
||||
enable_frame_interpolation=req.enable_frame_interpolation,
|
||||
@@ -1309,6 +1318,7 @@ class GPUWorker(GPUWorkerPostTrainingMixin):
|
||||
upscaling_model_path=req.upscaling_model_path,
|
||||
upscaling_scale=req.upscaling_scale,
|
||||
)
|
||||
output_batch.output_image_bytes = image_bytes
|
||||
|
||||
def _save_group_output_paths(
|
||||
self,
|
||||
@@ -1324,14 +1334,16 @@ class GPUWorker(GPUWorkerPostTrainingMixin):
|
||||
)
|
||||
|
||||
first_req = reqs[0]
|
||||
image_bytes = [] if first_req.return_image_bytes else None
|
||||
output_batch.output_file_paths = save_outputs(
|
||||
output_batch.output,
|
||||
first_req.data_type,
|
||||
first_req.fps,
|
||||
True,
|
||||
first_req.save_output,
|
||||
lambda idx: output_requests[idx].output_file_path(),
|
||||
audio=output_batch.audio,
|
||||
audio_sample_rate=output_batch.audio_sample_rate,
|
||||
image_bytes_out=image_bytes,
|
||||
output_compression=first_req.output_compression,
|
||||
x264_preset=first_req.x264_preset,
|
||||
enable_frame_interpolation=first_req.enable_frame_interpolation,
|
||||
@@ -1342,6 +1354,7 @@ class GPUWorker(GPUWorkerPostTrainingMixin):
|
||||
upscaling_model_path=first_req.upscaling_model_path,
|
||||
upscaling_scale=first_req.upscaling_scale,
|
||||
)
|
||||
output_batch.output_image_bytes = image_bytes
|
||||
|
||||
@staticmethod
|
||||
def _validate_group_forward_reqs(reqs: list[Req]) -> None:
|
||||
@@ -1351,6 +1364,7 @@ class GPUWorker(GPUWorkerPostTrainingMixin):
|
||||
"save_output",
|
||||
"return_frames",
|
||||
"return_file_paths_only",
|
||||
"return_image_bytes",
|
||||
"data_type",
|
||||
"fps",
|
||||
"output_compression",
|
||||
|
||||
@@ -1162,6 +1162,9 @@ class Scheduler(
|
||||
output_file_paths=self._slice_batched_value(
|
||||
output_batch.output_file_paths, start, end, total_items
|
||||
),
|
||||
output_image_bytes=self._slice_batched_value(
|
||||
output_batch.output_image_bytes, start, end, total_items
|
||||
),
|
||||
metrics=metrics,
|
||||
noise_pred=self._slice_batched_value(
|
||||
output_batch.noise_pred, start, end, total_items
|
||||
|
||||
@@ -484,6 +484,8 @@ class OutputBatch:
|
||||
trajectory_decoded: list[torch.Tensor] | None = None
|
||||
error: str | None = None
|
||||
output_file_paths: list[str] | None = None
|
||||
# encoded image per output when the request sets return_image_bytes
|
||||
output_image_bytes: list[bytes | None] | None = None
|
||||
# Decoded LiDAR of a joint camera/LiDAR request: file paths and metadata,
|
||||
# plus range_m/intensity/validity arrays when the client asked for frames.
|
||||
lidar: dict[str, Any] | None = None
|
||||
@@ -505,6 +507,7 @@ class OutputBatch:
|
||||
self.rollout_trajectory_data = None
|
||||
self.trajectory_decoded = None
|
||||
self.output_file_paths = None
|
||||
self.output_image_bytes = None
|
||||
self.lidar = None
|
||||
self.raw_frame_batches = None
|
||||
self.noise_pred = None
|
||||
|
||||
@@ -0,0 +1,256 @@
|
||||
"""Image responses carry the bytes encoded on the output rank instead of reading a file back."""
|
||||
|
||||
import base64
|
||||
import os
|
||||
from io import BytesIO
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import imageio
|
||||
import numpy as np
|
||||
import pytest
|
||||
import torch
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
from PIL import Image
|
||||
|
||||
from sglang.multimodal_gen.configs.sample.sampling_params import (
|
||||
DataType,
|
||||
SamplingParams,
|
||||
)
|
||||
from sglang.multimodal_gen.runtime.entrypoints.openai import image_api
|
||||
from sglang.multimodal_gen.runtime.entrypoints.utils import (
|
||||
MaterializedOutput,
|
||||
save_materialized_output,
|
||||
save_outputs,
|
||||
)
|
||||
from sglang.multimodal_gen.runtime.managers.gpu_worker import GPUWorker
|
||||
from sglang.multimodal_gen.runtime.managers.scheduler import Scheduler
|
||||
from sglang.multimodal_gen.runtime.pipelines_core.schedule_batch import (
|
||||
OutputBatch,
|
||||
Req,
|
||||
)
|
||||
|
||||
|
||||
def _frame(channels: int) -> np.ndarray:
|
||||
shape = (17, 23) if channels == 0 else (17, 23, channels)
|
||||
return np.random.default_rng(channels).integers(0, 256, shape, dtype=np.uint8)
|
||||
|
||||
|
||||
def _decode(data: bytes) -> np.ndarray:
|
||||
with Image.open(BytesIO(data)) as image:
|
||||
return np.asarray(image)
|
||||
|
||||
|
||||
def _uint8_hwc(output: torch.Tensor) -> np.ndarray:
|
||||
return (output * 255).clamp(0, 255).to(torch.uint8).permute(1, 2, 0).numpy()
|
||||
|
||||
|
||||
def _encode(frame: np.ndarray, path: str, **kwargs) -> bytes:
|
||||
return save_materialized_output(
|
||||
MaterializedOutput(sample=None, frames=[frame]),
|
||||
DataType.IMAGE,
|
||||
path,
|
||||
return_image_bytes=True,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
# channels 0 is a 2-D grayscale frame, 1 a single-channel HWC frame
|
||||
@pytest.mark.parametrize("channels", [0, 1, 3, 4])
|
||||
@pytest.mark.parametrize("output_compression", [None, 75, 100])
|
||||
def test_returned_png_holds_the_source_pixels_and_writes_nothing(
|
||||
tmp_path, channels, output_compression
|
||||
):
|
||||
frame = _frame(channels)
|
||||
|
||||
data = _encode(
|
||||
frame,
|
||||
str(tmp_path / "out.png"),
|
||||
save_output=False,
|
||||
output_compression=output_compression,
|
||||
)
|
||||
|
||||
np.testing.assert_array_equal(_decode(data), frame.reshape(17, 23, -1).squeeze())
|
||||
assert list(tmp_path.iterdir()) == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("extension", ["png", "jpg", "webp"])
|
||||
def test_saved_file_holds_the_returned_bytes(tmp_path, extension):
|
||||
path = tmp_path / f"out.{extension}"
|
||||
|
||||
data = _encode(_frame(3), str(path))
|
||||
|
||||
assert path.read_bytes() == data
|
||||
|
||||
|
||||
@pytest.mark.parametrize("extension", ["jpg", "webp"])
|
||||
@pytest.mark.parametrize("output_compression", [None, 90])
|
||||
def test_lossy_formats_keep_the_file_writer_bytes(
|
||||
tmp_path, extension, output_compression
|
||||
):
|
||||
frame = _frame(3)
|
||||
reference = tmp_path / f"reference.{extension}"
|
||||
imageio.imwrite(
|
||||
reference,
|
||||
frame,
|
||||
quality=75 if output_compression is None else output_compression,
|
||||
)
|
||||
|
||||
data = _encode(
|
||||
frame,
|
||||
str(tmp_path / f"out.{extension}"),
|
||||
save_output=False,
|
||||
output_compression=output_compression,
|
||||
)
|
||||
|
||||
assert data == reference.read_bytes()
|
||||
|
||||
|
||||
def test_save_outputs_returns_one_image_per_output(tmp_path):
|
||||
outputs = [torch.rand(3, 8, 8), torch.rand(3, 8, 8)]
|
||||
image_bytes = []
|
||||
|
||||
paths = save_outputs(
|
||||
outputs,
|
||||
DataType.IMAGE,
|
||||
1,
|
||||
False,
|
||||
lambda idx: str(tmp_path / f"out_{idx}.png"),
|
||||
image_bytes_out=image_bytes,
|
||||
)
|
||||
|
||||
assert paths == [str(tmp_path / "out_0.png"), str(tmp_path / "out_1.png")]
|
||||
for output, data in zip(outputs, image_bytes, strict=True):
|
||||
np.testing.assert_array_equal(_decode(data), _uint8_hwc(output))
|
||||
assert list(tmp_path.iterdir()) == []
|
||||
|
||||
|
||||
def _image_req(tmp_path, *, save_output: bool, return_image_bytes: bool) -> Req:
|
||||
return Req(
|
||||
sampling_params=SamplingParams(
|
||||
data_type=DataType.IMAGE,
|
||||
save_output=save_output,
|
||||
return_image_bytes=return_image_bytes,
|
||||
output_path=str(tmp_path),
|
||||
output_file_name="out.png",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("save_output", "return_image_bytes", "encodes"),
|
||||
[
|
||||
(True, False, True),
|
||||
(False, True, True),
|
||||
(True, True, True),
|
||||
(False, False, False),
|
||||
],
|
||||
)
|
||||
def test_output_rank_encodes_when_saving_or_returning_bytes(
|
||||
tmp_path, save_output, return_image_bytes, encodes
|
||||
):
|
||||
req = _image_req(
|
||||
tmp_path, save_output=save_output, return_image_bytes=return_image_bytes
|
||||
)
|
||||
|
||||
assert GPUWorker._encodes_on_output_rank(req) is encodes
|
||||
|
||||
|
||||
@pytest.mark.parametrize("save_output", [False, True])
|
||||
def test_worker_returns_image_bytes_and_writes_only_when_saving(tmp_path, save_output):
|
||||
req = _image_req(tmp_path, save_output=save_output, return_image_bytes=True)
|
||||
output = torch.rand(3, 8, 8)
|
||||
batch = OutputBatch(output=[output])
|
||||
|
||||
GPUWorker._save_output_paths(SimpleNamespace(is_output_rank=True), req, batch)
|
||||
|
||||
path = tmp_path / "out.png"
|
||||
assert batch.output_file_paths == [str(path)]
|
||||
(data,) = batch.output_image_bytes
|
||||
np.testing.assert_array_equal(_decode(data), _uint8_hwc(output))
|
||||
assert path.exists() == save_output
|
||||
if save_output:
|
||||
assert path.read_bytes() == data
|
||||
|
||||
|
||||
def test_worker_without_image_bytes_request_returns_none(tmp_path):
|
||||
req = _image_req(tmp_path, save_output=True, return_image_bytes=False)
|
||||
batch = OutputBatch(output=[torch.rand(3, 8, 8)])
|
||||
|
||||
GPUWorker._save_output_paths(SimpleNamespace(is_output_rank=True), req, batch)
|
||||
|
||||
assert batch.output_image_bytes is None
|
||||
assert (tmp_path / "out.png").exists()
|
||||
|
||||
|
||||
def test_dynamic_batch_split_keeps_each_requests_image_bytes():
|
||||
scheduler = object.__new__(Scheduler)
|
||||
reqs = [Req(sampling_params=SamplingParams()) for _ in range(2)]
|
||||
|
||||
outputs = scheduler._split_batched_output(
|
||||
OutputBatch(
|
||||
output_file_paths=["a.png", "b.png"], output_image_bytes=[b"a", b"b"]
|
||||
),
|
||||
reqs,
|
||||
)
|
||||
|
||||
assert [output.output_image_bytes for output in outputs] == [[b"a"], [b"b"]]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("endpoint", ["generations", "edits"])
|
||||
@pytest.mark.parametrize("persistent", [False, True])
|
||||
def test_b64_response_is_the_returned_bytes(
|
||||
monkeypatch, tmp_path, endpoint, persistent
|
||||
):
|
||||
output_path = str(tmp_path / "outputs") if persistent else None
|
||||
monkeypatch.setattr(
|
||||
image_api,
|
||||
"get_global_server_args",
|
||||
lambda: SimpleNamespace(output_path=output_path, input_save_path=None),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
image_api, "resolve_sampling_params_cls", lambda _: SamplingParams
|
||||
)
|
||||
monkeypatch.setattr(image_api, "maybe_enhance_prompt", AsyncMock(return_value="p"))
|
||||
monkeypatch.setattr(image_api.cloud_storage, "is_enabled", lambda: False)
|
||||
captured = {}
|
||||
|
||||
def build(request_id, **kwargs):
|
||||
captured.update(kwargs)
|
||||
return SamplingParams()
|
||||
|
||||
monkeypatch.setattr(image_api, "_build_request_sampling_params", build)
|
||||
monkeypatch.setattr(
|
||||
image_api, "prepare_request", lambda **_: SimpleNamespace(extra={})
|
||||
)
|
||||
png = BytesIO()
|
||||
Image.fromarray(_frame(3)).save(png, format="PNG")
|
||||
png = png.getvalue()
|
||||
|
||||
async def generate(_client, _batch):
|
||||
# no file exists at the returned path; the response must not need one
|
||||
path = os.path.join(captured["output_path"], captured["output_file_name"])
|
||||
return [path], OutputBatch(output_image_bytes=[png])
|
||||
|
||||
monkeypatch.setattr(image_api, "process_generation_batch", generate)
|
||||
app = FastAPI()
|
||||
app.include_router(image_api.router)
|
||||
with TestClient(app) as client:
|
||||
if endpoint == "generations":
|
||||
response = client.post(
|
||||
"/v1/images/generations",
|
||||
json={"prompt": "p", "response_format": "b64_json"},
|
||||
)
|
||||
else:
|
||||
response = client.post(
|
||||
"/v1/images/edits",
|
||||
data={"prompt": "p", "response_format": "b64_json"},
|
||||
files={"image": ("reference.png", png, "image/png")},
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.text
|
||||
(item,) = response.json()["data"]
|
||||
assert base64.b64decode(item["b64_json"]) == png
|
||||
assert captured["return_image_bytes"] is True
|
||||
assert captured["save_output"] is persistent
|
||||
@@ -208,9 +208,7 @@ def http_client(monkeypatch, tmp_path, request):
|
||||
|
||||
async def generate(client, req, **kwargs):
|
||||
admitted.append(req)
|
||||
path = req.output_file_path()
|
||||
Image.new("RGB", (64, 64)).save(path)
|
||||
return [path], OutputBatch()
|
||||
return [req.output_file_path()], OutputBatch(output_image_bytes=[png_bytes()])
|
||||
|
||||
async def upload(paths):
|
||||
return [None] * len(paths)
|
||||
|
||||
@@ -237,20 +237,22 @@ def test_response_resize_prefers_requested_size_over_generation_canvas():
|
||||
assert _get_response_resize(glm_sampling) == "1280x720"
|
||||
|
||||
|
||||
def test_response_resize_uses_actual_generated_image_size(tmp_path):
|
||||
output_path = tmp_path / "output.png"
|
||||
Image.new("RGB", (1280, 736)).save(output_path)
|
||||
def _png_bytes(size: tuple[int, int]) -> bytes:
|
||||
buffer = BytesIO()
|
||||
Image.new("RGB", size).save(buffer, format="PNG")
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def test_response_resize_uses_actual_generated_image_size():
|
||||
glm_sampling = GlmImageSamplingParams(image_path="input.png")
|
||||
|
||||
assert _get_response_resize(glm_sampling, str(output_path)) == "1280x736"
|
||||
assert _get_response_resize(glm_sampling, _png_bytes((1280, 736))) == "1280x736"
|
||||
|
||||
|
||||
def test_response_resize_prefers_final_output_over_sampling_canvas(tmp_path):
|
||||
output_path = tmp_path / "upscaled.png"
|
||||
Image.new("RGB", (2560, 1472)).save(output_path)
|
||||
def test_response_resize_prefers_final_output_over_sampling_canvas():
|
||||
glm_sampling = GlmImageSamplingParams(width=1280, height=736)
|
||||
|
||||
assert _get_response_resize(glm_sampling, str(output_path)) == "2560x1472"
|
||||
assert _get_response_resize(glm_sampling, _png_bytes((2560, 1472))) == "2560x1472"
|
||||
|
||||
|
||||
def test_url_response_uses_variant_fallback_urls_for_multiple_persistent_outputs():
|
||||
|
||||
@@ -43,7 +43,8 @@ def test_png_output_saving_preserves_pixels(tmp_path, output_compression):
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("output_compression", "expected_compress_level"), [(None, 1), (0, 0), (75, 1)]
|
||||
("output_compression", "expected_compress_level"),
|
||||
[(None, 0), (0, 0), (75, 0), (100, 9)],
|
||||
)
|
||||
def test_png_output_saving_uses_fast_pillow_path(
|
||||
tmp_path, monkeypatch, output_compression, expected_compress_level
|
||||
@@ -122,17 +123,16 @@ 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)
|
||||
def imwrite_spy(uri, frame, **kwargs):
|
||||
written.append((uri, kwargs))
|
||||
return imwrite(uri, frame, **kwargs)
|
||||
|
||||
monkeypatch.setattr(output_utils.imageio, "imwrite", imwrite_spy)
|
||||
monkeypatch.setattr(output_utils.tempfile, "tempdir", str(tmp_path))
|
||||
monkeypatch.chdir(tmp_path)
|
||||
|
||||
output_utils.warm_image_writer()
|
||||
|
||||
assert len(written) == 1
|
||||
assert written[0][0].endswith(".jpg") and written[0][1] == {"quality": 75}
|
||||
assert written == [("<bytes>", {"format": ".jpg", "quality": 75})]
|
||||
assert list(tmp_path.iterdir()) == []
|
||||
|
||||
|
||||
|
||||
@@ -369,7 +369,8 @@ def server(monkeypatch, tmp_path):
|
||||
path = tmp_path / f"{batch.request_id}_{idx}.png"
|
||||
Image.new("RGB", (16, 16), "red").save(path)
|
||||
paths.append(str(path))
|
||||
return OutputBatch(output_file_paths=paths)
|
||||
image_bytes = [open(path, "rb").read() for path in paths]
|
||||
return OutputBatch(output_file_paths=paths, output_image_bytes=image_bytes)
|
||||
|
||||
monkeypatch.setattr(async_scheduler_client, "forward", forward)
|
||||
app = http_server.create_app(args)
|
||||
|
||||
Reference in New Issue
Block a user