From 9d588f7ce26775ffad0035dcc4c1128d62b889fc Mon Sep 17 00:00:00 2001 From: Mike Ruberry <38511765+mruberry@users.noreply.github.com> Date: Tue, 16 Jun 2020 23:00:55 -0700 Subject: [PATCH] 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 --- docs/source/amp.rst | 1 - test/test_autograd.py | 2 +- test/test_jit.py | 8 ++++---- tools/autograd/templates/python_variable_methods.cpp | 3 +-- torch/tensor.py | 5 +---- torch/testing/_internal/autocast_test_lists.py | 2 +- torch/testing/_internal/common_methods_invocations.py | 4 ++-- 7 files changed, 10 insertions(+), 15 deletions(-) diff --git a/docs/source/amp.rst b/docs/source/amp.rst index ef50caaa976..2f7f9a27ae2 100644 --- a/docs/source/amp.rst +++ b/docs/source/amp.rst @@ -119,7 +119,6 @@ Ops that can autocast to ``float32`` """""""""""""""""""""""""""""""""""" ``__pow__``, -``__rdiv__``, ``__rpow__``, ``__rtruediv__``, ``acos``, diff --git a/test/test_autograd.py b/test/test_autograd.py index 04bc21e7988..3c4ffae0aa6 100644 --- a/test/test_autograd.py +++ b/test/test_autograd.py @@ -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 diff --git a/test/test_jit.py b/test/test_jit.py index 7b43021dab1..59ad24d41db 100644 --- a/test/test_jit.py +++ b/test/test_jit.py @@ -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", diff --git a/tools/autograd/templates/python_variable_methods.cpp b/tools/autograd/templates/python_variable_methods.cpp index d135354ae94..96341bb7625 100644 --- a/tools/autograd/templates/python_variable_methods.cpp +++ b/tools/autograd/templates/python_variable_methods.cpp @@ -929,10 +929,9 @@ PyMethodDef variable_methods[] = { {"__imul__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_, METH_VARARGS | METH_KEYWORDS, NULL}, {"__sub__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_, METH_VARARGS | METH_KEYWORDS, NULL}, {"__isub__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_, METH_VARARGS | METH_KEYWORDS, NULL}, - {"__div__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_, METH_VARARGS | METH_KEYWORDS, NULL}, {"__truediv__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_, METH_VARARGS | METH_KEYWORDS, NULL}, + {"__itruediv__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_, METH_VARARGS | METH_KEYWORDS, NULL}, {"__floordiv__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_, METH_VARARGS | METH_KEYWORDS, NULL}, - {"__idiv__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_, METH_VARARGS | METH_KEYWORDS, NULL}, {"__ifloordiv__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_, METH_VARARGS | METH_KEYWORDS, NULL}, {"__mod__", (PyCFunction)(void(*)(void))TypeError_to_NotImplemented_, METH_VARARGS | METH_KEYWORDS, NULL}, {"__bool__", (PyCFunction)THPVariable_bool_scalar, METH_NOARGS, NULL}, diff --git a/torch/tensor.py b/torch/tensor.py index 61726fda609..e3615140d33 100644 --- a/torch/tensor.py +++ b/torch/tensor.py @@ -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): diff --git a/torch/testing/_internal/autocast_test_lists.py b/torch/testing/_internal/autocast_test_lists.py index 953813fe8e9..2bfc17c8da3 100644 --- a/torch/testing/_internal/autocast_test_lists.py +++ b/torch/testing/_internal/autocast_test_lists.py @@ -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), ] diff --git a/torch/testing/_internal/common_methods_invocations.py b/torch/testing/_internal/common_methods_invocations.py index db4a48b8c11..274a60cd4b0 100644 --- a/torch/testing/_internal/common_methods_invocations.py +++ b/torch/testing/_internal/common_methods_invocations.py @@ -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,)),