-
Notifications
You must be signed in to change notification settings - Fork 8
Added Pow operator #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Swopper050
merged 7 commits into
AdvancedClimateSystems:develop
from
Swopper050:75-power-operator
Dec 22, 2024
Merged
Added Pow operator #222
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7a8bd1e
Added Pow operator
Swopper050 d9afb57
Handle non-float pow csaes
Swopper050 8650957
Fix lint
Swopper050 ea82830
Update pipeline go versions
Swopper050 b8c5188
Use go1.23
Swopper050 1a8eb88
Merged develop
Swopper050 2cdcbad
Back to go1.21
Swopper050 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.