-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_ranges.py
More file actions
411 lines (339 loc) · 14.1 KB
/
test_ranges.py
File metadata and controls
411 lines (339 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import functools
import math
import sys
import unittest
import numpy
import pytest
import dpnp as cupy
from tests.helper import has_support_aspect64
from tests.third_party.cupy import testing
def skip_int_equality_before_numpy_1_20(names=("dtype",)):
"""Require numpy/numpy#16841 or skip the equality check."""
def decorator(wrapped):
if numpy.lib.NumpyVersion(numpy.__version__) >= "1.20.0":
return wrapped
@functools.wraps(wrapped)
def wrapper(self, *args, **kwargs):
xp = kwargs["xp"]
dtypes = [kwargs[name] for name in names]
ret = wrapped(self, *args, **kwargs)
if any(numpy.issubdtype(dtype, numpy.integer) for dtype in dtypes):
ret = xp.zeros_like(ret)
return ret
return wrapper
return decorator
class TestRanges(unittest.TestCase):
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal()
def test_arange(self, xp, dtype):
return xp.arange(10, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal()
def test_arange2(self, xp, dtype):
return xp.arange(5, 10, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal()
def test_arange3(self, xp, dtype):
return xp.arange(1, 11, 2, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal()
def test_arange4(self, xp, dtype):
return xp.arange(20, 2, -3, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal()
def test_arange5(self, xp, dtype):
return xp.arange(0, 100, None, dtype=dtype)
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_arange6(self, xp, dtype):
return xp.arange(0, 2, dtype=dtype)
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_arange7(self, xp, dtype):
return xp.arange(10, 11, dtype=dtype)
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_arange8(self, xp, dtype):
return xp.arange(10, 8, -1, dtype=dtype)
def test_arange9(self):
for xp in (numpy, cupy):
with pytest.raises((ValueError, TypeError)):
xp.arange(10, dtype=xp.bool_)
@testing.numpy_cupy_array_equal()
def test_arange_no_dtype_int(self, xp):
return xp.arange(1, 11, 2)
@testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64())
def test_arange_no_dtype_float(self, xp):
return xp.arange(1.0, 11.0, 2.0)
@testing.numpy_cupy_array_equal()
def test_arange_negative_size(self, xp):
return xp.arange(3, 1)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal()
def test_linspace(self, xp, dtype):
return xp.linspace(0, 10, 5, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal()
def test_linspace2(self, xp, dtype):
return xp.linspace(10, 0, 5, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal()
@skip_int_equality_before_numpy_1_20()
def test_linspace3(self, xp, dtype):
if xp.dtype(dtype).kind == "u":
pytest.skip()
return xp.linspace(-10, 8, 9, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal()
def test_linspace_zero_num(self, xp, dtype):
return xp.linspace(0, 10, 0, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal()
def test_linspace_zero_num_no_endopoint_with_retstep(self, xp, dtype):
x, step = xp.linspace(
0, 10, 0, dtype=dtype, endpoint=False, retstep=True
)
self.assertTrue(math.isnan(step))
return x
@testing.with_requires("numpy>=1.18")
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal()
def test_linspace_one_num_no_endopoint_with_retstep(self, xp, dtype):
start, stop = 3, 7
x, step = xp.linspace(
start, stop, 1, dtype=dtype, endpoint=False, retstep=True
)
self.assertEqual(step, stop - start)
return x
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal()
def test_linspace_one_num(self, xp, dtype):
return xp.linspace(0, 2, 1, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose()
def test_linspace_no_endpoint(self, xp, dtype):
return xp.linspace(0, 10, 5, dtype=dtype, endpoint=False)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal()
def test_linspace_with_retstep(self, xp, dtype):
x, step = xp.linspace(0, 10, 5, dtype=dtype, retstep=True)
self.assertEqual(step, 2.5)
return x
@testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64())
def test_linspace_no_dtype_int(self, xp):
return xp.linspace(0, 10, 50)
@testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64())
def test_linspace_no_dtype_float(self, xp):
return xp.linspace(0.0, 10.0, 50)
@testing.numpy_cupy_array_equal()
def test_linspace_float_args_with_int_dtype(self, xp):
return xp.linspace(0.1, 9.1, 11, dtype=int)
def test_linspace_neg_num(self):
for xp in (numpy, cupy):
with pytest.raises(ValueError):
xp.linspace(0, 10, -1)
@testing.numpy_cupy_allclose()
def test_linspace_float_overflow(self, xp):
dtype = cupy.default_float_type()
return xp.linspace(0.0, xp.finfo(dtype).max / 5, 10, dtype=dtype)
@testing.numpy_cupy_allclose(rtol={numpy.float32: 1e-6, "default": 1e-7})
def test_linspace_float_underflow(self, xp):
# find minimum subnormal number
dtype = cupy.default_float_type()
# use .tiny instead of .min and while to get
# minimum subnormal number directly and avoid RuntimeWarning
x = xp.finfo(dtype).tiny
return xp.linspace(0.0, x, 10, dtype=dtype)
@testing.with_requires("numpy>=1.16")
@testing.for_all_dtypes_combination(
names=("dtype_range", "dtype_out"), no_bool=True, no_complex=True
)
@testing.numpy_cupy_allclose(rtol=1e-04)
def test_linspace_array_start_stop(self, xp, dtype_range, dtype_out):
start = xp.array([0, 120], dtype=dtype_range)
stop = xp.array([100, 0], dtype=dtype_range)
return xp.linspace(start, stop, num=50, dtype=dtype_out)
@testing.with_requires("numpy>=1.16")
@testing.for_all_dtypes_combination(
names=("dtype_range", "dtype_out"), no_bool=True, no_complex=True
)
@testing.numpy_cupy_allclose(rtol=1e-04)
def test_linspace_mixed_start_stop(self, xp, dtype_range, dtype_out):
start = 0.0
if xp.dtype(dtype_range).kind in "u":
stop = xp.array([100, 16], dtype=dtype_range)
else:
stop = xp.array([100, -100], dtype=dtype_range)
return xp.linspace(start, stop, num=50, dtype=dtype_out)
@testing.with_requires("numpy>=1.16")
@testing.for_all_dtypes_combination(
names=("dtype_range", "dtype_out"), no_bool=True, no_complex=True
)
@testing.numpy_cupy_allclose(rtol=1e-04)
def test_linspace_mixed_start_stop2(self, xp, dtype_range, dtype_out):
if xp.dtype(dtype_range).kind in "u":
start = xp.array([160, 120], dtype=dtype_range)
else:
start = xp.array([-120, 120], dtype=dtype_range)
stop = 0
return xp.linspace(start, stop, num=50, dtype=dtype_out)
@testing.with_requires("numpy>=1.16")
@testing.for_all_dtypes_combination(
names=("dtype_range", "dtype_out"), no_bool=True, no_complex=True
)
@testing.numpy_cupy_allclose(rtol=1e-04)
def test_linspace_array_start_stop_axis1(self, xp, dtype_range, dtype_out):
start = xp.array([0, 120], dtype=dtype_range)
stop = xp.array([100, 0], dtype=dtype_range)
return xp.linspace(start, stop, num=50, dtype=dtype_out, axis=1)
@testing.with_requires("numpy>=1.16")
@testing.for_complex_dtypes()
@testing.numpy_cupy_allclose()
def test_linspace_complex_start_stop(self, xp, dtype):
start = xp.array([0, 120], dtype=dtype)
stop = xp.array([100, 0], dtype=dtype)
return xp.linspace(start, stop, num=50, dtype=dtype)
@testing.with_requires("numpy>=1.16")
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose(rtol=1e-04)
def test_linspace_start_stop_list(self, xp, dtype):
start = [0, 0]
stop = [100, 16]
return xp.linspace(start, stop, num=50, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose()
def test_logspace(self, xp, dtype):
return xp.logspace(0, 2, 5, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose()
def test_logspace2(self, xp, dtype):
return xp.logspace(2, 0, 5, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose()
def test_logspace_zero_num(self, xp, dtype):
return xp.logspace(0, 2, 0, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose()
def test_logspace_one_num(self, xp, dtype):
return xp.logspace(0, 2, 1, dtype=dtype)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose()
def test_logspace_no_endpoint(self, xp, dtype):
return xp.logspace(0, 2, 5, dtype=dtype, endpoint=False)
@testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64())
def test_logspace_no_dtype_int(self, xp):
return xp.logspace(0, 2)
@testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64())
def test_logspace_no_dtype_float(self, xp):
return xp.logspace(0.0, 2.0)
@testing.numpy_cupy_allclose()
def test_logspace_float_args_with_int_dtype(self, xp):
return xp.logspace(0.1, 2.1, 11, dtype=int)
def test_logspace_neg_num(self):
for xp in (numpy, cupy):
with pytest.raises(ValueError):
xp.logspace(0, 10, -1)
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose(rtol=1e-04)
def test_logspace_base(self, xp, dtype):
return xp.logspace(0, 2, 5, base=2.0, dtype=dtype)
# See #7946 and https://github.com/numpy/numpy/issues/24957
@testing.with_requires("numpy>=1.16, !=1.25.*, !=1.26.*")
@testing.for_all_dtypes_combination(
names=("dtype_range", "dtype_out"), no_bool=True, no_complex=True
)
@testing.numpy_cupy_allclose(rtol=1e-6, contiguous_check=False)
def test_logspace_array_start_stop_axis1(self, xp, dtype_range, dtype_out):
start = xp.array([0, 2], dtype=dtype_range)
stop = xp.array([2, 0], dtype=dtype_range)
return xp.logspace(start, stop, num=5, dtype=dtype_out, axis=1)
@testing.parameterize(
*testing.product(
{
"indexing": ["xy", "ij"],
"sparse": [False, True],
"copy": [False, True],
}
)
)
class TestMeshgrid(unittest.TestCase):
@testing.for_all_dtypes()
def test_meshgrid0(self, dtype):
out = cupy.meshgrid(
indexing=self.indexing, sparse=self.sparse, copy=self.copy
)
assert out == []
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_meshgrid1(self, xp, dtype):
x = xp.arange(2).astype(dtype)
return xp.meshgrid(
x, indexing=self.indexing, sparse=self.sparse, copy=self.copy
)
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_meshgrid2(self, xp, dtype):
x = xp.arange(2).astype(dtype)
y = xp.arange(3).astype(dtype)
return xp.meshgrid(
x, y, indexing=self.indexing, sparse=self.sparse, copy=self.copy
)
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_meshgrid3(self, xp, dtype):
x = xp.arange(2).astype(dtype)
y = xp.arange(3).astype(dtype)
z = xp.arange(4).astype(dtype)
return xp.meshgrid(
x, y, z, indexing=self.indexing, sparse=self.sparse, copy=self.copy
)
class TestMgrid(unittest.TestCase):
@testing.numpy_cupy_array_equal()
def test_mgrid0(self, xp):
return xp.mgrid[0:]
@testing.numpy_cupy_array_equal()
def test_mgrid1(self, xp):
return xp.mgrid[-10:10]
@testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64())
def test_mgrid2(self, xp):
return xp.mgrid[-10:10:10j]
@testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64())
def test_mgrid3(self, xp):
x = xp.zeros(10)[:, None]
y = xp.ones(10)[:, None]
return xp.mgrid[x:y:10j]
@testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64())
def test_mgrid4(self, xp):
# check len(keys) > 1
return xp.mgrid[-10:10:10j, -10:10:10j]
@testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64())
def test_mgrid5(self, xp):
# check len(keys) > 1
x = xp.zeros(10)[:, None]
y = xp.ones(10)[:, None]
return xp.mgrid[x:y:10j, x:y:10j]
class TestOgrid(unittest.TestCase):
@testing.numpy_cupy_array_equal()
def test_ogrid0(self, xp):
return xp.ogrid[0:]
@testing.numpy_cupy_array_equal()
def test_ogrid1(self, xp):
return xp.ogrid[-10:10]
@testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64())
def test_ogrid2(self, xp):
return xp.ogrid[-10:10:10j]
@testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64())
def test_ogrid3(self, xp):
x = xp.zeros(10)[:, None]
y = xp.ones(10)[:, None]
return xp.ogrid[x:y:10j]
@testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64())
def test_ogrid4(self, xp):
# check len(keys) > 1
return xp.ogrid[-10:10:10j, -10:10:10j]
@testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64())
def test_ogrid5(self, xp):
# check len(keys) > 1
x = xp.zeros(10)[:, None]
y = xp.ones(10)[:, None]
return xp.ogrid[x:y:10j, x:y:10j]