Skip to content

Support __rmod__ #58476

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

Closed
wants to merge 7 commits into from
Closed

Support __rmod__ #58476

wants to merge 7 commits into from

Conversation

asi1024
Copy link
Contributor

@asi1024 asi1024 commented May 18, 2021

Fixes #58035.

This PR implements torch.Tensor.__rmod__ and torch.remainder(scalar, tensor) for the compatibility with NumPy’s interface.
(cc: @mruberry, @rgommers, @emcastillo, @kmaehashi)

TODO:

@facebook-github-bot
Copy link
Contributor

facebook-github-bot commented May 18, 2021

💊 CI failures summary and remediations

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


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

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 to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

@asi1024 asi1024 changed the title Support __rmod__ [WIP] Support __rmod__ May 18, 2021
@ezyang ezyang removed their request for review May 18, 2021 15:05
@codecov
Copy link

codecov bot commented May 19, 2021

Codecov Report

Merging #58476 (f625897) into master (ccd7141) will increase coverage by 0.03%.
The diff coverage is 87.21%.

❗ Current head f625897 differs from pull request most recent head e6c283c. Consider uploading reports for the commit e6c283c to get more accurate results

@@            Coverage Diff             @@
##           master   #58476      +/-   ##
==========================================
+ Coverage   76.44%   76.47%   +0.03%     
==========================================
  Files        1990     1992       +2     
  Lines      199690   199895     +205     
==========================================
+ Hits       152651   152868     +217     
+ Misses      47039    47027      -12     

Copy link
Collaborator

@mruberry mruberry left a comment

Choose a reason for hiding this comment

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

Cool! This is great Python Array API compatibility, @asi1024

This is going to need a "forward" test to validate that the operation computes what it should when give a scalar x tensor input.

We have some tests for remainder here:

def test_fmod_remainder(self, device, dtype):

That can probably be extended

cc @rgommers, looks like the Python Array API doesn't elaborate on rmod, but it might want to be more explicit about whether it should call fmod or remainder. NumPy's rmod looks like it calls remainder, so I assume that's what's intended?

sample_inputs_func=sample_inputs_rbinops,
supports_out=False,
skips=(SkipInfo('TestCommon', 'test_variant_consistency_jit',),),
supports_autograd=False,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting... why doesn't this support autograd? torch.remainder supports autograd typically, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

torch.remainder seems not to support autograd of the second argument currently.

>>> x1 = torch.tensor([3.], requires_grad=True)
>>> x2 = torch.tensor([5.], requires_grad=True)
>>> y = x1 % x2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: the derivative for 'other' is not implemented

Copy link
Collaborator

@mruberry mruberry May 23, 2021

Choose a reason for hiding this comment

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

Interesting. Would you add a comment to that effect here, too?

@rgommers
Copy link
Collaborator

cc @rgommers, looks like the Python Array API doesn't elaborate on rmod, but it might want to be more explicit about whether it should call fmod or remainder. NumPy's rmod looks like it calls remainder, so I assume that's what's intended?

__mod__ has a note saying it uses remainder: https://data-apis.org/array-api/latest/API_specification/array_object.html#mod-self-other. So __rmod__ should do the same.

@asi1024
Copy link
Contributor Author

asi1024 commented May 21, 2021

This is going to need a "forward" test to validate that the operation computes what it should when give a scalar x tensor input.

@mruberry I added torch.remainder(Scalar, Tensor) forward test now!

@asi1024 asi1024 changed the title [WIP] Support __rmod__ Support __rmod__ May 21, 2021
@H-Huang H-Huang added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 21, 2021
for fn, inplace_fn, ref_fn in fns_list:
np_x = x.cpu().numpy()
np_x = x.cpu().numpy() if torch.is_tensor(x) else x
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice update

((torch.fmod, torch.Tensor.fmod_, np.fmod),
(torch.remainder, torch.Tensor.remainder_, np.remainder),))

for dividend, mod in product([5, 3.14], mods):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add a comment here that this tests the scalar x tensor case

Copy link
Collaborator

@mruberry mruberry left a comment

Choose a reason for hiding this comment

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

This is looking good, @asi1024!; I have some small suggestions to add comments

Also, it looks like the tests didn't run, which is odd. Maybe pushing the comment updates will trigger them properly? Once we see the tests passing I think this is good to go!

@asi1024
Copy link
Contributor Author

asi1024 commented May 24, 2021

@mruberry CI seems to have failed in TestNNCOpInfoCPU::test_unsupported___rmod___cpu_float32 test, but this test case is skipped in my local. Do you know what should I do to reproduce the test failure in my environment? Here is the test log of pytest.

test/test_jit_fuser_te.py::TestNNCOpInfoCPU::test_unsupported___rmod___cpu_float32 SKIPPED (Compiles with TensorExprKernel)

@mruberry
Copy link
Collaborator

@mruberry CI seems to have failed in TestNNCOpInfoCPU::test_unsupported___rmod___cpu_float32 test, but this test case is skipped in my local. Do you know what should I do to reproduce the test failure in my environment? Here is the test log of pytest.

test/test_jit_fuser_te.py::TestNNCOpInfoCPU::test_unsupported___rmod___cpu_float32 SKIPPED (Compiles with TensorExprKernel)

Before bothering to reproduce it, what about doing what it suggests and adding dunder rmod to the list here?

works_list = [

@asi1024
Copy link
Contributor Author

asi1024 commented May 25, 2021

@mruberry Thanks! I fixed it as you commented and confirmed the test has passed!
The remaining CI failure is due to exceeding the time limit without output. Will this be green if CI is retriggered?

@mruberry
Copy link
Collaborator

@mruberry Thanks! I fixed it as you commented and confirmed the test has passed!
The remaining CI failure is due to exceeding the time limit without output. Will this be green if CI is retriggered?

Thank you, @asi1024. And don't worry about the remaining failure: it's unrelated. Also, if I don't respond to you in two business days please ping me. Sometimes notifications are lost. I'm sorry you waited so long for a response. I'll review the PR now, too.

Copy link
Collaborator

@mruberry mruberry left a comment

Choose a reason for hiding this comment

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

Great work, @asi1024! And PyTorch is one step closer to supporting the Python Array API!

@facebook-github-bot
Copy link
Contributor

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

@asi1024
Copy link
Contributor Author

asi1024 commented May 29, 2021

@mruberry Thanks! 😃

@mruberry
Copy link
Collaborator

Interestingly this hit an internal error:

stderr: caffe2/aten/src/ATen/native/BinaryOps.cpp:633:10: error: no matching function for call to 'remainder'
  return native::remainder(wrapped_scalar_tensor(self), other);
         ^~~~~~~~~~~~~~~~~
stderr: caffe2/aten/src/ATen/native/BinaryOps.cpp:633:10: error: no matching function for call to 'remainder'
  return native::remainder(wrapped_scalar_tensor(self), other);

maybe change the namespace to at?

@asi1024
Copy link
Contributor Author

asi1024 commented May 31, 2021

Fixed the namespace and rebase, but the CI failed with rocm build failure. The message No problems were identified. is left. Could you help me figure out why it's failing?

@facebook-github-bot
Copy link
Contributor

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

@mruberry
Copy link
Collaborator

No worries about the ROCm failure, it's in the base. We fixed it yesterday. Running this through internal tests now.

@mruberry
Copy link
Collaborator

mruberry commented Jun 5, 2021

Internal tests look good; landing this now

@facebook-github-bot
Copy link
Contributor

@mruberry merged this pull request in 0a5bfa9.

@asi1024
Copy link
Contributor Author

asi1024 commented Jun 6, 2021

@mruberry Thanks for your review!

@asi1024 asi1024 deleted the rmod branch June 6, 2021 09:46
deniskokarev pushed a commit to deniskokarev/pytorch that referenced this pull request Jun 9, 2021
Summary:
Fixes pytorch#58035.

This PR implements `torch.Tensor.__rmod__` and `torch.remainder(scalar, tensor)` for the compatibility with NumPy’s interface.
(cc: mruberry, rgommers, emcastillo, kmaehashi)

TODO:
  - [x] Update `tensor_binary_op` in test/test_binary_ufuncs.py after pytorch#58216 is merged.

Pull Request resolved: pytorch#58476

Reviewed By: ngimel

Differential Revision: D28776810

Pulled By: mruberry

fbshipit-source-id: 74f8aea80f439ef2cc370333524e39971eeb7bf4
@pmeier pmeier added the module: python array api Issues related to the Python Array API label Jun 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed Merged module: python array api Issues related to the Python Array API 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.

Support __rmod__
7 participants