-
Notifications
You must be signed in to change notification settings - Fork 4
/
shape.go
66 lines (52 loc) · 1.76 KB
/
shape.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
package opset13
import (
"github.com/advancedclimatesystems/gonnx/onnx"
"github.com/advancedclimatesystems/gonnx/ops"
"gorgonia.org/tensor"
)
const (
MinShapeInputs = 1
MaxShapeInputs = 1
)
// Shape represents the ONNX shape operator.
type Shape struct{}
// newShape creates a new shape operator.
func newShape() ops.Operator {
return &Shape{}
}
// Init initializes the shape operator.
func (s *Shape) Init(*onnx.NodeProto) error {
return nil
}
// Apply the shape operator to the graph. It creates a node that holds the shape of the
// input node as 1D int64 tensor.
func (s *Shape) Apply(inputs []tensor.Tensor) ([]tensor.Tensor, error) {
nodeShape := inputs[0].Shape()
shape := make([]int64, len(nodeShape))
for i, dimSize := range nodeShape {
shape[i] = int64(dimSize)
}
out := tensor.New(tensor.WithShape(len(nodeShape)), tensor.WithBacking(shape))
return []tensor.Tensor{out}, nil
}
// ValidateInputs validates the inputs that will be given to Apply for this operator.
func (s *Shape) ValidateInputs(inputs []tensor.Tensor) ([]tensor.Tensor, error) {
return ops.ValidateInputs(s, inputs)
}
// GetMinInputs returns the minimum number of input tensors this operator expects.
func (s *Shape) GetMinInputs() int {
return MinShapeInputs
}
// GetMaxInputs returns the maximum number of input tensors this operator expects.
func (s *Shape) GetMaxInputs() int {
return MaxShapeInputs
}
// GetInputTypeConstraints returns a list. Every element represents a set of allowed tensor dtypes
// for the corresponding input tensor.
func (s *Shape) GetInputTypeConstraints() [][]tensor.Dtype {
return [][]tensor.Dtype{ops.AllTypes}
}
// String implements the stringer interface, and can be used to format errors or messages.
func (s *Shape) String() string {
return "shape operator"
}