forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_test.go
131 lines (110 loc) · 3.38 KB
/
compile_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
package gorgonia
import "testing"
func TestCompile_medium(t *testing.T) {
g := NewGraph()
x := NewMatrix(g, Float64, WithShape(20, 20), WithName("x"))
y := NewMatrix(g, Float64, WithShape(20, 20), WithName("y"))
xpy := Must(Add(x, y))
xmy := Must(Sub(x, y))
xpys := Must(Slice(xpy, S(0, 10)))
Must(Square(xpys))
xmy2 := Must(Square(xmy))
var final Value
Set(xmy2, xpy)
Read(xmy2, &final)
prog, _, err := Compile(g)
if err != nil {
t.Fatalf("error while compiling: %v", err)
}
t.Log(prog)
onDev := xpy.Device() != CPU
// leakage test
if onDev {
reg0 := register{device: Device(0), id: 0}
reg1 := register{device: Device(0), id: 1}
reg2 := register{device: Device(0), id: 2}
if !prog.instructions.has(free{reg0}) {
t.Error("Expected GPU(0)0 to be freed")
}
if !prog.instructions.has(free{reg1}) {
t.Error("Expected GPU(0)1 to be freed")
}
if !prog.instructions.has(free{reg2}) {
t.Error("Expected GPU(0)2 to be freed")
}
}
// position tests
if onDev {
// last two instructions should be free
if _, ok := prog.instructions[len(prog.instructions)-1].(free); !ok {
t.Error("Expected last instruction to be a Free")
}
if _, ok := prog.instructions[len(prog.instructions)-2].(free); !ok {
t.Error("Expected second last instruction to be a Free")
}
// frag = prog.m[set]
// if _, ok := frag[len(frag)-1].(free); !ok {
// t.Error("Expected a `free` instruction after LET")
// }
// frag = prog.m[read]
// if _, ok := frag[len(frag)-2].(free); !ok {
// t.Error("Expected a `free` instruction after READ")
// }
}
}
func TestCompile_CompileFn(t *testing.T) {
g := NewGraph()
x := NewScalar(g, Float32, WithName("x"))
y := NewScalar(g, Float32, WithName("y"))
xpy := Must(Add(x, y))
xmy := Must(Mul(x, y))
x2 := Must(Square(x))
progAll, _, err := Compile(g)
if err != nil {
t.Fatal(err)
}
progAdd, _, err := CompileFunction(g, Nodes{x, y}, Nodes{xpy})
if err != nil {
t.Fatal(err)
}
progMul, _, err := CompileFunction(g, Nodes{x, y}, Nodes{xmy})
if err != nil {
t.Fatal(err)
}
if _, _, err = CompileFunction(g, Nodes{x, y}, Nodes{x2}); err == nil {
t.Error("expected an error when there is an unused node")
}
// properties based testing
if len(progAll.sorted) <= len(progAdd.sorted) || len(progAll.sorted) <= len(progMul.sorted) {
t.Error("progAll should have more nodes included than progAdd or progMul")
}
if len(progAll.instructions) <= len(progAdd.instructions) || len(progAll.instructions) <= len(progMul.instructions) {
t.Error("progAll should have more instructions than either progAdd or progMul")
}
// really this is more checking of the subgraphing
if !progAdd.sorted.Contains(x) {
t.Error("Expected progAdd to contain x")
}
if !progAdd.sorted.Contains(y) {
t.Error("Expected progAdd to contain y")
}
if !progAdd.sorted.Contains(xpy) {
t.Error("Expected progAdd to contain xpy")
}
if progAdd.sorted.Contains(xmy) || progAdd.sorted.Contains(x2) {
t.Error("Expected progAdd to not contain either x2 or xmy")
}
// same as above
if !progMul.sorted.Contains(x) {
t.Error("Expected progMul to contain x")
}
if !progMul.sorted.Contains(y) {
t.Error("Expected progMul to contain y")
}
if !progMul.sorted.Contains(xmy) {
t.Error("Expected progMul to contain xmy")
}
if progMul.sorted.Contains(xpy) || progMul.sorted.Contains(x2) {
t.Error("Expected progMul to not contain either x2 or xpy")
}
}