-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Fix mean bug for integral tensors.
#76584
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
Conversation
🔗 Helpful links
✅ No Failures (0 Pending)As of commit 9d8ae5f (more details on the Dr. CI page): Expand to see more💚 💚 Looks good so far! There are no failures yet. 💚 💚 This comment was automatically generated by Dr. CI (expand for details).Please report bugs/suggestions to the (internal) Dr. CI Users group. |
|
@ngimel suggested casting the input directly to a floating point tensor. But, since I wasn't sure what the output dtype should be ( |
|
about auto-casting integral tensor (and fixing #64897): I propose to auto-cast to For practical use-cases: computing accuracy by taking mean of booltensor and computing a mean value of [0, 255] uint8 images etc. For them upcasting to float32 might be sufficient |
aten/src/ATen/native/ReduceOps.cpp
Outdated
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.
Probably want to hide this computation in a conditional that checks the error conditions so we're not creating this string on every call
test/test_reductions.py
Outdated
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.
I think we can model this as an ErrorInput for the mean OpInfo?
test/test_reductions.py
Outdated
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.
Same thing -- can we make this an ErrorInput?
I think that makes sense to me, and hopefully it is not too confusing.
@mruberry @ngimel |
That looks correct to me, but I'll let @ngimel review this more thoroughly since she's been working closely with reductions recently |
|
For integral tensors one funny option may also be to do sum in int64 (or int32?) and then divide by the size - but maybe this might actually be slower and have worse dynamic range behavior than float |
aten/src/ATen/native/ReduceOps.cpp
Outdated
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.
this is a misleading error message, e.g. if self dtype if float but dtype arg is integer, then it's and invalid combination (and you'll rightly error out), but error message wouldn't explain it.
76074f7 to
52a2d4f
Compare
|
@pytorchbot merge this |
|
Hey @ysiraichi. |
Summary: Fixes #76430 **The Problem:** `opt_dtype` wasn't being taken into consideration when checking whether the input dtype was either floating point or complex dtype. **The Solution:** run those checks with the dtype returned by `get_dtype_from_self(self, opt_dtype, true)`. This fix restores the original behavior, before #61643. It also improves the error message so that the user better comprehends what happened. Finally, I also added 2 tests for ensuring the issue was fixed ----- #### Before ```python >>> a = torch.randint(0, 5, (5, 5), dtype=torch.int64) >>> b = torch.tensor([], dtype=torch.float32) >>> a.mean() # no dtype RuntimeError: mean(): input dtype should be either floating point or complex dtypes. Got Long instead. >>> a.mean(dtype=torch.float32) # with dtype RuntimeError: mean(): input dtype should be either floating point or complex dtypes. Got Long instead. >>> torch.mean(a, [], dtype=torch.float64, out=b) # with mismatching dtype and out dtype RuntimeError: mean(): input dtype should be either floating point or complex dtypes. Got Long instead. ``` #### After ```python >>> a = torch.randint(0, 5, (5, 5), dtype=torch.int64) >>> b = torch.tensor([], dtype=torch.float32) >>> a.mean() # no dtype RuntimeError: mean(): at least one of (i) the input dtype and (ii) the desired output dtype should be either floating point or complex. Got (i) Long and (ii) None instead. >>> a.mean(dtype=torch.float32) # with dtype tensor(1.6800) >>> torch.mean(a, [], dtype=torch.float64, out=b) # with mismatching dtype and out dtype RuntimeError: Expected out tensor to have dtype double, but got float instead ``` Pull Request resolved: #76584 Approved by: https://github.com/ngimel Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/459e10f4465fd5a75e6dea96597b427feae71e42 Reviewed By: atalman Differential Revision: D36412508 Pulled By: atalman fbshipit-source-id: 7acfec825bc3efaaf62e124ddc24a1034fc6792e
Fixes #76430
The Problem:
opt_dtypewasn't being taken into consideration when checking whether the input dtype was either floating point or complex dtype.The Solution: run those checks with the dtype returned by
get_dtype_from_self(self, opt_dtype, true).This fix restores the original behavior, before #61643. It also improves the error message so that the user better comprehends what happened. Finally, I also added 2 tests for ensuring the issue was fixed
Before
After