Skip to content
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

Fix MusicGen SDPA #31208

Merged
merged 4 commits into from
Jun 14, 2024
Merged

Fix MusicGen SDPA #31208

merged 4 commits into from
Jun 14, 2024

Conversation

ylacombe
Copy link
Contributor

@ylacombe ylacombe commented Jun 3, 2024

Fix #31189 and #30020.

SDPA produces Nan when given a padding mask that attends to no position at all (see pytorch/pytorch#103749 (comment)).
In the case of Musicgen, it can happen for two reasons:

  1. when we use get_unconditional_inputs (here)
  2. when guidance_scale>1 (see here)

There might be more elegant way to do this, WDYT @amyeroberts ?

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@@ -572,6 +572,24 @@ def forward(
output_attentions=output_attentions,
)

# Ignore copy
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ignore copy doesn't work, any ideas why @ydshieh ?

Copy link
Collaborator

@ydshieh ydshieh Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. Copy and ignore copy only works with a named entity (like function, method, class). It could not be used in a place (block) where no declared name is given. Either refactor the code or just remove the copy statement

Copied from transformers.models.bart.modeling_bart.BartSdpaAttention with Bart->Musicgen

if no code re-factorization is possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ydshieh, I'll remove the copy statement

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into and addressing this!

# Ignore copy
if (
attention_mask is not None
and (attention_mask.mean(dim=[1, 2, 3]) <= torch.finfo(attention_mask.dtype).min).any()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for using finfo here and not just 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The attention has already been processed and is filled with -inf of the correspondant dtype!

Comment on lines +584 to +591
return super().forward(
hidden_states,
key_value_states=key_value_states,
past_key_value=past_key_value,
attention_mask=attention_mask,
layer_head_mask=layer_head_mask,
output_attentions=output_attentions,
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than fallback, I would just raise an exception. Otherwise this expensive check and forward pass can easily go unnoticed

Copy link
Contributor Author

@ylacombe ylacombe Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, sdpa and guidance_scale>1 are used by default, so we'd raise the error almost every time. Also, even if the model uses eager mode for the cross-attention layers (in which the bug happens), it'll still benefit from the speed-up of the self-attention layers.

Should we find a better way of testing the attention mask ? For example, we could raise a warning that it will happens here and here and switch the cross-attention SDPA layers to eager layers by default when it happens?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see. I'm a bit concerned about this causing unexpected behaviour, in particular defaulting to eager like this as it's a bit magic. As there's other layers which can still use SDPA, this seems like a pragmatic solution.

Let's leave as-is. If more users raise issues, then we'll have to re-think.

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for digging into this and fixing!

@@ -545,7 +545,6 @@ def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query
)


# Copied from transformers.models.bart.modeling_bart.BartSdpaAttention with Bart->Musicgen
Copy link
Contributor Author

@ylacombe ylacombe Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the statement here, just want to make sure that it's okay for you @amyeroberts before merging!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep!

@ylacombe ylacombe merged commit 43ee585 into huggingface:main Jun 14, 2024
21 checks passed
itazap pushed a commit that referenced this pull request Jun 17, 2024
* fix sdpa musicgen

* make style

* remove copied from statement from Musicgen SDPA
itazap pushed a commit that referenced this pull request Jun 17, 2024
* fix sdpa musicgen

* make style

* remove copied from statement from Musicgen SDPA
itazap pushed a commit that referenced this pull request Jun 17, 2024
* fix sdpa musicgen

* make style

* remove copied from statement from Musicgen SDPA
itazap pushed a commit that referenced this pull request Jun 18, 2024
* fix sdpa musicgen

* make style

* remove copied from statement from Musicgen SDPA
itazap pushed a commit that referenced this pull request Jun 20, 2024
* fix sdpa musicgen

* make style

* remove copied from statement from Musicgen SDPA
@qubvel qubvel mentioned this pull request Jul 15, 2024
4 tasks
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.

MusicGen fails when being used with pipeline
4 participants