forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdual.go
264 lines (214 loc) · 5.6 KB
/
dual.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
package gorgonia
import (
"fmt"
"github.com/chewxy/gorgonia/tensor"
"github.com/chewxy/hm"
"github.com/pkg/errors"
)
type dualValue struct {
Value
d Value // the derivative wrt to each input
}
func (dv *dualValue) SetDeriv(d Value) error {
if t, ok := d.(tensor.Tensor); ok && t.IsScalar() {
d, _ = anyToScalar(t.ScalarValue())
}
dv.d = d
return dv.sanity()
}
func (dv *dualValue) SetValue(v Value) error {
dv.Value = v
return dv.sanity()
}
func (dv *dualValue) Clone() (retVal Value, err error) {
var v, d Value
if v, err = CloneValue(dv.Value); err != nil {
return nil, errors.Wrap(err, cloneFail)
}
if d, err = CloneValue(dv.d); err != nil {
return nil, errors.Wrap(err, cloneFail)
}
dv2 := borrowDV()
dv2.Value = v
dv2.d = d
retVal = dv2
return
}
func (dv *dualValue) Type() hm.Type { return TypeOf(dv.Value) }
func (dv *dualValue) Dtype() tensor.Dtype { return dv.Value.Dtype() }
func (dv *dualValue) String() string {
return fmt.Sprintf("%#+v", dv.Value)
}
func (dv *dualValue) sanity() error {
// check that d and v are the same type
dvv := TypeOf(dv.Value)
dvd := TypeOf(dv.d)
if !dvv.Eq(dvd) {
return errors.Errorf("DualValues do not have the same types: %v and %v", dvv, dvd)
}
// TODO: check that the shapes are the same
return nil
}
// clones the dualValue and zeroes out the ndarrays
func (dv *dualValue) clone0() (retVal *dualValue, err error) {
var v, d Value
if v, err = CloneValue(dv.Value); err != nil {
return nil, errors.Wrap(err, cloneFail)
}
if d, err = CloneValue(dv.d); err != nil {
return nil, errors.Wrap(err, cloneFail)
}
v = ZeroValue(v)
d = ZeroValue(d)
dv2 := borrowDV()
dv2.Value = v
dv2.d = d
retVal = dv2
return
}
// the derivative of a constant is zero.
//
// The original implementation was to have a constantDualValue type. This would lead to waaay less allocations of matrices
// but as it turns out, as I waws working, the constants turn out to be not so constant afterall.
// Is this a problem with the graph that leads to derivation of constant values? I don't quite know. TO CHECK
func constantDV(val Value) *dualValue {
enterLoggingContext()
defer leaveLoggingContext()
// retVal := &dualValue{Value: val}
retVal := borrowDV()
retVal.Value = val
var err error
if retVal.d, err = CloneValue(val); err != nil {
panic(err)
}
retVal.d = ZeroValue(retVal.d)
return retVal
}
// the derivative of x is 1.
func variableDV(val Value) *dualValue {
// retVal := &dualValue{Value: val}
retVal := borrowDV()
retVal.Value = val
switch v := val.(type) {
case Scalar:
retVal.d = one(v.Dtype())
case tensor.Tensor:
shp := v.Shape()
dt := v.Dtype()
retVal.d = tensor.Ones(dt, shp...)
default:
panic(fmt.Sprintf("%v(%T) not handled yet", v, v))
}
return retVal
}
// monadic unit() function. This unit() function will allocate a Value for dv.d
// this is useful for forward mode autodiff
func dvUnit(v Value) *dualValue {
enterLoggingContext()
defer leaveLoggingContext()
if dv, ok := v.(*dualValue); ok {
return dv
}
return constantDV(v)
}
func dvUnitVar(v Value) *dualValue {
if dv, ok := v.(*dualValue); ok {
return dv
}
return variableDV(v)
}
// no alloc is done. It'll just return a *dualValue with nil as the dv.d
func dvUnit0(v Value) *dualValue {
if dv, ok := v.(*dualValue); ok {
return dv
}
retVal := borrowDV()
retVal.Value = v
return retVal
}
// helper to unpack from []*dualValue
func idValue(inputs []*dualValue) (retVals []Value) {
retVals = make([]Value, len(inputs))
for i, input := range inputs {
retVals[i] = input.Value
}
return
}
func dvBind(op Op, inputs []*dualValue) (retVal *dualValue, err error) {
enterLoggingContext()
defer leaveLoggingContext()
vals := idValue(inputs)
var ret Value
if ret, err = op.Do(vals...); err == nil {
return dvUnit(ret), nil
}
return nil, errors.Wrap(err, opDoFail)
}
// dvBindVar returns a dvUnitVar instead of dvUnit (which zeroes the derivative).
// The default derivative of a variable wrt itself is 1 (dx/dx == 1)
func dvBindVar(op Op, inputs []*dualValue) (retVal *dualValue, err error) {
vals := idValue(inputs)
var ret Value
if ret, err = op.Do(vals...); err == nil {
return dvUnitVar(ret), nil
}
return nil, errors.Wrap(err, opDoFail)
}
//TODO test vecvecdot divBind0
// doesn't alloc a dualValue, and reuses whatever that is there, and zeroes out the deriv
func dvBind0(op Op, retVal *dualValue, inputs []*dualValue) (err error) {
prealloc := retVal.Value
vals := idValue(inputs)
var ret Value
if pd, ok := op.(UsePreallocDoer); ok {
ret, err = pd.UsePreallocDo(prealloc, vals...)
if err == nil {
goto next
}
}
if ret, err = op.Do(vals...); err != nil {
return errors.Wrap(err, opDoFail)
}
next:
if err != nil {
return
}
if err = retVal.SetValue(ret); err != nil {
return
}
retVal.SetDeriv(ZeroValue(retVal.d))
return
}
func dvBindVar0(op Op, retVal *dualValue, inputs []*dualValue) (err error) {
prealloc := retVal.Value
vals := idValue(inputs)
var ret Value
if pd, ok := op.(UsePreallocDoer); ok {
ret, err = pd.UsePreallocDo(prealloc, vals...)
} else {
if ret, err = op.Do(vals...); err != nil {
return errors.Wrap(err, opDoFail)
}
}
if err != nil {
return errors.Wrapf(err, opDoFail)
}
if err = retVal.SetValue(ret); err != nil {
return errors.Wrap(err, "Failed at setting the value")
}
switch v := retVal.d.(type) {
case Scalar:
retVal.d = one(v.Dtype())
case tensor.Tensor:
switch v.Dtype() {
case tensor.Float64:
err = v.Memset(float64(1))
case tensor.Float32:
err = v.Memset(float32(1))
}
retVal.d = v
default:
err = errors.Errorf(nyiTypeFail, "dvBindVar0", retVal.d)
}
return
}