Skip to content

[Multimodal] Encoder CUDA graphs: eager fallback for packed TORCH_SDPA - #1417

Closed
roberteg16 wants to merge 2 commits into
rogarcia.upstream-main-3c39ddafrom
rogarcia.mm-encoder-graph-infra
Closed

roberteg16 wants to merge 2 commits into
rogarcia.upstream-main-3c39ddafrom
rogarcia.mm-encoder-graph-infra

Conversation

@roberteg16

@roberteg16 roberteg16 commented Oct 30, 2026 •

Copy link
Copy Markdown

Encoder CUDA graphs fall back to eager when the ViT attention backend cannot be captured, instead of failing at startup, and packed TORCH_SDPA gets cheaper. Draft for review before proposing it upstream: the base branch is upstream main at 3c39dda (rogarcia.upstream-main-3c39dda), so the diff is exactly the upstream change.

Why

TORCH_SDPA splits packed sequences (cu_seqlens) on the host with .tolist(), which cannot run under CUDA graph capture. Models that pack their images and support encoder graphs (Qwen2.5-VL, Qwen3-VL, GLM-4.1V, Kimi, ...) therefore fail at startup with TORCH_SDPA and cudagraph_mm_encoder=True. TORCH_SDPA is the default ViT backend on ROCm RDNA without the flash-attn Triton backend, and on CDNA without AITER or flash-attn.

Eager packed TORCH_SDPA also syncs the device in every layer (the .tolist() of device cu_seqlens) and makes one SDPA call per image or video frame.

Changes

Two commits: the first only moves the cu_seqlens buffer padding (no functional change); the second carries the rest.

  • EncoderCudaGraphManager: catches CudaGraphCaptureUnsupportedError during capture, warns and runs that encoder eager (supports_modality turns false, no graphs counted). It also skips capture when a model reports no graph modalities (e.g. Qwen with video pruning used to capture graphs it never replays).
  • Memory profiling: when capture falls back to eager, the warmup forward run before capture leaves its activations in the caching allocator. The V1 runner's encoder graph estimate and the E-only runner's graph memory are 0 then, instead of being taken from the KV cache (Qwen2.5-VL-3B with max_num_batched_tokens=8192 on the V1 runner: 1250 MiB, 2223 fewer KV blocks, without this). The fallback warning caches only the message: the exception's traceback would keep those activations alive (1.3 GiB more on the V2 runner).
  • EncoderItemSpec.eager_only: lets a model send items its graphs cannot hold (e.g. longer than the captured max_seqlen) to the eager path; the manager packs the other items as before.
  • TORCH_SDPA wrapper:
    • Raises CudaGraphCaptureUnsupportedError for packed sequences under capture (CUDA/ROCm only; unpacked inputs without cu_seqlens still capture).
    • One SDPA call per sequence length (same-size images, a video's frames, adjacent or not), in chunks that bound the attention scores to 1 GiB in case SDPA falls back to the math kernel.
    • Returns an empty tensor when every sequence is empty.
  • MMEncoderAttention: maybe_recompute_cu_seqlens keeps cu_seqlens on the host for TORCH_SDPA, which reads it there (no sync per layer). cu_seqlens_buffer_padding(backend) (NFC commit) holds the FlashInfer-aware CUDA graph buffer padding that lived in Qwen2.5-VL, next to the layout it pads, so models do not branch on backend names; Qwen2.5-VL uses it.

Tests

On gfx1151 (Strix Halo, default ViT backend TORCH_SDPA):

  • pytest tests/kernels/attention/test_mha_attn.py tests/v1/cudagraph/test_encoder_cudagraph.py: 101 passed, 23 skipped (test_mha_attn_platform[hip] deselected: it fails on this box without flash_attn, also on the base branch). New: test_torch_sdpa_varlen_rejects_cuda_graph_capture, test_torch_sdpa_varlen_groups_equal_lengths (grouped and chunked calls match one call per sequence, empty input), test_eager_only_item_skips_graphs, test_unsupported_capture_falls_back_to_eager.
  • pytest tests/models/multimodal/generation/test_vit_cudagraph.py -k "gemma4 or qwen2_5_vl or qwen3_vl" and test_common.py -k qwen2_5_vl: 6 passed, 1 skipped and 5 passed with TORCH_SDPA (encoder graphs requested, capture falls back to eager). On the base branch test_vit_cudagraph.py -k qwen2_5_vl fails at startup ("Cannot copy between CPU and CUDA tensors during CUDA graph capture", from the host read of cu_seqlens).

FLASH_ATTN and FLASHINFER (CUDA only) were not run; their paths only change in where the buffer padding helper lives.

The Gemma 4 native vision tower (#1415) builds on this. No open PR covers the TORCH_SDPA capture failure or its per-layer sync. AI-assisted (Claude Code); the submitter reviews every line.

🤖 Generated with Claude Code

@roberteg16
roberteg16 force-pushed the rogarcia.mm-encoder-graph-infra branch 3 times, most recently from 84ee5f6 to 291a2fd Compare November 1, 2026 10:02
roberteg16 and others added 2 commits November 2, 2026 04:55
…oderAttention

Qwen2.5-VL's CUDA graph padding for cu_seqlens (cumulative, or FlashInfer's
two-section layout) depends on the layout maybe_recompute_cu_seqlens
produces, so it moves next to it as
MMEncoderAttention.cu_seqlens_buffer_padding(backend), and models no longer
branch on the attention backend to pick it. No functional change.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Robert Esclapez Garcia <robert.garcia@amd.com>
TORCH_SDPA splits packed sequences (cu_seqlens) on the host, so it
cannot run under CUDA graph capture: Qwen2.5-VL or Qwen3-VL with the
TORCH_SDPA ViT backend (the default on RDNA without the flash-attn
Triton backend) and cudagraph_mm_encoder failed at startup. The wrapper
now raises CudaGraphCaptureUnsupportedError there, and
EncoderCudaGraphManager catches it and runs that encoder eager, with a
warning. The manager also skips capture when no modality uses graphs,
and a new EncoderItemSpec.eager_only lets a model send items its graphs
cannot hold to the eager path.

When capture falls back, CUDA graph memory profiling (V1 and E-only
runners) counts no encoder graph memory, so the warmup forward's
activations are not taken from the KV cache.

TORCH_SDPA with packed sequences: maybe_recompute_cu_seqlens keeps
cu_seqlens on the host (no device sync per layer), and the wrapper makes
one SDPA call per sequence length (same-size images, a video's frames)
in chunks that bound the attention scores to 1 GiB.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Robert Esclapez Garcia <robert.garcia@amd.com>
@roberteg16
roberteg16 force-pushed the rogarcia.mm-encoder-graph-infra branch from 291a2fd to 4e21958 Compare November 2, 2026 12:58
@roberteg16

Copy link
Copy Markdown
Author

Folded into #1415 as its first two commits. With startup errors instead of an eager fallback for packed TORCH_SDPA under encoder CUDA graphs, what is left here (an NFC move, eager_only, opting out of encoder graphs and a clearer SDPA error) exists for the Gemma 4 tower, so it is reviewed with it.

@roberteg16 roberteg16 closed this Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant