Skip to content

Some torch.Tensor operators don’t return NotImplemented for invalid inputs #57719

@asi1024

Description

@asi1024

🐛 Bug

Some torch.Tensor operators

  1. don’t return NotImplemented and raise TypeError, or
  2. are not defined whereas expected to be defined.

(cc/ @mruberry, @emcastillo, @kmaehashi)

To Reproduce

  1. For example, in the following case, we expect torch.Tensor.__pow__ returns NotImplemented and A.__rpow__ is called
>>> class A:
...     def __init__(self, x):
...         self.x = x
...     def __radd__(self, other):
...         print('A.radd')
...     def __rpow__(self, other):
...         print('A.rpow')
...
>>> torch.tensor([1]) + A(1)
A.radd
>>> torch.tensor([1]) ** A(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: pow() received an invalid combination of arguments - got (A), but expected one of:
 * (Tensor exponent)
      didn't match because some of the arguments have invalid types: (A)
 * (Number exponent)
      didn't match because some of the arguments have invalid types: (A)

The expected behavior is:

>>> torch.tensor([1]) + A(1)
A.radd
>>> torch.tensor([1]) ** A(1)
A.rpow
  1. The following example shows that torch.Tensor.__rand__ is not registered
>>> torch.tensor([1]) & 1
tensor([1])
>>> 1 & torch.tensor([1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'int' and 'Tensor'

The expected behavior is:

>>> torch.tensor([1]) & 1
tensor([1])
>>> 1 & torch.tensor([1])
tensor([1])

Environment

  • PyTorch 1.8.1+cu102 installed via pip
  • OS: Linux
  • Python version: 3.9.1

Additional context

This issue is found on #56997.

cc @mruberry @rgommers @pmeier

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNot as big of a feature, but technically not a bug. Should be easy to fixmodule: pybindRelated to our Python bindings / interactions with other Python librariesmodule: python array apiIssues related to the Python Array APItriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions