Skip to content

Fix Blockwise and RandomVariable in Numba with repeated arguments #1222

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pytensor/tensor/blockwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,13 @@ def vectorize_node_fallback(op: Op, node: Apply, *bached_inputs) -> Apply:
class OpWithCoreShape(OpFromGraph):
"""Generalizes an `Op` to include core shape as an additional input."""

def __init__(self, *args, on_unused_input="ignore", **kwargs):
# We set on_unused_inputs="ignore" so that we can easily wrap nodes with repeated inputs
# In this case the subsequent appearance of repeated inputs get disconnected in the inner graph
# I can't think of a scenario where this will backfire, but if there's one
# I bet on inplacing operations (time will tell)
return super().__init__(*args, on_unused_input=on_unused_input, **kwargs)


class BlockwiseWithCoreShape(OpWithCoreShape):
"""Generalizes a Blockwise `Op` to include a core shape parameter."""
Expand Down
16 changes: 14 additions & 2 deletions tests/link/numba/test_blockwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import pytest

from pytensor import function
from pytensor.tensor import tensor
from pytensor.tensor import tensor, tensor3
from pytensor.tensor.basic import ARange
from pytensor.tensor.blockwise import Blockwise
from pytensor.tensor.blockwise import Blockwise, BlockwiseWithCoreShape
from pytensor.tensor.nlinalg import SVD, Det
from pytensor.tensor.slinalg import Cholesky, cholesky
from tests.link.numba.test_basic import compare_numba_and_py, numba_mode
Expand Down Expand Up @@ -58,3 +58,15 @@ def test_blockwise_benchmark(benchmark):
x_test = np.eye(3) * np.arange(1, 6)[:, None, None]
fn(x_test) # JIT compile
benchmark(fn, x_test)


def test_repeated_args():
x = tensor3("x")
x_test = np.full((1, 1, 1), 2.0, dtype=x.type.dtype)
out = x @ x
fn, _ = compare_numba_and_py([x], [out], [x_test], eval_obj_mode=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get it, where's the repeated arg?

Copy link
Member Author

@ricardoV94 ricardoV94 Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

matmul with x twice


# Confirm we are testing a Blockwise with repeated inputs
final_node = fn.maker.fgraph.outputs[0].owner
assert isinstance(final_node.op, BlockwiseWithCoreShape)
assert final_node.inputs[0] is final_node.inputs[1]
12 changes: 12 additions & 0 deletions tests/link/numba/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pytensor import shared
from pytensor.compile.builders import OpFromGraph
from pytensor.compile.function import function
from pytensor.tensor.random.op import RandomVariableWithCoreShape
from tests.link.numba.test_basic import (
compare_numba_and_py,
numba_mode,
Expand Down Expand Up @@ -672,3 +673,14 @@ def test_rv_inside_ofg():
def test_unnatural_batched_dims(batch_dims_tester):
"""Tests for RVs that don't have natural batch dims in Numba API."""
batch_dims_tester(mode="NUMBA")


def test_repeated_args():
v = pt.scalar()
x = ptr.beta(v, v)
fn, _ = compare_numba_and_py([v], [x], [0.5 * 1e6], eval_obj_mode=False)

# Confirm we are testing a RandomVariable with repeated inputs
final_node = fn.maker.fgraph.outputs[0].owner
assert isinstance(final_node.op, RandomVariableWithCoreShape)
assert final_node.inputs[-2] is final_node.inputs[-1]