-
Notifications
You must be signed in to change notification settings - Fork 26.3k
[numpy] Add torch.exp2
#44184
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] Add torch.exp2
#44184
Conversation
💊 CI failures summary and remediationsAs of commit fd02fb7 (more details on the Dr. CI page): 💚 💚 Looks good so far! There are no failures yet. 💚 💚 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. This comment has been revised 80 times. |
|
@mruberry Please review :) |
test/test_torch.py
Outdated
| b = torch.exp(torch.ones(1, dtype=dtype, device=device)) | ||
| self.assertEqual(a, b.expand(2 ** 31)) | ||
|
|
||
| def _unary_op_vs_numpy_template(self, torch_fn, np_fn, device, dtype, with_extremal): |
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.
exp is currently tested in TestTorchMathOps, but that's being refactored into a new testing mechanism that uses "OpInfos". Take a look at how unary ufuncs like exp2 are registered here:
| UnaryUfuncInfo('acos', |
You can register exp2 the same way and it will be tested automatically. You'll need to fill out the ref field and set the dtypesIfCPU and dtypesIfCUDA fields since exp2 doesn't support complex values.
The OpInfo method of testing is new and I would appreciate your feedback trying to use it. I hope it's straightforward.
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.
Should I remove this test as the PR will update the UnaryUfuncInfo Operator list. Or both existing would fine. ( I think removal makes more sense, as both do similar testing)
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.
Yep. It should be redundant with the OpInfo-based tests (in test_unary_ufuncs.py). Better remove it.
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.
Had to do
UnaryUfuncInfo('exp2',
ref=np.exp2,
dtypes=floating_types_and(torch.half),
dtypesIfCPU=None, # Had to explicitly set to None
dtypesIfCUDA=None,
dtypesIfROCM=None,), # Had to explicitly set to NoneOne would expect that if only dtypes is given all devices will verify only those dtypes based on the code below
pytorch/torch/testing/_internal/common_methods_invocations.py
Lines 73 to 76 in 63ca5e0
| self.dtypes = dtypes | |
| self.dtypesIfCPU = dtypesIfCPU if dtypesIfCPU is not None else dtypes | |
| self.dtypesIfCUDA = dtypesIfCUDA if dtypesIfCUDA is not None else dtypes | |
| self.dtypesIfROCM = dtypesIfROCM if dtypesIfROCM is not None else dtypes |
But with the following defaults in constructor,
pytorch/torch/testing/_internal/common_methods_invocations.py
Lines 160 to 167 in 63ca5e0
| def __init__(self, | |
| name, # the string name of the function | |
| *, | |
| ref, # a reference function | |
| dtypes=floating_types(), | |
| dtypesIfCPU=floating_and_complex_types_and(torch.bfloat16), | |
| dtypesIfCUDA=floating_and_complex_types_and(torch.half), | |
| dtypesIfROCM=floating_types_and(torch.half), |
That behaviour is not honored. I think the default value for those arguments should be None.
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're right that you have to explicitly override because of the defaults. We could add a more complex inference system for the default dtypes where the defaults are None but None is interpreted as first checking dtypes, if set, and then reverting to its current default value. Let's see if it comes up again?
| ('expand_as', (S, 1, 1), (torch.rand(S, S, S),), '', (False,)), | ||
| ('exp', (S, S, S), NO_ARGS, '', (True,)), | ||
| ('exp', (), NO_ARGS, 'scalar', (True,)), | ||
| ('exp2', (S, S, S), NO_ARGS, '', (True,)), |
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 can leave these entries for now. Although they'll be mostly redundant with the OpInfo entry there's still a couple tests that only use method_tests.
mruberry
left a comment
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.
Hey @kshitij12345! This is good work, as always. I made a few suggestions, including trying the new OpInfo-based test pattern. I'm very curious to hear how easy/hard it is for you to use it.
Codecov Report
@@ Coverage Diff @@
## master #44184 +/- ##
=======================================
Coverage 67.98% 67.98%
=======================================
Files 384 384
Lines 49586 49589 +3
=======================================
+ Hits 33712 33715 +3
Misses 15874 15874
Continue to review full report at Codecov.
|
|
Thanks for the help. Have updated the assertion and those errors are gone. However the previous error is still there and showing up. I had previously tried to run it locally. But couldn't reproduce the failure. Please take a look :). |
|
Would you try changing this line (and the other method test line(s)) from: to or I think that will fix this issue. What I think is happening is that the check is trying to fuse exp2 and verify it's fused, but it doesn't know how to do that because exp2 is a new function. If that doesn't work I recommend deleting the method_test entries to resolve this issue. |
Thanks for the clarification. That makes sense! Will try that! Any idea as to how it would work on my local system? 🤔 |
|
@mruberry Thanks. Looks like those tests are passing now. Would be great if you can have another look. I think the PR is almost done. |
It's because the fusion is only enabled on some builds. I think it's only Windows builds. |
mruberry
left a comment
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.
Nice work, @kshitij12345! Thank you for trying out our new OpInfo test method!
I made a few minor comments (eliminate an extra semicolon, delete a redundant test) but this looks great. Thanks for another awesome PR! Just ping me when the minor cleanup is done.
* remove redundant test * remove stray semi-colon
Ah. I see. Thanks! Also have addressed the latest comments. |
facebook-github-bot
left a comment
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.
@mruberry has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
Internal tests are failing because exp2 doesn't appear to be available in the standard namespace on android builds: cc @dreiss We've had this issue in the past with @muthuArivoli's PRs, here's the fix we identified: Basically a couple stubs need to be added. Let me know if you have additional questions about this, @kshitij12345. Once the stubs are added I can re-import this PR and run the Android tests again. |
|
@mruberry Thanks for the update. I don't think we need to update the vectorized neon implementations as we don't use the vector implementations anywhere currently. Is that right? So only place we might need to update is |
That's probably true. Let's try that first. |
|
@mruberry Have updated the |
facebook-github-bot
left a comment
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.
@mruberry has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
Looks like that did it. Thanks @kshitij12345! |

Reference #42515
TODO