Removes dunder div (#39151)

Summary:
BC-breaking note:

If a user is using one of these dunders directly they will not longer be available. Users should update to Python3 compatible dunders.

Original PR note:

`__div__` (and `__idiv__` and `__rdiv__`) are no longer special dunders in Python3. This PR replaces them with the `__truediv__` (`__itrudediv__`, `__rtruediv__`) dunders, since we no longer support Python2.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/39151

Differential Revision: D22075713

Pulled By: mruberry

fbshipit-source-id: d318b47b51f7cc4c3728b1606a34d81e49ba0fa1
This commit is contained in:
Mike Ruberry 2020-06-16 23:00:55 -07:00 committed by Facebook GitHub Bot
parent 00505adbad
commit 9d588f7ce2
7 changed files with 10 additions and 15 deletions

View file

@ -119,7 +119,6 @@ Ops that can autocast to ``float32``
""""""""""""""""""""""""""""""""""""
``__pow__``,
``__rdiv__``,
``__rpow__``,
``__rtruediv__``,
``acos``,

View file

@ -4325,7 +4325,7 @@ separate_complex_tests = ['log', 'log10', 'log1p', 'log2', 'reciprocal', 'tan']
complex_list = ['t', 'view', 'reshape', 'reshape_as', 'view_as', 'zero_', 'clone',
'tril', 'triu', 'fill_', 'eq_', 'ne_', 'permute', 'squeeze', 'unsqueeze',
'chunk', 'split', 'split_with_sizes', 'resize', 'resize_as', 'sin', 'cos',
'__rmul__', '__rdiv__', 'sum', 'transpose', 'round', 'add', 'roll',
'__rmul__', '__rtruediv__', 'sum', 'transpose', 'round', 'add', 'roll',
'__radd__', 'repeat', 'expand', 'mul', 'tanh', 'flip', 'fliplr', 'flipud',
'rot90'] + separate_complex_tests

View file

@ -137,8 +137,8 @@ def doAutodiffCheck(testname):
'test_nn_batch_norm',
'test_nn_max_pool2d_with_indices',
# AutogradJitGenerated
'test___rdiv___constant',
'test___rdiv___scalar_constant',
'test___rtruediv___constant',
'test___rtruediv___scalar_constant',
'test_split',
'test_split_dim',
'test_split_dim_neg0',
@ -17840,8 +17840,8 @@ class TestJitGeneratedFunctional(JitTestCase):
# UBSAN per-function exclusions don't seem to work with OpenMP pragmas,
# and we have to disable the failing tests here instead.
UBSAN_BLACKLISTED_TESTS = [
"test___rdiv___constant",
"test___rdiv___scalar_constant",
"test___rtruediv___constant",
"test___rtruediv___scalar_constant",
"test_addcdiv",
"test_addcdiv_broadcast_all",
"test_addcdiv_broadcast_rhs",

View file

@ -929,10 +929,9 @@ PyMethodDef variable_methods[] = {
{"__imul__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_<THPVariable_mul_>, METH_VARARGS | METH_KEYWORDS, NULL},
{"__sub__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_<THPVariable_sub>, METH_VARARGS | METH_KEYWORDS, NULL},
{"__isub__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_<THPVariable_sub_>, METH_VARARGS | METH_KEYWORDS, NULL},
{"__div__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_<THPVariable_div>, METH_VARARGS | METH_KEYWORDS, NULL},
{"__truediv__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_<THPVariable_div>, METH_VARARGS | METH_KEYWORDS, NULL},
{"__itruediv__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_<THPVariable_div_>, METH_VARARGS | METH_KEYWORDS, NULL},
{"__floordiv__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_<THPVariable_floor_divide>, METH_VARARGS | METH_KEYWORDS, NULL},
{"__idiv__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_<THPVariable_div_>, METH_VARARGS | METH_KEYWORDS, NULL},
{"__ifloordiv__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_<THPVariable_floor_divide_>, METH_VARARGS | METH_KEYWORDS, NULL},
{"__mod__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_<THPVariable_remainder>, METH_VARARGS | METH_KEYWORDS, NULL},
{"__bool__", (PyCFunction)THPVariable_bool_scalar, METH_NOARGS, NULL},

View file

@ -393,15 +393,12 @@ class Tensor(torch._C._TensorBase):
def __rsub__(self, other):
return _C._VariableFunctions.rsub(self, other)
def __rdiv__(self, other):
def __rtruediv__(self, other):
if self.dtype.is_floating_point or self.dtype.is_complex:
return self.reciprocal() * other
else:
return (self.double().reciprocal() * other).type_as(self)
__rtruediv__ = __rdiv__
__itruediv__ = _C._TensorBase.__idiv__
__pow__ = _C._TensorBase.pow
def __format__(self, format_spec):

View file

@ -55,7 +55,7 @@ class AutocastTestLists(object):
("__lt__", pointwise0_fp32 + pointwise1_fp16, torch.bool),
("__ne__", pointwise0_fp32 + pointwise1_fp16, torch.bool),
("__add__", pointwise0_fp32 + pointwise1_fp16, torch.float32),
("__div__", pointwise0_fp32 + pointwise1_fp16, torch.float32),
("__truediv__", pointwise0_fp32 + pointwise1_fp16, torch.float32),
("__mul__", pointwise0_fp32 + pointwise1_fp16, torch.float32),
]

View file

@ -163,10 +163,10 @@ def method_tests():
('div', (S, S, S), (uniform_scalar(0.1),), 'scalar_broadcast_rhs', (True,)),
('div', (), (uniform_scalar(0.1),), 'scalar_broadcast_lhs', (True,)),
('div', torch.rand(S, S, S) + 1e-1, (3.14,), 'constant', (True,)),
('__rdiv__', torch.rand(S, S, S) + 1e-1, (3.14,), 'constant',
('__rtruediv__', torch.rand(S, S, S) + 1e-1, (3.14,), 'constant',
(True, [], ['aten::mul', 'aten::reciprocal'])),
('div', uniform_scalar(1e-1, requires_grad=True), (3.14,), 'scalar_constant', (True,)),
('__rdiv__', uniform_scalar(1e-1, requires_grad=True), (3.14,), 'scalar_constant',
('__rtruediv__', uniform_scalar(1e-1, requires_grad=True), (3.14,), 'scalar_constant',
(True, [], ['aten::mul', 'aten::reciprocal'])),
('pow', torch.rand(S, S, S) + 1e-3, (torch.rand(S, S, S) + 0.1,), '', (True,)),
('pow', torch.rand(S, S, S) + 1e-3, (torch.rand(1,) + 0.1,), 'broadcast_rhs', (True,)),