-
Notifications
You must be signed in to change notification settings - Fork 8
Added Where operator #227
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 1 commit into
AdvancedClimateSystems:develop
from
Swopper050:118-where-operator
Dec 22, 2024
Merged
Added Where operator #227
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package where | ||
|
|
||
| import "github.com/advancedclimatesystems/gonnx/ops" | ||
|
|
||
| var whereVersions = ops.OperatorVersions{ | ||
| 9: ops.NewOperatorConstructor(newWhere, 9, whereTypeConstraints), | ||
| } | ||
|
|
||
| func GetVersions() ops.OperatorVersions { | ||
| return whereVersions | ||
| } | ||
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,105 @@ | ||
| package where | ||
|
|
||
| import ( | ||
| "github.com/advancedclimatesystems/gonnx/onnx" | ||
| "github.com/advancedclimatesystems/gonnx/ops" | ||
| "gorgonia.org/tensor" | ||
| ) | ||
|
|
||
| var whereTypeConstraints = [][]tensor.Dtype{ | ||
| {tensor.Bool}, | ||
| ops.AllTypes, | ||
| ops.AllTypes, | ||
| } | ||
|
|
||
| // Where represents the ONNX where operator. | ||
| type Where struct { | ||
| ops.BaseOperator | ||
| } | ||
|
|
||
| // newWhere creates a new where operator. | ||
| func newWhere(version int, typeConstraints [][]tensor.Dtype) ops.Operator { | ||
| return &Where{ | ||
| BaseOperator: ops.NewBaseOperator( | ||
| version, | ||
| 3, | ||
| 3, | ||
| typeConstraints, | ||
| "where", | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| // Init initializes the where operator. | ||
| func (w *Where) Init(*onnx.NodeProto) error { | ||
| return nil | ||
| } | ||
|
|
||
| // Apply applies the where operator. | ||
| func (w *Where) Apply(inputs []tensor.Tensor) ([]tensor.Tensor, error) { | ||
| condition := inputs[0] | ||
|
|
||
| X := inputs[1] | ||
| Y := inputs[2] | ||
|
|
||
| X, Y, err := ops.MultidirectionalBroadcast(X, Y) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| condition, X, err = ops.MultidirectionalBroadcast(condition, X) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| out, err := where(X, Y, condition) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return []tensor.Tensor{out}, err | ||
| } | ||
|
|
||
| func where(X, Y, condition tensor.Tensor) (tensor.Tensor, error) { | ||
| out := tensor.New(tensor.Of(X.Dtype()), tensor.WithShape(X.Shape()...)) | ||
|
|
||
| iterator := condition.Iterator() | ||
| iterator.Reset() | ||
|
|
||
| for !iterator.Done() { | ||
| coords := iterator.Coord() | ||
|
|
||
| conditionRaw, err := condition.At(coords...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| conditionValue, ok := conditionRaw.(bool) | ||
| if !ok { | ||
| return nil, ops.ErrCast | ||
| } | ||
|
|
||
| var value any | ||
| if conditionValue { | ||
| value, err = X.At(coords...) | ||
| } else { | ||
| value, err = Y.At(coords...) | ||
| } | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| err = out.SetAt(value, coords...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| _, err = iterator.Next() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| return out, nil | ||
| } |
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,92 @@ | ||
| package where | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "gorgonia.org/tensor" | ||
| ) | ||
|
|
||
| func TestWhereInit(t *testing.T) { | ||
| op := whereVersions[9]() | ||
| err := op.Init(nil) | ||
| assert.Nil(t, err) | ||
| } | ||
|
|
||
| func TestWhere(t *testing.T) { | ||
| tests := []struct { | ||
| version int64 | ||
| condition []bool | ||
| conditionShape []int | ||
| backing1 []float32 | ||
| backing1Shape []int | ||
| backing2 []float32 | ||
| backing2Shape []int | ||
| expectedBacking []float32 | ||
| }{ | ||
| { | ||
| 9, | ||
| []bool{true, false, true}, | ||
| []int{3}, | ||
| []float32{1, 2, 3}, | ||
| []int{3}, | ||
| []float32{4, 5, 6}, | ||
| []int{3}, | ||
| []float32{1, 5, 3}, | ||
| }, | ||
| { | ||
| 9, | ||
| []bool{true, false, true, false}, | ||
| []int{2, 2}, | ||
| []float32{1, 2, 3, 4}, | ||
| []int{2, 2}, | ||
| []float32{4, 5}, | ||
| []int{1, 2}, | ||
| []float32{1, 5, 3, 5}, | ||
| }, | ||
| { | ||
| 9, | ||
| []bool{false, true}, | ||
| []int{2}, | ||
| []float32{1, 2, 3, 4}, | ||
| []int{2, 2}, | ||
| []float32{4, 5}, | ||
| []int{1, 2}, | ||
| []float32{4, 2, 4, 4}, | ||
| }, | ||
| { | ||
| 9, | ||
| []bool{false, false, false, true, true, true}, | ||
| []int{2, 3}, | ||
| []float32{1, 2, 3, 4, 5, 6}, | ||
| []int{2, 3}, | ||
| []float32{4, 5, 6}, | ||
| []int{3}, | ||
| []float32{4, 5, 6, 4, 5, 6}, | ||
| }, | ||
| { | ||
| 9, | ||
| []bool{false, true, true, false, false, true}, | ||
| []int{2, 3}, | ||
| []float32{1, 2, 3, 4, 5, 6}, | ||
| []int{2, 3}, | ||
| []float32{4, 5, 6}, | ||
| []int{3}, | ||
| []float32{4, 2, 3, 4, 5, 6}, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| inputs := []tensor.Tensor{ | ||
| tensor.New(tensor.WithShape(test.conditionShape...), tensor.WithBacking(test.condition)), | ||
| tensor.New(tensor.WithShape(test.backing1Shape...), tensor.WithBacking(test.backing1)), | ||
| tensor.New(tensor.WithShape(test.backing2Shape...), tensor.WithBacking(test.backing2)), | ||
| } | ||
|
|
||
| op := whereVersions[test.version]() | ||
|
|
||
| res, err := op.Apply(inputs) | ||
| assert.Nil(t, err) | ||
| assert.Equal(t, test.expectedBacking, 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
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.