Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions docs/design/cuda_graphs_multimodal.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ For two-tower vision encoders (e.g., DeepSeek-OCR's SAM + CLIP with dynamic tili
Encoder CUDA Graph has currently been tested with `--mm-encoder-attn-backend=FLASH_ATTN` and `--mm-encoder-attn-backend=FLASHINFER` on Blackwell GPUs.
For Qwen2-VL and Qwen2.5-VL only FA2 and FA3 has been tested.
Encoder CUDA Graph has also been tested with AMD MI350X (gfx950) used `--mm-encoder-attn-backend=FLASH_ATTN` (the ROCm default).
Models that pack images with `cu_seqlens` cannot be captured with `--mm-encoder-attn-backend=TORCH_SDPA`, which splits the sequences on the host: startup stops with an error naming another backend (e.g. `TRITON_ATTN` on RDNA GPUs, where `TORCH_SDPA` is the default without the flash-attn Triton backend).

## Motivation

Expand Down
15 changes: 15 additions & 0 deletions tests/kernels/attention/test_mha_attn.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,18 @@ def test_mha_attn_fp8_rejects_wrong_backend(default_vllm_config):
MMEncoderAttention(4, 64)
finally:
vllm_config.model_config = old_model_config


def test_torch_sdpa_varlen_rejects_cuda_graph_capture():
"""Splitting packed sequences reads cu_seqlens on the host, which a CUDA
graph cannot capture: fail with a clear error instead of a capture one."""
from vllm.v1.attention.ops.vit_attn_wrappers import torch_sdpa_wrapper

q = k = v = torch.randn(1, 8, 2, 16)
cu_seqlens = torch.tensor([0, 3, 8], dtype=torch.int32)
with (
patch.object(current_platform, "is_cuda_alike", return_value=True),
patch("torch.cuda.is_current_stream_capturing", return_value=True),
pytest.raises(RuntimeError, match="cannot be captured in a CUDA graph"),
):
torch_sdpa_wrapper(q, k, v, cu_seqlens=cu_seqlens)
6 changes: 4 additions & 2 deletions tests/models/multimodal/pooling/test_embedding_gemma2.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ def test_encoder_contract():
"_process_video_input",
"_process_audio_input",
"embed_multimodal",
"_encoder_chunk",
"_pack_vision",
"_embed_vision",
]
# EmbeddingGemma2 borrows these without Gemma4's data-parallel dispatch.
gemma4_source = {
Expand Down Expand Up @@ -366,7 +367,8 @@ def test_instance_attribute_access(vllm_runner):
"_process_video_input",
"_process_audio_input",
"embed_multimodal",
"_encoder_chunk",
"_pack_vision",
"_embed_vision",
]
attrs_accessed = set()
for name in borrowed_methods:
Expand Down
265 changes: 152 additions & 113 deletions tests/models/multimodal/processing/test_gemma4.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

from collections.abc import Mapping
from functools import partial
from types import SimpleNamespace

import numpy as np
Expand All @@ -15,18 +16,19 @@
build_mm_input_norm,
)
from vllm.model_executor.model_loader import get_model_cls
from vllm.model_executor.models import gemma4_mm
from vllm.model_executor.models.gemma4_mm import (
Gemma4ForConditionalGeneration,
Gemma4ImagePixelInputs,
Gemma4ProcessingInfo,
)
from vllm.model_executor.models.gemma4_vision import Gemma4VisionModel
from vllm.model_executor.models.interfaces import supports_multimodal
from vllm.multimodal import MULTIMODAL_REGISTRY
from vllm.multimodal.cache import MultiModalProcessorOnlyCache
from vllm.multimodal.inputs import MultiModalFieldConfig
from vllm.platforms import current_platform
from vllm.utils.mem_constants import GiB_bytes
from vllm.v1.worker.encoder_cudagraph_defs import ENCODER_CUDAGRAPH_AXIS_KEYS_KWARG
from vllm.v1.attention.backends.registry import AttentionBackendEnum

from ....conftest import ImageTestAssets
from ...utils import build_model_context
Expand Down Expand Up @@ -359,33 +361,36 @@ def pixels(mm_device_do_normalize: bool) -> list[torch.Tensor]:
torch.testing.assert_close(output, normalized_values.to(device))


class _IdentityTower(torch.nn.Module):
dtype = torch.float32

def forward(self, pixel_values, encoder_metadata):
return pixel_values


@pytest.mark.usefixtures("default_vllm_config")
def test_embed_patches_normalizes_channel_last_patches():
def test_embed_vision_normalizes_channel_last_patches():
"""Gemma 4 patches are (patch, patch, channel); per-channel parameters must
reach the right pixels even when they differ across channels."""
device = current_platform.device_type
mean, std = [0.1, 0.4, 0.7], [0.5, 0.25, 0.125]
model = object.__new__(Gemma4ForConditionalGeneration) # type: ignore[type-abstract]
torch.nn.Module.__init__(model)
model.vision_input_norm = FusedMMInputNorm(
image_mean=mean, image_std=std, rescale_factor=1 / 255
)
model.vision_tower = SimpleNamespace(
config=SimpleNamespace(num_channels=3),
patch_embedder=SimpleNamespace(
input_proj=lambda pixel_values: pixel_values,
_position_embeddings=lambda *_: 0.0,
),
)
model.model_dtype = torch.float32
).to(device)
model.config = SimpleNamespace(vision_config=SimpleNamespace(num_channels=3))
model.vision_tower = _IdentityTower()
model.embed_vision = torch.nn.Identity()

pixel_values = torch.randint(0, 256, (2, 6, 16 * 16 * 3), dtype=torch.uint8)
output = model._embed_patches(pixel_values, None, None)
pixel_values = torch.randint(
0, 256, (12, 16 * 16 * 3), dtype=torch.uint8, device=device
)
output = model._embed_vision(pixel_values, {})

# `_embed_patches` also applies the embedder's [0, 1] -> [-1, 1] rescale.
expected = (
pixel_values.view(2, 6, -1, 3) / 255 - torch.tensor(mean)
) / torch.tensor(std)
expected = 2 * (expected - 0.5)
pixel_values.view(12, -1, 3) / 255 - torch.tensor(mean, device=device)
) / torch.tensor(std, device=device)
torch.testing.assert_close(output, expected.view(pixel_values.shape))


Expand Down Expand Up @@ -421,125 +426,159 @@ def test_limit_mm_per_prompt(
)


# Regression guard for PR #43169 follow-up: the batched Gemma4 vision encoder
# admitted ``chunk ~= 53`` on a 22 GiB L4 with a 26B AWQ model loaded,
# allocating 2.43 GiB int64 inside
# ``F.one_hot(num_classes=position_embedding_size)`` and OOMing because only
# 2.41 GiB was actually free. The fix sizes ``chunk`` from currently-free GPU
# memory and counts the ``F.one_hot`` transient as the dominant cost.

_encoder_chunk = Gemma4ForConditionalGeneration._encoder_chunk

# Gemma4 vision config default (HF: configuration_gemma4.py).
_POSITION_EMBEDDING_SIZE = 10240
# Video frame: max_soft_tokens=70, pooling_kernel_size=2 -> 70 * 4 patches.
_VIDEO_PATCHES_PER_FRAME = 280


def test_encoder_chunk_tight_budget_fits_in_free():
free = 3 * GiB_bytes # L4 22 GiB after 26B AWQ load.
total = 22 * GiB_bytes
chunk = _encoder_chunk(
_VIDEO_PATCHES_PER_FRAME, free, total, _POSITION_EMBEDDING_SIZE
def _gemma4_for_encoder_cudagraph(
attn_backend: AttentionBackendEnum = AttentionBackendEnum.TRITON_ATTN,
) -> Gemma4ForConditionalGeneration:
model = object.__new__(Gemma4ForConditionalGeneration) # type: ignore[type-abstract]
vision_config = SimpleNamespace(
patch_size=16,
pooling_kernel_size=3,
default_output_length=280,
position_embedding_size=10240,
num_attention_heads=12,
head_dim=64,
)
one_hot_bytes = chunk * _VIDEO_PATCHES_PER_FRAME * 2 * _POSITION_EMBEDDING_SIZE * 8
assert one_hot_bytes <= free // 2


def test_encoder_chunk_roomy_gpu_keeps_batching():
chunk = _encoder_chunk(
_VIDEO_PATCHES_PER_FRAME,
60 * GiB_bytes,
80 * GiB_bytes,
_POSITION_EMBEDDING_SIZE,
vision_tower = SimpleNamespace(
config=vision_config,
attn_backend=attn_backend,
fp8_padded_hidden_size=None,
dtype=torch.float32,
device=torch.device("cpu"),
)
assert chunk > 8


def test_encoder_chunk_zero_patches_is_safe():
assert (
_encoder_chunk(0, 60 * GiB_bytes, 80 * GiB_bytes, _POSITION_EMBEDDING_SIZE) == 1
vision_tower.prepare_encoder_metadata = partial(
Gemma4VisionModel.prepare_encoder_metadata, vision_tower
)


def test_encoder_chunk_zero_position_embedding_size_is_safe():
# Degenerate config: must not raise ZeroDivisionError.
assert (
_encoder_chunk(_VIDEO_PATCHES_PER_FRAME, 60 * GiB_bytes, 80 * GiB_bytes, 0) == 1
model.vision_tower = vision_tower
model.vision_input_norm = SimpleNamespace(input_dtype=None)
model.config = SimpleNamespace(
vision_config=vision_config, text_config=SimpleNamespace(hidden_size=8)
)
return model


def test_encoder_chunk_no_free_memory_falls_back_to_one():
def test_encode_vision_without_images_or_frames():
# Empty inputs on the local paths (DiffusionGemma, EmbeddingGemma2) and a
# video with no frames encode nothing instead of calling the tower.
model = _gemma4_for_encoder_cudagraph()
model.model_dtype = torch.bfloat16
model._tower_lora = False
assert (
_encoder_chunk(
_VIDEO_PATCHES_PER_FRAME, 0, 22 * GiB_bytes, _POSITION_EMBEDDING_SIZE
)
== 1
model._process_image_input_local({"pixel_values": [], "pixel_position_ids": []})
== []
)
videos = model._process_video_input_local(
{
"pixel_values_videos": torch.zeros(0, 2520, 768),
"pixel_position_ids_videos": torch.zeros(0, 2520, 2, dtype=torch.long),
"video_frame_counts": [0],
}
)
assert [video.shape for video in videos] == [(0, 8)]


def _gemma4_for_encoder_cudagraph() -> Gemma4ForConditionalGeneration:
model = object.__new__(Gemma4ForConditionalGeneration) # type: ignore[type-abstract]
vision_config = SimpleNamespace(
patch_size=16, pooling_kernel_size=3, default_output_length=280
)
model.vision_tower = SimpleNamespace(config=vision_config)
model.vision_input_norm = SimpleNamespace(input_dtype=None)
model.config = SimpleNamespace(vision_config=vision_config)
return model
def _padded_image(
columns: int, rows: int, max_patches: int = 2520
) -> tuple[torch.Tensor, torch.Tensor]:
"""Patches laid out like the processor's: row-major, padded at the end."""
grid = torch.meshgrid(torch.arange(columns), torch.arange(rows), indexing="xy")
positions = torch.full((max_patches, 2), -1, dtype=torch.long)
positions[: columns * rows] = torch.stack(grid, -1).reshape(-1, 2)
return torch.zeros(max_patches, 768), positions


@pytest.mark.parametrize(
("token_budget", "row_tokens"), [(4096, 70), (4096, 280), (70, 280)]
("tower_lora", "expected_calls"), [(False, [200, 106]), (True, [306])]
)
def test_encoder_cudagraph_capture_rows_fit_token_budget(
token_budget: int, row_tokens: int
def test_eager_vision_calls_are_bounded(
monkeypatch, tower_lora: bool, expected_calls: list[int]
):
# Regression for #61045: rows were max_frames_per_batch (58 * 32) at
# 280 tokens each, so the 4096-token buffer alone was 6.69 GiB.
"""Packed eager calls are capped in soft tokens, bounding the tower's peak
memory, except with tower LoRA: its mapping covers the whole batch."""
monkeypatch.setattr(gemma4_mm, "_MAX_SOFT_TOKENS_PER_CALL", 250)
model = _gemma4_for_encoder_cudagraph()
model._tower_lora = tower_lora
calls: list[int] = []

def embed_vision(pixel_values, metadata):
calls.append(pixel_values.shape[0] // 9)
return torch.full((calls[-1], 8), float(len(calls)))

model._embed_vision = embed_vision # type: ignore[method-assign]
images = [_padded_image(30, 30)] * 3 + [_padded_image(9, 6)]
out = model._process_image_input_local(
{
"pixel_values": [pixel_values for pixel_values, _ in images],
"pixel_position_ids": [positions for _, positions in images],
} # type: ignore[arg-type]
)

assert calls == expected_calls
assert [o.shape[0] for o in out] == [100, 100, 100, 6]
# Outputs keep the input order across calls.
expected_call_index = [1, 1, 2, 2] if not tower_lora else [1, 1, 1, 1]
assert [int(o[0, 0]) for o in out] == expected_call_index


@pytest.mark.parametrize("token_budget", [70, 4480])
def test_encoder_cudagraph_capture_buffers_fit_token_budget(token_budget: int):
# Regression for #61045: buffers sized by max_frames_per_batch rows
# instead of the token budget took 6.69 GiB at a 4096-token budget.
model = _gemma4_for_encoder_cudagraph()
inputs = model.prepare_encoder_cudagraph_capture_inputs(
values = model.prepare_encoder_cudagraph_capture_inputs(
token_budget=token_budget,
max_batch_size=58,
max_frames_per_batch=58 * 32,
device="meta",
max_batch_size=1,
max_frames_per_batch=32,
device=torch.device("cpu"),
dtype=torch.bfloat16,
axis_keys=(row_tokens,),
)
).values

num_rows, row_patches, _ = inputs.values["pixel_values"].shape
assert row_patches == row_tokens * 9
assert num_rows == max(1, token_budget // row_tokens)
assert values["pixel_values"].shape == (token_budget * 9, 768)
assert values["pixel_position_ids"].shape == (token_budget * 9, 2)
assert values["pool_gather_idx"].shape == (token_budget * 9,)
# A slot per image or frame that fits, each of at least 35 tokens: the
# manager does not bound frames, e.g. a 64-frame video in 4480 tokens.
assert values["cu_seqlens"].shape == (max(1, token_budget // 35) + 1,)
# The largest image of the configured max_soft_tokens (280).
assert values["max_seqlen"].item() == min(token_budget, 280) * 9


def test_encoder_cudagraph_mixed_width_images_pack_by_padded_width():
def test_encoder_cudagraph_replay_packs_images_without_padding():
model = _gemma4_for_encoder_cudagraph()
images = [
_padded_image(9, 6),
_padded_image(60, 42),
_padded_image(3, 3),
_padded_image(90, 42, max_patches=3780),
]
mm_kwargs = {
"pixel_values": [torch.zeros(2520, 768), torch.zeros(630, 768)],
"pixel_position_ids": [
torch.zeros(2520, 2, dtype=torch.long),
torch.zeros(630, 2, dtype=torch.long),
],
"pixel_values": [pixel_values for pixel_values, _ in images],
"pixel_position_ids": [positions for _, positions in images],
}

specs = model.get_encoder_cudagraph_item_specs(mm_kwargs)
assert [spec.output_tokens for spec in specs] == [280, 70]
assert [spec.get_path_output_tokens("default") for spec in specs] == [280, 280]

selected = model.select_encoder_cudagraph_items(mm_kwargs, [1])
assert selected[ENCODER_CUDAGRAPH_AXIS_KEYS_KWARG] == (70,)
selected = model.select_encoder_cudagraph_items(mm_kwargs, [0, 1])
assert selected[ENCODER_CUDAGRAPH_AXIS_KEYS_KWARG] == (280,)


@pytest.mark.parametrize(
"mm_processor_kwargs",
[{"max_soft_tokens": 560}, {"images_kwargs": {"max_soft_tokens": 560}}],
)
def test_encoder_cudagraph_config_respects_max_soft_tokens(mm_processor_kwargs):
assert [spec.output_tokens for spec in specs] == [6, 280, 1, 420]
# Below the 35 tokens the graphs' sequence slots assume, or above their
# max_seqlen (configured 280 tokens): eager.
assert [spec.eager_only for spec in specs] == [True, False, True, True]

selected = model.select_encoder_cudagraph_items(mm_kwargs, [0, 2])
values = model.prepare_encoder_cudagraph_replay_buffers(
selected, max_batch_size=4, max_frames_per_batch=4
).values
pixel_values = values["pixel_values"]
cu_seqlens = values["cu_seqlens"]
pool_gather_idx = values["pool_gather_idx"]
assert pixel_values is not None and pixel_values.shape == (54 + 9, 768)
assert cu_seqlens is not None and cu_seqlens.tolist() == [0, 54, 63]
# Each 3x3 block of the 9x6 image, then the 3x3 image.
assert pool_gather_idx is not None
assert pool_gather_idx[:9].tolist() == [0, 1, 2, 9, 10, 11, 18, 19, 20]
assert pool_gather_idx[9:18].tolist() == [3, 4, 5, 12, 13, 14, 21, 22, 23]
assert pool_gather_idx[54:].tolist() == list(range(54, 63))


def test_encoder_metadata_rejects_grids_not_divisible_by_pooling():
model = _gemma4_for_encoder_cudagraph()
model.config.text_config = SimpleNamespace(hidden_size=8)
model.multimodal_config = SimpleNamespace(mm_processor_kwargs=mm_processor_kwargs)

config = model.get_encoder_cudagraph_config()
assert config.capture_axes == ((70, 140, 280, 560),)
_, positions = _padded_image(4, 3)
with pytest.raises(ValueError, match="multiples of 3"):
model.vision_tower.prepare_encoder_metadata([positions[:12].numpy()])
Loading