forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
721 lines (614 loc) · 16.9 KB
/
node.go
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
package gorgonia
import (
"bytes"
"encoding/binary"
"fmt"
"hash"
"hash/fnv"
"unsafe"
"github.com/awalterschulze/gographviz"
"github.com/chewxy/gorgonia/tensor"
"github.com/chewxy/hm"
"github.com/pkg/errors"
)
// A Node is a node in the computation graph
type Node struct {
// metadata of the node
t hm.Type // pruned types only plz
shape tensor.Shape
// this node is the result of applying the op to the children
op Op
children Nodes // shortcut, instead of having to go through the graph
// For nicely grouping stuff in graphviz.
// TODO: Should this be in *Node?
name string
group string
g *ExprGraph // this node belongs in this graph
// value bondage
// inputs are bound to values directly
boundTo Value
// to track derivations
derivOf Nodes
deriv *Node
// for hashing nodes
hash uint32
hashed bool
inferredShape bool // is shape inferred?
unchanged bool // has this node been modified
isStmt bool // is this a statement node
ofInterest bool // is this node of particular interest? (for debugging)
}
// NodeConsOpt is a function that provides construction options for any Node.
type NodeConsOpt func(*Node)
// WithType is a node construction option to set a node to the specified type.
// Types in *Node are immutable once set. If the type has already been specified in the node,
// a check will be made to see if the both types are the same. If it isn't, it will panic.
func WithType(t hm.Type) NodeConsOpt {
f := func(n *Node) {
if n.t == nil {
n.t = t
} else if !n.t.Eq(t) {
panic(fmt.Sprintf("Node's type is %v. Asking to construct a Node with %v", n.t, t))
}
}
return f
}
// WithChildren sets the children of a node to the specified chidren.
// This construction option does NOT check if existing children exists, and will overwrite the existing children.
func WithChildren(children Nodes) NodeConsOpt {
f := func(n *Node) {
n.children = children
}
return f
}
// WithOp is a node construction option to set a node's Op to the specified Op.
// `Op`s in `*Node`s are immutable once set and cannot be changed. If the node already has an Op specified
// a check will be made to see if the provided Op and the one already specified in the `*Node` is the same -
// do note that comparison of Ops is done using the `Hashcode()` method of Ops, and hash collisions MAY occur -
// If both ops are different, this function will panic.
func WithOp(op Op) NodeConsOpt {
f := func(n *Node) {
if n.op != nil {
if op.Hashcode() != n.op.Hashcode() {
panic(fmt.Sprintf("Node Ops are immutable. Cannot set op %v", op))
}
return
}
n.op = op
if _, ok := op.(stmtOp); ok {
n.isStmt = true
}
}
return f
}
// In is a node construction option to set a node's graph.
// A `*Node`'s graph is immutable. If the graph has already been set, a check will be made that the specifiec *Graph
// and the *Graph set in *Node are the same. If they are not, the function will panic/
func In(g *ExprGraph) NodeConsOpt {
f := func(n *Node) {
if n.g != nil {
if g != n.g {
panic(fmt.Sprintf("Node Graphs are immutable. Cannot set g %v", g))
}
}
n.g = g
}
return f
}
// WithName is a node construction option that gives the *Node the provided name. This is especially useful in debugging graphs.
func WithName(name string) NodeConsOpt {
f := func(n *Node) {
n.name = name
}
return f
}
// WithValue is a node construction option that binds the value to the *Node. This function may panic if:
// - Gorgonia was unable to convert interface{} into a Value.
// - The type of the Value does not match the type of the nodes.
func WithValue(any interface{}) NodeConsOpt {
v, t, _, err := anyToValue(any)
if err != nil {
panic(err)
}
f := func(n *Node) {
if n.t == nil {
n.t = t
} else if !n.t.Eq(t) {
panic(fmt.Sprintf("TypeError: Want %v, Got %v instead", n.t, t)) // yes this is a runtime error
}
n.bind(v)
if n.shape == nil {
n.shape = v.Shape()
}
}
return f
}
// WithInit is a node construction option to initialize a *Node with the InitWFn provided.
func WithInit(fn InitWFn) NodeConsOpt {
f := func(n *Node) {
dt, err := dtypeOf(n.t)
if err != nil {
panic(err)
}
var v Value
v = tensor.New(tensor.WithShape(n.shape...), tensor.WithBacking(fn(dt, n.shape...)))
WithValue(v)(n)
}
return f
}
// WithShape is a node construction option to initialize a *Node with a particular shape.
// This function panics if the shape's dimensions do not match the specified dimensions of the *Node.
func WithShape(shp ...int) NodeConsOpt {
s := tensor.Shape(shp)
f := func(n *Node) {
nd := n.Dims()
// if nd == 1 && s.IsVector() {
// goto safe
// }
if nd != s.Dims() {
panic(fmt.Sprintf("Node %v, has %d dimensions(Shape: %v). Input shape is %v, which has %d dimensions", n, n.Dims(), n.shape, s, s.Dims()))
}
// safe:
n.shape = s
}
return f
}
// WithGroupName is a node construction option to group a *Node within a particular group. This option is useful for debugging with graphs.
func WithGroupName(name string) NodeConsOpt {
f := func(n *Node) {
if n.group == "" {
n.group = name
}
}
return f
}
func newNode(opts ...NodeConsOpt) *Node {
n := borrowNode()
for _, opt := range opts {
opt(n)
}
n.fix()
incrNN()
return n
}
// NewUniqueNode creates a new unique node in a graph. If no graph was specified in the construction options then it will just return a graphless node.
func NewUniqueNode(opts ...NodeConsOpt) *Node {
n := newNode(opts...)
if n.g == nil {
return n
}
n.fixChildren() // ensure that all the kids are in the graph first
m := n.g.AddNode(n)
if n != m {
returnNode(n)
}
m.fixEdges()
return m
}
// ID returns the ID of the node. This satisfies the gonum/graph.Node interface
func (n *Node) ID() int { return int(uintptr(unsafe.Pointer(n))) }
// helper functions to help compilation process
func (n *Node) isArg() bool { return n.op == nil }
func (n *Node) isInput() bool { return (n.isArg() || n.isRandom()) && !n.isStmt }
func (n *Node) isMutable() bool { return !n.isInput() && n.op.ReturnsPtr() }
func (n *Node) isConstant() bool { _, ok := n.op.(constant); return ok }
func (n *Node) isRandom() bool { _, ok := n.op.(randomOp); return ok }
func (n *Node) isRoot() bool {
if n.g == nil {
return true
}
return len(n.g.to[n]) == 0
}
// type related isX() helper methods
// IsScalar indicates if a node represents a a scalar value. This is based on the type of the node, not the actual value associated with the node
func (n *Node) IsScalar() bool { _, ok := n.t.(tensor.Dtype); return ok }
// IsVector indicates if a node represents a vector value. This is based on the type of the node, not the actual value associated with the node
func (n *Node) IsVector() bool {
if t, ok := n.t.(TensorType); ok {
return t.Dims == 1
}
return false
}
// IsColVec indicates if a node represents a Column Vector. This is based on the type of the node, not the actual value associated with the node
func (n *Node) IsColVec() bool {
if _, ok := n.t.(TensorType); ok {
if n.shape != nil {
return n.shape.IsColVec()
}
}
return false
}
// IsRowVec indicates if a node represents a Row Vector. This is based on the type of the node, not the actual value associated with the node
func (n *Node) IsRowVec() bool {
if _, ok := n.t.(TensorType); ok {
if n.shape != nil {
return n.shape.IsRowVec()
}
}
return false
}
// IsMatrix indicates if a node represents a matrix. This is based on the type of the node, not the actual value associated with the node
func (n *Node) IsMatrix() bool {
if _, ok := n.t.(TensorType); ok {
return n.shape.Dims() == 2
}
return false
}
// methods
// CloneTo clones the node into a new graph. If CloneTo() is called on the same graph as the n, it will return n. The reason this is done is because
// at any given time, every node should be unique in the *ExprGraph.
//
//TODO: clone children as well (this means that CloneTo() is only currently suitable fo input nodes)
func (n *Node) CloneTo(g *ExprGraph) *Node {
if n.g != nil && g == n.g {
return n
}
n2 := newNode(In(g), WithOp(n.op), WithName(n.name), WithType(n.t))
if n.shape != nil {
n2.shape = n.shape.Clone()
n2.inferredShape = n.inferredShape
}
if n.boundTo != nil {
var err error
if n2.boundTo, err = CloneValue(n.boundTo); err != nil {
panic(err)
}
}
n2 = g.AddNode(n2)
return n2
}
// Value returns the valuse bound to the node. May return nil
func (n *Node) Value() Value {
if n.isConstant() {
return n.op.(constant).Value()
}
if dv, ok := n.boundTo.(*dualValue); ok {
return dv.Value
}
return n.boundTo
}
// Grad returns the gradient if there is one.
func (n *Node) Grad() (Value, error) {
if dv, ok := n.boundTo.(*dualValue); ok {
return dv.d, nil
}
if n.deriv != nil {
return n.deriv.Value(), nil
}
return nil, errors.Errorf("No Gradient node/value found for %v", n)
}
// Dims indicates how many dimensions the node's result has
func (n *Node) Dims() int {
if n.shape != nil {
return n.shape.Dims()
}
switch nt := n.t.(type) {
case TensorType:
return nt.Dims
case tensor.Dtype:
return 0
default:
panic(fmt.Sprintf("Dims undefined for %v(%T)", nt, nt))
}
}
// Shape returns the shape of the node
func (n *Node) Shape() tensor.Shape { return n.shape.Clone() }
// IsVec returns whether this node is a vector
func (n *Node) IsVec() bool { return n.IsVector() }
// Name returns the name of the node. If a name was specified and it is too long,
// the short name will be used instead (except in inputs)
//
// The short name is typically of the form: OpName(%1, %2 ...), making it read more like a function call
func (n *Node) Name() string {
if n.name != "" {
return n.name
}
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s(", n.op)
for i, child := range n.children {
fmt.Fprintf(&buf, "%%%x", child.Hashcode())
if i < len(n.children)-1 {
buf.WriteString(", ")
}
}
buf.WriteString(")")
return buf.String()
}
// WriteHash writes the hash to the provided Hash32.
func (n *Node) WriteHash(h hash.Hash32) {
fmt.Fprintf(h, "%v%v", n.t, n.shape)
if n.isInput() {
h.Write([]byte(n.name))
} else {
n.op.WriteHash(h)
}
// if len(n.children) == 0 {
// binary.Write(h, binary.LittleEndian, byte(0))
// }
binary.Write(h, binary.LittleEndian, byte(len(n.children)))
for _, child := range n.children {
binary.Write(h, binary.LittleEndian, child.Hashcode())
}
}
// Hashcode provides the hash for the tree, assuming that the node is the root of the tree.
// Original implementation was here by Vatine (who's apparently 80 years old and using SO!?!):
// http://stackoverflow.com/questions/1988665/hashing-a-tree-structure
func (n *Node) Hashcode() uint32 {
if n.hashed {
return n.hash
}
h := fnv.New32a()
n.WriteHash(h)
n.hash = h.Sum32()
n.hashed = true
return n.hash
}
// ToDot returns the graph as a graphviz compatible string
func (n *Node) ToDot() string {
graphName := exprgraphClust
g := gographviz.NewEscape()
g.SetName(graphName)
g.SetDir(true)
g.AddAttr(exprgraphClust, "splines", "spline")
g.AddAttr(exprgraphClust, "nodesep", "0.5")
g.AddAttr(exprgraphClust, "ranksep", "1.2 equally")
seen := make(map[*Node]string)
n.dot(g, graphName, seen)
return g.String()
}
// RestrictedToDot prints the graphviz compatible string but does not print the entire tree
// up and down indicates how many levels to look up, and how many levels to look down
func (n *Node) RestrictedToDot(up, down int) string {
if n.g == nil {
return n.ToDot()
}
g := n.g
var ns, upQ, downQ Nodes
// up
ns = Nodes{n}
upQ = Nodes{n}
for l := 0; l < up; l++ {
origLen := len(upQ)
for i := 0; i < origLen; i++ {
qn := upQ[i]
toQN := graphNodeToNode(g.To(qn))
upQ = append(upQ, toQN...)
ns = append(ns, toQN...)
}
upQ = upQ[origLen:]
}
// down
downQ = Nodes{n}
for d := 0; d < down; d++ {
origLen := len(downQ)
for i := 0; i < origLen; i++ {
qn := downQ[i]
downQ = append(downQ, qn.children...)
ns = append(ns, qn.children...)
}
downQ = downQ[origLen:]
}
sg := g.subgraph(ns)
n.ofInterest = true
defer func() {
n.ofInterest = false
}()
return sg.ToDot()
}
// String() implements the fmt.Stringer interface
func (n *Node) String() string {
var buf bytes.Buffer
if n.Name() != "" {
fmt.Fprintf(&buf, "%s :: ", n.Name())
} else {
fmt.Fprintf(&buf, "%s :: ", n.op)
}
if c, ok := n.op.(constant); ok {
fmt.Fprintf(&buf, "%v{%v}", n.t, c.Value())
} else {
fmt.Fprintf(&buf, "%v", n.t)
}
return buf.String()
}
// private methods
// TODO: check type, check shape, check if needsGrad -> promote to dualValue
func (n *Node) bind(v Value) error {
if n.boundTo == nil {
n.boundTo = v
return nil
}
if dv, ok := n.boundTo.(*dualValue); ok {
if vdv, ok := v.(*dualValue); ok {
if vdv == dv {
return nil
}
if n.isRandom() {
// then simply replace the value in it
dv.Value = vdv.Value
return nil
}
panic("Undefined behaviour") // no seriously there literally is no defined behaviour of what should the right thing be. I'll come back to this TODO.
}
dv.Value = v
return nil
}
n.boundTo = v
return nil
}
// bindCopy copies the value if to the bound value.
func (n *Node) bindCopy(v Value) (err error) {
if n.boundTo == nil {
var cloned Value
if cloned, err = CloneValue(v); err != nil {
return
}
n.boundTo = cloned
return nil
}
var copied Value
if dv, ok := n.boundTo.(*dualValue); ok {
if vdv, ok := v.(*dualValue); ok {
if vdv == dv {
return nil // no need to copy!
}
if n.isRandom() {
// returnValue(dv.Value)
dv.Value = vdv.Value
return nil
}
return errors.Errorf("Cannot yet handle bindCopy() of *dualValue into *dualValue") // TODO FIX
}
if copied, err = Copy(dv.Value, v); err != nil {
return errors.Wrapf(err, "Failed to copy while binding to node with *dualValue")
}
dv.Value = copied // in case they're scalars
return nil
}
if copied, err = Copy(n.boundTo, v); err != nil {
return errors.Wrapf(err, "Failed to copy while binding to node")
}
n.boundTo = copied // in case it's a scalar
return nil
}
// unbind releases the values back to the pool
func (n *Node) unbind() {
if n.boundTo == nil {
return
}
if dv, ok := n.boundTo.(*dualValue); ok {
returnDV(dv)
}
if t, ok := n.boundTo.(tensor.Tensor); ok {
returnTensor(t)
}
n.boundTo = nil
}
func (n *Node) dotCluster() string {
var group string
var isConst bool
var isInput = n.isInput()
if n.op != nil {
_, isConst = n.op.(constant)
}
switch {
case isConst:
group = constantsClust
case isInput:
group = inputsClust
case n.group == "":
group = exprgraphClust
default:
group = n.group
}
return group
}
func (n *Node) dot(g *gographviz.Escape, graphName string, seen map[*Node]string) string {
var id string
var ok bool
if id, ok = seen[n]; !ok {
id = n.dotString(g, graphName)
seen[n] = id
} else {
return id
}
for i, child := range n.children {
childID := child.dot(g, graphName, seen)
edgeAttrs := gographviz.NewAttrs()
edgeAttrs.Add("taillabel", fmt.Sprintf(" %d ", i+1))
edgeAttrs.Add("labelfloat", "false")
// edgeAttrs.Add("dir", "back")
g.AddPortEdge(id, id+":anchor:s", childID, childID+":anchor:n", true, edgeAttrs)
}
return id
}
func (n *Node) fix() {
if n.IsScalar() {
n.shape = scalarShape
}
if n.isConstant() {
return
}
if n.g == nil {
panic(fmt.Sprintf("no graph supplied %v", n))
}
}
func (n *Node) fixChildren() {
if n.g == nil {
return
}
for i, child := range n.children {
newChild := n.g.AddNode(child)
if child != newChild {
n.children[i] = newChild
}
}
}
func (n *Node) fixEdges() {
if n.g == nil {
return
}
if len(n.children) > 0 {
for _, child := range n.children {
e := edge{from: n, to: child}
n.g.SetEdge(e)
}
} else {
n.g.leaves = append(n.g.leaves, n)
}
}
func (n *Node) setShape(s tensor.Shape, inferred bool) {
n.shape = s
n.inferredShape = inferred
}
func (n *Node) setGroup(grp string) {
n.group = grp
}
func (n *Node) clone(opts ...NodeConsOpt) *Node {
if n.isInput() {
return n
}
nn := newNode(WithChildren(n.children),
WithType(n.t),
WithOp(n.op),
WithName(n.name),
In(n.g),
)
for _, opt := range opts {
opt(nn)
}
// if the shape is already known...
if n.shape != nil {
nn.shape = n.shape
nn.inferredShape = n.inferredShape
}
return nn
}
func (n *Node) diffWRT() []bool {
if sdop, ok := n.op.(SDOp); ok {
return sdop.DiffWRT(len(n.children))
}
return nil
}
// dfs but does not use channels. useful for extracting paths. used particularly in test
func (n *Node) seqWalk() Nodes {
retVal := Nodes{n}
for _, child := range n.children {
retVal = append(retVal, child.seqWalk()...)
}
return retVal
}
// dotString returns the ID of the node.
func (n *Node) dotString(g *gographviz.Escape, graphName string) string {
var buf bytes.Buffer
if err := exprNodeTempl.ExecuteTemplate(&buf, "node", n); err != nil {
panic(err)
}
id := fmt.Sprintf("Node_%p", n)
label := buf.String()
attrs := gographviz.NewAttrs()
attrs.Add("fontname", "monospace")
attrs.Add("shape", "none")
attrs.Add("label", label)
g.AddNode(graphName, id, attrs)
return id
}