Honor native attention backend selection in direct SDPA calls - #3217
Merged
Merged
Conversation
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.
Selecting
native-mathpreviously changed only Diffusers' dispatch registry. SDXL'sAttnProcessor2_0calls PyTorch SDPA directly, so it could still select flash attention. Scope PyTorch's SDPA selection alongside the existing Diffusers context for explicit native backends, restoring both when the backend changes.The real CPU profiler regression previously observed
aten::_scaled_dot_product_flash_attention_for_cpuand its backward despite selectingnative-math. It now observesaten::_scaled_dot_product_attention_mathwith finite gradients. Additional tests cover backend switching and train/evaluation restoration for all explicit native SDPA choices.Validation:
.venv/bin/python -m unittest -v -f tests.test_attention_backend— 34 passed. Both the processor dispatch and backend-switch regressions failed before the fix and passed afterward.Supplemental investigation: a tiny SDXL-shaped UNet with BF16 LoRA, math attention, gradient checkpointing, and SimpleTuner's AdamWBF16 completed three steps each on CPU and MPS. Convolution hooks observed recomputation during the first backward and every subsequent backward. This checks the control flow at reduced scale; it is not a full SDXL or RDNA2 hardware reproduction.
Related to #3195. This corrects the ineffective native-math diagnostic; resolution of the reported RDNA2 SIGSEGV requires testing on affected hardware.