Skip to content

[MPS] CrossAttention: (partial) fix for NaN attention_scores on PyTorch nightly #2643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/diffusers/models/attention_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,20 @@ def get_attention_scores(self, query, key, attention_mask=None):
key = key.float()

if attention_mask is None:
baddbmm_input = torch.empty(
query.shape[0], query.shape[1], key.shape[1], dtype=query.dtype, device=query.device
batch_x_heads, query_tokens, _ = query.shape
_, key_tokens, _ = key.shape
# expanding dims isn't strictly necessary (baddbmm supports broadcasting bias),
# but documents the expected shape without allocating any additional memory
attention_bias = torch.zeros(1, 1, 1, dtype=query.dtype, device=query.device).expand(
batch_x_heads, query_tokens, key_tokens
)
beta = 0
else:
baddbmm_input = attention_mask
attention_bias = attention_mask
beta = 1

attention_scores = torch.baddbmm(
baddbmm_input,
attention_bias,
query,
key.transpose(-1, -2),
beta=beta,
Expand Down