forked from owulveryck/onnx-go
/
gather_operator.go
177 lines (151 loc) · 4.21 KB
/
gather_operator.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
package gorgonnx
import (
"encoding/binary"
"errors"
"fmt"
"hash"
"hash/fnv"
"github.com/chewxy/hm"
"github.com/godshen/onnx-go"
"gorgonia.org/gorgonia"
"gorgonia.org/tensor"
)
func init() {
register("Gather", newGather)
}
func newGather() operator {
return &gather{}
}
type gather struct {
axis int64
dataShape tensor.Shape
indicesShape tensor.Shape
}
func (g *gather) Arity() int {
return 2
}
func (g *gather) Type() hm.Type {
a := hm.TypeVariable('a')
c := hm.TypeVariable('c')
dataType := gorgonia.TensorType{Dims: len(g.dataShape), Of: a}
indicesType := gorgonia.TensorType{Dims: len(g.indicesShape), Of: c}
retType := gorgonia.TensorType{Dims: len(g.dataShape) + len(g.indicesShape) - 1, Of: a}
return hm.NewFnType(dataType, indicesType, retType)
}
func gatherInferShape(axis int64, dataShape, indicesShape tensor.Shape) tensor.Shape {
shape := make(tensor.Shape, 0, len(dataShape)+len(indicesShape)-1)
shape = append(shape, dataShape[:axis]...)
shape = append(shape, indicesShape...)
shape = append(shape, dataShape[axis+1:]...)
return shape
}
func (g *gather) InferShape(inputs ...gorgonia.DimSizer) (tensor.Shape, error) {
if inputs[0] == nil || inputs[1] == nil {
return nil, errors.New("gather: infershape failed, nil shape")
}
return gatherInferShape(g.axis, inputs[0].(tensor.Shape), inputs[1].(tensor.Shape)), nil
}
func doGather[T float32 | float64](axis int64, data, indices gorgonia.Value) (gorgonia.Value, error) {
vals, ok := data.Data().([]T)
if !ok {
return nil, errors.New(fmt.Sprintf("expected []%T, but cannot cast", T(0)))
}
indexVals, ok := indices.Data().([]int64)
if !ok {
return nil, errors.New(fmt.Sprintf("expected []int64, but cannot cast %T", indices.Data()))
}
retVal := tensor.NewDense(data.Dtype(), gatherInferShape(axis, data.Shape(), indices.Shape()))
var totalStartIndices, totalMidIndices, totalEndIndices int64 = 1, 1, 1
for _, dim := range data.Shape()[:axis] {
totalStartIndices *= int64(dim)
}
for _, dim := range indices.Shape() {
totalMidIndices *= int64(dim)
}
for _, dim := range data.Shape()[axis+1:] {
totalEndIndices *= int64(dim)
}
axisIndices := int64(data.Shape()[axis])
var i, j, k int64
for i = 0; i < totalStartIndices; i++ {
for j = 0; j < totalMidIndices; j++ {
replaceIdx := indexVals[j]
for k = 0; k < totalEndIndices; k++ {
value := vals[(i*axisIndices+replaceIdx)*totalEndIndices+k]
newIdx := (i*totalMidIndices+j)*totalEndIndices + k
retVal.Set(int(newIdx), value)
}
}
}
return retVal, nil
}
func (g *gather) Do(inputs ...gorgonia.Value) (gorgonia.Value, error) {
if len(inputs) != g.Arity() {
return nil, errors.New("gather: wrong number of arguments")
}
data, ok := inputs[0].(*tensor.Dense)
if !ok {
return nil, errors.New("gather: only dense are supported")
}
indices, ok := inputs[1].(*tensor.Dense)
if !ok {
return nil, errors.New("gather: only dense are supported")
}
switch data.Dtype() {
case tensor.Float64:
return doGather[float64](g.axis, data, indices)
case tensor.Float32:
return doGather[float32](g.axis, data, indices)
default:
return nil, errors.New("gather Unsupported type")
}
}
func (g *gather) ReturnsPtr() bool {
return false
}
func (g *gather) CallsExtern() bool {
return false
}
func (g *gather) OverwritesInput() int {
return -1
}
func (g *gather) WriteHash(h hash.Hash) {
if err := binary.Write(h, binary.LittleEndian, []byte(`gather`)); err != nil {
panic(err)
}
}
func (g *gather) Hashcode() uint32 {
h := fnv.New32a()
g.WriteHash(h)
return h.Sum32()
}
func (g *gather) String() string {
return "gather"
}
func (g *gather) apply(gg *Graph, ns ...*Node) error {
n := ns[0]
var err error
children := getOrderedChildren(gg.g, n)
if err := checkCondition(children, 2); err != nil {
return err
}
data := children[0]
indices := children[1]
g.dataShape = data.gorgoniaNode.Shape()
g.indicesShape = indices.gorgoniaNode.Shape()
n.gorgoniaNode, err = gorgonia.ApplyOp(g, data.gorgoniaNode, indices.gorgoniaNode)
if err != nil {
return err
}
return nil
}
func (g *gather) init(o onnx.Operation) error {
g.axis = 0
axis, ok := o.Attributes["axis"]
if ok {
if g.axis, ok = axis.(int64); !ok {
return errors.New("axis is not an int64")
}
}
return nil
}