forked from CalebDu/DLsys-hw3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops.py
350 lines (237 loc) · 7.67 KB
/
ops.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
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
"""Operatpr table."""
# Global operator table.
from numbers import Number
from typing import Optional, List
from .autograd import NDArray
from .autograd import Op, Tensor, Value, TensorOp
from .autograd import TensorTuple, TensorTupleOp
import numpy
from .backend_selection import array_api, NDArray
class MakeTensorTuple(TensorTupleOp):
def compute(self, *args) -> tuple:
return tuple(args)
def gradient(self, out_grad, node):
assert isinstance(out_grad, TensorTuple)
return tuple(*[out_grad[i] for i in range(len(out_grad))])
def make_tuple(*args):
return MakeTensorTuple()(*args)
class TupleGetItem(TensorOp):
def __init__(self, index):
self.index = index
def __call__(self, a: TensorTuple, fold_const=True) -> Value:
assert isinstance(a, TensorTuple)
# constant folding
if fold_const and isinstance(a.op, MakeTensorTuple):
return a.inputs[self.index]
return Tensor.make_from_op(self, [a])
def compute(self, a):
return a[self.index]
def gradient(self, out_grad, node):
index = self.index
in_grad = []
for i, value in enumerate(node.inputs[0]):
if i != index:
in_grad.append(init.zeros_like(value))
else:
in_grad.append(out_grad)
return MakeTensorTuple()(*in_grad)
def tuple_get_item(value, index):
return TupleGetItem(index)(value)
class FusedAddScalars(TensorTupleOp):
def __init__(self, c0: float, c1: float):
self.c0 = c0
self.c1 = c1
def compute(self, a):
return a + self.c0, a + self.c1
def gradient(self, out_grad, node):
return out_grad[0] + out_grad[1]
def fused_add_scalars(x, c0, c1):
return FusedAddScalars(c0, c1)(x)
class EWiseAdd(TensorOp):
def compute(self, a: NDArray, b: NDArray):
return a + b
def gradient(self, out_grad: Tensor, node: Tensor):
return out_grad, out_grad
def add(a, b):
return EWiseAdd()(a, b)
class AddScalar(TensorOp):
def __init__(self, scalar):
self.scalar = scalar
def compute(self, a: NDArray):
return a + self.scalar
def gradient(self, out_grad: Tensor, node: Tensor):
return out_grad
def add_scalar(a, scalar):
return AddScalar(scalar)(a)
class EWiseMul(TensorOp):
def compute(self, a: NDArray, b: NDArray):
return a * b
def gradient(self, out_grad: Tensor, node: Tensor):
lhs, rhs = node.inputs
return out_grad * rhs, out_grad * lhs
def multiply(a, b):
return EWiseMul()(a, b)
class MulScalar(TensorOp):
def __init__(self, scalar):
self.scalar = scalar
def compute(self, a: NDArray):
return a * self.scalar
def gradient(self, out_grad: Tensor, node: Tensor):
return (out_grad * self.scalar,)
def mul_scalar(a, scalar):
return MulScalar(scalar)(a)
class PowerScalar(TensorOp):
"""Op raise a tensor to an (integer) power."""
def __init__(self, scalar: int):
self.scalar = scalar
def compute(self, a: NDArray) -> NDArray:
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def gradient(self, out_grad, node):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def power_scalar(a, scalar):
return PowerScalar(scalar)(a)
class EWiseDiv(TensorOp):
"""Op to element-wise divide two nodes."""
def compute(self, a, b):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def gradient(self, out_grad, node):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def divide(a, b):
return EWiseDiv()(a, b)
class DivScalar(TensorOp):
def __init__(self, scalar):
self.scalar = scalar
def compute(self, a):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def gradient(self, out_grad, node):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def divide_scalar(a, scalar):
return DivScalar(scalar)(a)
class Transpose(TensorOp):
def __init__(self, axes: Optional[tuple] = None):
self.axes = axes
def compute(self, a):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def gradient(self, out_grad, node):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def transpose(a, axes=None):
return Transpose(axes)(a)
class Reshape(TensorOp):
def __init__(self, shape):
self.shape = shape
def compute(self, a):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def gradient(self, out_grad, node):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def reshape(a, shape):
return Reshape(shape)(a)
class BroadcastTo(TensorOp):
def __init__(self, shape):
self.shape = shape
def compute(self, a):
return array_api.broadcast_to(a, self.shape)
def gradient(self, out_grad, node):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def broadcast_to(a, shape):
return BroadcastTo(shape)(a)
class Summation(TensorOp):
def __init__(self, axes: Optional[tuple] = None):
self.axes = axes
def compute(self, a):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def gradient(self, out_grad, node):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def summation(a, axes=None):
return Summation(axes)(a)
class MatMul(TensorOp):
def compute(self, a, b):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def gradient(self, out_grad, node):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def matmul(a, b):
return MatMul()(a, b)
class Negate(TensorOp):
def compute(self, a):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def gradient(self, out_grad, node):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def negate(a):
return Negate()(a)
class Log(TensorOp):
def compute(self, a):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def gradient(self, out_grad, node):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def log(a):
return Log()(a)
class Exp(TensorOp):
def compute(self, a):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def gradient(self, out_grad, node):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def exp(a):
return Exp()(a)
# TODO
class ReLU(TensorOp):
def compute(self, a):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def gradient(self, out_grad, node):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def relu(a):
return ReLU()(a)
class LogSumExp(TensorOp):
def __init__(self, axes: Optional[tuple] = None):
self.axes = axes
def compute(self, Z):
### BEGIN YOUR SOLUTION
raise NotImplementedError()
### END YOUR SOLUTION
def gradient(self, out_grad, node):
### BEGIN YOUR SOLUTION
raise NotImplementedError()