-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathndarray.py
More file actions
executable file
·606 lines (477 loc) · 19.7 KB
/
Copy pathndarray.py
File metadata and controls
executable file
·606 lines (477 loc) · 19.7 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
import operator
import math
from functools import reduce
import numpy as np
from . import ndarray_backend_numpy
from . import ndarray_backend_cpu
# math.prod not in Python 3.7
def prod(x):
return reduce(operator.mul, x, 1)
class BackendDevice:
"""A backend device, wrapps the implementation module."""
def __init__(self, name, mod):
self.name = name
self.mod = mod
def __eq__(self, other):
return self.name == other.name
def __repr__(self):
return self.name + "()"
def __getattr__(self, name):
return getattr(self.mod, name)
def enabled(self):
return self.mod is not None
def randn(self, *shape, dtype="float32"):
# note: numpy doesn't support types within standard random routines, and
# .astype("float32") does work if we're generating a singleton
return NDArray(np.random.randn(*shape).astype(dtype), device=self)
def rand(self, *shape, dtype="float32"):
# note: numpy doesn't support types within standard random routines, and
# .astype("float32") does work if we're generating a singleton
return NDArray(np.random.rand(*shape).astype(dtype), device=self)
def one_hot(self, n, i, dtype="float32"):
return NDArray(np.eye(n, dtype=dtype)[i], device=self)
def empty(self, shape, dtype="float32"):
dtype = "float32" if dtype is None else dtype
assert dtype == "float32"
return NDArray.make(shape, device=self)
def full(self, shape, fill_value, dtype="float32"):
dtype = "float32" if dtype is None else dtype
assert dtype == "float32"
arr = self.empty(shape, dtype)
arr.fill(fill_value)
return arr
def cuda():
"""Return cuda device"""
try:
from . import ndarray_backend_cuda
return BackendDevice("cuda", ndarray_backend_cuda)
except ImportError:
return BackendDevice("cuda", None)
def cpu_numpy():
"""Return numpy device"""
return BackendDevice("cpu_numpy", ndarray_backend_numpy)
def cpu():
"""Return cpu device"""
return BackendDevice("cpu", ndarray_backend_cpu)
def default_device():
return cpu_numpy()
def all_devices():
"""return a list of all available devices"""
return [cpu(), cuda(), cpu_numpy()]
class NDArray:
"""A generic ND array class that may contain multipe different backends
i.e., a Numpy backend, a native CPU backend, or a GPU backend.
This class will only contains those functions that you need to implement
to actually get the desired functionality for the programming examples
in the homework, and no more.
For now, for simplicity the class only supports float32 types, though
this can be extended if desired.
"""
def __init__(self, other, device=None):
"""Create by copying another NDArray, or from numpy"""
if isinstance(other, NDArray):
# create a copy of existing NDArray
if device is None:
device = other.device
self._init(other.to(device) + 0.0) # this creates a copy
elif isinstance(other, np.ndarray):
# create copy from numpy array
device = device if device is not None else default_device()
array = self.make(other.shape, device=device)
array.device.from_numpy(np.ascontiguousarray(other), array._handle)
self._init(array)
else:
# see if we can create a numpy array from input
array = NDArray(np.array(other), device=device)
self._init(array)
def _init(self, other):
self._shape = other._shape
self._strides = other._strides
self._offset = other._offset
self._device = other._device
self._handle = other._handle
@staticmethod
def compact_strides(shape):
"""Utility function to compute compact strides"""
stride = 1
res = []
for i in range(1, len(shape) + 1):
res.append(stride)
stride *= shape[-i]
return tuple(res[::-1])
@staticmethod
def make(shape, strides=None, device=None, handle=None, offset=0):
"""Create a new NDArray with the given properties. This will allocation the
memory if handle=None, otherwise it will use the handle of an existing
array."""
array = NDArray.__new__(NDArray)
array._shape = tuple(shape)
array._strides = NDArray.compact_strides(shape) if strides is None else strides
array._offset = offset
array._device = device if device is not None else default_device()
if handle is None:
array._handle = array.device.Array(prod(shape))
else:
array._handle = handle
return array
### Properies and string representations
@property
def shape(self):
return self._shape
@property
def strides(self):
return self._strides
@property
def device(self):
return self._device
@property
def dtype(self):
# only support float32 for now
return "float32"
@property
def ndim(self):
"""Return number of dimensions."""
return len(self._shape)
@property
def size(self):
return prod(self._shape)
def __repr__(self):
return "NDArray(" + self.numpy().__str__() + f", device={self.device})"
def __str__(self):
return self.numpy().__str__()
### Basic array manipulation
def fill(self, value):
"""Fill (in place) with a constant value."""
self._device.fill(self._handle, value)
def to(self, device):
"""Convert between devices, using to/from numpy calls as the unifying bridge."""
if device == self.device:
return self
else:
return NDArray(self.numpy(), device=device)
def numpy(self):
"""convert to a numpy array"""
return self.device.to_numpy(
self._handle, self.shape, self.strides, self._offset
)
def is_compact(self):
"""Return true if array is compact in memory and internal size equals product
of the shape dimensions"""
return (
self._strides == self.compact_strides(self._shape)
and prod(self.shape) == self._handle.size
)
def compact(self):
"""Convert a matrix to be compact"""
if self.is_compact():
return self
else:
out = NDArray.make(self.shape, device=self.device)
self.device.compact(
self._handle, out._handle, self.shape, self.strides, self._offset
)
return out
def as_strided(self, shape, strides):
"""Restride the matrix without copying memory."""
assert len(shape) == len(strides)
return NDArray.make(
shape, strides=strides, device=self.device, handle=self._handle
)
@property
def flat(self):
return self.reshape((self.size,))
def reshape(self, new_shape):
"""
Reshape the matrix without copying memory. This will return a matrix
that corresponds to a reshaped array but points to the same memory as
the original array.
Raises:
ValueError if product of current shape is not equal to the product
of the new shape, or if the matrix is not compact.
Args:
new_shape (tuple): new shape of the array
Returns:
NDArray : reshaped array; this will point to thep
"""
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def permute(self, new_axes):
"""
Permute order of the dimensions. new_axes describes a permuation of the
existing axes, so e.g.:
- If we have an array with dimension "BHWC" then .permute((0,3,1,2))
would convert this to "BCHW" order.
- For a 2D array, .permute((1,0)) would transpose the array.
Like reshape, this operation should not copy memory, but achieves the
permuting by just adjusting the shape/strides of the array. That is,
it returns a new array that has the dimensions permuted as desired, but
which points to the same memroy as the original array.
Args:
new_axes (tuple): permuation order of the dimensions
Returns:
NDarray : new NDArray object with permuted dimensions, pointing
to the same memory as the original NDArray (i.e., just shape and
strides changed).
"""
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def broadcast_to(self, new_shape):
"""
Broadcast an array to a new shape. new_shape's elements must be the
same as the original shape, except for dimensions in the self where
the size = 1 (which can then be broadcast to any size). As with the
previous calls, this will not copy memory, and just achieves
broadcasting by manipulating the strides.
Raises:
assertion error if new_shape[i] != shape[i] for all i where
shape[i] != 1
Args:
new_shape (tuple): shape to broadcast to
Returns:
NDArray: the new NDArray object with the new broadcast shape; should
point to the same memory as the original array.
"""
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
### Get and set elements
def process_slice(self, sl, dim):
"""Convert a slice to an explicit start/stop/step"""
start, stop, step = sl.start, sl.stop, sl.step
if start == None:
start = 0
if start < 0:
start = self.shape[dim]
if stop == None:
stop = self.shape[dim]
if stop < 0:
stop = self.shape[dim] + stop
if step == None:
step = 1
# we're not gonna handle negative strides and that kind of thing
assert stop > start, "Start must be less than stop"
assert step > 0, "No support for negative increments"
return slice(start, stop, step)
def __getitem__(self, idxs):
"""
The __getitem__ operator in Python allows us to access elements of our
array. When passed notation such as a[1:5,:-1:2,4,:] etc, Python will
convert this to a tuple of slices and integers (for singletons like the
'4' in this example). Slices can be a bit odd to work with (they have
three elements .start .stop .step), which can be None or have negative
entries, so for simplicity we wrote the code for you to convert these
to always be a tuple of slices, one of each dimension.
For this tuple of slices, return an array that subsets the desired
elements. As before, this can be done entirely through compute a new
shape, stride, and offset for the new "view" into the original array,
pointing to the same memory
Raises:
AssertionError if a slice has negative size or step, or if number
of slices is not equal to the number of dimension (the stub code
already raises all these errors.
Args:
idxs tuple: (after stub code processes), a tuple of slice elements
coresponding to the subset of the matrix to get
Returns:
NDArray: a new NDArray object corresponding to the selected
subset of elements. As before, this should not copy memroy but just
manipulate the shape/strides/offset of the new array, referecing
the same array as the original one.
"""
# handle singleton as tuple, everything as slices
if not isinstance(idxs, tuple):
idxs = (idxs,)
idxs = tuple(
[
self.process_slice(s, i) if isinstance(s, slice) else slice(s, s + 1, 1)
for i, s in enumerate(idxs)
]
)
assert len(idxs) == self.ndim, "Need indexes equal to number of dimensions"
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def __setitem__(self, idxs, other):
"""Set the values of a view into an array, using the same semantics
as __getitem__()."""
view = self.__getitem__(idxs)
if isinstance(other, NDArray):
assert prod(view.shape) == prod(other.shape)
self.device.ewise_setitem(
other.compact()._handle,
view._handle,
view.shape,
view.strides,
view._offset,
)
else:
self.device.scalar_setitem(
prod(view.shape),
other,
view._handle,
view.shape,
view.strides,
view._offset,
)
### Collection of elementwise and scalar function: add, multiply, boolean, etc
def ewise_or_scalar(self, other, ewise_func, scalar_func):
"""Run either an elementwise or scalar version of a function,
depending on whether "other" is an NDArray or scalar
"""
out = NDArray.make(self.shape, device=self.device)
if isinstance(other, NDArray):
assert self.shape == other.shape, "operation needs two equal-sized arrays"
ewise_func(self.compact()._handle, other.compact()._handle, out._handle)
else:
scalar_func(self.compact()._handle, other, out._handle)
return out
def __add__(self, other):
return self.ewise_or_scalar(
other, self.device.ewise_add, self.device.scalar_add
)
__radd__ = __add__
def __sub__(self, other):
return self + (-other)
def __rsub__(self, other):
return other + (-self)
def __mul__(self, other):
return self.ewise_or_scalar(
other, self.device.ewise_mul, self.device.scalar_mul
)
__rmul__ = __mul__
def __truediv__(self, other):
return self.ewise_or_scalar(
other, self.device.ewise_div, self.device.scalar_div
)
def __neg__(self):
return self * (-1)
def __pow__(self, other):
out = NDArray.make(self.shape, device=self.device)
self.device.scalar_power(self.compact()._handle, other, out._handle)
return out
def maximum(self, other):
return self.ewise_or_scalar(
other, self.device.ewise_maximum, self.device.scalar_maximum
)
### Binary operators all return (0.0, 1.0) floating point values, could of course be optimized
def __eq__(self, other):
return self.ewise_or_scalar(other, self.device.ewise_eq, self.device.scalar_eq)
def __ge__(self, other):
return self.ewise_or_scalar(other, self.device.ewise_ge, self.device.scalar_ge)
def __ne__(self, other):
return 1 - (self == other)
def __gt__(self, other):
return (self >= other) * (self != other)
def __lt__(self, other):
return 1 - (self >= other)
def __le__(self, other):
return 1 - (self > other)
### Elementwise functions
def log(self):
out = NDArray.make(self.shape, device=self.device)
self.device.ewise_log(self.compact()._handle, out._handle)
return out
def exp(self):
out = NDArray.make(self.shape, device=self.device)
self.device.ewise_exp(self.compact()._handle, out._handle)
return out
def tanh(self):
out = NDArray.make(self.shape, device=self.device)
self.device.ewise_tanh(self.compact()._handle, out._handle)
return out
### Matrix multiplication
def __matmul__(self, other):
"""Matrix multplication of two arrays. This requires that both arrays
be 2D (i.e., we don't handle batch matrix multiplication), and that the
sizes match up properly for matrix multiplication.
In the case of the CPU backend, you will implement an efficient "tiled"
version of matrix multiplication for the case when all dimensions of
the array are divisible by self.device.__tile_size__. In this case,
the code below will restride and compact the matrix into tiled form,
and then pass to the relevant CPU backend. For the CPU version we will
just fall back to the naive CPU implementation if the array shape is not
a multiple of the tile size
The GPU (and numpy) versions don't have any tiled version (or rather,
the GPU version will just work natively by tiling any input size).
"""
assert self.ndim == 2 and other.ndim == 2
assert self.shape[1] == other.shape[0]
m, n, p = self.shape[0], self.shape[1], other.shape[1]
# if the matrix is aligned, use tiled matrix multiplication
if hasattr(self.device, "matmul_tiled") and all(
d % self.device.__tile_size__ == 0 for d in (m, n, p)
):
def tile(a, tile):
return a.as_strided(
(a.shape[0] // tile, a.shape[1] // tile, tile, tile),
(a.shape[1] * tile, tile, a.shape[1], 1),
)
t = self.device.__tile_size__
a = tile(self.compact(), t).compact()
b = tile(other.compact(), t).compact()
out = NDArray.make((a.shape[0], b.shape[1], t, t), device=self.device)
self.device.matmul_tiled(a._handle, b._handle, out._handle, m, n, p)
return (
out.permute((0, 2, 1, 3))
.compact()
.reshape((self.shape[0], other.shape[1]))
)
else:
out = NDArray.make((m, p), device=self.device)
self.device.matmul(
self.compact()._handle, other.compact()._handle, out._handle, m, n, p
)
return out
### Reductions, i.e., sum/max over all element or over given axis
def reduce_view_out(self, axis):
"""Return a view to the array set up for reduction functions and output array."""
if axis is None:
view = self.reshape((1,) * (self.ndim - 1) + (prod(self.shape),))
out = NDArray.make((1,) * self.ndim, device=self.device)
else:
if isinstance(axis, (tuple, list)):
assert len(axis) == 1, "Only support reduction over a single axis"
axis = axis[0]
view = self.permute(
tuple([a for a in range(self.ndim) if a != axis]) + (axis,)
)
out = NDArray.make(
tuple([1 if i == axis else s for i, s in enumerate(self.shape)]),
device=self.device,
)
return view, out
def sum(self, axis=None):
view, out = self.reduce_view_out(axis)
self.device.reduce_sum(view.compact()._handle, out._handle, view.shape[-1])
return out
def max(self, axis=None):
view, out = self.reduce_view_out(axis)
self.device.reduce_max(view.compact()._handle, out._handle, view.shape[-1])
return out
def array(a, dtype="float32", device=None):
"""Convenience methods to match numpy a bit more closely."""
dtype = "float32" if dtype is None else dtype
assert dtype == "float32"
return NDArray(a, device=device)
def empty(shape, dtype="float32", device=None):
device = device if device is not None else default_device()
return device.empty(shape, dtype)
def full(shape, fill_value, dtype="float32", device=None):
device = device if device is not None else default_device()
return device.full(shape, fill_value, dtype)
def broadcast_to(array, new_shape):
return array.broadcast_to(new_shape)
def reshape(array, new_shape):
return array.reshape(new_shape)
def maximum(a, b):
return a.maximum(b)
def log(a):
return a.log()
def exp(a):
return a.exp()
def tanh(a):
return a.tanh()
def sum(a, axis=None):
return a.sum(axis=axis)