-
-
Notifications
You must be signed in to change notification settings - Fork 777
/
test_from_data.py
208 lines (175 loc) · 6.56 KB
/
test_from_data.py
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
import unittest
import cupy
from cupy import cuda
from cupy import testing
import numpy
@testing.gpu
class TestFromData(unittest.TestCase):
_multiprocess_can_split_ = True
@testing.for_orders('CFAK')
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_array(self, xp, dtype, order):
return xp.array([[1, 2, 3], [2, 3, 4]], dtype=dtype, order=order)
@testing.for_orders('CFAK')
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_array_from_numpy(self, xp, dtype, order):
a = testing.shaped_arange((2, 3, 4), numpy, dtype)
return xp.array(a, order=order)
@testing.for_orders('CFAK')
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_array_copy(self, xp, dtype, order):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
return xp.array(a, order=order)
@testing.for_orders('CFAK')
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_array_copy_is_copied(self, xp, dtype, order):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
b = xp.array(a, order=order)
a.fill(0)
return b
@testing.for_orders('CFAK')
@testing.for_all_dtypes(name='dtype1')
@testing.for_all_dtypes(name='dtype2')
@testing.numpy_cupy_array_equal()
def test_array_copy_with_dtype(self, xp, dtype1, dtype2, order):
a = testing.shaped_arange((2, 3, 4), xp, dtype1)
return xp.array(a, dtype=dtype2, order=order)
@testing.for_orders('CFAK')
@testing.numpy_cupy_array_equal()
def test_array_copy_with_dtype_being_none(self, xp, order):
a = testing.shaped_arange((2, 3, 4), xp)
return xp.array(a, dtype=None, order=order)
@testing.for_orders('CFAK')
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_array_no_copy(self, xp, dtype, order):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
b = xp.array(a, copy=False, order=order)
a.fill(0)
return b
@testing.for_orders('CFAK')
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_array_f_contiguous_input(self, xp, dtype, order):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
a = xp.asfortranarray(a)
b = xp.array(a, copy=False, order=order)
return b
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_array_f_contiguous_output(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
b = xp.array(a, copy=False, order='F')
self.assertTrue(b.flags.f_contiguous)
return b
@testing.multi_gpu(2)
def test_array_multi_device(self):
with cuda.Device(0):
x = testing.shaped_arange((2, 3, 4), cupy, dtype='f')
with cuda.Device(1):
y = cupy.array(x)
self.assertIsInstance(y, cupy.ndarray)
self.assertIsNot(x, y) # Do copy
self.assertEqual(int(x.device), 0)
self.assertEqual(int(y.device), 1)
testing.assert_array_equal(x, y)
@testing.multi_gpu(2)
def test_array_multi_device_zero_size(self):
with cuda.Device(0):
x = testing.shaped_arange((0,), cupy, dtype='f')
with cuda.Device(1):
y = cupy.array(x)
self.assertIsInstance(y, cupy.ndarray)
self.assertIsNot(x, y) # Do copy
self.assertIsNone(x.device)
self.assertIsNone(y.device)
testing.assert_array_equal(x, y)
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_array_no_copy_ndmin(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
b = xp.array(a, copy=False, ndmin=5)
self.assertEqual(a.shape, (2, 3, 4))
a.fill(0)
return b
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_asarray(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
return xp.asarray(a)
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_asarray_is_not_copied(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
b = xp.asarray(a)
a.fill(0)
return b
def test_ascontiguousarray_on_noncontiguous_array(self):
a = testing.shaped_arange((2, 3, 4))
b = a.transpose(2, 0, 1)
c = cupy.ascontiguousarray(b)
self.assertTrue(c.flags.c_contiguous)
testing.assert_array_equal(b, c)
def test_ascontiguousarray_on_contiguous_array(self):
a = testing.shaped_arange((2, 3, 4))
b = cupy.ascontiguousarray(a)
self.assertIs(a, b)
@testing.for_CF_orders()
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_copy(self, xp, dtype, order):
a = xp.zeros((2, 3, 4), dtype=dtype)
b = a.copy(order=order)
a[1] = 1
return b
@testing.multi_gpu(2)
@testing.for_CF_orders()
@testing.for_all_dtypes()
def test_copy_multigpu(self, dtype, order):
with cuda.Device(0):
src = cupy.random.uniform(-1, 1, (2, 3)).astype(dtype)
with cuda.Device(1):
dst = src.copy(order)
testing.assert_allclose(src, dst, rtol=0, atol=0)
@testing.parameterize(
*testing.product({
'ndmin': [0, 1, 2, 3],
'copy': [True, False],
'xp': [numpy, cupy]
})
)
class TestArrayPreservationOfShape(unittest.TestCase):
@testing.for_all_dtypes()
def test_cupy_array(self, dtype):
shape = 2, 3
a = testing.shaped_arange(shape, self.xp, dtype)
cupy.array(a, copy=self.copy, ndmin=self.ndmin)
# Check if cupy.ndarray does not alter
# the shape of the original array.
self.assertEqual(a.shape, shape)
@testing.parameterize(
*testing.product({
'ndmin': [0, 1, 2, 3],
'copy': [True, False],
'xp': [numpy, cupy]
})
)
class TestArrayCopy(unittest.TestCase):
@testing.for_all_dtypes()
def test_cupy_array(self, dtype):
a = testing.shaped_arange((2, 3), self.xp, dtype)
actual = cupy.array(a, copy=self.copy, ndmin=self.ndmin)
should_copy = (self.xp is numpy) or self.copy
# TODO(Kenta Oono): Better determination of copy.
is_copied = not ((actual is a) or (actual.base is a) or
(actual.base is a.base and a.base is not None))
self.assertEqual(should_copy, is_copied)
class TestArrayInvalidObject(unittest.TestCase):
def test_invalid_type(self):
a = numpy.array([1, 2, 3], dtype=object)
with self.assertRaises(ValueError):
cupy.array(a)