Fix NaN attention scores on MPS from uninitialized baddbmm buffer - #14459
Fix NaN attention scores on MPS from uninitialized baddbmm buffer#14459RudraMantri123 wants to merge 1 commit into
Conversation
On MPS, torch.baddbmm does not honor the documented beta=0 semantics: NaN/Inf present in the input buffer propagate to the output. Both copies of get_attention_scores (Attention and AttentionModuleMixin) pass torch.empty() as that buffer, so recycled allocator pages containing NaN poison the attention scores, producing all-black images with SlicedAttnProcessor (e.g. SDXL + enable_model_cpu_offload + enable_attention_slicing). Use a buffer-free scaled bmm on MPS when there is no attention mask. This avoids relying on the beta=0 contract, skips the scores-sized buffer allocation entirely (lower peak memory on the memory-constrained devices the sliced path targets), and benchmarks ~35% faster than the baddbmm+empty path on Apple Silicon. Other backends are unchanged. Fixes huggingface#14438
061b461 to
92bd5b5
Compare
|
Upstream status update — correcting my earlier note, and it changes the framing of this PR. I said above that I'd file the
But the fix is not in any released version yet. The import torch # 2.13.0
inp = torch.full((1, 1, 1), float("nan"), device="mps")
torch.baddbmm(inp, torch.ones(1,1,1,device="mps"), torch.ones(1,1,1,device="mps"), beta=0)
# MPS -> nan CPU -> 1.0 (docs: input ignored when beta=0, nan/inf not propagated)So every Apple Silicon user on torch ≤ 2.13.0 — i.e. everyone on the current stable release — still hits #14438 with sliced attention, and will until 2.14.0 ships and they upgrade. What that means for this PR, three honest options:
Tell me which you prefer and I'll adjust — (2) is a small change on this branch. One process note: CI hasn't run here yet, since first-time-contributor workflows need a maintainer's approval click. Whenever someone has a spare second for that, the test suites (including the new MPS regression test) can go green and make this reviewable at a glance. |
|
Gentle ping @yiyixuxu @dg845 @asomoza — this one is blocked only on workflow approval for a first-time contributor, so CI has never actually run here. A single approval click would get the suites green (including the new MPS regression test, which fails deterministically on On scope, in case it helps prioritise: it closes #14438 and also #11229, which has been open since April 2025 with the same root cause. And I'm still happy to switch to the |
What does this PR do?
Fixes #14438 — SDXL produces all-black images on MPS when
enable_attention_slicing()is used (most visibly together withenable_model_cpu_offload()).Root cause
get_attention_scores(in bothAttentionandAttentionModuleMixin) passestorch.empty(...)totorch.baddbmmwithbeta=0, relying on the documented guarantee that the input is ignored and NaN/Inf in it are not propagated. The MPS backend violates that guarantee, so NaN garbage in recycled allocator pages leaks into the attention scores. Only the sliced attention processors reach this code path (the default processor uses SDPA), which makesenable_attention_slicing()the trigger; offload merely churns the allocator sotorch.emptyrecycles dirty pages more often — plainpipe.to("mps")+enable_attention_slicing("max")reproduces without any offload.Minimal reproduction of the underlying defect (torch 2.13.0, Apple Silicon, no diffusers):
I will file this against PyTorch separately; this PR makes diffusers robust to it.
The fix
On MPS, when there is no attention mask, compute scores with a buffer-free scaled
bmminstead ofbaddbmm:beta=0semantics and the uninitialized buffer,baddbmm+emptypath at SDXL slice dimensions on Apple Silicon,The issue's reproduction script now renders correctly across seeds (verified on M5 Pro 24GB; end-to-end images inspected).
Tests
test_get_attention_scores_no_nan_from_recycled_buffer— poisons the MPS allocator pool and exercises both implementations; fails deterministically on currentmain, passes with the fix.test_get_attention_scores_matches_cpu_reference— guards numerical equivalence with the CPU path, not just NaN-absence.Both are gated to MPS.
Notes for reviewers
AI disclosure: Claude Code assisted with debugging and drafting; all experiments were run and verified by the author on real hardware.
query * self.scale) instead of post-scaling viaalpha— mathematically identical, bounded by the CPU-parity test (fp32 exact). A zero-initialized buffer also fixes the bug but benchmarked +53% slower and keeps the allocation.torch.empty+baddbmm(beta=0)pattern exists inpipelines/kolors/text_encoder.py; left for a follow-up since I cannot end-to-end test Kolors on this hardware.Who can review?
@yiyixuxu @dg845 @asomoza — cc @pupa3066 for co-verification on M1 8GB (the memory-constrained case I can't cover).
🤖 Generated with Claude Code