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

[numpy] torch.{all, any} : Extend Dtype Support #44790

Closed

Conversation

kshitij12345
Copy link
Collaborator

Reference #44779

@kshitij12345 kshitij12345 marked this pull request as draft September 16, 2020 15:56
@dr-ci
Copy link

dr-ci bot commented Sep 16, 2020

💊 CI failures summary and remediations

As of commit 1b242ef (more details on the Dr. CI page):


  • 3/3 failures possibly* introduced in this PR
    • 1/3 non-CircleCI failure(s)

🕵️ 2 new failures recognized by patterns

The following CI failures do not appear to be due to upstream breakages:

See CircleCI build binary_linux_libtorch_3_7m_cpu_devtoolset7_shared-with-deps_build (1/2)

Step: "Checkout pytorch/builder repo" (full log | diagnosis details | 🔁 rerun)

fatal: reference is not a tree: cd5a9b73c3028d2496666201588111a8c8d84878
+ git submodule update --init --recursive 
Warning: Permanently added the RSA host key for IP address '140.82.112.3' to the list of known hosts.  
fatal: reference is not a tree: cd5a9b73c3028d2496666201588111a8c8d84878 
Unable to checkout 'cd5a9b73c3028d2496666201588111a8c8d84878' in submodule path 'third_party/nccl/nccl' 
+ sleep 4 
+ git submodule update --init --recursive 
fatal: reference is not a tree: cd5a9b73c3028d2496666201588111a8c8d84878 
Unable to checkout 'cd5a9b73c3028d2496666201588111a8c8d84878' in submodule path 'third_party/nccl/nccl' 
+ sleep 8 
+ git submodule update --init --recursive 
fatal: reference is not a tree: cd5a9b73c3028d2496666201588111a8c8d84878 
Unable to checkout 'cd5a9b73c3028d2496666201588111a8c8d84878' in submodule path 'third_party/nccl/nccl' 

See CircleCI build pytorch_xla_linux_bionic_py3_6_clang9_test (2/2)

Step: "Run tests" (full log | diagnosis details | 🔁 rerun)

Nov 10 11:14:45 FAIL [0.175s]: test_all_any_vs_numpy_xla_uint8 (__main__.TestTorchDeviceTypeXLA)
Nov 10 11:14:45     return DeviceTypeTestBase.assertEqual(self, x, y, *args, **kwargs) 
Nov 10 11:14:45   File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_utils.py", line 1037, in assertEqual 
Nov 10 11:14:45     exact_dtype=exact_dtype, exact_device=exact_device) 
Nov 10 11:14:45   File "/var/lib/jenkins/workspace/xla/test/pytorch_test_base.py", line 552, in assertEqual 
Nov 10 11:14:45     return DeviceTypeTestBase.assertEqual(self, x, y, *args, **kwargs) 
Nov 10 11:14:45   File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_utils.py", line 1151, in assertEqual 
Nov 10 11:14:45     super().assertEqual(x, y, msg=msg) 
Nov 10 11:14:45 AssertionError: True != 46 
Nov 10 11:14:45  
Nov 10 11:14:45 ====================================================================== 
Nov 10 11:14:45 FAIL [0.175s]: test_all_any_vs_numpy_xla_uint8 (__main__.TestTorchDeviceTypeXLA) 
Nov 10 11:14:45 ---------------------------------------------------------------------- 
Nov 10 11:14:45 Traceback (most recent call last): 
Nov 10 11:14:45   File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_device_type.py", line 272, in instantiated_test 
Nov 10 11:14:45     result = test_fn(self, *args) 
Nov 10 11:14:45   File "/var/lib/jenkins/workspace/xla/test/../../test/test_torch.py", line 19633, in test_all_any_vs_numpy 
Nov 10 11:14:45     _test_all_any(x) 
Nov 10 11:14:45   File "/var/lib/jenkins/workspace/xla/test/../../test/test_torch.py", line 19618, in _test_all_any 
Nov 10 11:14:45     self.compare_with_numpy(torch.all, np.all, x) 
Nov 10 11:14:45   File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_utils.py", line 913, in compare_with_numpy 
Nov 10 11:14:45     self.assertEqual(np_result, torch_result, **kwargs) 

