-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Blockwise Op to vectorize existing Ops
Inspired by: aesara-devs/aesara#1215 Co-authored-by: Brandon T. Willard <[email protected]> Co-authored-by: Purna Chandra Mansingh <[email protected]> Co-authored-by: Sayam Kumar <[email protected]> Co-authored-by: Kaustubh <[email protected]>
- Loading branch information
1 parent
f49b2cc
commit 0ff0f29
Showing
10 changed files
with
966 additions
and
46 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from pytensor.compile.mode import optdb | ||
from pytensor.graph import node_rewriter | ||
from pytensor.graph.rewriting.basic import copy_stack_trace, out2in | ||
from pytensor.tensor.blockwise import Blockwise, vectorize_node | ||
|
||
|
||
@node_rewriter([Blockwise]) | ||
def local_useless_blockwise(fgraph, node): | ||
""" | ||
If there is a dispatch implementation that does not require Blockwise, use that instead. | ||
This means a user created a Blockwise manually when there was no need. | ||
Note: This rewrite is not registered by default anywhere | ||
""" | ||
op = node.op | ||
inputs = node.inputs | ||
dummy_core_node = op._create_dummy_core_node(node.inputs) | ||
vect_node = vectorize_node(dummy_core_node, *inputs) | ||
if not isinstance(vect_node.op, Blockwise): | ||
return copy_stack_trace(node.outputs, vect_node.outputs) | ||
|
||
|
||
@node_rewriter([Blockwise]) | ||
def local_useless_unbatched_blockwise(fgraph, node): | ||
"""Remove Blockwise that don't have any batched dims.""" | ||
op = node.op | ||
inputs = node.inputs | ||
|
||
if max(inp.type.ndim - len(sig) for inp, sig in zip(inputs, op.inputs_sig)) == 0: | ||
return copy_stack_trace(node.outputs, op.core_op.make_node(*inputs).outputs) | ||
|
||
|
||
# We register this rewrite late, so that other rewrites need only target Blockwise Ops | ||
optdb.register( | ||
"local_useless_unbatched_blockwise", | ||
out2in(local_useless_unbatched_blockwise, ignore_newtrees=True), | ||
"fast_run", | ||
"fast_compile", | ||
"blockwise", | ||
position=49, | ||
) |
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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from pytensor import function | ||
from pytensor.graph import FunctionGraph | ||
from pytensor.scalar import log as scalar_log | ||
from pytensor.tensor import matrix, tensor3 | ||
from pytensor.tensor.blockwise import Blockwise | ||
from pytensor.tensor.elemwise import Elemwise | ||
from pytensor.tensor.nlinalg import MatrixPinv | ||
from pytensor.tensor.rewriting.blockwise import local_useless_blockwise | ||
|
||
|
||
def test_useless_blockwise_of_elemwise(): | ||
x = matrix("x") | ||
out = Blockwise(Elemwise(scalar_log), signature="()->()")(x) | ||
assert isinstance(out.owner.op, Blockwise) | ||
assert isinstance(out.owner.op.core_op, Elemwise) | ||
|
||
fg = FunctionGraph([x], [out], clone=False) | ||
[new_out] = local_useless_blockwise.transform(fg, out.owner) | ||
assert isinstance(new_out.owner.op, Elemwise) | ||
|
||
|
||
def test_useless_unbatched_blockwise(): | ||
x = matrix("x") | ||
blockwise_op = Blockwise(MatrixPinv(hermitian=False), signature="(m,n)->(n,m)") | ||
out = blockwise_op(x) | ||
|
||
assert isinstance(out.owner.op, Blockwise) | ||
assert isinstance(out.owner.op.core_op, MatrixPinv) | ||
|
||
fn = function([x], out, mode="FAST_COMPILE") | ||
assert isinstance(fn.maker.fgraph.outputs[0].owner.op, MatrixPinv) | ||
|
||
# Test that it's not removed when there are batched dims | ||
x = tensor3("x") | ||
out = blockwise_op(x) | ||
fn = function([x], out, mode="FAST_COMPILE") | ||
assert isinstance(fn.maker.fgraph.outputs[0].owner.op, Blockwise) | ||
assert isinstance(fn.maker.fgraph.outputs[0].owner.op.core_op, MatrixPinv) |
Oops, something went wrong.