Fix Hiera global attention to use 4D tensors for efficient SDPA dispatch#2680
Merged
rwightman merged 1 commit intohuggingface:mainfrom Mar 9, 2026
Merged
Conversation
…ntion dispatch The global attention path in MaskUnitAttention.forward() used a 5D tensor reshape with num_windows=1 as a shortcut. This caused PyTorch SDPA to silently fall back from efficient backends (FlashAttention, Memory-Efficient, CuDNN) to the O(N^2) math backend, as all efficient kernels require 4D contiguous tensors. At high resolutions (e.g. 2048x2048 -> 16384 tokens), the math backend materializes the full N*N attention matrix, causing catastrophic VRAM usage and OOM on consumer GPUs. Changes: - Branch forward() into windowed (5D, unchanged) and global (4D) paths - Global path reshapes directly to [B, N, 3, heads, head_dim] -> 4D QKV - Adjust q_stride pooling dim from amax(dim=3) to amax(dim=2) for global - Add .contiguous() on q, k, v to guarantee FlashAttention compatibility - Split output transpose: transpose(1,3) for windowed, transpose(1,2) for global
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
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.
Problem
Hiera's
MaskUnitAttention.forward()takes a shortcut for global attention by settingnum_windows=1and funneling everything through the same 5D reshape as windowed attention. This produces Q/K/V tensors shaped[B, heads, 1, N, head_dim](5D, non-contiguous after permute).PyTorch's
F.scaled_dot_product_attentionsilently rejects these for all efficient backends (FlashAttention, Memory-Efficient, CuDNN) and falls back to the math backend, which materializes the full N x N attention matrix. At high resolutions (e.g. 2048x2048, 16384 tokens), this allocates several GB of VRAM per layer and OOMs on consumer GPUs.Fix
Branch the forward pass so that:
use_mask_unit_attn=True): Unchanged 5D path.use_mask_unit_attn=False): Reshapes directly to 4D[B, heads, N, head_dim], calls.contiguous()on Q/K/V, and adjusts downstream indexing (amaxdim, output transpose) accordingly.This is a minimal, non-breaking change. The mathematical output is identical to the original implementation, but SDPA can now dispatch to efficient O(N) kernels instead of the O(N^2) fallback.
Changes
[B, N, 3, heads, head_dim]and permute to 4Dq_stridepooling fromamax(dim=3)toamax(dim=2)on the global path.contiguous()on Q, K, V for the global pathtranspose(1, 3)for windowed,transpose(1, 2)for global