Repository navigation
[Multimodal] Encoder CUDA graphs: eager fallback for packed TORCH_SDPA - #1417
Closed
roberteg16 wants to merge 2 commits into
Closed
roberteg16 wants to merge 2 commits into
roberteg16 wants to merge 2 commits into
Conversation
2 of 6 tasks
roberteg16
force-pushed
the
rogarcia.mm-encoder-graph-infra
branch
3 times, most recently
from
November 1, 2026 10:02
84ee5f6 to
291a2fd
Compare
…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
force-pushed
the
rogarcia.mm-encoder-graph-infra
branch
from
November 2, 2026 12:58
291a2fd to
4e21958
Compare
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Encoder CUDA graphs fall back to eager when the ViT attention backend cannot be captured, instead of failing at startup, and packed
TORCH_SDPAgets cheaper. Draft for review before proposing it upstream: the base branch is upstreammainat 3c39dda (rogarcia.upstream-main-3c39dda), so the diff is exactly the upstream change.Why
TORCH_SDPAsplits 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 withTORCH_SDPAandcudagraph_mm_encoder=True.TORCH_SDPAis the default ViT backend on ROCm RDNA without the flash-attn Triton backend, and on CDNA without AITER or flash-attn.Eager packed
TORCH_SDPAalso syncs the device in every layer (the.tolist()of devicecu_seqlens) and makes one SDPA call per image or video frame.Changes
Two commits: the first only moves the
cu_seqlensbuffer padding (no functional change); the second carries the rest.EncoderCudaGraphManager: catchesCudaGraphCaptureUnsupportedErrorduring capture, warns and runs that encoder eager (supports_modalityturns 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).max_num_batched_tokens=8192on 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 capturedmax_seqlen) to the eager path; the manager packs the other items as before.TORCH_SDPAwrapper:CudaGraphCaptureUnsupportedErrorfor packed sequences under capture (CUDA/ROCm only; unpacked inputs withoutcu_seqlensstill capture).MMEncoderAttention:maybe_recompute_cu_seqlenskeepscu_seqlenson the host forTORCH_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 withoutflash_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"andtest_common.py -k qwen2_5_vl: 6 passed, 1 skipped and 5 passed withTORCH_SDPA(encoder graphs requested, capture falls back to eager). On the base branchtest_vit_cudagraph.py -k qwen2_5_vlfails at startup ("Cannot copy between CPU and CUDA tensors during CUDA graph capture", from the host read ofcu_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_SDPAcapture failure or its per-layer sync. AI-assisted (Claude Code); the submitter reviews every line.🤖 Generated with Claude Code