Rewrite more cases of Blockwise IncSubtensor #1560
Open
+377
−192
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.
Spin-off from #1558 with the rewrite to remove Blockwise IncSubtensor extended to basic IncSubtensor (before only covered AdvancedIncSubtensor) and to batch indices. This shows up frequently if building vectorized jacobians so we want to make sure it's optimized away.
We could perhaps do the rewrite eagerly when we call
vectorize_node
, but since the logic is pretty complex I decided to keep it in a rewrite. The graph with Blockwise is still readable, so it's merely a matter of performance / enabling other Subtensor rewrites (including inplace!)PS: We should be able to reuse the arange logic to rewrite away blockwise Subtensor with batch indices and AdvancedSubtensor.
Except for batched slices, which may not always be vectorizable in a "square manner", we shouldn't ever end up with a Blockwise of a subtensor (or a subtensor update) in the final graph. Numpy indexing is flexible enough to cover any vectorization case, it's just not trivial to write it :D
📚 Documentation preview 📚: https://pytensor--1560.org.readthedocs.build/en/1560/