Skip to content

Fix is_causal being a tensor#35791

Merged
ArthurZucker merged 4 commits into
mainfrom
fix-gpt2-is-causal
Jan 30, 2025
Merged

Fix is_causal being a tensor#35791
ArthurZucker merged 4 commits into
mainfrom
fix-gpt2-is-causal

Conversation

@IlyasMoutawwakil

Copy link
Copy Markdown
Member

What does this PR do?

We encountered this issue when gpt2 is part of a vision encoder decoder (add_cross_attention=True)

FAILED exporters/onnx/test_exporters_onnx_cli.py::OnnxCLIExportTestCase::test_exporters_cli_pytorch_cpu_639_vision_encoder_decoder_image_to_text_with_past_default_hf_internal_testing_tiny_random_VisionEncoderDecoderModel_vit_gpt2 - TypeError: scaled_dot_product_attention(): argument 'is_causal' must be bool, not Tensor

Usually is_causal is evaluated in is_causal = attention_mask is None and query_states.shape[-2] > 1 and not is_cross_attention by the fact that attention_mask is None is False, skipping the rest, but when attention mask is not None, query_states.shape[-2] > 1 is evaluated and returns a tensor, resulting in sdpa failing because it's expecting a bool.

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?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Find it a bit weird because I can't reproduce the tensor shape being eval as tensor

>>> torch.ones(*(1,2),1).shape[0] > 1
False

@IlyasMoutawwakil

IlyasMoutawwakil commented Jan 20, 2025

Copy link
Copy Markdown
Member Author

Yeah I was surprised at first and thought it might be a behavior change in torch but it's actually related to tracing:

import torch


class model(torch.nn.Module):
    def __init__(self):
        super(model, self).__init__()
        self.fc = torch.nn.Linear(1, 1)

    def forward(self, x):
        print("shape > 1: ", x.shape[0] > 1)
        return self.fc(x)


model = model()
x = torch.randn(1, 1)

model(x)  # shape > 1:  False
torch.jit.trace(model, x, check_trace=False)  # shape > 1:  tensor(False)
torch.onnx.export(model, x, "model.onnx")  # shape > 1:  tensor(False)

This should've impacted the gpt2 onnx tests but I guess it wasn't caught because it's only tested as a pure decoder.

@ArthurZucker

Copy link
Copy Markdown
Collaborator

Got it, does the device synch not cause any issue for the exported model afterwards?

@IlyasMoutawwakil

IlyasMoutawwakil commented Jan 24, 2025

Copy link
Copy Markdown
Member Author

Got it, does the device synch not cause any issue for the exported model afterwards?

@ArthurZucker didn't see any issues in optimum onnx exported models, but I added a guard for jit tracing just in case.

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Okay thanks

@ArthurZucker
ArthurZucker merged commit 19f2ec8 into main Jan 30, 2025
@ArthurZucker
ArthurZucker deleted the fix-gpt2-is-causal branch January 30, 2025 08:22
ArthurZucker pushed a commit that referenced this pull request Jan 30, 2025
* fix is_causal being a tensor

* convert in sdpa attention only when  jit tracing
bursteratom pushed a commit to bursteratom/transformers that referenced this pull request Feb 5, 2025
* fix is_causal being a tensor

* convert in sdpa attention only when  jit tracing
elvircrn pushed a commit to elvircrn/transformers that referenced this pull request Feb 13, 2025
* fix is_causal being a tensor

* convert in sdpa attention only when  jit tracing
sbucaille pushed a commit to sbucaille/transformers that referenced this pull request Feb 16, 2025
* fix is_causal being a tensor

* convert in sdpa attention only when  jit tracing
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.

3 participants