13
13
14
14
from .helper import (
15
15
assert_dtype_allclose ,
16
+ generate_random_numpy_array ,
16
17
get_abs_array ,
17
18
get_all_dtypes ,
18
19
get_complex_dtypes ,
22
23
has_support_aspect16 ,
23
24
numpy_version ,
24
25
)
25
- from .test_umath import (
26
- _get_numpy_arrays_2in_1out ,
27
- _get_output_data_type ,
28
- )
29
26
30
27
"""
31
28
The scope includes tests with only functions which are instances of
@@ -39,7 +36,9 @@ class TestAdd:
39
36
40
37
@pytest .mark .parametrize ("dtype" , ALL_DTYPES )
41
38
def test_add (self , dtype ):
42
- a , b , expected = _get_numpy_arrays_2in_1out ("add" , dtype , [- 5 , 5 , 10 ])
39
+ a = generate_random_numpy_array (10 , dtype )
40
+ b = generate_random_numpy_array (10 , dtype )
41
+ expected = numpy .add (a , b )
43
42
44
43
ia , ib = dpnp .array (a ), dpnp .array (b )
45
44
iout = dpnp .empty (expected .shape , dtype = dtype )
@@ -139,91 +138,63 @@ def test_invalid_out(self, xp, out):
139
138
assert_raises (TypeError , xp .add , a , 2 , out )
140
139
141
140
141
+ @pytest .mark .parametrize ("func" , ["fmax" , "fmin" , "maximum" , "minimum" ])
142
142
class TestBoundFuncs :
143
- @pytest .fixture (
144
- params = [
145
- {"func_name" : "fmax" , "input_values" : [- 5 , 5 , 10 ]},
146
- {"func_name" : "fmin" , "input_values" : [- 5 , 5 , 10 ]},
147
- {"func_name" : "maximum" , "input_values" : [- 5 , 5 , 10 ]},
148
- {"func_name" : "minimum" , "input_values" : [- 5 , 5 , 10 ]},
149
- ],
150
- ids = [
151
- "fmax" ,
152
- "fmin" ,
153
- "maximum" ,
154
- "minimum" ,
155
- ],
156
- )
157
- def func_params (self , request ):
158
- return request .param
159
-
160
- @pytest .mark .parametrize (
161
- "dtype" , get_all_dtypes (no_bool = True , no_complex = True )
162
- )
163
- def test_out (self , func_params , dtype ):
164
- func_name = func_params ["func_name" ]
165
- input_values = func_params ["input_values" ]
166
- a , b , expected = _get_numpy_arrays_2in_1out (
167
- func_name , dtype , input_values
168
- )
143
+ @pytest .mark .parametrize ("dtype" , get_all_dtypes (no_none = True ))
144
+ def test_out (self , func , dtype ):
145
+ a = generate_random_numpy_array (10 , dtype )
146
+ b = generate_random_numpy_array (10 , dtype )
147
+ expected = getattr (numpy , func )(a , b )
169
148
170
149
ia , ib = dpnp .array (a ), dpnp .array (b )
171
150
iout = dpnp .empty (expected .shape , dtype = dtype )
172
- result = getattr (dpnp , func_name )(ia , ib , out = iout )
151
+ result = getattr (dpnp , func )(ia , ib , out = iout )
173
152
174
153
assert result is iout
175
154
assert_dtype_allclose (result , expected )
176
155
177
156
@pytest .mark .parametrize (
178
- "dtype" , get_all_dtypes (no_bool = True , no_complex = True )
157
+ "dtype" , get_all_dtypes (no_none = True , no_bool = True )
179
158
)
180
- def test_out_overlap (self , func_params , dtype ):
181
- func_name = func_params ["func_name" ]
159
+ def test_out_overlap (self , func , dtype ):
182
160
size = 15
183
161
a = numpy .arange (2 * size , dtype = dtype )
184
162
ia = dpnp .array (a )
185
163
186
- getattr (dpnp , func_name )(ia [size ::], ia [::2 ], out = ia [:size :])
187
- getattr (numpy , func_name )(a [size ::], a [::2 ], out = a [:size :])
164
+ getattr (dpnp , func )(ia [size ::], ia [::2 ], out = ia [:size :])
165
+ getattr (numpy , func )(a [size ::], a [::2 ], out = a [:size :])
188
166
189
167
assert_dtype_allclose (ia , a )
190
168
191
169
@pytest .mark .parametrize ("shape" , [(0 ,), (15 ,), (2 , 2 )])
192
- def test_invalid_shape (self , func_params , shape ):
193
- func_name = func_params ["func_name" ]
170
+ def test_invalid_shape (self , func , shape ):
194
171
a , b = dpnp .arange (10 ), dpnp .arange (10 )
195
172
out = dpnp .empty (shape )
196
173
197
174
with pytest .raises (ValueError ):
198
- getattr (dpnp , func_name )(a , b , out = out )
175
+ getattr (dpnp , func )(a , b , out = out )
199
176
200
177
@pytest .mark .parametrize ("xp" , [dpnp , numpy ])
201
178
@pytest .mark .parametrize (
202
179
"out" ,
203
180
[4 , (), [], (3 , 7 ), [2 , 4 ]],
204
181
ids = ["scalar" , "empty_tuple" , "empty_list" , "tuple" , "list" ],
205
182
)
206
- def test_invalid_out (self , func_params , xp , out ):
207
- func_name = func_params ["func_name" ]
183
+ def test_invalid_out (self , func , xp , out ):
208
184
a = xp .arange (10 )
209
- assert_raises (TypeError , getattr (xp , func_name ), a , 2 , out )
185
+ assert_raises (TypeError , getattr (xp , func ), a , 2 , out )
210
186
211
187
212
188
class TestDivide :
213
189
@pytest .mark .usefixtures ("suppress_divide_invalid_numpy_warnings" )
214
- @pytest .mark .parametrize (
215
- "dtype" , get_all_dtypes (no_none = True , no_bool = True )
216
- )
190
+ @pytest .mark .parametrize ("dtype" , get_all_dtypes (no_none = True ))
217
191
def test_divide (self , dtype ):
218
- a , b , expected = _get_numpy_arrays_2in_1out (
219
- "divide" , dtype , [ - 5 , 5 , 10 ]
220
- )
192
+ a = generate_random_numpy_array ( 10 , dtype )
193
+ b = generate_random_numpy_array ( 10 , dtype )
194
+ expected = numpy . divide ( a , b )
221
195
222
196
ia , ib = dpnp .array (a ), dpnp .array (b )
223
- if numpy .issubdtype (dtype , numpy .integer ):
224
- out_dtype = map_dtype_to_device (dpnp .float64 , ia .sycl_device )
225
- else :
226
- out_dtype = _get_output_data_type (dtype )
197
+ out_dtype = map_dtype_to_device (expected .dtype , ia .sycl_device )
227
198
iout = dpnp .empty (expected .shape , dtype = out_dtype )
228
199
result = dpnp .divide (ia , ib , out = iout )
229
200
@@ -318,7 +289,9 @@ def do_inplace_op(self, base, other, func):
318
289
@pytest .mark .usefixtures ("suppress_divide_numpy_warnings" )
319
290
@pytest .mark .parametrize ("dtype" , ALL_DTYPES )
320
291
def test_basic (self , func , dtype ):
321
- a , b , expected = _get_numpy_arrays_2in_1out (func , dtype , [- 5 , 5 , 10 ])
292
+ a = generate_random_numpy_array (10 , dtype )
293
+ b = generate_random_numpy_array (10 , dtype )
294
+ expected = getattr (numpy , func )(a , b )
322
295
323
296
ia , ib = dpnp .array (a ), dpnp .array (b )
324
297
iout = dpnp .empty (expected .shape , dtype = dtype )
@@ -398,9 +371,9 @@ def test_invalid_out(self, func, xp, out):
398
371
assert_raises (TypeError , getattr (xp , func ), a , 2 , out )
399
372
400
373
374
+ @pytest .mark .parametrize ("func" , ["fmax" , "fmin" ])
401
375
class TestFmaxFmin :
402
376
@pytest .mark .skipif (not has_support_aspect16 (), reason = "no fp16 support" )
403
- @pytest .mark .parametrize ("func" , ["fmax" , "fmin" ])
404
377
def test_half (self , func ):
405
378
a = numpy .array ([0 , 1 , 2 , 4 , 2 ], dtype = numpy .float16 )
406
379
b = numpy .array ([- 2 , 5 , 1 , 4 , 3 ], dtype = numpy .float16 )
@@ -415,7 +388,6 @@ def test_half(self, func):
415
388
expected = getattr (numpy , func )(b , c )
416
389
assert_equal (result , expected )
417
390
418
- @pytest .mark .parametrize ("func" , ["fmax" , "fmin" ])
419
391
@pytest .mark .parametrize ("dtype" , get_float_dtypes ())
420
392
def test_float_nans (self , func , dtype ):
421
393
a = numpy .array ([0 , numpy .nan , numpy .nan ], dtype = dtype )
@@ -426,7 +398,6 @@ def test_float_nans(self, func, dtype):
426
398
expected = getattr (numpy , func )(a , b )
427
399
assert_equal (result , expected )
428
400
429
- @pytest .mark .parametrize ("func" , ["fmax" , "fmin" ])
430
401
@pytest .mark .parametrize ("dtype" , get_complex_dtypes ())
431
402
@pytest .mark .parametrize (
432
403
"nan_val" ,
@@ -446,7 +417,6 @@ def test_complex_nans(self, func, dtype, nan_val):
446
417
expected = getattr (numpy , func )(a , b )
447
418
assert_equal (result , expected )
448
419
449
- @pytest .mark .parametrize ("func" , ["fmax" , "fmin" ])
450
420
@pytest .mark .parametrize ("dtype" , get_float_dtypes (no_float16 = False ))
451
421
def test_precision (self , func , dtype ):
452
422
dtmin = numpy .finfo (dtype ).min
@@ -602,9 +572,9 @@ class TestMultiply:
602
572
603
573
@pytest .mark .parametrize ("dtype" , ALL_DTYPES )
604
574
def test_multiply (self , dtype ):
605
- a , b , expected = _get_numpy_arrays_2in_1out (
606
- "multiply" , dtype , [ 0 , 10 , 10 ]
607
- )
575
+ a = generate_random_numpy_array ( 10 , dtype )
576
+ b = generate_random_numpy_array ( 10 , dtype )
577
+ expected = numpy . multiply ( a , b )
608
578
609
579
ia , ib = dpnp .array (a ), dpnp .array (b )
610
580
iout = dpnp .empty (expected .shape , dtype = dtype )
@@ -853,8 +823,9 @@ def test_basic(self, array, val, data_type, val_type):
853
823
854
824
@pytest .mark .parametrize ("dtype" , ALL_DTYPES )
855
825
def test_power (self , dtype ):
856
- numpy .random .seed (42 )
857
- a , b , expected = _get_numpy_arrays_2in_1out ("power" , dtype , [0 , 10 , 10 ])
826
+ a = generate_random_numpy_array (10 , dtype , low = 0 )
827
+ b = generate_random_numpy_array (10 , dtype , low = 0 )
828
+ expected = numpy .power (a , b )
858
829
859
830
ia , ib = dpnp .array (a ), dpnp .array (b )
860
831
out_dtype = numpy .int8 if dtype == numpy .bool_ else dtype
@@ -1075,9 +1046,9 @@ class TestSubtract:
1075
1046
1076
1047
@pytest .mark .parametrize ("dtype" , ALL_DTYPES )
1077
1048
def test_add (self , dtype ):
1078
- a , b , expected = _get_numpy_arrays_2in_1out (
1079
- "subtract" , dtype , [ - 5 , 5 , 10 ]
1080
- )
1049
+ a = generate_random_numpy_array ( 10 , dtype )
1050
+ b = generate_random_numpy_array ( 10 , dtype )
1051
+ expected = numpy . subtract ( a , b )
1081
1052
1082
1053
ia , ib = dpnp .array (a ), dpnp .array (b )
1083
1054
iout = dpnp .empty (expected .shape , dtype = dtype )
0 commit comments