Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e431df5
[MM] Lazy media decode: move decoding into mm processor
Isotr0py Sep 19, 2026
473985d
[MM] Lazy decode refinements: per-modality IOs, vision_chunk, error s…
Isotr0py Sep 19, 2026
68fd9ee
[MM] Add lazy media decode design doc
Isotr0py Sep 19, 2026
4c35986
[MM] Fix hidden PNG decode on the lazy hash path; add benchmark
Isotr0py Sep 19, 2026
ed807a1
[MM] Fix uuid validation force-decoding lazy media on the mm worker
Isotr0py Sep 19, 2026
9f8b241
[MM] Two-phase apply: overlap lazy decode across requests
Isotr0py Sep 19, 2026
ca360b3
Merge branch 'main' into mm/processor-decode-refactor
DarkLight1337 Sep 23, 2026
ee0c2ab
[MM] Unify fetched media as MediaRef, resolve decoders in the processor
DarkLight1337 Sep 23, 2026
13d62e1
[MM] Report the item index in parser-side decode errors
DarkLight1337 Sep 23, 2026
5fbf2d4
Address comments
DarkLight1337 Sep 30, 2026
830e0d5
Merge branch 'main' into mm/processor-decode-refactor
DarkLight1337 Sep 30, 2026
d81c52f
Merge branch 'main' into mm/processor-decode-refactor
DarkLight1337 Sep 30, 2026
5f26f70
Address comment
DarkLight1337 Sep 30, 2026
36b67e8
Clean up code
DarkLight1337 Sep 30, 2026
849c4f1
More clean up
DarkLight1337 Sep 30, 2026
df35654
Fix lazy media cache identity, ownership, and cancellation
DarkLight1337 Sep 30, 2026
93518c2
Improve code quality
DarkLight1337 Sep 30, 2026
7effcc7
Merge branch 'main' into mm/processor-decode-refactor
DarkLight1337 Sep 30, 2026
ade1a91
Fix
DarkLight1337 Sep 30, 2026
248ac30
Unnecessary assert
DarkLight1337 Sep 30, 2026
4c15108
More cleanup
DarkLight1337 Sep 30, 2026
e05a51c
More clean up
DarkLight1337 Sep 30, 2026
436e470
Clean up
DarkLight1337 Sep 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
533 changes: 533 additions & 0 deletions benchmarks/benchmark_lazy_decode.py

Large diffs are not rendered by default.

147 changes: 147 additions & 0 deletions docs/design/lazy-media-decode.md

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
)
from vllm.logger import init_logger
from vllm.logprobs import Logprob
from vllm.multimodal.media import MediaWithBytes
from vllm.multimodal.utils import fetch_image
from vllm.outputs import RequestOutput
from vllm.platforms import current_platform
Expand Down Expand Up @@ -1736,11 +1735,7 @@ def url_for(self, name: str) -> str:
return f"{self.base_url}/{name}"

def get_image_asset(self, name: str) -> Image.Image:
image = fetch_image(self.url_for(name))
# Unwrap MediaWithBytes if present
if isinstance(image, MediaWithBytes):
image = image.media
return image
return fetch_image(self.url_for(name))


@pytest.fixture(scope="session")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from tests.entrypoints.multimodal.conftest import TEST_IMAGE_ASSETS
from tests.utils import ROCM_EXTRA_ARGS, RemoteOpenAIServer
from vllm.multimodal.media import MediaWithBytes
from vllm.multimodal.utils import encode_image_url, fetch_image
from vllm.platforms import current_platform

Expand Down Expand Up @@ -198,9 +197,6 @@ def get_hf_prompt_tokens(model_name, content, image_url):
}
]
image = fetch_image(image_url)
# Unwrap MediaWithBytes if present
if isinstance(image, MediaWithBytes):
image = image.media
images = [image]

