/
nodes.py
274 lines (213 loc) · 7.07 KB
/
nodes.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
from collections import deque
import numpy as np
class Node(np.ndarray):
def __new__(subtype, shape,
dtype=float,
buffer=None,
offset=0,
strides=None,
order=None):
"""
craetes a new object that wraps an numpy.ndarray into a structure that
represents a node in a computational graph
"""
newobj = np.ndarray.__new__(
subtype, shape, dtype,
buffer, offset, strides,
order
)
return newobj
def _nodify(self, method_name, other, opname, self_first=True):
"""
augments the operation of given arithmetic super method
by creating and returning an OperationalNode for the operation
Parameters:
----------
method_name: String
the name of the super method to be augmented
other: Node | np.ndarray | Number
the other operand to the operation
opname: String
the name of OperationalNode
self_first: Boolean
a flag indicating if self is the 1st operand in non commutative ops
Returns: OperationalNode
"""
if not isinstance(other, Node):
other = ConstantNode.create_using(other)
opvalue = getattr(np.ndarray, method_name)(self, other)
return OperationalNode.create_using(opvalue, opname,
self if self_first else other,
other if self_first else self
)
def __add__(self, other):
return self._nodify('__add__', other, 'add')
def __radd__(self, other):
return self._nodify('__radd__', other, 'add')
def __sub__(self, other):
return self._nodify('__sub__', other, 'sub')
def __rsub__(self, other):
return self._nodify('__rsub__', other, 'sub', False)
def __mul__(self, other):
return self._nodify('__mul__', other, 'mul')
def __rmul__(self, other):
return self._nodify('__rmul__', other, 'mul')
def __div__(self, other):
return self._nodify('__div__', other, 'div')
def __rdiv__(self, other):
return self._nodify('__rdiv__', other, 'div', False)
def __truediv__(self, other):
return self._nodify('__truediv__', other, 'div')
def __rtruediv__(self, other):
return self._nodify('__rtruediv__', other, 'div', False)
def __pow__(self, other):
return self._nodify('__pow__', other, 'pow')
def __rpow__(self, other):
return self._nodify('__rpow__', other, 'pow', False)
@property
def T(self):
"""
augments numpy's T attribute by creating a node for the operation
"""
opvalue = np.transpose(self)
return OperationalNode.create_using(opvalue, 'transpose', self)
class OperationalNode(Node):
# a static attribute to count for unnamed nodes
nodes_counter = {}
@staticmethod
def create_using(opresult, opname, operand_a, operand_b=None, name=None):
"""
craetes an graph node representing an operation
Parameters:
----------
opresult: np.ndarray
the result of the operation
opname: String
the name of the operation
operand_a: Node
the first operand to the operation
operand_b: Node
the second operand to the operation if any
name: String
the name of the node
Returns: OperationalNode
"""
obj = OperationalNode(
strides=opresult.strides,
shape=opresult.shape,
dtype=opresult.dtype,
buffer=np.copy(opresult)
)
obj.opname = opname
obj.operand_a = operand_a
obj.operand_b = operand_b
if name is not None:
obj.name = name
else:
if opname not in OperationalNode.nodes_counter:
OperationalNode.nodes_counter[opname] = 0
node_id = OperationalNode.nodes_counter[opname]
OperationalNode.nodes_counter[opname] += 1
obj.name = "%s_%d" % (opname, node_id)
return obj
class ConstantNode(Node):
# a static attribute to count the unnamed instances
count = 0
@staticmethod
def create_using(val, name=None):
"""
creates a graph node representing a constant
Parameters:
----------
val: np.ndarray | Number
the value of the constant
name: String
the node's name
"""
if not isinstance(val, np.ndarray):
val = np.array(val, dtype=float)
obj = ConstantNode(
strides=val.strides,
shape=val.shape,
dtype=val.dtype,
buffer=val
)
if name is not None:
obj.name = name
else:
obj.name = "const_%d" % (ConstantNode.count)
ConstantNode.count += 1
return obj
class VariableNode(Node):
# a static attribute to count the unnamed instances
count = 0
@staticmethod
def create_using(val, name=None):
"""
creates a graph node representing a variable
Parameters:
----------
val: np.ndarray | Number
the value of the constant
name: String
the node's name
"""
if not isinstance(val, np.ndarray):
val = np.array(val, dtype=float)
obj = VariableNode(
strides=val.strides,
shape=val.shape,
dtype=val.dtype,
buffer=val
)
if name is not None:
obj.name = name
else:
obj.name = "_%d" % (VariableNode.count)
VariableNode.count += 1
return obj
class NodesQueue:
def __init__(self):
"""
creates an object that runs two parallel queus, one for the nodes
and the other for the node names, this captures the uniqueness of
a node via name even if it shares the same value as another
"""
self.nodes = deque()
self.nodes_ids = deque()
def push(self, node):
"""
pushes a given node, along with its name, to the queue
Parameters:
----------
node: Node
the node to be pushed
"""
self.nodes.append(node)
self.nodes_ids.append(node.name)
def pop(self):
"""
pops the front node from the queue, along with its name
Returns: Node
"""
node = self.nodes.popleft()
self.nodes_ids.popleft()
return node
def __contains__(self, node):
"""
implements the searching operator via `in` by searching in the names
queue instead of the nodes themselves queue to capture unique nodes
with exact numerical values
Parameters:
----------
node: Node
the node to search for
Returns: Boolean
"""
return node.name in self.nodes_ids
def __len__(self):
"""
returns the length on any of the underlying deques
Returns: int
"""
return len(self.nodes)