Fix flash/sage varlen prep under torch.compile with dynamic shapes - #14568
Open
ShivamShrirao wants to merge 1 commit into
Open
Fix flash/sage varlen prep under torch.compile with dynamic shapes#14568ShivamShrirao wants to merge 1 commit into
ShivamShrirao wants to merge 1 commit into
Conversation
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.
What does this PR do?
Makes the flash-attn / sage varlen attention backends usable under
torch.compilewith dynamic shapes. Two changes in the_prepare_for_flash_attn_or_sage_varlen_*helpers, with no numerical change:cu_seqlensis now built withtorch.arangeinstead ofcumsum(full(...)). Inductor rewrites the cumsum-of-a-constant pattern internally, and that rewrite fails when the fill value is a symbolic sequence length. The lengths are uniform here (every sequence in the batch has the same length), sotorch.arange(0, (batch_size + 1) * seq_len, seq_len)produces identical offsets directly — and is cheaper.Minimal repro (fails on torch 2.6.0 and 2.12.1):
max_seqlen_q/max_seqlen_kare returned as the already-known Python int instead ofseqlens.max().item(). The.item()forces a GPU→CPU sync and a graph break; the max of uniform lengths is justseq_lenitself, which the caller already has as an int. Same type as before (.item()also returned an int), so all call sites are unaffected.In the masked helper only the query side changes: key lengths are data-dependent (
attn_mask.sum(dim=1)), so their cumsum does not hit the inductor rewrite and their.item()is inherent to varlen with padding.Found while enabling flash-attn varlen +
torch.compilefor the Bria Fibo pipelines, but the helpers are shared by every model using these backends.Verification
flash_attn_varlen_func(flash-attn 2.8.3, H200) compiles withtorch.compile(dynamic=True, fullgraph=True)across changing batch/sequence shapes and matches eager output exactly (max diff 0.0).Before submitting
self-reviewskill on the diff?Self-review notes
Two-function diff reviewed against
.ai/review-rules.md. Verdict: READY. No API change: same return tuple structure, same values, and the max-seqlen entries keep the type.item()produced (Python int). Checked all 10 call sites inattention_dispatch.py, including the hub-kernel forward/backward ops added recently — the masked-path callers already discard the helper'smax_seqlen_qand useseq_len_qlocally, consistent with this change; unmasked-path callers consume the returned ints unchanged (ctxscalar attributes, neversave_for_backward). Equivalence verified numerically against the previous cumsum construction, masked and unmasked. Left for the reviewer: no CPU unit test covers these helpers directly; the behavior is exercised through the GPU flash-attn backend tests.Who can review?
@sayakpaul @DN6