prompt = processor.tokenizer.apply_chat_template(
Expand Down
4 changes: 0 additions & 4 deletions tests/entrypoints/pooling/embed/test_online_vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from tests.utils import VLLM_PATH, RemoteOpenAIServer
from vllm.entrypoints.pooling.embed.protocol import EmbeddingResponse
from vllm.multimodal.media import MediaWithBytes
from vllm.multimodal.utils import encode_image_url, fetch_image

MODEL_NAME = "TIGER-Lab/VLM2Vec-Full"
Expand Down Expand Up @@ -169,9 +168,6 @@ def get_hf_prompt_tokens(model_name, content, image_url):
placeholder = "<|image_1|> "
prompt = f"{placeholder}{content}"
image = fetch_image(image_url)
# Unwrap MediaWithBytes if present
if isinstance(image, MediaWithBytes):
image = image.media
images = [image]
inputs = processor(prompt, images, return_tensors="pt")
return inputs.input_ids.shape[1]
Expand Down
284 changes: 283 additions & 1 deletion tests/entrypoints/unit_tests/test_chat_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

import asyncio
import threading
import warnings
from collections.abc import Mapping
from typing import Literal
Expand All @@ -19,15 +20,17 @@
AsyncMultiModalItemTracker,
ChatCompletionMessageParam,
ConversationMessage,
MultiModalItemTracker,
_load_embeds_dict,
_parse_metadata_array,
_postprocess_messages,
parse_chat_messages,
parse_chat_messages_async,
validate_chat_template,
)
from vllm.exceptions import VLLMValidationError
from vllm.exceptions import VLLMUnprocessableEntityError, VLLMValidationError
from vllm.inputs import MultiModalDataDict, MultiModalUUIDDict
from vllm.multimodal.media import MediaConnector, MediaRef
from vllm.multimodal.utils import (
encode_audio_url,
encode_image_url,
Expand Down Expand Up @@ -3072,6 +3075,176 @@ async def _fetch(should_fail: bool, delay: float):
)


@pytest.mark.asyncio
@pytest.mark.parametrize("modality", ["image", "video"])
async def test_resolve_items_decodes_lazy_vision_chunk_off_event_loop(modality):
"""Lazy vision_chunk items decode on the media thread pool, not the
event-loop thread, when use_unified_vision_chunk_modality is active."""
loop_thread_name = threading.current_thread().name
decode_thread_names: list[str] = []

def _decode():
decode_thread_names.append(threading.current_thread().name)
return "decoded-image" if modality == "image" else ("decoded-frames", {})

lazy_item = MediaRef(_decode, b"fake-image-bytes")

async def _fetch():
return lazy_item, "uuid-0"

tracker = AsyncMultiModalItemTracker(
MagicMock(
is_multimodal_model=True,
hf_config=MagicMock(use_unified_vision_chunk=True),
)
)
processor = MagicMock()
processor.split_video_chunks.return_value = [
{"video_chunk": "split-frames", "prompt": "video-prompt"}
]
tracker.__dict__["mm_processor"] = processor
tracker._items_by_modality["vision_chunk"] = [lambda: _fetch()]
tracker._modality_order["vision_chunk"] = [modality]

mm_data, mm_uuids = await tracker.resolve_items()

assert decode_thread_names
assert all(name != loop_thread_name for name in decode_thread_names)
assert lazy_item.is_decoded
assert mm_data is not None and mm_data["vision_chunk"] is not None
chunk = mm_data["vision_chunk"][0]
assert chunk is not None
if modality == "image":
assert chunk["type"] == "image"
assert chunk["image"] == "decoded-image"
else:
processor.split_video_chunks.assert_called_once_with("decoded-frames")
assert chunk["type"] == "video_chunk"
assert chunk["video_chunk"] == "split-frames"
assert mm_uuids == {"vision_chunk": ["uuid-0"]}


@pytest.mark.asyncio
async def test_resolve_items_drains_vision_chunk_decode_on_cancellation():
"""Cancellation waits for the media worker before returning to the caller."""
loop = asyncio.get_running_loop()
started = asyncio.Event()
finish = threading.Event()
completed = threading.Event()

