Skip to content

SelfBlock: make v contiguous so SDPA can use flash attention - #17

Open
TheGhoul21 wants to merge 1 commit into
davnords:mainfrom
TheGhoul21:fix/selfblock-sdpa-flash
Open

TheGhoul21 wants to merge 1 commit into
davnords:mainfrom
TheGhoul21:fix/selfblock-sdpa-flash

Conversation

@TheGhoul21

Copy link
Copy Markdown

qkv[..., 2] in SelfBlock.forward is a view with last-dim stride 3. F.scaled_dot_product_attention requires 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/k are unaffected because apply_cached_rotary_emb returns fresh tensors; CrossBlock already 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:

pairs/s
main 16.4
this PR 79.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.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant