-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Pow operator * Handle non-float pow csaes * Fix lint * Update pipeline go versions * Use go1.23 * Back to go1.21
- Loading branch information
1 parent
22331fb
commit c97d70c
Showing
11 changed files
with
247 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package pow | ||
|
||
import ( | ||
"github.com/advancedclimatesystems/gonnx/onnx" | ||
"github.com/advancedclimatesystems/gonnx/ops" | ||
"gorgonia.org/tensor" | ||
) | ||
|
||
var pow7TypeConstraints = [][]tensor.Dtype{ | ||
{tensor.Float32, tensor.Float64}, | ||
{tensor.Float32, tensor.Float64}, | ||
} | ||
|
||
var powTypeConstraints = [][]tensor.Dtype{ | ||
{tensor.Int32, tensor.Int64, tensor.Float32, tensor.Float64}, | ||
{tensor.Uint8, tensor.Uint16, tensor.Uint32, tensor.Uint64, tensor.Int8, tensor.Int16, tensor.Int32, tensor.Int64, tensor.Float32, tensor.Float64}, | ||
} | ||
|
||
// Pow represents the ONNX pow operator. | ||
type Pow struct { | ||
ops.BaseOperator | ||
} | ||
|
||
// newPow creates a new pow operator. | ||
func newPow(version int, typeConstraints [][]tensor.Dtype) ops.Operator { | ||
return &Pow{ | ||
BaseOperator: ops.NewBaseOperator( | ||
version, | ||
2, | ||
2, | ||
typeConstraints, | ||
"pow", | ||
), | ||
} | ||
} | ||
|
||
// Init initializes the pow operator. | ||
func (a *Pow) Init(*onnx.NodeProto) error { | ||
return nil | ||
} | ||
|
||
// Apply applies the pow operator. | ||
func (a *Pow) Apply(inputs []tensor.Tensor) ([]tensor.Tensor, error) { | ||
powTensor := inputs[1] | ||
if inputs[0].Dtype() != powTensor.Dtype() { | ||
to, err := ops.DTypeToONNXType(inputs[0].Dtype()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
powTensor, err = ops.ConvertTensorDtype(powTensor, to) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return ops.ApplyBinaryOperation( | ||
inputs[0], | ||
powTensor, | ||
ops.Pow, | ||
ops.MultidirectionalBroadcasting, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package pow | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/advancedclimatesystems/gonnx/ops" | ||
"github.com/stretchr/testify/assert" | ||
"gorgonia.org/tensor" | ||
) | ||
|
||
func TestPowInit(t *testing.T) { | ||
p := &Pow{} | ||
err := p.Init(nil) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestPow(t *testing.T) { | ||
tests := []struct { | ||
version int64 | ||
backing0 any | ||
backing1 any | ||
shapes [][]int | ||
expected any | ||
}{ | ||
{ | ||
13, | ||
[]float32{0, 1, 2, 3}, | ||
[]float32{1, 1, 1, 1}, | ||
[][]int{{2, 2}, {2, 2}}, | ||
[]float32{0, 1, 2, 3}, | ||
}, | ||
{ | ||
13, | ||
[]float32{0, 1, 2, 3, 4, 5}, | ||
[]float32{2, 2, 2, 2, 2, 2}, | ||
[][]int{{3, 2}, {3, 2}}, | ||
[]float32{0, 1, 4, 9, 16, 25}, | ||
}, | ||
{ | ||
13, | ||
[]float32{0, 1}, | ||
[]float32{0, 1, 2, 3}, | ||
[][]int{{2}, {2, 2}}, | ||
[]float32{1, 1, 0, 1}, | ||
}, | ||
{ | ||
13, | ||
[]int32{1, 2, 3}, | ||
[]int32{4, 5, 6}, | ||
[][]int{{3}, {3}}, | ||
[]int32{1, 32, 729}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
inputs := []tensor.Tensor{ | ||
ops.TensorWithBackingFixture(test.backing0, test.shapes[0]...), | ||
ops.TensorWithBackingFixture(test.backing1, test.shapes[1]...), | ||
} | ||
|
||
pow := powVersions[test.version]() | ||
|
||
res, err := pow.Apply(inputs) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, test.expected, res[0].Data()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package pow | ||
|
||
import ( | ||
"github.com/advancedclimatesystems/gonnx/ops" | ||
) | ||
|
||
var powVersions = ops.OperatorVersions{ | ||
7: ops.NewOperatorConstructor(newPow, 7, pow7TypeConstraints), | ||
12: ops.NewOperatorConstructor(newPow, 12, powTypeConstraints), | ||
13: ops.NewOperatorConstructor(newPow, 13, powTypeConstraints), | ||
} | ||
|
||
func GetVersions() ops.OperatorVersions { | ||
return powVersions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters