test_pinned_array.py 14.5 KB
Newer Older
root's avatar
root committed
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
412
413
414
415
416
417
418
419
420
421
422
import unittest

import numpy
import pytest

import cupy
from cupy import testing
from cupy.testing._loops import _wraps_partial
import cupyx


def numpy_cupyx_array_equal(target_func, name='func'):
    _mod = (cupy, numpy)

    _numpy_funcs = {
        'empty': numpy.empty,
        'empty_like': numpy.empty_like,
        'zeros': numpy.zeros,
        'zeros_like': numpy.zeros_like,
    }

    _cupy_funcs = {
        'empty': cupyx.empty_pinned,
        'empty_like': cupyx.empty_like_pinned,
        'zeros': cupyx.zeros_pinned,
        'zeros_like': cupyx.zeros_like_pinned,
    }

    def _get_test_func(xp, func):
        if xp is numpy:
            return _numpy_funcs[func]
        elif xp is cupy:
            return _cupy_funcs[func]
        else:
            assert False

    def _check_pinned_mem_used(a, xp):
        if xp is cupy:
            assert isinstance(a.base, cupy.cuda.PinnedMemoryPointer)
            assert a.base.ptr == a.ctypes.data

    def decorator(impl):
        @_wraps_partial(impl, name)
        def test_func(self, *args, **kw):
            out = []
            for xp in _mod:
                func = _get_test_func(xp, target_func)
                kw[name] = func
                a = impl(self, *args, **kw)
                _check_pinned_mem_used(a, xp)
                out.append(a)
            numpy.testing.assert_array_equal(*out)
        return test_func
    return decorator


# test_empty_scalar_none is removed
# test_zeros_scalar_none is removed
class TestBasic(unittest.TestCase):
    @testing.for_CF_orders()
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='empty')
    def test_empty(self, dtype, order, func):
        a = func((2, 3, 4), dtype=dtype, order=order)
        a.fill(0)
        return a

    @testing.slow
    def test_empty_huge_size(self):
        a = cupyx.empty_pinned((1024, 2048, 1024), dtype='b')
        a.fill(123)
        assert (a == 123).all()
        # Free huge memory for slow test
        del a
        cupy.get_default_pinned_memory_pool().free_all_blocks()

    @testing.slow
    def test_empty_huge_size_fill0(self):
        a = cupyx.empty_pinned((1024, 2048, 1024), dtype='b')
        a.fill(0)
        assert (a == 0).all()
        # Free huge memory for slow test
        del a
        cupy.get_default_pinned_memory_pool().free_all_blocks()

    @testing.for_CF_orders()
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='empty')
    def test_empty_scalar(self, dtype, order, func):
        a = func((), dtype=dtype, order=order)
        a.fill(0)
        return a

    @testing.for_CF_orders()
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='empty')
    def test_empty_int(self, dtype, order, func):
        a = func(3, dtype=dtype, order=order)
        a.fill(0)
        return a

    @testing.slow
    def test_empty_int_huge_size(self):
        a = cupyx.empty_pinned(2 ** 31, dtype='b')
        a.fill(123)
        assert (a == 123).all()
        # Free huge memory for slow test
        del a
        cupy.get_default_pinned_memory_pool().free_all_blocks()

    @testing.slow
    def test_empty_int_huge_size_fill0(self):
        a = cupyx.empty_pinned(2 ** 31, dtype='b')
        a.fill(0)
        assert (a == 0).all()
        # Free huge memory for slow test
        del a
        cupy.get_default_pinned_memory_pool().free_all_blocks()

    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='empty_like')
    def test_empty_like(self, dtype, order, func):
        a = testing.shaped_arange((2, 3, 4), numpy, dtype)
        b = func(a, order=order)
        b.fill(0)
        return b

    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='empty_like')
    def test_empty_like_contiguity(self, dtype, order, func):
        a = testing.shaped_arange((2, 3, 4), numpy, dtype)
        b = func(a, order=order)
        b.fill(0)
        if order in ['f', 'F']:
            assert b.flags.f_contiguous
        else:
            assert b.flags.c_contiguous
        return b

    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='empty_like')
    def test_empty_like_contiguity2(self, dtype, order, func):
        a = testing.shaped_arange((2, 3, 4), numpy, dtype)
        a = numpy.asfortranarray(a)
        b = func(a, order=order)
        b.fill(0)
        if order in ['c', 'C']:
            assert b.flags.c_contiguous
        else:
            assert b.flags.f_contiguous
        return b

    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='empty_like')
    def test_empty_like_contiguity3(self, dtype, order, func):
        a = testing.shaped_arange((2, 3, 4), numpy, dtype)
        # test strides that are both non-contiguous and non-descending
        a = a[:, ::2, :].swapaxes(0, 1)
        b = func(a, order=order)
        b.fill(0)
        if order in ['k', 'K', None]:
            assert not b.flags.c_contiguous
            assert not b.flags.f_contiguous
        elif order in ['f', 'F']:
            assert not b.flags.c_contiguous
            assert b.flags.f_contiguous
        else:
            assert b.flags.c_contiguous
            assert not b.flags.f_contiguous
        return b

    @testing.for_all_dtypes()
    def test_empty_like_K_strides(self, dtype):
        # test strides that are both non-contiguous and non-descending;
        # also test accepting cupy.ndarray
        a = testing.shaped_arange((2, 3, 4), numpy, dtype)
        a = a[:, ::2, :].swapaxes(0, 1)
        b = numpy.empty_like(a, order='K')
        b.fill(0)

        # GPU case
        ag = testing.shaped_arange((2, 3, 4), cupy, dtype)
        ag = ag[:, ::2, :].swapaxes(0, 1)
        bg = cupyx.empty_like_pinned(ag, order='K')
        bg.fill(0)

        # make sure NumPy and CuPy strides agree
        assert b.strides == bg.strides

    @testing.with_requires('numpy>=1.19')
    @testing.for_all_dtypes()
    def test_empty_like_invalid_order(self, dtype):
        a = testing.shaped_arange((2, 3, 4), numpy, dtype)
        with pytest.raises(ValueError):
            cupyx.empty_like_pinned(a, order='Q')

    def test_empty_like_subok(self):
        a = testing.shaped_arange((2, 3, 4), numpy)
        with pytest.raises(TypeError):
            cupyx.empty_like_pinned(a, subok=True)

    @testing.for_CF_orders()
    def test_empty_zero_sized_array_strides(self, order):
        a = numpy.empty((1, 0, 2), dtype='d', order=order)
        b = cupyx.empty_pinned((1, 0, 2), dtype='d', order=order)
        assert b.strides == a.strides

    @testing.for_CF_orders()
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='zeros')
    def test_zeros(self, dtype, order, func):
        return func((2, 3, 4), dtype=dtype, order=order)

    @testing.for_CF_orders()
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='zeros')
    def test_zeros_scalar(self, dtype, order, func):
        return func((), dtype=dtype, order=order)

    @testing.for_CF_orders()
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='zeros')
    def test_zeros_int(self, dtype, order, func):
        return func(3, dtype=dtype, order=order)

    @testing.for_CF_orders()
    def test_zeros_strides(self, order):
        a = numpy.zeros((2, 3), dtype='d', order=order)
        b = cupyx.zeros_pinned((2, 3), dtype='d', order=order)
        assert b.strides == a.strides

    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='zeros_like')
    def test_zeros_like(self, dtype, order, func):
        a = numpy.ndarray((2, 3, 4), dtype=dtype)
        return func(a, order=order)

    def test_zeros_like_subok(self):
        a = numpy.ndarray((2, 3, 4))
        with pytest.raises(TypeError):
            cupyx.zeros_like_pinned(a, subok=True)


@testing.parameterize(
    *testing.product({
        'shape': [4, (4, ), (4, 2), (4, 2, 3), (5, 4, 2, 3)],
    })
)
class TestBasicReshape(unittest.TestCase):

    @testing.with_requires('numpy>=1.17.0')
    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='empty_like')
    def test_empty_like_reshape(self, dtype, order, func):
        a = testing.shaped_arange((2, 3, 4), numpy, dtype)
        b = func(a, order=order, shape=self.shape)
        b.fill(0)
        return b

    @testing.for_CF_orders()
    @testing.for_all_dtypes()
    def test_empty_like_reshape_cupy_only(self, dtype, order):
        a = testing.shaped_arange((2, 3, 4), cupy, dtype)
        b = cupyx.empty_like_pinned(a, shape=self.shape)
        b.fill(0)
        c = cupyx.empty_pinned(self.shape, order=order, dtype=dtype)
        c.fill(0)
        numpy.testing.assert_array_equal(b, c)

    @testing.with_requires('numpy>=1.17.0')
    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='empty_like')
    def test_empty_like_reshape_contiguity(self, dtype, order, func):
        a = testing.shaped_arange((2, 3, 4), numpy, dtype)
        b = func(a, order=order, shape=self.shape)
        b.fill(0)
        if order in ['f', 'F']:
            assert b.flags.f_contiguous
        else:
            assert b.flags.c_contiguous
        return b

    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    def test_empty_like_reshape_contiguity_cupy_only(self, dtype, order):
        a = testing.shaped_arange((2, 3, 4), cupy, dtype)
        b = cupyx.empty_like_pinned(a, order=order, shape=self.shape)
        b.fill(0)
        c = cupyx.empty_pinned(self.shape)
        c.fill(0)
        if order in ['f', 'F']:
            assert b.flags.f_contiguous
        else:
            assert b.flags.c_contiguous
        numpy.testing.assert_array_equal(b, c)

    @testing.with_requires('numpy>=1.17.0')
    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='empty_like')
    def test_empty_like_reshape_contiguity2(self, dtype, order, func):
        a = testing.shaped_arange((2, 3, 4), numpy, dtype)
        a = numpy.asfortranarray(a)
        b = func(a, order=order, shape=self.shape)
        b.fill(0)
        shape = self.shape if not numpy.isscalar(self.shape) else (self.shape,)
        if (order in ['c', 'C'] or
                (order in ['k', 'K', None] and len(shape) != a.ndim)):
            assert b.flags.c_contiguous
        else:
            assert b.flags.f_contiguous
        return b

    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    def test_empty_like_reshape_contiguity2_cupy_only(self, dtype, order):
        a = testing.shaped_arange((2, 3, 4), cupy, dtype)
        a = cupy.asfortranarray(a)
        b = cupyx.empty_like_pinned(a, order=order, shape=self.shape)
        b.fill(0)
        c = cupyx.empty_pinned(self.shape)
        c.fill(0)
        shape = self.shape if not numpy.isscalar(self.shape) else (self.shape,)
        if (order in ['c', 'C'] or
                (order in ['k', 'K', None] and len(shape) != a.ndim)):
            assert b.flags.c_contiguous
        else:
            assert b.flags.f_contiguous
        numpy.testing.assert_array_equal(b, c)

    @testing.with_requires('numpy>=1.17.0')
    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='empty_like')
    def test_empty_like_reshape_contiguity3(self, dtype, order, func):
        a = testing.shaped_arange((2, 3, 4), numpy, dtype)
        # test strides that are both non-contiguous and non-descending
        a = a[:, ::2, :].swapaxes(0, 1)
        b = func(a, order=order, shape=self.shape)
        b.fill(0)
        shape = self.shape if not numpy.isscalar(self.shape) else (self.shape,)
        if len(shape) == 1:
            assert b.flags.c_contiguous
            assert b.flags.f_contiguous
        elif order in ['k', 'K', None] and len(shape) == a.ndim:
            assert not b.flags.c_contiguous
            assert not b.flags.f_contiguous
        elif order in ['f', 'F']:
            assert not b.flags.c_contiguous
            assert b.flags.f_contiguous
        else:
            assert b.flags.c_contiguous
            assert not b.flags.f_contiguous
        return b

    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    def test_empty_like_reshape_contiguity3_cupy_only(self, dtype, order):
        a = testing.shaped_arange((2, 3, 4), cupy, dtype)
        # test strides that are both non-contiguous and non-descending
        a = a[:, ::2, :].swapaxes(0, 1)
        b = cupyx.empty_like_pinned(a, order=order, shape=self.shape)
        b.fill(0)
        shape = self.shape if not numpy.isscalar(self.shape) else (self.shape,)
        if len(shape) == 1:
            assert b.flags.c_contiguous
            assert b.flags.f_contiguous
        elif order in ['k', 'K', None] and len(shape) == a.ndim:
            assert not b.flags.c_contiguous
            assert not b.flags.f_contiguous
        elif order in ['f', 'F']:
            assert not b.flags.c_contiguous
            assert b.flags.f_contiguous
        else:
            assert b.flags.c_contiguous
            assert not b.flags.f_contiguous

        c = cupyx.zeros_pinned(self.shape)
        c.fill(0)
        testing.assert_array_equal(b, c)

    @testing.with_requires('numpy>=1.17.0')
    @testing.for_all_dtypes()
    def test_empty_like_K_strides_reshape(self, dtype):
        # test strides that are both non-contiguous and non-descending
        a = testing.shaped_arange((2, 3, 4), numpy, dtype)
        a = a[:, ::2, :].swapaxes(0, 1)
        b = cupyx.empty_like_pinned(a, order='K', shape=self.shape)
        b.fill(0)

        # GPU case
        ag = testing.shaped_arange((2, 3, 4), cupy, dtype)
        ag = ag[:, ::2, :].swapaxes(0, 1)
        bg = cupyx.empty_like_pinned(ag, order='K', shape=self.shape)
        bg.fill(0)

        # make sure NumPy and CuPy strides agree
        assert b.strides == bg.strides
        return

    @testing.with_requires('numpy>=1.17.0')
    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    @numpy_cupyx_array_equal(target_func='zeros_like')
    def test_zeros_like_reshape(self, dtype, order, func):
        a = numpy.ndarray((2, 3, 4), dtype=dtype)
        return func(a, order=order, shape=self.shape)

    @testing.for_CF_orders()
    @testing.for_all_dtypes()
    def test_zeros_like_reshape_cupy_only(self, dtype, order):
        a = testing.shaped_arange((2, 3, 4), cupy, dtype)
        b = cupyx.zeros_like_pinned(a, shape=self.shape)
        c = cupyx.zeros_pinned(self.shape, order=order, dtype=dtype)
        numpy.testing.assert_array_equal(b, c)