-
Notifications
You must be signed in to change notification settings - Fork 132
Canonicalize Subtensor slices #761
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
from pytensor.compile.ops import DeepCopyOp | ||
from pytensor.configdefaults import config | ||
from pytensor.graph import FunctionGraph, vectorize_graph | ||
from pytensor.graph.basic import Constant, Variable, ancestors | ||
from pytensor.graph.basic import Constant, Variable, ancestors, equal_computations | ||
from pytensor.graph.rewriting.basic import check_stack_trace | ||
from pytensor.graph.rewriting.db import RewriteDatabaseQuery | ||
from pytensor.graph.rewriting.utils import rewrite_graph | ||
|
@@ -2402,3 +2402,44 @@ def test_local_blockwise_advanced_inc_subtensor(set_instead_of_inc): | |
else: | ||
expected_out[:, :, core_idxs] += test_y | ||
np.testing.assert_allclose(fn(test_x, test_y), expected_out) | ||
|
||
|
||
def test_slice_canonicalize(): | ||
rng = np.random.default_rng(43) | ||
x = tensor(shape=(3, 5, None, 9)) | ||
test_x = rng.normal(size=(3, 5, 8, 9)) | ||
# Test case 1 | ||
y = x[0:None, 0:5, 0:7, 0:9:1] | ||
f = pytensor.function([x], y, allow_input_downcast=True) | ||
|
||
# Get the DeepCopy input and assert that the Op is a DeepCopy | ||
test_y = f.maker.fgraph.outputs[0].owner.inputs[0] | ||
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. Why not the output directly, there's a deepcopy? |
||
assert isinstance(f.maker.fgraph.outputs[0].owner.op, DeepCopyOp) | ||
|
||
expected_y = x[None:None:None, None:None:None, None:7:None] | ||
|
||
assert equal_computations([test_y], [expected_y]) | ||
|
||
np.testing.assert_allclose( | ||
f(test_x), | ||
test_x[ | ||
0:None, 0:5, 0:7, 0:9:1 | ||
], # Use the unoptimized slice to make sure our rewrite logic is correct | ||
) | ||
|
||
# Test case 2 | ||
y1 = x[0:-1, 0:5, 0:7, 0:-1:-1] | ||
f1 = pytensor.function([x], y1, allow_input_downcast=True) | ||
|
||
# Get the DeepCopy input and assert that the Op is a DeepCopy | ||
test_y1 = f1.maker.fgraph.outputs[0].owner.inputs[0] | ||
assert isinstance(f1.maker.fgraph.outputs[0].owner.op, DeepCopyOp) | ||
|
||
expected_y1 = x[None:-1:None, None:None:None, None:7:None, None:-1:-1] | ||
|
||
assert equal_computations([test_y1], [expected_y1]) | ||
|
||
np.testing.assert_allclose( | ||
f1(test_x), | ||
test_x[0:-1, 0:5, 0:7, 0:-1:-1], | ||
) |
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.