Broadcast SDPA attn_mask instead of materializing a per-head copy#268
Open
jannisringwald wants to merge 1 commit into
Open
Broadcast SDPA attn_mask instead of materializing a per-head copy#268jannisringwald wants to merge 1 commit into
jannisringwald wants to merge 1 commit into
Conversation
F.scaled_dot_product_attention broadcasts the attention mask, so the [B, 1, N, N] view is mathematically identical to repeating it per head. The .repeat(1, num_heads, 1, 1) materializes a physical [B, num_heads, N, N] copy before the kernel runs - at DA3's multi-view global-attention shapes that is 8.3 GiB for a 14-view masked call (N = 13,622 tokens, vitb, fp32) and grows quadratically with view count. The broadcast view allocates nothing. In-repo pipelines call the backbone with attn_mask=None and are unaffected; the copy only hits callers that thread a mask through the ViT kwargs. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
jannisringwald
force-pushed
the
fix/broadcast-sdpa-attn-mask
branch
2 times, most recently
from
July 11, 2026 12:01
189551d to
f914baf
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
One-line change in
Attention.forward(src/depth_anything_3/model/dinov2/layers/attention.py, fused path):F.scaled_dot_product_attentionbroadcasts the attention mask across the head dimension (docs), so the[B, 1, N, N]view is mathematically identical to the repeated[B, num_heads, N, N]tensor — the kernel sees the same mask values either way.Why
For any caller that threads
attn_maskthrough the backbone kwargs (DinoV2.forward(**kwargs)→get_intermediate_layers→ the global-attention blocks), the.repeat()materializes a physical per-head copy before the kernel even runs. At DA3's multi-view global-attention token counts that is a very large single allocation, growing quadratically with view count (378×504 inputs,vitb: 973 tokens/view, 12 heads, fp32 mask):.repeat()A 14-view masked call would allocate 8.3 GiB just for the mask copy — more than half the memory of a 16 GB T4. We noticed the line while auditing the SDPA memory behavior of 14-view inference on 16 GB T4s.
Reproducible measurement of the line itself:
Safety
attn_mask=None, so this is a no-op for them — verified A/B (patched vs unpatched) on a real 8-view T4 inference: identical peak memory (7.77 GB), outputs matching to run-to-run kernel noise (median/p95 relative depth error 0.000000).else) branch is untouched — it never materialized a per-head mask.