Repository navigation
Fix mask creation not being skipped under torch.compile
#48975
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
Changes from 6 commits
2dc4531
90fb842
d323359
34b833e
56a7180
c6fbba4
3c6ffb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| from transformers.testing_utils import ( | ||
| is_torch_available, | ||
| require_torch, | ||
| require_torch_greater_or_equal, | ||
| torch_device, | ||
| ) | ||
|
|
||
|
|
@@ -177,6 +178,51 @@ def test_nonpacked_sequence_mask_skip(self): | |
| # cannot be skipped under compile, should result into a triu mask | ||
| self.assertTrue(torch.equal(~torch.ones(*causal_mask.shape).triu(diagonal=1).bool(), causal_mask)) | ||
|
|
||
| # Before https://github.com/pytorch/pytorch/pull/176499 (torch 2.14), dynamo replaced calls to | ||
| # `torch.compiler.is_exporting()` by a constant `True`, so we treated `torch.compile` as export and never | ||
| # skipped. On older versions the mask is therefore still materialized. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd move this into the docstring
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| @require_torch_greater_or_equal("2.14") | ||
| def test_mask_skip_without_padding_mask_under_compile(self): | ||
| """ | ||
| Checks whether the mask creation can still be skipped under `torch.compile` if we have no padding mask at all. | ||
| Whether a padding mask is provided is a static property that dynamo guards on - only the checks reading its | ||
| values are data-dependent, and have to be skipped while tracing. | ||
| """ | ||
| config = LlamaConfig() | ||
| config._attn_implementation = "sdpa" | ||
|
|
||
| batch_size = 2 | ||
| sequence_length = 10 | ||
| inputs_embeds = torch.empty((batch_size, sequence_length, 8), dtype=torch.float16, device=torch_device) | ||
| padded_mask = torch.ones(batch_size, sequence_length, dtype=torch.long, device=torch_device) | ||
| padded_mask[0, :3] = 0 | ||
|
|
||
| def create_masks(attention_mask): | ||
| causal_mask = create_causal_mask( | ||
| config=config, | ||
| inputs_embeds=inputs_embeds, | ||
| attention_mask=attention_mask, | ||
| past_key_values=None, | ||
| ) | ||
| bidirectional_mask = create_bidirectional_mask( | ||
| config=config, | ||
| inputs_embeds=inputs_embeds, | ||
| attention_mask=attention_mask, | ||
| ) | ||
| return causal_mask, bidirectional_mask | ||
|
|
||
| compiled_create_masks = torch.compile(create_masks, fullgraph=True) | ||
|
|
||
| # Without a padding mask, both masks are skipped in eager as well as under compile | ||
| self.assertEqual(create_masks(None), (None, None)) | ||
| self.assertEqual(compiled_create_masks(None), (None, None)) | ||
|
|
||
| # With a padding mask, the masks are materialized. Under compile, reading its values must be skipped | ||
| # instead of raising a data-dependent control flow error | ||
| for causal_mask, bidirectional_mask in (create_masks(padded_mask), compiled_create_masks(padded_mask)): | ||
| self.assertIsNotNone(causal_mask) | ||
| self.assertIsNotNone(bidirectional_mask) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure why we loop here tbh, can just keep it simple as above
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
|
||
| def test_chunked_mask_with_left_padding_and_large_prefill(self): | ||
| # Make sure we have an attention_chunk_size in the config | ||
| config = LlamaConfig(attention_chunk_size=3, attn_implementation="sdpa") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldnt this be one complete NOTE section as this is an exception under base compile?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.