forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operatorPointwise_binary_test.go
256 lines (218 loc) · 6.02 KB
/
operatorPointwise_binary_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
package gorgonia
import (
"fmt"
"io/ioutil"
"math/rand"
"runtime"
"testing"
"github.com/chewxy/gorgonia/tensor"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
func ssBinOpTest(t *testing.T, op ʘBinaryOperatorType, dt tensor.Dtype) (err error) {
assert := assert.New(t)
var randX, randY interface{}
switch dt {
case Float64:
randX = rand.ExpFloat64()
randY = rand.ExpFloat64()
case Float32:
randX = float32(rand.ExpFloat64())
randY = float32(rand.ExpFloat64())
default:
return errors.Errorf("op %v Test not yet implemented for %v ", op, dt)
}
binOp := newEBOByType(op, dt, dt)
t.Logf("ssBinOp %v %v %v", randX, op, randY)
var g, g2 *ExprGraph
var x, y, z *Node
var a, b, c *Node
g = NewGraph()
x = NewScalar(g, dt, WithName("x"))
y = NewScalar(g, dt, WithName("y"))
if z, err = applyOp(binOp, x, y); err != nil {
return err
}
g2 = NewGraph()
a = NewScalar(g2, dt, WithName("a"))
b = NewScalar(g2, dt, WithName("b"))
if c, err = applyOp(binOp, a, b); err != nil {
return err
}
// var grads Nodes
var m1 VM
if op.isArith() {
if _, err = Grad(c, a, b); err != nil {
return err
}
m1 = NewLispMachine(g)
} else {
m1 = NewLispMachine(g, ExecuteFwdOnly())
}
prog, locMap, err := Compile(g2)
if err != nil {
return err
}
m2 := NewTapeMachine(prog, locMap, TraceExec(), BindDualValues())
defer runtime.GC()
Let(x, randX)
Let(y, randY)
if err = m1.RunAll(); err != nil {
return
}
Let(a, randX)
Let(b, randY)
if err = m2.RunAll(); err != nil {
return
}
var xG, aG, yG, bG, zG, cG Value
if op.isArith() {
if xG, err = x.Grad(); err != nil {
return
}
if yG, err = y.Grad(); err != nil {
return
}
if aG, err = a.Grad(); err != nil {
return
}
if bG, err = b.Grad(); err != nil {
return
}
if zG, err = z.Grad(); err != nil {
return
}
if cG, err = c.Grad(); err != nil {
return
}
assert.True(ValueEq(xG, aG), "Test ssDiff of %v. xG != aG. Got %v and %v", op, xG, aG)
assert.True(ValueEq(yG, bG), "Test ssDiff of %v. yG != bG. Got %v and %v", op, yG, bG)
assert.True(ValueEq(zG, cG), "Test ssDiff of %v. zG != cG. Got %v and %v", op, zG, cG)
}
assert.True(ValueEq(x.Value(), a.Value()), "Test ss op %v. Values are different: x: %v, a %v", op, x.Value(), a.Value())
assert.True(ValueEq(y.Value(), b.Value()), "Test ss op %v. Values are different: y: %v, b %v", op, y.Value(), b.Value())
assert.True(ValueEq(z.Value(), c.Value()), "Test ss op %v. Values are different: z: %v, c %v", op, z.Value(), c.Value())
return nil
}
func ttBinOpTest(t *testing.T, op ʘBinaryOperatorType, dt tensor.Dtype) (err error) {
assert := assert.New(t)
var x, y, z, a, b, c, cost *Node
var g, g2 *ExprGraph
var randX, randY interface{}
switch dt {
case Float32:
randX = []float32{1, 2, 3, 4}
randY = []float32{2, 2, 2, 2}
case Float64:
randX = []float64{1, 2, 3, 4}
randY = []float64{2, 2, 2, 2}
}
t.Logf("ttBinOp: %v %v %v", randX, op, randY)
// randX := Gaussian(0, 1)(dt, 2, 2)
// randY := Gaussian(0, 1)(dt, 2, 2)
xV := tensor.New(tensor.WithShape(2, 2), tensor.WithBacking(randX))
yV := tensor.New(tensor.WithShape(2, 2), tensor.WithBacking(randY))
g = NewGraph()
g2 = NewGraph()
x = NewMatrix(g, dt, WithName("x"), WithShape(2, 2))
y = NewMatrix(g, dt, WithName("y"), WithShape(2, 2))
a = NewMatrix(g2, dt, WithName("a"), WithShape(2, 2))
b = NewMatrix(g2, dt, WithName("b"), WithShape(2, 2))
binOp := newEBOByType(op, x.t, y.t)
if z, err = applyOp(binOp, x, y); err != nil {
return err
}
if c, err = applyOp(binOp, a, b); err != nil {
return err
}
var m1 VM
if op.isArith() {
if _, err = Sum(z); err != nil {
return err
}
if cost, err = Sum(c); err != nil {
return err
}
if _, err = Grad(cost, a, b); err != nil {
return err
}
m1 = NewLispMachine(g)
} else {
m1 = NewLispMachine(g, ExecuteFwdOnly())
}
prog, locMap, err := Compile(g2)
if err != nil {
return err
}
// lg := log.New(os.Stderr, "", 0)
m2 := NewTapeMachine(prog, locMap, TraceExec())
defer runtime.GC()
// m2 := NewTapeMachine(prog, locMap, TraceExec(), WithLogger(logger), WithWatchlist())
Let(x, xV)
Let(y, yV)
if err = m1.RunAll(); err != nil {
return
}
Let(a, xV)
Let(b, yV)
if err = m2.RunAll(); err != nil {
return
}
var xG, aG, yG, bG, zG, cG Value
if op.isArith() {
if xG, err = x.Grad(); err != nil {
return
}
if yG, err = y.Grad(); err != nil {
return
}
if aG, err = a.Grad(); err != nil {
return
}
if bG, err = b.Grad(); err != nil {
return
}
if zG, err = z.Grad(); err != nil {
return
}
if cG, err = c.Grad(); err != nil {
return
}
assert.True(ValueEq(xG, aG), "Test ttDiff of %v. xG != aG. Got %+v \nand %+v", op, xG, aG)
assert.True(ValueEq(yG, bG), "Test ttDiff of %v. yG != bG. Got %+v \nand %+v", op, yG, bG)
assert.True(ValueEq(zG, cG), "Test ttDiff of %v. zG != cG. Got %+v \nand %+v", op, zG, cG)
}
assert.True(ValueEq(x.Value(), a.Value()), "Test tt op %v. Values are different: x: %+v\n a %+v", op, x.Value(), a.Value())
assert.True(ValueEq(y.Value(), b.Value()), "Test tt op %v. Values are different: y: %+v\n b %+v", op, y.Value(), b.Value())
assert.True(ValueEq(z.Value(), c.Value()), "Test tt op %v. Values are different: z: %+v\n c %+v", op, z.Value(), c.Value())
if t.Failed() {
ioutil.WriteFile(fmt.Sprintf("Test_%v_tt.dot", op), []byte(g2.ToDot()), 0644)
}
return nil
}
func TestBinOps(t *testing.T) {
for op := addOpType; op < maxʘBinaryOpType; op++ {
// for op := subOpType; op < mulOpType; op++ {
var err error
err = ssBinOpTest(t, op, Float64)
if err != nil {
t.Errorf("Float64 version err: %v", err)
}
err = ssBinOpTest(t, op, Float32)
if err != nil {
t.Errorf("Float32 version err: %v", err)
}
t.Logf("Float64 T-T test for %v", op)
err = ttBinOpTest(t, op, Float64)
if err != nil {
t.Errorf("ttBinOp Float64 version err %v", err)
}
t.Logf("Float32 T-T test")
err = ttBinOpTest(t, op, Float32)
if err != nil {
t.Errorf("ttBinOp Float64 version err %v", err)
}
}
// single tests
// ttBinOpTest(t, subOpType, Float64)
}