-
Notifications
You must be signed in to change notification settings - Fork 4
/
constant_of_shape.go
106 lines (87 loc) · 2.83 KB
/
constant_of_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
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
package opset13
import (
"github.com/advancedclimatesystems/gonnx/onnx"
"github.com/advancedclimatesystems/gonnx/ops"
"gorgonia.org/tensor"
)
const (
MinConstantOfShapeInputs = 1
MaxConstantOfShapeInputs = 1
)
// ConstantOfShape represents the ONNX constant of shape operator.
type ConstantOfShape struct {
// One element tensor, giving the value and type of the output tensor
// defaults to value 0 and type float32.
value *tensor.Dense
}
// newConstantOfShape creates a new constant of shape operator.
func newConstantOfShape() ops.Operator {
return &ConstantOfShape{}
}
// Init initializes the constant of shape operator.
func (c *ConstantOfShape) Init(n *onnx.NodeProto) error {
attributes := n.GetAttribute()
if len(attributes) > 1 {
return ops.ErrInvalidAttributeCount(1, len(attributes), c)
}
if len(attributes) == 1 {
attr := attributes[0]
if attr.GetName() == "value" {
t, err := onnx.TensorFromProto(attr.GetT())
if err != nil {
return err
}
c.value = tensor.New(tensor.WithBacking(t.Data()))
if c.value.Len() != 1 {
return ops.ErrInvalidTensor("expected tensor to have one element", c)
}
} else {
return ops.ErrInvalidAttribute(attr.GetName(), c)
}
} else {
c.value = tensor.New(tensor.FromScalar(float32(0.0)))
}
return nil
}
// Apply applies the constant of shape operator.
func (c *ConstantOfShape) Apply(inputs []tensor.Tensor) ([]tensor.Tensor, error) {
shape, err := ops.AnyToIntSlice(ops.IfScalarToSlice(inputs[0].Data()))
if err != nil {
return nil, err
}
// Empty dimensions in a tensor are not supported
for i := range shape {
if shape[i] <= 0 {
return nil, ops.ErrInvalidTensor("empty dimensions are not allowed", c)
}
}
t := tensor.New(tensor.WithShape(shape...), tensor.Of(c.value.Dtype()))
t, err = t.AddScalar(c.value, true)
if err != nil {
return nil, err
}
return []tensor.Tensor{t}, err
}
// ValidateInputs validates the inputs that will be given to Apply for this operator.
func (c *ConstantOfShape) ValidateInputs(inputs []tensor.Tensor) ([]tensor.Tensor, error) {
return ops.ValidateInputs(c, inputs)
}
// GetMinInputs returns the minimum number of input tensors this operator expects.
func (c *ConstantOfShape) GetMinInputs() int {
return MinConstantOfShapeInputs
}
// GetMaxInputs returns the maximum number of input tensors this operator expects.
func (c *ConstantOfShape) GetMaxInputs() int {
return MaxConstantOfShapeInputs
}
// GetInputTypeConstraints returns a list. Every element represents a set of allowed tensor dtypes
// for the corresponding input tensor.
func (c *ConstantOfShape) GetInputTypeConstraints() [][]tensor.Dtype {
return [][]tensor.Dtype{
{tensor.Int64},
}
}
// String implements the stringer interface, and can be used to format errors or messages.
func (c *ConstantOfShape) String() string {
return "constant of shape operator"
}