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

Feature Request: support int->float type promotion for torch.reciprocal #49091

Closed
mruberry opened this issue Dec 9, 2020 · 1 comment
Closed
Assignees
Labels
triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@mruberry
Copy link
Collaborator

mruberry commented Dec 9, 2020

torch.reciprocal does not support integer inputs today:

t = torch.tensor((1, 2, 3))
print(torch.reciprocal(t))
: RuntimeError: "reciprocal_cpu" not implemented for 'Long'

This is because reciprocal's codomain for the integers is not the integers, and historically PyTorch did not implement integer->floating point type promotion for unary ufuncs. This has changed, however. For example, torch.sin() promotes integer inputs to float:

t = torch.tensor((1, 2, 3))
print(torch.sin(t))
: tensor([0.8415, 0.9093, 0.1411])

And this is compatible with NumPy's behavior:

a = np.array((1, 2, 3))
print(np.sin(a))
: [0.84147098 0.90929743 0.14112001]

PyTorch has a pattern for implementing this behavior. See for example, this PR: #48644. In addition to implementing this behavior a UnaryUfuncInfo should be added for torch.reciprocal, and other tests of it that are made redundant removed.

Note that NumPy's np.reciprocal() is bugged (numpy/numpy#10374) and does not promote integer inputs to float:

a = np.array((1, 2, 3))
print(np.reciprocal(a))
: [1 0 0]
@mruberry mruberry added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Dec 9, 2020
@kshitij12345
Copy link
Collaborator

Reference Issue: #42515

hwangdeyu pushed a commit to hwangdeyu/pytorch that referenced this issue Jan 6, 2021
Summary:
Fixes pytorch#49091

Pull Request resolved: pytorch#49102

Reviewed By: VitalyFedyunin

Differential Revision: D25639541

Pulled By: soulitzer

fbshipit-source-id: 1dd360bd7b77f106d606143d8d3961610bac8cb7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants