-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathauto_diff.py
More file actions
427 lines (324 loc) · 13.2 KB
/
auto_diff.py
File metadata and controls
427 lines (324 loc) · 13.2 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
from typing import Any, Dict, List
import numpy as np
class Node:
"""Node in a computational graph.
Fields
------
inputs: List[Node]
The list of input nodes to this node.
op: Op
The op of this node.
attrs: Dict[str, Any]
The attribute dictionary of this node.
E.g. "constant" is the constant operand of add_by_const.
name: str
Name of the node for debugging purposes.
"""
inputs: List["Node"]
op: "Op"
attrs: Dict[str, Any]
name: str
def __init__(
self, inputs: List["Node"], op: "Op", attrs: Dict[str, Any] = {}, name: str = ""
) -> None:
self.inputs = inputs
self.op = op
self.attrs = attrs
self.name = name
def __add__(self, other):
if isinstance(other, Node):
return add(self, other)
else:
assert isinstance(other, (int, float))
return add_by_const(self, other)
def __sub__(self, other):
return self + (-1) * other
def __rsub__(self, other):
return (-1) * self + other
def __mul__(self, other):
if isinstance(other, Node):
return mul(self, other)
else:
assert isinstance(other, (int, float))
return mul_by_const(self, other)
def __truediv__(self, other):
if isinstance(other, Node):
return div(self, other)
else:
assert isinstance(other, (int, float))
return div_by_const(self, other)
# Allow left-hand-side add and multiplication.
__radd__ = __add__
__rmul__ = __mul__
def __str__(self):
"""Allow printing the node name."""
return self.name
def __getattr__(self, attr_name: str) -> Any:
if attr_name in self.attrs:
return self.attrs[attr_name]
raise KeyError(f"Attribute {attr_name} does not exist in node {self}")
__repr__ = __str__
class Variable(Node):
"""A variable node with given name."""
def __init__(self, name: str) -> None:
super().__init__(inputs=[], op=placeholder, name=name)
class Op:
"""The class of operations performed on nodes."""
def __call__(self, *kwargs) -> Node:
"""Create a new node with this current op.
Returns
-------
The created new node.
"""
raise NotImplementedError
def compute(self, node: Node, input_values: List[np.ndarray]) -> np.ndarray:
"""Compute the output value of the given node with its input
node values given.
Parameters
----------
node: Node
The node whose value is to be computed
input_values: List[np.ndarray]
The input values of the given node.
Returns
-------
output: np.ndarray
The computed output value of the node.
"""
raise NotImplementedError
def gradient(self, node: Node, output_grad: Node) -> List[Node]:
"""Given a node and its output gradient node, compute partial
adjoints with regards to each input node.
Parameters
----------
node: Node
The node whose inputs' partial adjoints are to be computed.
output_grad: Node
The output gradient with regard to given node.
Returns
-------
input_grads: List[Node]
The list of partial gradients with regard to each input of the node.
"""
raise NotImplementedError
class PlaceholderOp(Op):
"""The placeholder op to denote computational graph input nodes."""
def __call__(self, name: str) -> Node:
return Node(inputs=[], op=self, name=name)
def compute(self, node: Node, input_values: List[np.ndarray]) -> np.ndarray:
raise RuntimeError(
"Placeholder nodes have no inputs, and there values cannot be computed."
)
def gradient(self, node: Node, output_grad: Node) -> List[Node]:
raise RuntimeError("Placeholder nodes have no inputs.")
class AddOp(Op):
"""Op to element-wise add two nodes."""
def __call__(self, node_A: Node, node_B: Node) -> Node:
return Node(
inputs=[node_A, node_B],
op=self,
name=f"({node_A.name}+{node_B.name})",
)
def compute(self, node: Node, input_values: List[np.ndarray]) -> np.ndarray:
"""Return the element-wise addition of input values."""
assert len(input_values) == 2
return input_values[0] + input_values[1]
def gradient(self, node: Node, output_grad: Node) -> List[Node]:
"""Given gradient of add node, return partial adjoint to each input."""
return [output_grad, output_grad]
class AddByConstOp(Op):
"""Op to element-wise add a node by a constant."""
def __call__(self, node_A: Node, const_val: float) -> Node:
return Node(
inputs=[node_A],
op=self,
attrs={"constant": const_val},
name=f"({node_A.name}+{const_val})",
)
def compute(self, node: Node, input_values: List[np.ndarray]) -> np.ndarray:
"""Return the element-wise addition of the input value and the constant."""
assert len(input_values) == 1
return input_values[0] + node.constant
def gradient(self, node: Node, output_grad: Node) -> List[Node]:
"""Given gradient of add node, return partial adjoint to the input."""
return [output_grad]
class MulOp(Op):
"""Op to element-wise multiply two nodes."""
def __call__(self, node_A: Node, node_B: Node) -> Node:
return Node(
inputs=[node_A, node_B],
op=self,
name=f"({node_A.name}*{node_B.name})",
)
def compute(self, node: Node, input_values: List[np.ndarray]) -> np.ndarray:
"""Return the element-wise multiplication of input values."""
"""TODO: Your code here"""
def gradient(self, node: Node, output_grad: Node) -> List[Node]:
"""Given gradient of multiplication node, return partial adjoint to each input."""
"""TODO: Your code here"""
class MulByConstOp(Op):
"""Op to element-wise multiply a node by a constant."""
def __call__(self, node_A: Node, const_val: float) -> Node:
return Node(
inputs=[node_A],
op=self,
attrs={"constant": const_val},
name=f"({node_A.name}*{const_val})",
)
def compute(self, node: Node, input_values: List[np.ndarray]) -> np.ndarray:
"""Return the element-wise multiplication of the input value and the constant."""
"""TODO: Your code here"""
def gradient(self, node: Node, output_grad: Node) -> List[Node]:
"""Given gradient of multiplication node, return partial adjoint to the input."""
"""TODO: Your code here"""
class DivOp(Op):
"""Op to element-wise divide two nodes."""
def __call__(self, node_A: Node, node_B: Node) -> Node:
return Node(
inputs=[node_A, node_B],
op=self,
name=f"({node_A.name}/{node_B.name})",
)
def compute(self, node: Node, input_values: List[np.ndarray]) -> np.ndarray:
"""Return the element-wise division of input values."""
"""TODO: Your code here"""
def gradient(self, node: Node, output_grad: Node) -> List[Node]:
"""Given gradient of division node, return partial adjoint to each input."""
"""TODO: Your code here"""
class DivByConstOp(Op):
"""Op to element-wise divide a nodes by a constant."""
def __call__(self, node_A: Node, const_val: float) -> Node:
return Node(
inputs=[node_A],
op=self,
attrs={"constant": const_val},
name=f"({node_A.name}/{const_val})",
)
def compute(self, node: Node, input_values: List[np.ndarray]) -> np.ndarray:
"""Return the element-wise division of the input value and the constant."""
"""TODO: Your code here"""
def gradient(self, node: Node, output_grad: Node) -> List[Node]:
"""Given gradient of division node, return partial adjoint to the input."""
"""TODO: Your code here"""
class MatMulOp(Op):
"""Matrix multiplication op of two nodes."""
def __call__(
self, node_A: Node, node_B: Node, trans_A: bool = False, trans_B: bool = False
) -> Node:
"""Create a matrix multiplication node.
Parameters
----------
node_A: Node
The lhs matrix.
node_B: Node
The rhs matrix
trans_A: bool
A boolean flag denoting whether to transpose A before multiplication.
trans_B: bool
A boolean flag denoting whether to transpose B before multiplication.
Returns
-------
result: Node
The node of the matrix multiplication.
"""
return Node(
inputs=[node_A, node_B],
op=self,
attrs={"trans_A": trans_A, "trans_B": trans_B},
name=f"({node_A.name + ('.T' if trans_A else '')}@{node_B.name + ('.T' if trans_B else '')})",
)
def compute(self, node: Node, input_values: List[np.ndarray]) -> np.ndarray:
"""Return the matrix multiplication result of input values.
Note
----
For this assignment, you can assume the matmul only works for 2d matrices.
That being said, the test cases guarantee that input values are
always 2d numpy.ndarray.
"""
"""TODO: Your code here"""
def gradient(self, node: Node, output_grad: Node) -> List[Node]:
"""Given gradient of matmul node, return partial adjoint to each input.
Note
----
- Same as the `compute` method, you can assume that the input are 2d matrices.
However, it would be a good exercise to think about how to handle
more general cases, i.e., when input can be either 1d vectors,
2d matrices, or multi-dim tensors.
- You may want to look up some materials for the gradients of matmul.
"""
"""TODO: Your code here"""
class ZerosLikeOp(Op):
"""Zeros-like op that returns an all-zero array with the same shape as the input."""
def __call__(self, node_A: Node) -> Node:
return Node(inputs=[node_A], op=self, name=f"ZerosLike({node_A.name})")
def compute(self, node: Node, input_values: List[np.ndarray]) -> np.ndarray:
"""Return an all-zero tensor with the same shape as input."""
assert len(input_values) == 1
return np.zeros(input_values[0].shape)
def gradient(self, node: Node, output_grad: Node) -> List[Node]:
return [zeros_like(node.inputs[0])]
class OnesLikeOp(Op):
"""Ones-like op that returns an all-one array with the same shape as the input."""
def __call__(self, node_A: Node) -> Node:
return Node(inputs=[node_A], op=self, name=f"OnesLike({node_A.name})")
def compute(self, node: Node, input_values: List[np.ndarray]) -> np.ndarray:
"""Return an all-one tensor with the same shape as input."""
assert len(input_values) == 1
return np.ones(input_values[0].shape)
def gradient(self, node: Node, output_grad: Node) -> List[Node]:
return [zeros_like(node.inputs[0])]
# Create global instances of ops.
# Your implementation should just use these instances, rather than creating new instances.
placeholder = PlaceholderOp()
add = AddOp()
mul = MulOp()
div = DivOp()
add_by_const = AddByConstOp()
mul_by_const = MulByConstOp()
div_by_const = DivByConstOp()
matmul = MatMulOp()
zeros_like = ZerosLikeOp()
ones_like = OnesLikeOp()
class Evaluator:
"""The node evaluator that computes the values of nodes in a computational graph."""
eval_nodes: List[Node]
def __init__(self, eval_nodes: List[Node]) -> None:
"""Constructor, which takes the list of nodes to evaluate in the computational graph.
Parameters
----------
eval_nodes: List[Node]
The list of nodes whose values are to be computed.
"""
self.eval_nodes = eval_nodes
def run(self, input_values: Dict[Node, np.ndarray]) -> List[np.ndarray]:
"""Computes values of nodes in `eval_nodes` field with
the computational graph input values given by the `input_values` dict.
Parameters
----------
input_values: Dict[Node, np.ndarray]
The dictionary providing the values for input nodes of the
computational graph.
Throw ValueError when the value of any needed input node is
not given in the dictionary.
Returns
-------
eval_values: List[np.ndarray]
The list of values for nodes in `eval_nodes` field.
"""
"""TODO: Your code here"""
def gradients(output_node: Node, nodes: List[Node]) -> List[Node]:
"""Construct the backward computational graph, which takes gradient
of given output node with respect to each node in input list.
Return the list of gradient nodes, one for each node in the input list.
Parameters
----------
output_node: Node
The output node to take gradient of, whose gradient is 1.
nodes: List[Node]
The list of nodes to take gradient with regard to.
Returns
-------
grad_nodes: List[Node]
A list of gradient nodes, one for each input nodes respectively.
"""
"""TODO: Your code here"""