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

bugfix: torch.export failure caused by _make_causal_mask #35291

Merged
merged 2 commits into from
Dec 20, 2024

Conversation

jiwoong-choi
Copy link
Contributor

What does this PR do?

Fix the torch.export failure caused by AttentionMaskConverter._make_causal_mask.

Recent changes in torch dynamo prevent mutations on tensors converted with aten::_to_copy. To address this, we can clone such tensor before performing in-place operation masked_fill_ only when the code is being compiled by torch dynamo. (relevant issue on PyTorch: pytorch/pytorch#127571)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

PyTorch: @gante @Rocketknight1

Recent changes in torch dynamo prevent mutations on tensors converted with aten::_to_copy. To address this, we can clone such tensor before performing in-place operation `masked_fill_` only when the code is being compiled by torch dynamo.
(relevant issue: pytorch/pytorch#127571)
@Rocketknight1
Copy link
Member

This seems legit to me, but cc @ArthurZucker for core maintainer review

Copy link
Member

@qubvel qubvel left a comment

Choose a reason for hiding this comment

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

Hi @jiwoong-choi, thanks for the update! I faced a similar issue with vision models and can confirm that this should fix torch.export. Alternatively, we could use a non-inplace operation for masked_fill, but your solution seems better because it does not change the original behavior.

src/transformers/modeling_attn_mask_utils.py Outdated Show resolved Hide resolved
@qubvel qubvel added the torch export Issues and PRs related to torch.export compatibility label Dec 16, 2024
@qubvel qubvel requested a review from ArthurZucker December 19, 2024 10:19
Copy link
Collaborator

@ArthurZucker ArthurZucker 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 fixing

Comment on lines +172 to 176
# Recent changes in PyTorch prevent mutations on tensors converted with aten::_to_copy
# See https://github.com/pytorch/pytorch/issues/127571
if is_torchdynamo_compiling():
mask = mask.clone()
mask.masked_fill_(context_mask, torch.finfo(dtype).min)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree with @qubvel, tho I don't mind modifying to have an inplace operation!

@ArthurZucker ArthurZucker merged commit 40292aa into huggingface:main Dec 20, 2024
20 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
torch export Issues and PRs related to torch.export compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants