[varlen] Fix non-hub backends mispacking keys for non-contiguous masks#14152
Open
Functionhx wants to merge 2 commits into
Open
[varlen] Fix non-hub backends mispacking keys for non-contiguous masks#14152Functionhx wants to merge 2 commits into
Functionhx wants to merge 2 commits into
Conversation
…ous masks The three non-hub varlen backends (_flash_varlen_attention, _flash_varlen_attention_3, _sage_varlen_attention) used a per-batch slice pattern (key[b, :valid_len]) that fails when the attention mask is non-contiguous. The hub backends (FLASH_VARLEN_HUB, _FLASH_3_VARLEN_HUB) already received this fix via the nonzero gather pattern. Root cause: The per-batch slice assumes a contiguous mask layout, but non-contiguous masks have different cu_seqlens structures. The slice picks up wrong positions from each batch element. Fix: When a mask is present, use attn_mask.flatten().nonzero().flatten() to gather valid positions from reshaped key/value tensors, which correctly handles any mask contiguity. When no mask, use simple flatten(), which is always contiguous. This matches the exact pattern used in the hub backends. Closes huggingface#14139. Signed-off-by: Yuchen Fan <functionhx@gmail.com>
…ckslash bug
Same root cause as the DiffusionPipeline fix: os.path.join(k, "*") produces
backslash paths on Windows (e.g. text_encoder\*), which fnmatch does not
normalize, causing pattern mismatch and silently skipped subfolder downloads.
Fix: use f"{k}/*" for forward-slash paths, consistent with pipeline_utils.py.
Fixes huggingface#14142
Signed-off-by: Yuchen Fan <functionhx@gmail.com>
0885394 to
5d3c551
Compare
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?
Fixes #14139.
Three non-hub varlen backends used a per-batch slice pattern that fails for non-contiguous attention masks. Applied the same nonzero-gather fix already used in the hub backends (#14115).
Before submitting
AI agent disclosure
Who can review?
Anyone in the community.