6
6
from tests .third_party .cupy import testing
7
7
8
8
9
- # Note: numpy.sum() always upcast integers to (u)int64 and float32 to
10
- # float64 for dtype=None. `np.sum` does that too for integers, but not for
11
- # float32, so we need to special-case it for these tests
12
- def _get_dtype_kwargs (xp , dtype ):
13
- if xp is numpy and dtype == numpy .float32 and has_support_aspect64 ():
14
- return {"dtype" : numpy .float64 }
15
- return {}
16
-
17
-
18
9
class TestSumprod :
19
10
def tearDown (self ):
20
11
# Free huge memory for slow test
@@ -26,43 +17,43 @@ def tearDown(self):
26
17
@testing .numpy_cupy_allclose ()
27
18
def test_sum_all (self , xp , dtype ):
28
19
a = testing .shaped_arange ((2 , 3 , 4 ), xp , dtype )
29
- return a .sum (** _get_dtype_kwargs ( xp , dtype ) )
20
+ return a .sum ()
30
21
31
22
@testing .for_all_dtypes ()
32
23
@testing .numpy_cupy_allclose ()
33
24
def test_sum_all_keepdims (self , xp , dtype ):
34
25
a = testing .shaped_arange ((2 , 3 , 4 ), xp , dtype )
35
- return a .sum (** _get_dtype_kwargs ( xp , dtype ), keepdims = True )
26
+ return a .sum (keepdims = True )
36
27
37
28
@testing .for_all_dtypes ()
38
29
@testing .numpy_cupy_allclose ()
39
30
def test_external_sum_all (self , xp , dtype ):
40
31
a = testing .shaped_arange ((2 , 3 , 4 ), xp , dtype )
41
- return xp .sum (a , ** _get_dtype_kwargs ( xp , dtype ) )
32
+ return xp .sum (a )
42
33
43
34
@testing .for_all_dtypes ()
44
35
@testing .numpy_cupy_allclose (rtol = 1e-06 )
45
36
def test_sum_all2 (self , xp , dtype ):
46
37
a = testing .shaped_arange ((20 , 30 , 40 ), xp , dtype )
47
- return a .sum (** _get_dtype_kwargs ( xp , dtype ) )
38
+ return a .sum ()
48
39
49
40
@testing .for_all_dtypes ()
50
41
@testing .numpy_cupy_allclose ()
51
42
def test_sum_all_transposed (self , xp , dtype ):
52
43
a = testing .shaped_arange ((2 , 3 , 4 ), xp , dtype ).transpose (2 , 0 , 1 )
53
- return a .sum (** _get_dtype_kwargs ( xp , dtype ) )
44
+ return a .sum ()
54
45
55
46
@testing .for_all_dtypes ()
56
47
@testing .numpy_cupy_allclose (rtol = 1e-06 )
57
48
def test_sum_all_transposed2 (self , xp , dtype ):
58
49
a = testing .shaped_arange ((20 , 30 , 40 ), xp , dtype ).transpose (2 , 0 , 1 )
59
- return a .sum (** _get_dtype_kwargs ( xp , dtype ) )
50
+ return a .sum ()
60
51
61
52
@testing .for_all_dtypes ()
62
53
@testing .numpy_cupy_allclose ()
63
54
def test_sum_axis (self , xp , dtype ):
64
55
a = testing .shaped_arange ((2 , 3 , 4 ), xp , dtype )
65
- return a .sum (** _get_dtype_kwargs ( xp , dtype ), axis = 1 )
56
+ return a .sum (axis = 1 )
66
57
67
58
@testing .slow
68
59
@testing .numpy_cupy_allclose ()
@@ -74,57 +65,57 @@ def test_sum_axis_huge(self, xp):
74
65
@testing .numpy_cupy_allclose ()
75
66
def test_external_sum_axis (self , xp , dtype ):
76
67
a = testing .shaped_arange ((2 , 3 , 4 ), xp , dtype )
77
- return xp .sum (a , ** _get_dtype_kwargs ( xp , dtype ), axis = 1 )
68
+ return xp .sum (a , axis = 1 )
78
69
79
70
# float16 is omitted, since NumPy's sum on float16 arrays has more error
80
71
# than CuPy's.
81
72
@testing .for_all_dtypes (no_float16 = True )
82
73
@testing .numpy_cupy_allclose ()
83
74
def test_sum_axis2 (self , xp , dtype ):
84
75
a = testing .shaped_arange ((20 , 30 , 40 ), xp , dtype )
85
- return a .sum (** _get_dtype_kwargs ( xp , dtype ), axis = 1 )
76
+ return a .sum (axis = 1 )
86
77
87
78
@testing .for_all_dtypes ()
88
79
@testing .numpy_cupy_allclose (contiguous_check = False )
89
80
def test_sum_axis_transposed (self , xp , dtype ):
90
81
a = testing .shaped_arange ((2 , 3 , 4 ), xp , dtype ).transpose (2 , 0 , 1 )
91
- return a .sum (** _get_dtype_kwargs ( xp , dtype ), axis = 1 )
82
+ return a .sum (axis = 1 )
92
83
93
84
@testing .for_all_dtypes ()
94
85
@testing .numpy_cupy_allclose (contiguous_check = False )
95
86
def test_sum_axis_transposed2 (self , xp , dtype ):
96
87
a = testing .shaped_arange ((20 , 30 , 40 ), xp , dtype ).transpose (2 , 0 , 1 )
97
- return a .sum (** _get_dtype_kwargs ( xp , dtype ), axis = 1 )
88
+ return a .sum (axis = 1 )
98
89
99
90
@testing .for_all_dtypes ()
100
91
@testing .numpy_cupy_allclose ()
101
92
def test_sum_axes (self , xp , dtype ):
102
93
a = testing .shaped_arange ((2 , 3 , 4 , 5 ), xp , dtype )
103
- return a .sum (** _get_dtype_kwargs ( xp , dtype ), axis = (1 , 3 ))
94
+ return a .sum (axis = (1 , 3 ))
104
95
105
96
@testing .for_all_dtypes ()
106
97
@testing .numpy_cupy_allclose (rtol = 1e-4 )
107
98
def test_sum_axes2 (self , xp , dtype ):
108
99
a = testing .shaped_arange ((20 , 30 , 40 , 50 ), xp , dtype )
109
- return a .sum (** _get_dtype_kwargs ( xp , dtype ), axis = (1 , 3 ))
100
+ return a .sum (axis = (1 , 3 ))
110
101
111
102
@testing .for_all_dtypes ()
112
103
@testing .numpy_cupy_allclose (rtol = 1e-6 )
113
104
def test_sum_axes3 (self , xp , dtype ):
114
105
a = testing .shaped_arange ((2 , 3 , 4 , 5 ), xp , dtype )
115
- return a .sum (** _get_dtype_kwargs ( xp , dtype ), axis = (0 , 2 , 3 ))
106
+ return a .sum (axis = (0 , 2 , 3 ))
116
107
117
108
@testing .for_all_dtypes ()
118
109
@testing .numpy_cupy_allclose (rtol = 1e-6 )
119
110
def test_sum_axes4 (self , xp , dtype ):
120
111
a = testing .shaped_arange ((20 , 30 , 40 , 50 ), xp , dtype )
121
- return a .sum (** _get_dtype_kwargs ( xp , dtype ), axis = (0 , 2 , 3 ))
112
+ return a .sum (axis = (0 , 2 , 3 ))
122
113
123
114
@testing .for_all_dtypes ()
124
115
@testing .numpy_cupy_allclose ()
125
116
def test_sum_empty_axis (self , xp , dtype ):
126
117
a = testing .shaped_arange ((2 , 3 , 4 , 5 ), xp , dtype )
127
- return a .sum (** _get_dtype_kwargs ( xp , dtype ), axis = ())
118
+ return a .sum (axis = ())
128
119
129
120
@testing .for_all_dtypes_combination (names = ["src_dtype" , "dst_dtype" ])
130
121
@testing .numpy_cupy_allclose ()
@@ -142,7 +133,7 @@ def test_sum_keepdims_and_dtype(self, xp, src_dtype, dst_dtype):
142
133
@testing .numpy_cupy_allclose ()
143
134
def test_sum_keepdims_multiple_axes (self , xp , dtype ):
144
135
a = testing .shaped_arange ((2 , 3 , 4 ), xp , dtype )
145
- return a .sum (** _get_dtype_kwargs ( xp , dtype ), axis = (1 , 2 ), keepdims = True )
136
+ return a .sum (axis = (1 , 2 ), keepdims = True )
146
137
147
138
@testing .for_all_dtypes ()
148
139
@testing .numpy_cupy_allclose ()
@@ -162,25 +153,25 @@ def test_sum_out_wrong_shape(self):
162
153
@testing .numpy_cupy_allclose ()
163
154
def test_prod_all (self , xp , dtype ):
164
155
a = testing .shaped_arange ((2 , 3 ), xp , dtype )
165
- return a .prod (** _get_dtype_kwargs ( xp , dtype ) )
156
+ return a .prod ()
166
157
167
158
@testing .for_all_dtypes ()
168
159
@testing .numpy_cupy_allclose ()
169
160
def test_external_prod_all (self , xp , dtype ):
170
161
a = testing .shaped_arange ((2 , 3 ), xp , dtype )
171
- return xp .prod (a , ** _get_dtype_kwargs ( xp , dtype ) )
162
+ return xp .prod (a )
172
163
173
164
@testing .for_all_dtypes ()
174
165
@testing .numpy_cupy_allclose ()
175
166
def test_prod_axis (self , xp , dtype ):
176
167
a = testing .shaped_arange ((2 , 3 , 4 ), xp , dtype )
177
- return a .prod (axis = 1 , ** _get_dtype_kwargs ( xp , dtype ) )
168
+ return a .prod (axis = 1 )
178
169
179
170
@testing .for_all_dtypes ()
180
171
@testing .numpy_cupy_allclose ()
181
172
def test_external_prod_axis (self , xp , dtype ):
182
173
a = testing .shaped_arange ((2 , 3 , 4 ), xp , dtype )
183
- return xp .prod (a , axis = 1 , ** _get_dtype_kwargs ( xp , dtype ) )
174
+ return xp .prod (a , axis = 1 )
184
175
185
176
@testing .for_all_dtypes_combination (names = ["src_dtype" , "dst_dtype" ])
186
177
@testing .numpy_cupy_allclose ()
@@ -228,12 +219,7 @@ def _test(self, xp, dtype):
228
219
if not issubclass (dtype , xp .integer ):
229
220
a [:, 1 ] = xp .nan
230
221
func = getattr (xp , self .func )
231
- return func (
232
- a ,
233
- ** _get_dtype_kwargs (xp , dtype ),
234
- axis = self .axis ,
235
- keepdims = self .keepdims ,
236
- )
222
+ return func (a , axis = self .axis , keepdims = self .keepdims )
237
223
238
224
@testing .for_all_dtypes (no_bool = True , no_float16 = True )
239
225
@testing .numpy_cupy_allclose (type_check = has_support_aspect64 ())
@@ -299,15 +285,15 @@ def test_nansum_axes(self, xp, dtype):
299
285
a = testing .shaped_arange (self .shape , xp , dtype )
300
286
if not issubclass (dtype , xp .integer ):
301
287
a [:, 1 ] = xp .nan
302
- return xp .nansum (a , ** _get_dtype_kwargs ( xp , dtype ), axis = self .axis )
288
+ return xp .nansum (a , axis = self .axis )
303
289
304
290
305
291
class TestNansumNanprodHuge :
306
292
def _test (self , xp , nan_slice ):
307
293
a = testing .shaped_random ((2048 , 1 , 1024 ), xp , "f" )
308
294
a [nan_slice ] = xp .nan
309
295
a = xp .broadcast_to (a , (2048 , 256 , 1024 ))
310
- return xp .nansum (a , ** _get_dtype_kwargs ( xp , a . dtype ), axis = 2 )
296
+ return xp .nansum (a , axis = 2 )
311
297
312
298
@testing .slow
313
299
@testing .numpy_cupy_allclose (atol = 1e-1 )
0 commit comments