forked from gorgonia/gorgonia
/
op_by_indices.go
253 lines (190 loc) · 6.08 KB
/
op_by_indices.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
package gorgonia
import (
"fmt"
"hash"
"github.com/chewxy/hm"
"github.com/pkg/errors"
"gorgonia.org/tensor"
)
type byIndicesOp struct {
axis int
}
func newByIndicesOp(axis int) *byIndicesOp {
if axis < 0 {
axis = 0
}
return &byIndicesOp{
axis: axis,
}
}
// ByIndices is an operation that takes the indices as input and return the selected values from those indices.
// The default axis in 0
func ByIndices(x *Node, indices *Node, axis int) (*Node, error) {
op := newByIndicesOp(axis)
return ApplyOp(op, x, indices)
}
func (op *byIndicesOp) Arity() int { return 2 }
func (op *byIndicesOp) ReturnsPtr() bool { return false }
func (op *byIndicesOp) CallsExtern() bool { return false }
func (op *byIndicesOp) WriteHash(h hash.Hash) { fmt.Fprintf(h, op.String()) }
func (op *byIndicesOp) Hashcode() uint32 { return simpleHash(op) }
func (op *byIndicesOp) String() string {
return fmt.Sprintf("ByIndicesOp{axis=%d}", op.axis)
}
func (op *byIndicesOp) InferShape(inputs ...DimSizer) (tensor.Shape, error) {
s := inputs[0].(tensor.Shape).Clone()
i := inputs[1].(tensor.Shape).Clone()
if !i.IsVectorLike() {
return nil, errors.Errorf("Expected indices to be a vector-like. Got %v instead", i)
}
s[op.axis] = i.TotalSize()
return s, nil
}
func (op *byIndicesOp) Type() hm.Type {
a := hm.TypeVariable('a')
b := makeTensorType(1, tensor.Int)
return hm.NewFnType(a, b, a)
}
func (op *byIndicesOp) OverwritesInput() int { return -1 }
func (op *byIndicesOp) checkInput(inputs ...Value) (x, indices tensor.Tensor, err error) {
if err := checkArity(op, len(inputs)); err != nil {
return nil, nil, err
}
var ok bool
if x, ok = inputs[0].(tensor.Tensor); !ok {
return nil, nil, errors.Errorf("Expected input to be a tensor, got %T", inputs[0])
}
if indices, ok = inputs[1].(tensor.Tensor); !ok {
return nil, nil, errors.Errorf("Expected indices to be a tensor. Got %T instead", inputs[1])
}
if indices.Dtype() != tensor.Int {
return nil, nil, errors.Errorf("Expected indices to have tensor.Int as a Dtype. Got %T instead", indices.Dtype())
}
return x, indices, nil
}
func (op *byIndicesOp) Do(inputs ...Value) (Value, error) {
inputTensor, indices, err := op.checkInput(inputs...)
if err != nil {
return nil, fmt.Errorf("Can't check ByIndicesOp input: %w", err)
}
return tensor.ByIndices(inputTensor, indices, op.axis)
}
// DoDiff calculates the diff and sets its value to the output node. Implementation for ADOp interface.
func (op *byIndicesOp) DoDiff(ctx ExecutionContext, inputs Nodes, output *Node) error {
if len(inputs) != 2 {
return fmt.Errorf("byIndicesOp.DoDiff needs 2 arguments")
}
odv := output.boundTo.(*dualValue)
odvd := odv.Value.(tensor.Tensor)
diffOp := &byIndicesOpDiffOp{op}
result, err := diffOp.Do(inputs[0].boundTo, inputs[1].boundTo)
if err != nil {
return err
}
err = result.(*tensor.Dense).Reshape(odvd.Shape()...)
if err != nil {
return err
}
sum, err := odvd.(*tensor.Dense).Add(result.(*tensor.Dense), tensor.UseUnsafe())
if err != nil {
return err
}
odv.d = sum
return nil
}
// SymDiff applies the diff op. Implementation for SDOp interface.
func (op *byIndicesOp) SymDiff(inputs Nodes, output, grad *Node) (Nodes, error) {
err := checkArity(op, len(inputs))
if err != nil {
return nil, err
}
x := inputs[0]
indices := inputs[1]
diffOp := &byIndicesOpDiffOp{op}
nodes := make(Nodes, op.Arity())
nodes[0], err = ApplyOp(diffOp, x, grad, indices)
return nodes, err
}
// DiffWRT is an implementation for the SDOp interface
func (op *byIndicesOp) DiffWRT(inputs int) []bool {
if inputs != op.Arity() {
panic(fmt.Sprintf("ByIndicesOp operator needs %d inputs, got %d instead", op.Arity(), inputs))
}
return []bool{true, false}
}
type byIndicesOpDiffOp struct {
*byIndicesOp
}
func (op *byIndicesOpDiffOp) Arity() int { return 3 }
func (op *byIndicesOpDiffOp) ReturnsPtr() bool { return false }
func (op *byIndicesOpDiffOp) CallsExtern() bool { return false }
func (op *byIndicesOpDiffOp) WriteHash(h hash.Hash) {
fmt.Fprintf(h, op.String())
}
func (op *byIndicesOpDiffOp) Hashcode() uint32 { return simpleHash(op) }
func (op *byIndicesOpDiffOp) String() string {
return fmt.Sprintf("ByIndicesOpDiff{}(%d)", op.axis)
}
func (op *byIndicesOpDiffOp) InferShape(inputs ...DimSizer) (tensor.Shape, error) {
s := inputs[0].(tensor.Shape).Clone()
return s, nil
}
func (op *byIndicesOpDiffOp) Type() hm.Type {
a := hm.TypeVariable('a')
b := makeTensorType(1, tensor.Int)
return hm.NewFnType(a, a, b, a)
}
func (op *byIndicesOpDiffOp) OverwritesInput() int { return -1 }
func (op *byIndicesOpDiffOp) checkInput(inputs ...Value) (in, indices, gradient *tensor.Dense, err error) {
if err := checkArity(op, len(inputs)); err != nil {
return nil, nil, nil, err
}
var (
ok bool
)
switch t := inputs[0].(type) {
case *dualValue:
if in, ok = t.Value.(*tensor.Dense); !ok {
return nil, nil, nil, errors.Errorf("input should be a tensor.Tensor, got %T", inputs[0])
}
case *tensor.Dense:
in = t
default:
return nil, nil, nil, errors.Errorf("input type is not supported, got %T", inputs[0])
}
switch t := inputs[2].(type) {
case *dualValue:
if gradient, ok = t.Value.(*tensor.Dense); !ok {
return nil, nil, nil, errors.Errorf("gradient should be a tensor, got %T", inputs[2])
}
case *tensor.Dense:
gradient = t
default:
return nil, nil, nil, errors.Errorf("gradient type is not supported, got %T", inputs[2])
}
switch t := inputs[1].(type) {
case *tensor.Dense:
indices = t
default:
return nil, nil, nil, errors.Errorf("indices type %T is not supported", inputs[1])
}
return in, indices, gradient, nil
}
func (op *byIndicesOpDiffOp) Do(inputs ...Value) (Value, error) {
inputTensor, gradTensor, indices, err := op.checkInput(inputs...)
if err != nil {
return nil, fmt.Errorf("Can't check ByIndicesOpDiff input: %w", err)
}
output, err := tensor.ByIndicesB(inputTensor, gradTensor, indices, op.axis)
if err != nil {
return nil, err
}
return output, nil
}
// ensure it complies with the Op interface
var (
_ Op = &byIndicesOpDiffOp{}
_ Op = &byIndicesOp{}
_ SDOp = &byIndicesOp{}
_ ADOp = &byIndicesOp{}
)