-
Notifications
You must be signed in to change notification settings - Fork 114
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
Add torch implementation of IfElse #974
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
881bca1
Add torch ifelse
738393f
Merge branch 'main' into ifelse_torch
Ch0ronomato 0bf00ab
Fix linter
d0a8120
Rearrange test for code coverage
a27ed03
remove stack
f2d88a8
Merge branch 'main' into ifelse_torch
Ch0ronomato 5e47869
Fix lint
eda2dbc
Fix more lint
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 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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
from pytensor.graph.basic import Apply | ||
from pytensor.graph.fg import FunctionGraph | ||
from pytensor.graph.op import Op | ||
from pytensor.ifelse import ifelse | ||
from pytensor.raise_op import CheckAndRaise | ||
from pytensor.tensor import alloc, arange, as_tensor, empty, eye | ||
from pytensor.tensor.type import matrix, scalar, vector | ||
|
@@ -301,3 +302,20 @@ def test_pytorch_MakeVector(): | |
x_fg = FunctionGraph([], [x]) | ||
|
||
compare_pytorch_and_py(x_fg, []) | ||
|
||
|
||
def test_pytorch_ifelse(): | ||
p1_vals = np.r_[1, 2, 3] | ||
p2_vals = np.r_[-1, -2, -3] | ||
|
||
a = scalar("a") | ||
x = ifelse(a < 0.5, tuple(np.r_[p1_vals, p2_vals]), tuple(np.r_[p2_vals, p1_vals])) | ||
x_fg = FunctionGraph([a], x) | ||
|
||
compare_pytorch_and_py(x_fg, np.array([0.2], dtype=config.floatX)) | ||
|
||
a = scalar("a") | ||
x = ifelse(a < 0.4, tuple(np.r_[p1_vals, p2_vals]), tuple(np.r_[p2_vals, p1_vals])) | ||
x_fg = FunctionGraph([a], x) | ||
Comment on lines
+318
to
+320
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to test twice, to cover the false case, do a case where the multiple outputs are not something that can be stacked (say I would have the other test case return a single output, to test the single output case as well |
||
|
||
compare_pytorch_and_py(x_fg, np.array([0.5], dtype=config.floatX)) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to stack, and shouldn't, because outputs can have different dimensions / sizes