forked from pymc-devs/pytensor
-
Notifications
You must be signed in to change notification settings - Fork 0
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 <brandonwillard@users.noreply.github.com> Co-authored-by: Purna Chandra Mansingh <purnachandramansingh135@gmail.com> Co-authored-by: Sayam Kumar <sayamkumar049@gmail.com3> Co-authored-by: Kaustubh <ckaustubhm06@gmail.com>
- Loading branch information
1 parent
a3869c6
commit e396906
Showing
10 changed files
with
962 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,39 @@ | ||
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 | ||
from pytensor.tensor.rewriting.basic import register_useless | ||
|
||
|
||
@register_useless("fast_compile") | ||
@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. | ||
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,36 @@ | ||
from pytensor import function | ||
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 | ||
|
||
|
||
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) | ||
|
||
fn = function([x], out, mode="FAST_COMPILE") | ||
assert isinstance(fn.maker.fgraph.outputs[0].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.