ci.pytorch.org: 1 failed


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 29 times.

@kshitij12345 kshitij12345 marked this pull request as ready for review October 11, 2020 10:04
@kshitij12345 kshitij12345 changed the title [WIP][numpy] torch.{all, any} : Extend Dtype Support [numpy] torch.{all, any} : Extend Dtype Support Oct 11, 2020
@heitorschueroff heitorschueroff added module: reductions module: numpy Related to numpy support, and also numpy compatibility of our operators triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Oct 14, 2020
Copy link
Contributor

@heitorschueroff heitorschueroff left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for this PR, it's a very welcomed change.

Note: Now that torch.all and torch.any supports all dtypes, we should document it in the public APIs as mentioned here #44779, but this can be a separate PR.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@heitorschueroff has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@heitorschueroff heitorschueroff left a comment

Choose a reason for hiding this comment

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

It looks like the XLA failures are related to the changes. Could you look into what's causing it please?

@kshitij12345
Copy link
Collaborator Author

@heitorschueroff Thanks for looking at it.

As for XLA, I m not really sure what is happening.
Maybe @JackCaoG can have a look.

Thanks!

@JackCaoG
Copy link
Collaborator

JackCaoG commented Nov 5, 2020

@kshitij12345 XLA change is ready, I will merge it when this pr is merged.

@heitorschueroff
Copy link
Contributor

@JackCaoG Thanks for updating XLA. @kshitij12345 Could you rebase please, I'll merge it then.

@kshitij12345
Copy link
Collaborator Author

@heitorschueroff Have fixed the conflict. ROCm failure looks irrelevant.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@heitorschueroff has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@heitorschueroff
Copy link
Contributor

@heitorschueroff Have fixed the conflict. ROCm failure looks irrelevant.

Thank you for this contribution, I'm important your changes now.

Copy link
Contributor

@heitorschueroff heitorschueroff left a comment

Choose a reason for hiding this comment

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

It looks like I missed some details in my review. Our internal tests on phabricator are complaining. I left some comments from phabricator, they should be fairly quick to fix and then I can land it without problems.

