@@ -164,7 +164,7 @@ def _numel(x):
164
164
# Assumes x is a scalar
165
165
return 1
166
166
167
- if _numel (l ) < 10 and _numel (r ) < 10 :
167
+ if _numel (l ) <= 100 and _numel (r ) <= 100 :
168
168
msg = (
169
169
"Failed to produce expected results! Input lhs tensor was"
170
170
" {0}, rhs tensor was {1}, torch result is {2}, and reference result is"
@@ -1261,8 +1261,7 @@ def test_inplace_dunders(self, device):
1261
1261
t *= 1
1262
1262
t /= 1
1263
1263
t **= 1
1264
- with self .assertWarnsOnceRegex (UserWarning , "floor_divide" ):
1265
- t //= 1
1264
+ t //= 1
1266
1265
t %= 1
1267
1266
self .assertEqual (expected , t .data_ptr ())
1268
1267
@@ -1902,8 +1901,6 @@ def test_binary_op_scalar_device_unspecified(self, devices):
1902
1901
def test_div_and_floordiv_vs_python (self , device ):
1903
1902
# Tests torch division ops which can handle both arguments being
1904
1903
# scalars.
1905
- # NOTE: torch.floor_divide currently truncates instead of flooring.
1906
- # the quotient. See https://github.com/pytorch/pytorch/issues/43874.
1907
1904
def _scalar_helper (python_op , torch_op ):
1908
1905
for a , b in product (range (- 10 , 10 ), range (- 10 , 10 )):
1909
1906
for op in (lambda x : x * 0.5 , lambda x : math .floor (x )):
@@ -1926,19 +1923,16 @@ def _scalar_helper(python_op, torch_op):
1926
1923
actual_first_tensor = torch_op (a_t , b )
1927
1924
actual_second_tensor = torch_op (a , b_t )
1928
1925
1929
- self .assertEqual (actual_scalar , expected_div )
1930
- self .assertEqual (actual_tensor .item (), expected_div )
1926
+ self .assertEqual (actual_scalar , expected )
1927
+ self .assertEqual (actual_tensor .item (), expected )
1931
1928
self .assertEqual (actual_first_tensor , actual_tensor )
1932
1929
self .assertEqual (actual_second_tensor , actual_tensor )
1933
1930
1934
1931
_scalar_helper (operator .truediv , operator .truediv )
1935
1932
_scalar_helper (operator .truediv , torch .true_divide )
1936
- with self .assertWarnsOnceRegex (UserWarning , "floor_divide" ):
1937
- _scalar_helper (lambda a , b : math .trunc (a / b ), operator .floordiv )
1938
- _scalar_helper (lambda a , b : math .trunc (a / b ), torch .floor_divide )
1933
+ _scalar_helper (lambda a , b : math .floor (a / b ), operator .floordiv )
1934
+ _scalar_helper (lambda a , b : math .floor (a / b ), torch .floor_divide )
1939
1935
1940
- # NOTE: torch.floor_divide currently truncates instead of flooring.
1941
- # See https://github.com/pytorch/pytorch/issues/43874.
1942
1936
@onlyNativeDeviceTypes
1943
1937
def test_div_and_floordiv_script_vs_python (self , device ):
1944
1938
# Creates jitted functions of two tensors
@@ -1960,13 +1954,12 @@ def _wrapped_floordiv(a, b):
1960
1954
continue
1961
1955
1962
1956
expected_div = a / b
1963
- expected_truncdiv = math .trunc (a / b )
1957
+ expected_floordiv = math .floor (a / b )
1964
1958
a_t = torch .tensor (a , device = device )
1965
1959
b_t = torch .tensor (b , device = device )
1966
1960
1967
1961
self .assertEqual (scripted_div (a_t , b_t ), expected_div )
1968
- with self .assertWarnsOnceRegex (UserWarning , "floor_divide" ):
1969
- self .assertEqual (scripted_floordiv (a_t , b_t ), expected_truncdiv )
1962
+ self .assertEqual (scripted_floordiv (a_t , b_t ), expected_floordiv )
1970
1963
1971
1964
# Creates jitted functions of one tensor
1972
1965
def _wrapped_div_scalar (a ):
@@ -1996,8 +1989,6 @@ def _wrapped_rfloordiv_scalar(a):
1996
1989
a_t = torch .tensor (a , device = device )
1997
1990
1998
1991
self .assertEqual (a / 5 , scripted_div_scalar (a_t ))
1999
- with self .assertWarnsOnceRegex (UserWarning , "floor_divide" ):
2000
- self .assertEqual (math .trunc (a / 5 ), scripted_floordiv_scalar (a_t ))
2001
1992
2002
1993
# Skips zero divisors
2003
1994
if a == 0 :
@@ -2014,8 +2005,6 @@ def _wrapped_rfloordiv_scalar(a):
2014
2005
# See issue gh-52387
2015
2006
self .assertEqual (5 // a , scripted_rfloordiv_scalar (a_t ))
2016
2007
2017
- # NOTE: torch.floor_divide currently truncates instead of flooring
2018
- # the quotient. See https://github.com/pytorch/pytorch/issues/43874.
2019
2008
@onlyNativeDeviceTypes
2020
2009
def test_idiv_and_ifloordiv_vs_python (self , device ):
2021
2010
def _wrapped_idiv_tensor (a , b ):
@@ -2075,7 +2064,6 @@ def _wrapped_ifloordiv_scalar(a):
2075
2064
2076
2065
expected_idiv = a / b
2077
2066
expected_ifloordiv = a // b
2078
- expected_itruncdiv = math .trunc (a / b )
2079
2067
2080
2068
a_t = torch .tensor (a , device = device )
2081
2069
b_t = torch .tensor (b , device = device )
@@ -2110,39 +2098,27 @@ def _wrapped_ifloordiv_scalar(a):
2110
2098
if not a_t .is_floating_point () and b_t .is_floating_point ():
2111
2099
# Inplace modification fails because a float tensor is required
2112
2100
# if the divisor is a float tensor
2113
- with self .assertRaises (RuntimeError ), self .assertWarnsOnceRegex (
2114
- UserWarning , "floor_divide"
2115
- ):
2116
- a_t .clone ().floor_divide_ (b_t )
2117
- with self .assertRaises (RuntimeError ), self .assertWarnsOnceRegex (
2118
- UserWarning , "floor_divide"
2119
- ):
2120
- scripted_floor_divide_tensor (a_t .clone (), b_t )
2101
+ a_t .clone ().floor_divide_ (b_t )
2102
+ scripted_floor_divide__tensor (a_t .clone (), b_t )
2121
2103
tmp = a_t .clone ()
2122
- with self .assertRaises (RuntimeError ), self .assertWarnsOnceRegex (
2123
- UserWarning , "floor_divide"
2124
- ):
2125
- tmp //= b_t
2104
+ tmp //= b_t
2126
2105
else :
2127
2106
# Inplace modification is OK when both or neither tensor is
2128
2107
# a float tensor
2129
- with self .assertWarnsOnceRegex (UserWarning , "floor_divide" ):
2130
- self .assertEqual (
2131
- a_t .clone ().floor_divide_ (b_t ).item (), expected_itruncdiv
2132
- )
2133
- self .assertEqual (
2134
- scripted_floor_divide__tensor (a_t .clone (), b_t ).item (),
2135
- expected_itruncdiv ,
2136
- )
2137
- tmp = a_t .clone ()
2138
- with self .assertWarnsOnceRegex (UserWarning , "floor_divide" ):
2139
- tmp //= b_t
2140
- self .assertEqual (tmp .item (), expected_itruncdiv )
2141
-
2142
- with self .assertWarnsOnceRegex (UserWarning , "floor_divide" ):
2143
2108
self .assertEqual (
2144
- scripted_floor_divide__scalar (a_t ), math .trunc (a / 5 )
2109
+ a_t .clone ().floor_divide_ (b_t ).item (), expected_ifloordiv
2110
+ )
2111
+ self .assertEqual (
2112
+ scripted_floor_divide__tensor (a_t .clone (), b_t ).item (),
2113
+ expected_ifloordiv ,
2145
2114
)
2115
+ tmp = a_t .clone ()
2116
+ tmp //= b_t
2117
+ self .assertEqual (tmp .item (), expected_ifloordiv )
2118
+
2119
+ self .assertEqual (
2120
+ scripted_floor_divide__scalar (a_t ), math .floor (a / 5 )
2121
+ )
2146
2122
2147
2123
# Tests binary op equivalence with Python builtin ops
2148
2124
# Also tests that reverse operations are equivalent to forward ops
@@ -2747,9 +2723,8 @@ def test_floor_divide_tensor(self, device, dtype):
2747
2723
x = torch .randn (10 , device = device ).mul (30 ).to (dtype )
2748
2724
y = torch .arange (1 , 11 , dtype = dtype , device = device )
2749
2725
2750
- with self .assertWarnsOnceRegex (UserWarning , "__floordiv__" ):
2751
- z = x // y
2752
- z_alt = torch .trunc (x .double () / y .double ()).to (dtype )
2726
+ z = x // y
2727
+ z_alt = torch .floor (x .double () / y .double ()).to (dtype )
2753
2728
2754
2729
self .assertEqual (z .dtype , x .dtype )
2755
2730
self .assertEqual (z , z_alt )
@@ -2761,36 +2736,14 @@ def test_floor_divide_tensor(self, device, dtype):
2761
2736
def test_floor_divide_scalar (self , device , dtype ):
2762
2737
x = torch .randn (100 , device = device ).mul (10 ).to (dtype )
2763
2738
2764
- with self .assertWarnsOnceRegex (UserWarning , "__floordiv__" ):
2765
- z = x // 3
2739
+ z = x // 3
2766
2740
z_alt = torch .tensor (
2767
- [math .trunc (v .item () / 3.0 ) for v in x ], dtype = x .dtype , device = device
2741
+ [math .floor (v .item () / 3.0 ) for v in x ], dtype = x .dtype , device = device
2768
2742
)
2769
2743
2770
2744
self .assertEqual (z .dtype , x .dtype )
2771
2745
self .assertEqual (z , z_alt )
2772
2746
2773
- # Note: this tests fails on XLA
2774
- @onlyNativeDeviceTypes
2775
- @dtypes (torch .float , torch .long )
2776
- def test_floor_divide_out (self , device , dtype ):
2777
- x = torch .randn (10 , device = device ).mul (10 ).to (dtype )
2778
- y = torch .arange (1 , 11 , dtype = dtype , device = device )
2779
- o = torch .empty (10 , dtype = dtype , device = device )
2780
-
2781
- with self .assertWarnsOnceRegex (UserWarning , "floor_divide" ):
2782
- torch .floor_divide (x , y , out = o )
2783
- self .assertEqual (o , x // y )
2784
-
2785
- # Tests scalar with out
2786
- torch .floor_divide (x , 2 , out = o )
2787
- self .assertEqual (o , x // 2 )
2788
-
2789
- if dtype == torch .int :
2790
- o = torch .empty (10 , dtype = torch .float , device = device )
2791
- torch .floor_divide (x , y , out = o )
2792
- self .assertEqual (o , torch .floor_divide (x .float (), y .float ()))
2793
-
2794
2747
@onlyCPU
2795
2748
@dtypes (* get_all_math_dtypes ("cpu" ))
2796
2749
def test_rdiv (self , device , dtype ):
0 commit comments