forked from gorgonia/gorgonia
/
op_minmaxBetween.go
223 lines (177 loc) · 5.98 KB
/
op_minmaxBetween.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
package gorgonia
import (
"fmt"
"hash"
"github.com/chewxy/hm"
"github.com/pkg/errors"
"gorgonia.org/tensor"
)
/* MIN BETWEEN */
type minBetween struct{}
// Arity returns the number of inputs the Op expects. -1 indicates that it's n-ary and will be determined at runtime
func (op minBetween) Arity() int { return 2 }
// Informs the type of the Op (not the node). This will be used by the type system to infer the final type of the node
func (op minBetween) Type() hm.Type {
return hm.NewFnType(hm.TypeVariable('a'), hm.TypeVariable('a'), hm.TypeVariable('a'))
}
// returns the output shape as a function of the inputs
func (op minBetween) InferShape(shps ...DimSizer) (tensor.Shape, error) {
if err := checkArity(op, len(shps)); err != nil {
return nil, err
}
a := shps[0].(tensor.Shape)
b := shps[1].(tensor.Shape)
if !a.Eq(b) {
return nil, errors.Errorf("Expected both inputs to have the same shape. Got %v and %v instead", a, b)
}
return a.Clone(), nil
}
// Do executes the op
func (op minBetween) Do(vs ...Value) (Value, error) {
if err := checkArity(op, len(vs)); err != nil {
return nil, err
}
a := vs[0]
b := vs[1]
return tensor.MinBetween(a, b)
}
// ReturnsPtr returns false
func (op minBetween) ReturnsPtr() bool { return false }
// CallsExtern returns false
func (op minBetween) CallsExtern() bool { return false }
func (op minBetween) OverwritesInput() int { return -1 }
/* Other methods */
func (op minBetween) WriteHash(h hash.Hash) { fmt.Fprintf(h, op.String()) }
func (op minBetween) Hashcode() uint32 { return simpleHash(op) }
func (op minBetween) String() string { return "MinBetween" }
func (op minBetween) UsePreallocDo(prealloc Value, vs ...Value) (Value, error) {
if err := checkArity(op, len(vs)); err != nil {
return nil, err
}
a := vs[0]
b := vs[1]
return tensor.MinBetween(a, b, tensor.WithReuse(prealloc.(tensor.Tensor)))
}
func (op minBetween) DiffWRT(inputs int) []bool { return []bool{true, true} }
func (op minBetween) SymDiff(inputs Nodes, output, grad *Node) (Nodes, error) {
return minmaxSymDiff(inputs[0], inputs[1], output, grad)
}
func (op minBetween) DoDiff(ctx ExecutionContext, inputs Nodes, output *Node) error {
return minmaxAutoDiff(ctx, inputs[0], inputs[1], output)
}
/* MAX BETWEEN */
type maxBetween struct{}
// Arity returns the number of inputs the Op expects. -1 indicates that it's n-ary and will be determaxed at runtime
func (op maxBetween) Arity() int { return 2 }
// Informs the type of the Op (not the node). This will be used by the type system to infer the final type of the node
func (op maxBetween) Type() hm.Type {
return hm.NewFnType(hm.TypeVariable('a'), hm.TypeVariable('a'), hm.TypeVariable('a'))
}
// returns the output shape as a function of the inputs
func (op maxBetween) InferShape(shps ...DimSizer) (tensor.Shape, error) {
if err := checkArity(op, len(shps)); err != nil {
return nil, err
}
a := shps[0].(tensor.Shape)
b := shps[1].(tensor.Shape)
if !a.Eq(b) {
return nil, errors.Errorf("Expected both inputs to have the same shape. Got %v and %v instead", a, b)
}
return a.Clone(), nil
}
// Do executes the op
func (op maxBetween) Do(vs ...Value) (Value, error) {
if err := checkArity(op, len(vs)); err != nil {
return nil, err
}
a := vs[0]
b := vs[1]
return tensor.MaxBetween(a, b)
}
// ReturnsPtr returns false
func (op maxBetween) ReturnsPtr() bool { return false }
// CallsExtern returns false
func (op maxBetween) CallsExtern() bool { return false }
func (op maxBetween) OverwritesInput() int { return -1 }
/* Other methods */
func (op maxBetween) WriteHash(h hash.Hash) { fmt.Fprintf(h, op.String()) }
func (op maxBetween) Hashcode() uint32 { return simpleHash(op) }
func (op maxBetween) String() string { return "MaxBetween" }
func (op maxBetween) UsePreallocDo(prealloc Value, vs ...Value) (Value, error) {
if err := checkArity(op, len(vs)); err != nil {
return nil, err
}
a := vs[0]
b := vs[1]
return tensor.MaxBetween(a, b, tensor.WithReuse(prealloc.(tensor.Tensor)))
}
func (op maxBetween) DiffWRT(inputs int) []bool { return []bool{true, true} }
func (op maxBetween) SymDiff(inputs Nodes, output, grad *Node) (Nodes, error) {
return minmaxSymDiff(inputs[0], inputs[1], output, grad)
}
func (op maxBetween) DoDiff(ctx ExecutionContext, inputs Nodes, output *Node) error {
return minmaxAutoDiff(ctx, inputs[0], inputs[1], output)
}
func minmaxSymDiff(a, b *Node, out *Node, grad *Node) (Nodes, error) {
mask, err := Eq(a, out, true)
if err != nil {
return nil, err
}
WithGroupName(gradClust)(mask)
gradA, err := HadamardProd(grad, mask)
if err != nil {
return nil, err
}
WithGroupName(gradClust)(gradA)
gradB, err := Sub(grad, gradA)
if err != nil {
return nil, err
}
WithGroupName(gradClust)(gradB)
return Nodes{gradA, gradB}, nil
}
func minmaxAutoDiff(ctx ExecutionContext, a, b *Node, output *Node) (err error) {
// dummy for now so let's keep everything as simple as possible
adv, bdv := getDV(a, b)
outdv := output.boundTo.(*dualValue)
eqOp := newElemBinOp(ltOpType, a, b)
eqOp.retSame = true
eq := &ExternalOp{
Op: eqOp,
ExecutionContext: ctx,
}
ctx.Device = a.Device()
mask, err := eq.Do(adv.Value, outdv.Value)
if err != nil {
return errors.Wrap(err, "Unable to get mask")
}
dev := a.Device()
var gradA, gradB, gradOut Value
var extra bool
if gradOut, extra, err = output.GradOnDevice(dev, ctx.External); err != nil {
return errors.Wrapf(err, gradOnDeviceFail, output, dev)
}
if extra {
defer ctx.PutValue(dev, gradOut)
}
if gradA, extra, err = a.GradOnDevice(dev, ctx.External); err != nil {
return errors.Wrapf(err, gradOnDeviceFail, a, dev)
}
if extra {
defer ctx.PutValue(dev, gradA)
}
mul := NewHadamardProdOp(a, output, ctx)
mul.Incr = gradA
var d Value
if d, err = mul.Do(gradOut, mask); err != nil {
return errors.Wrapf(err, "IncrDo gradA failed")
}
adv.SetDeriv(d)
sub := NewSubOp(b, a, ctx)
sub.Incr = gradB
if d, err = sub.Do(gradOut, adv.d); err != nil {
return errors.Wrapf(err, "IncrDo gradB failed")
}
bdv.SetDeriv(d)
return nil
}