return c;
},
/*ident=*/true);
if (c10::isIntegralType(iter.dtype(), /*include_bool=*/true)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

include_bool -> includeBool

return c;
},
/*ident=*/false);
if (c10::isIntegralType(iter.dtype(), /*include_bool=*/true)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

include_bool -> includeBool

if (c10::isIntegralType(iter.dtype(), /*include_bool=*/true)) {
binary_kernel_reduce_vec(
iter,
[=](uint8_t a, uint8_t b) -> uint8_t { return a && b; },
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid the implicit cast with:
[=](uint8_t a, uint8_t b) -> uint8_t { return ((a && b) ? 1 : 0); },

if (c10::isIntegralType(iter.dtype(), /*include_bool=*/true)) {
binary_kernel_reduce_vec(
iter,
[=](uint8_t a, uint8_t b) -> uint8_t { return a || b; },
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid the implicit cast with:
[=](uint8_t a, uint8_t b) -> uint8_t { return ((a && b) ? 1 : 0); },

// true/false.
Vec256<uint8_t> c = Vec256<uint8_t>();
for (int i = 0; i != Vec256<uint8_t>::size(); i++) {
c[i] = a[i] && b[i];
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid implicit cast with:
c[i] = ((a[i] && b[i]) ? 1 : 0);

[=](Vec256<uint8_t> a, Vec256<uint8_t> b) {
Vec256<uint8_t> c = Vec256<uint8_t>();
for (int i = 0; i != Vec256<uint8_t>::size(); i++) {
c[i] = a[i] || b[i];
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid implicit cast with:
c[i] = ((a[i] && b[i]) ? 1 : 0);

@kshitij12345
Copy link
Collaborator Author

@heitorschueroff Done.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@heitorschueroff has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@heitorschueroff merged this pull request in 6575e67.

1 similar comment
@facebook-github-bot
Copy link
Contributor

@heitorschueroff merged this pull request in 6575e67.

@kshitij12345 kshitij12345 deleted the support/all-any/all-dtypes branch November 11, 2020 03:19
@kshitij12345
Copy link
Collaborator Author

@JackCaoG Please merge the XLA fix. Thanks!

@ngimel
Copy link
Collaborator

ngimel commented Nov 11, 2020

@kshitij12345 there are a few problems with this PR

  1. as noted already, it breaks XLA CI, @heitorschueroff please avoid landing things before XLA fix is merged
  2. it produces results of the same dtype as input, this is different from numpy:
In [9]: a=torch.randn(3,3)                                                                                                                                                                    
In [10]: anp=a.numpy()                                                                                                                                                                        
In [11]: np.all(anp, 0)                                                                                                                                                                       
Out[11]: array([ True,  True,  True])
In [12]: a.all(0)                                                                                                                                                                             
Out[12]: tensor([1., 1., 1.])
  1. it introduces performance cliff for non-bool input types, because it does not use binary_kernel_reduce_vec, and instead uses binary_kernel_reduce which is much (5-10x) slower.

Can you please work on fixing 2) and 3) ?

@JackCaoG
Copy link
Collaborator

@kshitij12345 there are a few problems with this PR

  1. as noted already, it breaks XLA CI, @heitorschueroff please avoid landing things before XLA fix is merged
  2. it produces results of the same dtype as input, this is different from numpy:
In [9]: a=torch.randn(3,3)                                                                                                                                                                    
In [10]: anp=a.numpy()                                                                                                                                                                        
In [11]: np.all(anp, 0)                                                                                                                                                                       
Out[11]: array([ True,  True,  True])
In [12]: a.all(0)                                                                                                                                                                             
Out[12]: tensor([1., 1., 1.])
  1. it introduces performance cliff for non-bool input types, because it does not use binary_kernel_reduce_vec, and instead uses binary_kernel_reduce which is much (5-10x) slower.

Can you please work on fixing 2) and 3) ?

I merged XLA change. If this pr will be reverted I will revert the xla pr as well. Otherwise I will work on a companion pr to fix the result type.

@gchanan
Copy link
Contributor

gchanan commented Nov 11, 2020

I'd look at the logical_or and logical_and kernels for how to get the dtypes correct; note that it's a little different because of the performance issue @ngimel mentioned above.

@ngimel
Copy link
Collaborator

ngimel commented Nov 11, 2020

Also please don't forget to update documentation.

@kshitij12345
Copy link
Collaborator Author

@ngimel
For 2, it would be a BC-Breaking to change the result-type to bool,
I think it would be better to add a warning first otherwise it may break code for users. Let me know what you think about that.

Behaviour in previous version (1.5.1)

>>> import torch
>>> torch.__version__
'1.5.1+cu101'
>>> x = torch.zeros(3,3)
>>> x.to(torch.uint8).all()
tensor(0, dtype=torch.uint8)
>>> x.to(torch.bool).all()
tensor(False)

@ngimel
Copy link
Collaborator

ngimel commented Nov 12, 2020

cc @mruberry for deprecating return type for uint8. In any case, for all other types there are no bc breaking concerns, so we should implement correct behavior.

@mruberry
Copy link
Collaborator

I would try to update the uint8 behavior to be consistent and document the change as BC-breaking. If a scripted network relies on the current behavior (extremely unlikely) we can write an upgrader.

facebook-github-bot pushed a commit that referenced this pull request Dec 15, 2020
Summary:
BC-breaking note:

This PR changes the behavior of the any and all functions to always return a bool tensor. Previously these functions were only defined on bool and uint8 tensors, and when called on uint8 tensors they would also return a uint8 tensor. (When called on a bool tensor they would return a bool tensor.)

PR summary:

#44790 (comment)

Fixes 2 and 3

Also Fixes #48352

Changes
* Output dtype is always `bool` (consistent with numpy) **BC Breaking (Previously used to match the input dtype**)
* Uses vectorized version for all dtypes on CPU
* Enables test for complex
* Update doc for `torch.all` and `torch.any`

TODO
* [x] Update docs
* [x] Benchmark
* [x] Raise issue on XLA

Pull Request resolved: #47878

Reviewed By: H-Huang

Differential Revision: D25421263

Pulled By: mruberry

fbshipit-source-id: c6c681ef94004d2bcc787be61a72aa059b333e69
hwangdeyu pushed a commit to hwangdeyu/pytorch that referenced this pull request Jan 6, 2021
Summary:
BC-breaking note:

This PR changes the behavior of the any and all functions to always return a bool tensor. Previously these functions were only defined on bool and uint8 tensors, and when called on uint8 tensors they would also return a uint8 tensor. (When called on a bool tensor they would return a bool tensor.)

PR summary:

pytorch#44790 (comment)

Fixes 2 and 3

Also Fixes pytorch#48352

Changes
* Output dtype is always `bool` (consistent with numpy) **BC Breaking (Previously used to match the input dtype**)
* Uses vectorized version for all dtypes on CPU
* Enables test for complex
* Update doc for `torch.all` and `torch.any`

TODO
* [x] Update docs
* [x] Benchmark
* [x] Raise issue on XLA

Pull Request resolved: pytorch#47878

Reviewed By: H-Huang

Differential Revision: D25421263

Pulled By: mruberry

fbshipit-source-id: c6c681ef94004d2bcc787be61a72aa059b333e69
facebook-github-bot pushed a commit that referenced this pull request Jan 8, 2021
Summary:
BC-breaking note:

This PR changes the behavior of the any and all functions to always return a bool tensor. Previously these functions were only defined on bool and uint8 tensors, and when called on uint8 tensors they would also return a uint8 tensor. (When called on a bool tensor they would return a bool tensor.)

PR summary:

#44790 (comment)

Fixes 2 and 3

Also Fixes #48352

Changes
* Output dtype is always `bool` (consistent with numpy) **BC Breaking (Previously used to match the input dtype**)
* Uses vectorized version for all dtypes on CPU
* Enables test for complex
* Update doc for `torch.all` and `torch.any`

TODO
* [x] Update docs
* [x] Benchmark
* [x] Raise issue on XLA

Pull Request resolved: #47878

Reviewed By: albanD

Differential Revision: D25714324

Pulled By: mruberry

fbshipit-source-id: a87345f725297524242d69402dfe53060521ea5d
hwangdeyu pushed a commit to hwangdeyu/pytorch that referenced this pull request Jan 14, 2021
Summary:
BC-breaking note:

This PR changes the behavior of the any and all functions to always return a bool tensor. Previously these functions were only defined on bool and uint8 tensors, and when called on uint8 tensors they would also return a uint8 tensor. (When called on a bool tensor they would return a bool tensor.)

PR summary:

pytorch#44790 (comment)

Fixes 2 and 3

Also Fixes pytorch#48352

Changes
* Output dtype is always `bool` (consistent with numpy) **BC Breaking (Previously used to match the input dtype**)
* Uses vectorized version for all dtypes on CPU
* Enables test for complex
* Update doc for `torch.all` and `torch.any`

TODO
* [x] Update docs
* [x] Benchmark
* [x] Raise issue on XLA

Pull Request resolved: pytorch#47878

Reviewed By: albanD

Differential Revision: D25714324

Pulled By: mruberry

fbshipit-source-id: a87345f725297524242d69402dfe53060521ea5d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed Merged module: numpy Related to numpy support, and also numpy compatibility of our operators module: reductions open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants