x = torch.randn(10, device=device).mul(30).to(dtype)
y = torch.arange(1, 11, dtype=dtype, device=device)
- with self.assertWarnsOnceRegex(UserWarning, "floor_divide"):
+ with self.assertWarnsOnceRegex(UserWarning, "__floordiv__"):
z = x // y
z_alt = torch.trunc(x.double() / y.double()).to(dtype)
def test_floor_divide_scalar(self, device, dtype):
x = torch.randn(100, device=device).mul(10).to(dtype)
- with self.assertWarnsOnceRegex(UserWarning, "floor_divide"):
+ with self.assertWarnsOnceRegex(UserWarning, "__floordiv__"):
z = x // 3
z_alt = torch.tensor([math.trunc(v.item() / 3.) for v in x], dtype=x.dtype, device=device)
self.assertEqual(self.safeToDense(y1), expected)
self.assertEqual(self.safeToDense(y2), expected)
- with self.assertWarnsOnceRegex(UserWarning, 'floor_divide'):
+ with self.assertWarnsOnceRegex(UserWarning, '__floordiv__'):
y1 = x1 // 37.5
y2 = x1.clone()
with self.assertWarnsOnceRegex(UserWarning, 'floor_divide'):
/ torch.tensor(1., device=device).to_sparse())
def test_floor_divide_by_sparse_error(self, device):
- self.assertRaisesRegex(RuntimeError, 'Sparse floor division requires',
+ self.assertRaisesRegex(RuntimeError, 'Sparse division requires',
lambda: torch.tensor(1., device=device).to_sparse()
// torch.tensor(1., device=device).to_sparse())
@_wrap_type_error_to_not_implemented
def __floordiv__(self, other):
- return torch.floor_divide(self, other)
+ warnings.warn("__floordiv__ is deprecated, and its behavior will change in a future version of pytorch. "
+ "It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). "
+ "This results in incorrect rounding for negative values. "
+ "To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), "
+ "or for actual floor division, use torch.div(a, b, rounding_mode='floor').", stacklevel=3)
+ return torch.div(self, other, rounding_mode='trunc')
@_wrap_type_error_to_not_implemented
def __rfloordiv__(self, other):
- return torch.floor_divide(other, self)
+ warnings.warn("__rfloordiv__ is deprecated, and its behavior will change in a future version of pytorch. "
+ "It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). "
+ "This results in incorrect rounding for negative values. "
+ "To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), "
+ "or for actual floor division, use torch.div(a, b, rounding_mode='floor').", stacklevel=3)
+ return torch.div(other, self, rounding_mode='trunc')
@_wrap_type_error_to_not_implemented
def __rlshift__(self, other):