/
unsqueeze.go
116 lines (89 loc) · 3.06 KB
/
unsqueeze.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
package opset13
import (
"sort"
"github.com/advancedclimatesystems/gonnx/onnx"
"github.com/advancedclimatesystems/gonnx/ops"
"gorgonia.org/tensor"
)
const (
MinUnsqueezeInputs = 2
MaxUnsqueezeInputs = 2
)
// Unsqueeze represents the ONNX unsqueeze operator.
type Unsqueeze struct{}
// newUnsqueeze creates a new unsqueeze operator.
func newUnsqueeze() ops.Operator {
return &Unsqueeze{}
}
// Init initializes the unsqueeze operator.
func (u *Unsqueeze) Init(*onnx.NodeProto) error {
return nil
}
// Apply applies the unsqueeze operator.
func (u *Unsqueeze) Apply(inputs []tensor.Tensor) ([]tensor.Tensor, error) {
dataShape := inputs[0].Shape()
axes, err := ops.AnyToIntSlice(inputs[1].Data())
if err != nil {
return nil, err
}
outputRank := len(dataShape) + len(axes)
if !ops.AllInRange(axes, -outputRank, outputRank-1) {
return nil, ops.ErrNotAllAxesInRange(outputRank, outputRank)
}
// negative entries should be offset by the rank of the output tensor
// i.e. -1 -> outputRank - 1, -outputrank -> 0
ops.OffsetArrayIfNegative(axes, outputRank)
sort.Ints(axes)
if ops.HasDuplicates(axes) {
return nil, ops.ErrInvalidInput("axes cannot have duplicate entries after offset", u)
}
newShape := insertOnes(dataShape, axes)
out, ok := inputs[0].Clone().(tensor.Tensor)
if !ok {
return nil, ops.ErrTypeAssert("tensor.Tensor", inputs[0].Clone())
}
err = out.Reshape(newShape...)
return []tensor.Tensor{out}, err
}
// ValidateInputs validates the inputs that will be given to Apply for this operator.
func (u *Unsqueeze) ValidateInputs(inputs []tensor.Tensor) ([]tensor.Tensor, error) {
return ops.ValidateInputs(u, inputs)
}
// GetMinInputs returns the minimum number of input tensors this operator expects.
func (u *Unsqueeze) GetMinInputs() int {
return MinUnsqueezeInputs
}
// GetMaxInputs returns the maximum number of input tensors this operator expects.
func (u *Unsqueeze) GetMaxInputs() int {
return MaxUnsqueezeInputs
}
// GetInputTypeConstraints returns a list. Every element represents a set of allowed tensor dtypes
// for the corresponding input tensor.
func (u *Unsqueeze) GetInputTypeConstraints() [][]tensor.Dtype {
return [][]tensor.Dtype{ops.AllTypes, {tensor.Int64}}
}
// String implements the stringer interface, and can be used to format errors or messages.
func (u *Unsqueeze) String() string {
return "unsqueeze operator"
}
// Creates a new array, which is `original` with ones added at the indices specified by `indices`
// `indices` may not contain duplicates, the elements are assumed to be in the range 0 <= x < N
// and should be sorted in increasing order.
// Is done in a single pass through the new array with length: len(original) + len(indices).
func insertOnes(original, indices []int) []int {
N := len(indices) + len(original)
// Pre-allocate the output shape
newShape := make([]int, N)
originalIdx := 0
indicesIdx := 0
for i := 0; i < N; i++ {
if indicesIdx < len(indices) && indices[indicesIdx] == i {
newShape[i] = 1
indicesIdx++
} else {
newShape[i] = original[originalIdx]
originalIdx++
}
}
return newShape
}