def decode():
loop.call_soon_threadsafe(started.set)
assert finish.wait(timeout=10)
completed.set()
return "decoded-image"

ref = MediaRef(decode, b"image")

async def fetch():
return ref, "uuid-0"

tracker = AsyncMultiModalItemTracker(
MagicMock(
is_multimodal_model=True,
hf_config=MagicMock(use_unified_vision_chunk=True),
)
)
tracker.__dict__["mm_processor"] = MagicMock()
tracker._items_by_modality["vision_chunk"] = [fetch]
tracker._modality_order["vision_chunk"] = ["image"]

task = asyncio.create_task(tracker.resolve_items())
try:
await asyncio.wait_for(started.wait(), timeout=10)
task.cancel()
# Let the cancellation handler run while the worker is still blocked.
await asyncio.sleep(0)
assert not task.done()
finally:
finish.set()
with pytest.raises(asyncio.CancelledError):
await asyncio.wait_for(task, timeout=10)

assert completed.is_set()
assert ref.is_decoded


@pytest.mark.asyncio
async def test_resolve_items_lazy_vision_chunk_decode_error_propagates_async():
"""A MediaRef decode failure in a vision_chunk item must raise
VLLMUnprocessableEntityError, not be logged and swallowed."""

def _decode():
raise ValueError("corrupt media")

lazy_item = MediaRef(_decode, b"corrupt-video-bytes")

async def _fetch():
return lazy_item, None

tracker = AsyncMultiModalItemTracker(
MagicMock(
is_multimodal_model=True,
hf_config=MagicMock(use_unified_vision_chunk=True),
)
)
tracker.__dict__["mm_processor"] = MagicMock()
tracker._items_by_modality["vision_chunk"] = [lambda: _fetch()]
tracker._modality_order["vision_chunk"] = ["video"]

with pytest.raises(VLLMUnprocessableEntityError, match="corrupt media"):
await tracker.resolve_items()


def test_resolve_items_lazy_vision_chunk_decode_error_propagates_sync(caplog):
"""Sync path: a MediaRef decode failure in a vision_chunk item must
propagate as VLLMUnprocessableEntityError instead of hitting the
"Failed to split video chunks" log-and-append fallback."""

def _decode():
raise ValueError("corrupt media")

lazy_item = MediaRef(_decode, b"corrupt-video-bytes")

tracker = MultiModalItemTracker(MagicMock(is_multimodal_model=True))
tracker.__dict__["mm_processor"] = MagicMock()
tracker._items_by_modality["vision_chunk"] = [(lazy_item, None)]
tracker._modality_order["vision_chunk"] = ["video"]

with pytest.raises(VLLMUnprocessableEntityError, match="corrupt media"):
tracker.resolve_items()
assert "Failed to split video chunks" not in caplog.text


def test_resolve_items_vision_chunk_decode_error_numbers_within_modality():
"""The index counts items of the failing item's own modality rather than
chunk positions, so it matches what the mm processor reports for the same
item. Here the corrupt video is the third chunk but the second video."""

def _corrupt():
raise ValueError("corrupt media")

tracker = MultiModalItemTracker(
MagicMock(
is_multimodal_model=True,
hf_config=MagicMock(use_unified_vision_chunk=True),
)
)
tracker.__dict__["mm_processor"] = MagicMock()
tracker._items_by_modality["vision_chunk"] = [
(MediaRef(lambda: object(), b"image-bytes"), None),
(MediaRef(lambda: object(), b"video-bytes"), None),
(MediaRef(_corrupt, b"corrupt-video-bytes"), None),
]
tracker._modality_order["vision_chunk"] = ["image", "video", "video"]

with pytest.raises(VLLMUnprocessableEntityError) as exc_info:
tracker.resolve_items()

assert "video media at index 1" in str(exc_info.value)
assert exc_info.value.parameter == "video_url"


