-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathgraph_test.go
404 lines (344 loc) · 9.28 KB
/
graph_test.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
package gorgonia
import (
"testing"
"github.com/stretchr/testify/assert"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/iterator"
"gonum.org/v1/gonum/graph/topo"
"gorgonia.org/tensor"
)
func TestGraphBasics(t *testing.T) {
assert := assert.New(t)
g, x, y, xy := simpleEqn()
// basic stuff
assert.Equal(g, xy.g)
assert.Contains(g.AllNodes(), x)
assert.Contains(g.AllNodes(), y)
assert.Contains(g.AllNodes(), xy)
assert.Equal(Nodes{x, y}, g.leaves)
// Node/addressing stuff
xid := x.ID()
xFromID := g.Node(xid)
assert.Equal(x, xFromID)
var correctTo Nodes
correctTo = Nodes{xy}
assert.Equal(correctTo, g.to[x])
assert.Equal(correctTo, g.to[y])
// test Uniquifying ability of ExprGraph
newX := g.AddNode(x)
assert.Equal(x, newX)
newY := g.AddNode(y)
assert.Equal(y, newY)
newXY := Must(Add(x, y))
correctTo = append(correctTo, xy) // note this is correct. .Set() will be called when graph.To() is called
assert.Equal(xy, newXY)
assert.Equal(correctTo, g.to[y])
assert.Equal(correctTo, g.to[x])
correctTo = Nodes{xy}
assert.Equal(correctTo, sliceNodesToNodes(graph.NodesOf(g.To(y.ID()))))
assert.Equal(correctTo, sliceNodesToNodes(graph.NodesOf(g.To(x.ID()))))
assert.Equal(3, g.Nodes().Len())
// Now, time to deal with constants
xy1 := Must(Add(xy, onef64))
assert.Nil(onef64.g)
assert.Equal(g, xy1.g)
var containsOne bool
it := g.Nodes()
for it.Next() {
node := it.Node()
n := node.(*Node)
if n.Hashcode() == onef64.Hashcode() {
containsOne = true
break
}
}
if !containsOne {
t.Errorf("graph does not contain a clone of onef64: %v", g.Nodes())
}
// duplicate constants
one := NewConstant(1.0)
newOne := g.AddNode(one)
if one == newOne {
t.Error("one should not have been added to the graph")
}
assert.NotNil(newOne.g)
assert.NotEqual(one, newOne)
}
// This test is added to make sure I'm sane when dealing with sorted graphs
// because sometimes Eobard Thawne is needed
func TestGraphSort(t *testing.T) {
assert := assert.New(t)
g, _, _, z := simpleVecEqn()
WithName("z")(z)
var sortedNodes []graph.Node
var err error
// stability tests
for i := 0; i < 100; i++ {
if sortedNodes, err = topo.Sort(g); err != nil {
t.Error(err)
}
// expected := Nodes{z, y, x} // the old version of ExprGraph was stable with topo.Sort, but the new version ain't
// assert.Equal(expected, sortedNodes)
assert.Equal(z, sortedNodes[0])
}
// this is to remind myself how this thing sorts:
t.Logf("%v", graphNodeToNode(iterator.NewOrderedNodes(sortedNodes)))
}
// test that collisions are handled correctly
func TestGraphCollisions(t *testing.T) {
assert := assert.New(t)
g, _, _, xy := simpleEqn()
delete(g.byHash, xy.hash)
g.byHash[0xdeadbeef] = xy
xy.hash = 0xdeadbeef
xy.name = "original"
t.Logf("original: %p, hash %x", xy, xy.Hashcode())
col := new(Node)
col.name = "COLIN THE COLLISION"
col.hash = 0xdeadbeef
col.hashed = true
col2 := g.AddNode(col)
assert.Equal(col, col2)
assert.Equal(4, len(g.AllNodes()), "%v", g.AllNodes())
assert.True(g.Has(col.ID()))
colleen := new(Node)
colleen.name = "COLLEEN THE COLLISION"
colleen.hash = 0xdeadbeef
colleen.hashed = true
colleen2 := g.AddNode(colleen)
assert.Equal(colleen, colleen2)
assert.Equal(5, len(g.AllNodes()), "%v", g.AllNodes())
assert.True(g.Has(colleen.ID()))
}
func TestGraphEquality(t *testing.T) {
_, x, y, z := simpleVecEqn()
xh1 := x.Hashcode()
yh1 := y.Hashcode()
if xh1 == yh1 {
t.Error("Different nodes, should have different hashes")
}
_, x2, y2, z2 := simpleVecEqn()
if x.Hashcode() != x2.Hashcode() {
t.Error("They should have the same hash")
}
if y.Hashcode() != y2.Hashcode() {
t.Error("They should have the same hash")
}
if z.Hashcode() != z2.Hashcode() {
t.Error("They should have the same hash")
}
}
func TestGraphSubgraph(t *testing.T) {
var err error
var sortedNodes Nodes
assert := assert.New(t)
g, x, y, z := simpleVecEqn()
sub := Nodes{x, y}
g2 := g.subgraph(sub, true)
t.Logf("%v", g2.AllNodes())
if sortedNodes, err = Sort(g2); err != nil {
t.Fatal(err)
}
assert.NotContains(sortedNodes, z)
assert.Contains(g2.roots, x)
assert.Contains(g2.roots, y)
assert.Equal(2, len(g2.roots))
}
func TestGraph_SubgraphRoots(t *testing.T) {
assert := assert.New(t)
g, x, y, z := simpleVecEqn()
sz := Must(Sum(z))
a := NewVector(g, Float64, WithName("a"), WithShape(2))
b := NewVector(g, Float64, WithName("b"), WithShape(2))
c := Must(Add(a, b))
sc := Must(Sum(c))
var szVal, scVal Value
readSZ := Read(sz, &szVal)
readSC := Read(sc, &scVal)
// check that stmt nodes aren't included in the roots
sg := g.SubgraphRoots(readSZ, readSC)
assert.Contains(sg.roots, sz)
assert.Contains(sg.roots, sc)
assert.Equal(2, len(sg.roots))
// check that subgrapphing actually works
sg = g.SubgraphRoots(c)
ns := sg.AllNodes()
assert.NotContains(ns, sc)
assert.NotContains(ns, readSC)
assert.NotContains(ns, x)
assert.NotContains(ns, y)
assert.NotContains(ns, z)
assert.NotContains(ns, sz)
assert.NotContains(ns, readSZ)
}
func TestGraph_ExactSubgraphRoots(t *testing.T) {
assert := assert.New(t)
g, x, y, z := simpleVecEqn()
sz := Must(Sum(z))
setXtoZ := Set(x, z) // setting x = z
sg0 := g.SubgraphRoots(sz)
sg1 := g.ExactSubgraphRoots(sz)
ns0 := sg0.AllNodes()
ns1 := sg1.AllNodes()
assert.Contains(ns0, setXtoZ)
assert.NotContains(ns1, setXtoZ)
assert.Contains(ns0, x)
assert.Contains(ns0, y)
assert.Contains(ns0, z)
assert.Contains(ns0, sz)
}
func TestGraph_Constant(t *testing.T) {
g := NewGraph()
v1 := NewF64(1.0)
c0 := g.Constant(v1)
c1 := g.Constant(v1)
if c0 != c1 {
t.Errorf("Expected c0 and c1 to be the same (pointer and all that)")
}
}
func TestGraph_Clone(t *testing.T) {
g, x, y, z := simpleVecEqn()
z2 := Must(Square(z))
// add a collided
z2t := z2.Type()
delete(g.byHash, z2.hash)
g.byHash[0xdeadbeef] = z2
col := new(Node)
col.g = g
col.name = "COLIN THE COLLISION"
col.hash = 0xdeadbeef
col.hashed = true
col.boundTo = NewF64(0)
col.t = z2t
g.AddNode(col)
colleen := new(Node)
colleen.g = g
colleen.name = "COLLEEN THE COLLISION"
colleen.hash = 0xdeadbeef
colleen.hashed = true
colleen.boundTo = NewF64(0)
colleen.t = z2t
g.AddNode(colleen)
one := onef64
z2p1 := Must(Add(z2, one)) // add a constant
rando := UniformRandomNode(g, Float64, 0, 1, z2p1.Shape()...) // add a weird node
blah := Must(HadamardProd(z2p1, rando))
cost := Must(Sum(blah))
_, err := Grad(cost, x, y)
if err != nil {
t.Fatal(err)
}
g.Roots() // call it to populate the roots field
// clone with nil values
g2 := g.Clone().(*ExprGraph)
for i, n := range g.all {
cloned := g2.all[i]
if !deepNodeEq(n, cloned) {
t.Errorf("Expected %d of all to be %v. Got %v instead", i, n, cloned)
break
}
}
if len(g.evac) != len(g2.evac) && len(g.evac) > 0 {
t.Errorf("Expected the evacs to have the same length")
}
for k, v := range g.evac {
var v2 Nodes
var ok bool
if v2, ok = g2.evac[k]; !ok {
t.Errorf("Key %v not found in cloned evac", k)
break
}
for i, n := range v {
if !deepNodeEq(n, v2[i]) {
t.Errorf("Expected v[%d] to have equal values", i)
break
}
}
if t.Failed() {
break
}
}
if len(g.roots) != len(g2.roots) {
t.Errorf("Expected roots to be %d. Got %d instead", len(g.roots), len(g2.roots))
}
for i, root := range g.roots {
if !deepNodeEq(root, g2.roots[i]) {
t.Errorf("Expected roots[%d] to have equal nodes", i)
break
}
}
if len(g.leaves) != len(g2.leaves) {
t.Errorf("Expected leaves to be %d. Got %d instead", len(g.leaves), len(g2.leaves))
}
for i, leaf := range g.leaves {
if !deepNodeEq(leaf, g2.leaves[i]) {
t.Errorf("Expected leaves[%d] to be equal", i)
break
}
}
Let(x, tensor.New(tensor.WithBacking([]float64{1, 2})))
Let(y, tensor.New(tensor.WithBacking([]float64{3, 4})))
m := NewLispMachine(g, ExecuteFwdOnly()) // the gradient has been precalculated
defer m.Close()
if err := m.RunAll(); err != nil {
t.Fatal(err)
}
g2 = g.Clone().(*ExprGraph)
for i, n := range g.all {
cloned := g2.all[i]
if !deepNodeEq(n, cloned) {
t.Errorf("Expected %d of all to be %v. Got %v instead", i, n, cloned)
break
}
}
if len(g.evac) != len(g2.evac) && len(g.evac) > 0 {
t.Errorf("Expected the evacs to have the same length")
}
for k, v := range g.evac {
var v2 Nodes
var ok bool
if v2, ok = g2.evac[k]; !ok {
t.Errorf("Key %v not found in cloned evac", k)
break
}
for i, n := range v {
if !deepNodeEq(n, v2[i]) {
t.Errorf("Expected v[%d] to have equal values", i)
break
}
}
if t.Failed() {
break
}
}
if len(g.roots) != len(g2.roots) {
t.Errorf("Expected roots to be %d. Got %d instead", len(g.roots), len(g2.roots))
}
for i, root := range g.roots {
if !deepNodeEq(root, g2.roots[i]) {
t.Errorf("Expected roots[%d] to have equal nodes", i)
break
}
}
if len(g.leaves) != len(g2.leaves) {
t.Errorf("Expected leaves to be %d. Got %d instead", len(g.leaves), len(g2.leaves))
}
for i, leaf := range g.leaves {
if !deepNodeEq(leaf, g2.leaves[i]) {
t.Errorf("Expected leaves[%d] to be equal", i)
break
}
}
}
func TestExprGraph_Edges(t *testing.T) {
g := NewGraph()
var x, y *Node
// define the expression
x = NewScalar(g, Float64, WithName("x"))
y = NewScalar(g, Float64, WithName("y"))
Add(x, y)
edgesIT := g.Edges()
if edgesIT.Len() != 2 {
t.Fail()
}
}