Repository navigation
SelfBlock: make v contiguous so SDPA can use flash attention - #17
Open
TheGhoul21 wants to merge 1 commit into
Open
TheGhoul21 wants to merge 1 commit into
TheGhoul21 wants to merge 1 commit into
Conversation
qkv[..., 2] is a view with last-dim stride 3, which disables the flash / mem-efficient SDPA kernels: self-attention silently fell back to the math kernel, materializing the full BxHxNxN attention matrix. LoMa-R, 4096 kpts, RTX 4090, torch 2.6: 16.4 -> 79.1 pairs/s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YS8exFbatQSwkjsunDWWyE
This branch has not been deployed
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.
qkv[..., 2]inSelfBlock.forwardis a view with last-dim stride 3.F.scaled_dot_product_attentionrequires a unit last-dim stride for the flash / mem-efficient kernels, so self-attention silently falls back to_scaled_dot_product_attention_math, which materializes the full B×H×N×N attention matrix.q/kare unaffected becauseapply_cached_rotary_embreturns fresh tensors;CrossBlockalready hits flash. LightGlue avoids this by calling.contiguous()before attention.Fix:
v = v.contiguous()(2 lines).Benchmark — LoMa-R, 4096 keypoints, RTX 4090, torch 2.6.0+cu124, bf16 autocast, 394 real image pairs, batch 1:
Profiler: each self-attention call goes from ~2.9 ms (math kernel) to ~0.11 ms (flash).
Outputs: the mean number of matches is unchanged (1838 vs 1837). The match sets overlap with a mean Jaccard of 0.955; the differences are borderline matches near
filter_threshold, from flash-bf16 vs math numerics.