-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathcudarray.py
More file actions
274 lines (211 loc) · 7.83 KB
/
Copy pathcudarray.py
File metadata and controls
274 lines (211 loc) · 7.83 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
import numpy as np
from .wrap.array_data import ArrayData
from .wrap import array_ops
from . import elementwise
from . import base
from . import helpers
class ndarray(object):
def __init__(self, shape, dtype=None, np_data=None, array_data=None,
array_owner=None):
shape = helpers.require_iterable(shape)
if shape == ():
shape = (1,)
self.shape = shape
self.transposed = False
self.isbool = False
if dtype is None:
if np_data is None:
dtype = np.dtype(base.float_)
else:
dtype = np_data.dtype
if dtype == np.dtype('float64'):
dtype = np.dtype(base.float_)
elif dtype == np.dtype('int64'):
dtype = np.dtype(base.int_)
elif dtype == np.dtype('bool'):
dtype = np.dtype(base.bool_)
self.isbool = True
else:
dtype = np.dtype(dtype)
if np_data is not None:
np_data = np.require(np_data, dtype=dtype, requirements='C')
if array_data is None:
self._data = ArrayData(self.size, dtype, np_data)
else:
self._data = array_data
def __array__(self):
np_array = np.empty(self.shape, dtype=self.dtype)
self._data.to_numpy(np_array)
if self.isbool:
np_array = np_array.astype(np.dtype('bool'))
return np_array
def __str__(self):
return self.__array__().__str__()
def __repr__(self):
return self.__array__().__repr__()
def _same_array(self, other):
return self.data == other.data
@property
def data(self):
return self._data.data
@property
def dtype(self):
return self._data.dtype
@property
def itemsize(self):
return self._data.dtype.itemsize
@property
def nbytes(self):
return self.size*self.itemsize
@property
def ndim(self):
return len(self.shape)
@property
def size(self):
return helpers.prod(self.shape)
@property
def T(self):
return base.transpose(self)
def view(self):
return ndarray(self.shape, self.dtype, None, self._data)
def fill(self, value):
array_ops._fill(self._data, self.size, value)
def __len__(self):
return self.shape[0]
def __add__(self, other):
return elementwise.add(self, other)
def __radd__(self, other):
return elementwise.add(other, self)
def __iadd__(self, other):
return elementwise.add(self, other, self)
def __sub__(self, other):
return elementwise.subtract(self, other)
def __rsub__(self, other):
return elementwise.subtract(other, self)
def __isub__(self, other):
return elementwise.subtract(self, other, self)
def __mul__(self, other):
return elementwise.multiply(self, other)
def __rmul__(self, other):
return elementwise.multiply(other, self)
def __imul__(self, other):
return elementwise.multiply(self, other, self)
def __div__(self, other):
return elementwise.divide(self, other)
def __rdiv__(self, other):
return elementwise.divide(other, self)
def __idiv__(self, other):
return elementwise.divide(self, other, self)
def __truediv__(self, other):
return elementwise.divide(self, other)
def __rtruediv__(self, other):
return elementwise.divide(other, self)
def __itruediv__(self, other):
return elementwise.divide(self, other, self)
def __pow__(self, other):
return elementwise.power(self, other)
def __rpow__(self, other):
return elementwise.power(other, self)
def __ipow__(self, other):
return elementwise.power(self, other, self)
def __eq__(self, other):
return elementwise.equal(self, other)
def __gt__(self, other):
return elementwise.greater(self, other)
def __ge__(self, other):
return elementwise.greater_equal(self, other)
def __lt__(self, other):
return elementwise.less(self, other)
def __le__(self, other):
return elementwise.less_equal(self, other)
def __ne__(self, other):
return elementwise.not_equal(self, other)
def __neg__(self):
return elementwise.negative(self)
def __ineg__(self):
return elementwise.negative(self, self)
def __getitem__(self, indices):
if isinstance(indices, int):
# Speedup case with a single index
view_shape = self.shape[1:]
view_size = helpers.prod(view_shape)
offset = indices * view_size
data_view = ArrayData(view_size, self.dtype, owner=self._data,
offset=offset)
return ndarray(view_shape, self.dtype, np_data=None,
array_data=data_view)
elif isinstance(indices, slice):
indices = (indices,)
# Standardize indices to a list of slices
elif len(indices) > len(self.shape):
raise IndexError('too many indices for array')
view_shape = []
rest_must_be_contiguous = False
offset = 0
for i, dim in enumerate(self.shape):
start = 0
stop = dim
append_dim = True
if i < len(indices):
idx = indices[i]
if isinstance(idx, int):
append_dim = False
start = idx
stop = idx+1
elif isinstance(idx, slice):
if idx.start is not None:
start = idx.start
if idx.stop is not None:
stop = idx.stop
if idx.step is not None:
raise NotImplementedError('only contiguous indices '
+ 'are supported')
elif idx is Ellipsis:
diff = self.ndim - len(indices)
indices = indices[:i] + [slice(None)]*diff + indices[i:]
return self[indices]
else:
raise IndexError('only integers, slices and ellipsis are '
+ 'valid indices')
view_dim = stop-start
offset = offset * dim + start
if append_dim:
view_shape.append(view_dim)
if rest_must_be_contiguous and view_dim < dim:
raise NotImplementedError('only contiguous indices are '
+ 'supported')
if view_dim > 1:
rest_must_be_contiguous = True
view_shape = tuple(view_shape)
view_size = helpers.prod(view_shape)
# Construct view
data_view = ArrayData(view_size, self.dtype, owner=self._data,
offset=offset)
return ndarray(view_shape, self.dtype, np_data=None,
array_data=data_view)
def __setitem__(self, indices, c):
view = self.__getitem__(indices)
base.copyto(view, c)
def array(object, dtype=None, copy=True):
np_array = np.array(object)
return ndarray(np_array.shape, np_data=np_array)
def empty(shape, dtype=None):
return ndarray(shape, dtype=dtype)
def empty_like(a, dtype=None):
if not isinstance(a, (np.ndarray, ndarray)):
a = np.array(a)
return ndarray(a.shape, dtype=a.dtype)
def ones(shape, dtype=None):
return array(np.ones(shape, dtype=dtype))
def ones_like(a, dtype=None):
if not isinstance(a, (np.ndarray, ndarray)):
a = np.array(a)
return array(np.ones_like(a, dtype=dtype))
def zeros(shape, dtype=None):
a = empty(shape, dtype)
a.fill(0)
return a
def zeros_like(a, dtype=None):
if not isinstance(a, (np.ndarray, ndarray)):
a = np.array(a)
return array(np.zeros_like(a, dtype=dtype))