|
| 1 | +from pytensor.compile.mode import optdb |
| 2 | +from pytensor.graph import node_rewriter |
| 3 | +from pytensor.graph.rewriting.basic import copy_stack_trace, out2in |
| 4 | +from pytensor.tensor.blockwise import Blockwise, vectorize_node |
| 5 | +from pytensor.tensor.rewriting.basic import register_useless |
| 6 | + |
| 7 | + |
| 8 | +@register_useless("fast_compile") |
| 9 | +@node_rewriter([Blockwise]) |
| 10 | +def local_useless_blockwise(fgraph, node): |
| 11 | + # If there is a dispatch implementation that does not require Blockwise, use that instead. |
| 12 | + # This means a user created a Blockwise manually when there was no need. |
| 13 | + op = node.op |
| 14 | + inputs = node.inputs |
| 15 | + dummy_core_node = op._create_dummy_core_node(node.inputs) |
| 16 | + vect_node = vectorize_node(dummy_core_node, *inputs) |
| 17 | + if not isinstance(vect_node.op, Blockwise): |
| 18 | + return copy_stack_trace(node.outputs, vect_node.outputs) |
| 19 | + |
| 20 | + |
| 21 | +@node_rewriter([Blockwise]) |
| 22 | +def local_useless_unbatched_blockwise(fgraph, node): |
| 23 | + """Remove Blockwise that don't have any batched dims.""" |
| 24 | + op = node.op |
| 25 | + inputs = node.inputs |
| 26 | + |
| 27 | + if max(inp.type.ndim - len(sig) for inp, sig in zip(inputs, op.inputs_sig)) == 0: |
| 28 | + return copy_stack_trace(node.outputs, op.core_op.make_node(*inputs).outputs) |
| 29 | + |
| 30 | + |
| 31 | +# We register this rewrite late, so that other rewrites need only target Blockwise Ops |
| 32 | +optdb.register( |
| 33 | + "local_useless_unbatched_blockwise", |
| 34 | + out2in(local_useless_unbatched_blockwise, ignore_newtrees=True), |
| 35 | + "fast_run", |
| 36 | + "fast_compile", |
| 37 | + "blockwise", |
| 38 | + position=49, |
| 39 | +) |
0 commit comments