Skip to content
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

Allow for more elemwise torch functions using broadcast_tensor and vmap #1032

Merged
merged 1 commit into from
Nov 19, 2024
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
18 changes: 15 additions & 3 deletions pytensor/link/pytorch/dispatch/elemwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,21 @@
scalar_op = op.scalar_op
base_fn = pytorch_funcify(scalar_op, node=node, **kwargs)

def elemwise_fn(*inputs):
Elemwise._check_runtime_broadcast(node, inputs)
return base_fn(*inputs)
if hasattr(scalar_op, "nfunc_spec") and hasattr(torch, scalar_op.nfunc_spec[0]):
Copy link
Member

Choose a reason for hiding this comment

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

Should do the same trick for scipy.x you did in another PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

...but then I have to write a new test for this PR 😅

# torch can handle this scalar
# broadcast, we'll let it.
def elemwise_fn(*inputs):
Elemwise._check_runtime_broadcast(node, inputs)
return base_fn(*inputs)
else:

def elemwise_fn(*inputs):
Ch0ronomato marked this conversation as resolved.
Show resolved Hide resolved
Elemwise._check_runtime_broadcast(node, inputs)
broadcast_inputs = torch.broadcast_tensors(*inputs)
ufunc = base_fn

Check warning on line 25 in pytensor/link/pytorch/dispatch/elemwise.py

View check run for this annotation

Codecov / codecov/patch

pytensor/link/pytorch/dispatch/elemwise.py#L23-L25

Added lines #L23 - L25 were not covered by tests
for _ in range(broadcast_inputs[0].dim()):
ufunc = torch.vmap(ufunc)
return ufunc(*broadcast_inputs)

Check warning on line 28 in pytensor/link/pytorch/dispatch/elemwise.py

View check run for this annotation

Codecov / codecov/patch

pytensor/link/pytorch/dispatch/elemwise.py#L27-L28

Added lines #L27 - L28 were not covered by tests

return elemwise_fn

Expand Down
33 changes: 33 additions & 0 deletions tests/link/pytorch/test_elemwise.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import numpy as np
import pytest

import pytensor
import pytensor.tensor as pt
import pytensor.tensor.math as ptm
from pytensor.configdefaults import config
from pytensor.graph.fg import FunctionGraph
from pytensor.scalar.basic import ScalarOp, get_scalar_type
from pytensor.tensor.elemwise import Elemwise
from pytensor.tensor.special import SoftmaxGrad, log_softmax, softmax
from pytensor.tensor.type import matrix, tensor, tensor3, vector
from tests.link.pytorch.test_basic import compare_pytorch_and_py
Expand Down Expand Up @@ -150,3 +153,33 @@ def test_cast():
fgraph, [np.arange(6, dtype="float32").reshape(2, 3)]
)
assert res.dtype == torch.int32


def test_vmap_elemwise():
from pytensor.link.pytorch.dispatch.basic import pytorch_funcify

class TestOp(ScalarOp):
ricardoV94 marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self):
super().__init__(
output_types_preference=lambda *_: [get_scalar_type("float32")]
)
self.call_shapes = []
self.nin = 1

def perform(self, *_):
raise RuntimeError("In perform")

@pytorch_funcify.register(TestOp)
def relu(op, node, **kwargs):
def relu(row):
op.call_shapes.append(row.size())
return torch.max(torch.zeros_like(row), row)

return relu

x = matrix("x", shape=(2, 3))
op = TestOp()
f = pytensor.function([x], Elemwise(op)(x), mode="PYTORCH")
vals = torch.zeros(2, 3).normal_()
np.testing.assert_allclose(f(vals), torch.relu(vals))
assert op.call_shapes == [torch.Size([])], op.call_shapes
Loading