def _assistant_tool_call(arguments, name="write"):
return [
{
Expand Down Expand Up @@ -3196,3 +3369,112 @@ def test_validate_chat_template_rejects_invalid_type():
) as exc_info:
validate_chat_template(123) # type: ignore[arg-type]
assert exc_info.value.parameter == "chat_template"


_STUB_VIDEO_URL = "https://example.com/video.mp4"


@pytest.fixture
def downloads(monkeypatch: pytest.MonkeyPatch) -> list[str]:
"""Every URL the connector downloads, in order.

The bytes are never decoded by these tests, so a stub payload is enough to
count how many times one URL is fetched.
"""
fetched: list[str] = []
payload = b"stub-video-container"

def fetch_url_bytes(self, url, url_spec, **kwargs):
fetched.append(url)
return payload

async def fetch_url_bytes_async(self, url, url_spec, **kwargs):
fetched.append(url)
return payload

monkeypatch.setattr(MediaConnector, "_fetch_url_bytes", fetch_url_bytes)
monkeypatch.setattr(MediaConnector, "_fetch_url_bytes_async", fetch_url_bytes_async)
return fetched


def _video_conversation(url: str):
return [
{
"role": "user",
"content": [
{"type": "video_url", "video_url": {"url": url}},
{"type": "text", "text": "What's in this video?"},
],
}
]


@pytest.mark.parametrize("use_audio_in_video", [False, True])
def test_use_audio_in_video_downloads_the_video_once(
qwen25omni_model_config_mm_interleaved,
downloads: list[str],
use_audio_in_video: bool,
):
"""`use_audio_in_video` reads the audio track out of the video payload, so
it must feed two decoders from one download, not fetch the URL twice."""
_, mm_data, _ = parse_chat_messages(
_video_conversation(_STUB_VIDEO_URL),
qwen25omni_model_config_mm_interleaved,
content_format="string",
mm_processor_kwargs={"use_audio_in_video": use_audio_in_video},
)

assert downloads == [_STUB_VIDEO_URL]
expected: MultiModalDataCounts = (
{"video": 1, "audio": 1} if use_audio_in_video else {"video": 1}
)
_assert_mm_data_inputs(mm_data, expected)


@pytest.mark.asyncio
@pytest.mark.parametrize("use_audio_in_video", [False, True])
async def test_use_audio_in_video_downloads_the_video_once_async(
qwen25omni_model_config_mm_interleaved,
downloads: list[str],
use_audio_in_video: bool,
):
"""Same for the async parser: both tracker entries await one download."""
_, mm_data, _ = await parse_chat_messages_async(
_video_conversation(_STUB_VIDEO_URL),
qwen25omni_model_config_mm_interleaved,
content_format="string",
mm_processor_kwargs={"use_audio_in_video": use_audio_in_video},
)

assert downloads == [_STUB_VIDEO_URL]
expected: MultiModalDataCounts = (
{"video": 1, "audio": 1} if use_audio_in_video else {"video": 1}
)
_assert_mm_data_inputs(mm_data, expected)


@pytest.mark.parametrize("predecoded", [False, True])
def test_resolve_items_passes_materialized_video_to_chunk_splitter(predecoded):
"""The synchronous tracker passes decoded frames to the video splitter."""
ref: MediaRef[tuple[str, dict]] = MediaRef(lambda: ("decoded-frames", {}), b"video")
if predecoded:
ref.decode()
processor = MagicMock()
processor.split_video_chunks.return_value = [
{"video_chunk": "split-frames", "prompt": "video-prompt"}
]
tracker = MultiModalItemTracker(
MagicMock(hf_config=MagicMock(use_unified_vision_chunk=True))
)
tracker.__dict__["mm_processor"] = processor
tracker._items_by_modality["vision_chunk"] = [(ref, "video-id")]
tracker._modality_order["vision_chunk"] = ["video"]

mm_data, _ = tracker.resolve_items()

processor.split_video_chunks.assert_called_once_with("decoded-frames")
assert mm_data is not None
chunks = mm_data["vision_chunk"]
assert isinstance(chunks, list)
assert chunks[0] is not None
assert chunks[0]["video_chunk"] == "split-frames"
Loading
Loading