diff --git a/python/sglang/multimodal_gen/runtime/entrypoints/utils.py b/python/sglang/multimodal_gen/runtime/entrypoints/utils.py index 2458c62963..e8f89038d6 100644 --- a/python/sglang/multimodal_gen/runtime/entrypoints/utils.py +++ b/python/sglang/multimodal_gen/runtime/entrypoints/utils.py @@ -14,9 +14,11 @@ import json import mmap import os import queue +import struct import subprocess import tempfile import threading +import zlib from concurrent.futures import ThreadPoolExecutor from contextlib import contextmanager from dataclasses import dataclass, field @@ -1266,6 +1268,38 @@ def save_materialized_output( return image_bytes +# PNG color type by channel count: gray, gray + alpha, RGB, RGBA +_PNG_COLOR_TYPES = {1: 0, 2: 4, 3: 2, 4: 6} + + +def _png_chunk(tag: bytes, data) -> bytes: + crc = zlib.crc32(data, zlib.crc32(tag)) + return b"".join((struct.pack(">I", len(data)), tag, data, struct.pack(">I", crc))) + + +def _encode_png_stored(frame: np.ndarray) -> bytes: + """an 8-bit PNG of frame with unfiltered rows in stored (uncompressed) deflate blocks + + PIL still filters every row at compress_level 0, which cannot shrink stored + blocks; skipping the filter writes the same pixels over ten times faster + """ + height, width = frame.shape[:2] + channels = 1 if frame.ndim == 2 else frame.shape[2] + rows = np.zeros((height, 1 + width * channels), np.uint8) + rows[:, 1:] = frame.reshape(height, width * channels) + header = struct.pack( + ">IIBBBBB", width, height, 8, _PNG_COLOR_TYPES[channels], 0, 0, 0 + ) + return b"".join( + ( + b"\x89PNG\r\n\x1a\n", + _png_chunk(b"IHDR", header), + _png_chunk(b"IDAT", zlib.compress(rows, 0)), + _png_chunk(b"IEND", b""), + ) + ) + + def _encode_image_frame( frame: np.ndarray, path: str, output_compression: int | None ) -> bytes: @@ -1278,6 +1312,8 @@ def _encode_image_frame( compress_level = max(0, min(9, round(output_compression / 100 * 9))) if frame.ndim == 3 and frame.shape[-1] == 1: frame = frame[..., 0] + if compress_level == 0 and frame.dtype == np.uint8: + return _encode_png_stored(frame) buffer = io.BytesIO() Image.fromarray(frame).save(buffer, format="PNG", compress_level=compress_level) return buffer.getvalue() diff --git a/python/sglang/multimodal_gen/test/unit/test_image_response_bytes.py b/python/sglang/multimodal_gen/test/unit/test_image_response_bytes.py index 3ee2815a4c..6196aadd4b 100644 --- a/python/sglang/multimodal_gen/test/unit/test_image_response_bytes.py +++ b/python/sglang/multimodal_gen/test/unit/test_image_response_bytes.py @@ -57,7 +57,7 @@ def _encode(frame: np.ndarray, path: str, **kwargs) -> bytes: # 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("channels", [0, 1, 2, 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 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 857816386c..70a2a3532c 100644 --- a/python/sglang/multimodal_gen/test/unit/test_output_saving.py +++ b/python/sglang/multimodal_gen/test/unit/test_output_saving.py @@ -42,18 +42,19 @@ def test_png_output_saving_preserves_pixels(tmp_path, output_compression): np.testing.assert_array_equal(np.array(Image.open(output_path)), frame) +# compress_level 0 writes stored blocks itself; a compressed PNG goes through Pillow @pytest.mark.parametrize( - ("output_compression", "expected_compress_level"), - [(None, 0), (0, 0), (75, 0), (100, 9)], + ("output_compression", "expected_pillow_saves"), + [(None, []), (0, []), (75, []), (100, [("PNG", 9)])], ) -def test_png_output_saving_uses_fast_pillow_path( - tmp_path, monkeypatch, output_compression, expected_compress_level +def test_png_output_saving_skips_pillow_row_filters_when_uncompressed( + tmp_path, monkeypatch, output_compression, expected_pillow_saves ): frame = _rgb_frame() output_path = tmp_path / f"sample_{output_compression}.png" def fail_imageio_imwrite(*args, **kwargs): - raise AssertionError("PNG output should use Pillow's PNG fast path") + raise AssertionError("PNG output should not go through imageio") original_save = Image.Image.save save_calls = [] @@ -73,7 +74,8 @@ def test_png_output_saving_uses_fast_pillow_path( output_compression=output_compression, ) - assert save_calls == [("PNG", expected_compress_level)] + assert save_calls == expected_pillow_saves + np.testing.assert_array_equal(np.array(Image.open(output_path)), frame) @pytest.mark.parametrize("extension", ["jpg", "jpeg", "JPG"])