-
Notifications
You must be signed in to change notification settings - Fork 23.2k
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
Assert if padding mask type is unexpected (#86353) #87106
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/87106
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 7ed2fcf: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This pull request was exported from Phabricator. Differential Revision: D40129968 |
This pull request was exported from Phabricator. Differential Revision: D40129968 |
4d9efc3
to
9c54c65
Compare
This pull request was exported from Phabricator. Differential Revision: D40129968 |
9c54c65
to
2fa3431
Compare
This pull request was exported from Phabricator. Differential Revision: D40129968 |
… & unit test (pytorch#87106) Summary: Pull Request resolved: pytorch#87106 Pull Request resolved: pytorch#86353 Fix the issue described in pytorch#86120 Test Plan: buck test mode/opt caffe2/test:test_transformers -- test_train_with_long_type_pad Differential Revision: D40129968 fbshipit-source-id: 2be6869f2f8a39c780fdfaf3fb16034c4726a571
2fa3431
to
ef641ac
Compare
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.
LGTM, but please consider the posted feedback
torch/nn/modules/transformer.py
Outdated
@@ -443,6 +448,11 @@ def forward(self, src: Tensor, src_mask: Optional[Tensor] = None, | |||
""" | |||
|
|||
# see Fig. 1 of https://arxiv.org/pdf/2002.04745v1.pdf |
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.
Perhaps put the assertion above the comment (as Fig1 is unlikely to have anything to do with checks below
torch/nn/modules/transformer.py
Outdated
if src_key_padding_mask.dtype != torch.bool: | ||
if not torch.is_floating_point(src_key_padding_mask): | ||
raise AssertionError( | ||
"only bool and floating type of key_padding_mask is supported") |
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.
English is not my native, but isn't assertion discusses plural types, isn't it?
"only bool and floating type of key_padding_mask is supported") | |
"only bool and floating types of key_padding_mask are supported") |
torch/nn/modules/transformer.py
Outdated
if src_key_padding_mask is not None: | ||
if src_key_padding_mask.dtype != torch.bool: | ||
if not torch.is_floating_point(src_key_padding_mask): |
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.
Please consider dismantling pyramid of doom by a bit
if src_key_padding_mask is not None: | |
if src_key_padding_mask.dtype != torch.bool: | |
if not torch.is_floating_point(src_key_padding_mask): | |
if src_key_padding_mask is not None: | |
_skpm_dtype = src_key_padding_mask.dtype | |
if _skpm_dtype != torch.bool and not _skpm_dtype.is_floating_point: |
torch/nn/modules/activation.py
Outdated
if key_padding_mask is not None: | ||
if key_padding_mask.dtype != torch.bool: | ||
if not torch.is_floating_point(key_padding_mask): | ||
raise AssertionError( | ||
"only bool and floating type of key_padding_mask is supported") |
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.
You are adding the same check thrice, which seems to warrant a convenience function
def _assert_if_tensor_is_not_float_or_bool(t: Optional[torch.Tensor], name: str) -> None:
if t is None:
return
if t.dtype != torch.bool and not t.dtype.is_floating_point:
raise AssertionError(f"only bool and floating types of {name} are supported")
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.
That is a good idea, but dont want to add new helper functions in this diff anymore in case it will break torch script :(
@pytorchbot merge |
Also, please change title, as this PR is not 1.13 cherry pick, it simply targets master. |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
The merge job was canceled. If you believe this is a mistake,then you can re trigger it through pytorch-bot. |
ef641ac
to
0f7a52b
Compare
This pull request was exported from Phabricator. Differential Revision: D40129968 |
This pull request was exported from Phabricator. Differential Revision: D40129968 |
… & unit test (pytorch#87106) Summary: Pull Request resolved: pytorch#87106 Pull Request resolved: pytorch#86353 Fix the issue described in pytorch#86120 Test Plan: buck test mode/opt caffe2/test:test_transformers -- test_train_with_long_type_pad Reviewed By: malfet Differential Revision: D40129968 fbshipit-source-id: 4a3ac5750082b9b1806003e36b04edd96c36f538
0f7a52b
to
dbf9fb2
Compare
dbf9fb2
to
14af130
Compare
This pull request was exported from Phabricator. Differential Revision: D40129968 |
14af130
to
47a676f
Compare
This pull request was exported from Phabricator. Differential Revision: D40129968 |
torch/nn/modules/activation.py
Outdated
@@ -1062,6 +1061,11 @@ def forward(self, query: Tensor, key: Tensor, value: Tensor, key_padding_mask: O | |||
`batch_first` argument is ignored for unbatched inputs. | |||
""" | |||
is_batched = query.dim() == 3 | |||
if key_padding_mask is not None: | |||
_skpm_dtype = key_padding_mask.dtype | |||
if _skpm_dtype != torch.bool and not torch.is_floating_point(key_padding_mask): |
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.
Why call a function rather than query the property directly?
if _skpm_dtype != torch.bool and not torch.is_floating_point(key_padding_mask): | |
if _skpm_dtype != torch.bool and not _skpm_dtype.is_floating_point: |
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.
@malfet I tagged you in phabricator. It is because some scripted tensors have really weird tensor dtype, like an "int" instead of torch.int. I pasted some links in pharicator, and msged you directly ;)
torch/nn/modules/activation.py
Outdated
@@ -1062,6 +1061,11 @@ def forward(self, query: Tensor, key: Tensor, value: Tensor, key_padding_mask: O | |||
`batch_first` argument is ignored for unbatched inputs. | |||
""" | |||
is_batched = query.dim() == 3 | |||
if key_padding_mask is not None: | |||
_skpm_dtype = key_padding_mask.dtype |
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.
Variable name supposed to start with first letter of long variable name, i.e. for key_padding_mask
is should be _kpm_dtype
_skpm_dtype = key_padding_mask.dtype | |
_kpm_dtype = key_padding_mask.dtype |
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.
lol right
Summary: Pull Request resolved: pytorch#87106 Pull Request resolved: pytorch#86353 Fix the issue described in pytorch#86120 Test Plan: buck test mode/opt caffe2/test:test_transformers -- test_train_with_long_type_pad Reviewed By: malfet Differential Revision: D40129968 fbshipit-source-id: d7530d8c4548b9d3a3ef8b9740c93bee000ed809
47a676f
to
7ed2fcf
Compare
This pull request was exported from Phabricator. Differential Revision: D40129968 |
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: 3 additional jobs have failed, first few of them are: trunk ,trunk / linux-focal-rocm5.2-py3.7 / test (default, 2, 2, linux.rocm.gpu) ,trunk / cuda11.6-py3.10-gcc7-sm86 / test (default, 1, 4, linux.g5.4xlarge.nvidia.gpu) Details for Dev Infra teamRaised by workflow job |
@pytorchmergebot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Summary: Pull Request resolved: pytorch#87106 Pull Request resolved: pytorch#86353 Fix the issue described in pytorch#86120 Test Plan: buck test mode/opt caffe2/test:test_transformers -- test_train_with_long_type_pad Reviewed By: malfet Differential Revision: D40129968 fbshipit-source-id: d7530d8c4548b9d3a3ef8b9740c93bee000ed809
Summary: Pull Request resolved: pytorch#87106 Pull Request resolved: pytorch#86353 Fix the issue described in pytorch#86120 Test Plan: buck test mode/opt caffe2/test:test_transformers -- test_train_with_long_type_pad Reviewed By: malfet Differential Revision: D40129968 fbshipit-source-id: d7530d8c4548b9d3a3ef8b9740c93bee000ed809
Summary: Pull Request resolved: #87106 Pull Request resolved: #86353 Fix the issue described in #86120 Test Plan: buck test mode/opt caffe2/test:test_transformers -- test_train_with_long_type_pad Reviewed By: malfet Differential Revision: D40129968 fbshipit-source-id: d7530d8c4548b9d3a3ef8b9740c93bee000ed809
Summary:
Pull Request resolved: #86353
Fix the issue described in
#86120
Test Plan: buck test mode/opt caffe2/test:test_transformers -- test_train_with_long_type_pad
Differential Revision: D40129968