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

Torch dispatch for scipy-like functions and Softplus #1066

Merged
merged 5 commits into from
Nov 27, 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: 17 additions & 1 deletion pytensor/link/pytorch/dispatch/elemwise.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import importlib

import torch

from pytensor.link.pytorch.dispatch.basic import pytorch_funcify
Expand All @@ -11,12 +13,26 @@
scalar_op = op.scalar_op
base_fn = pytorch_funcify(scalar_op, node=node, **kwargs)

if hasattr(scalar_op, "nfunc_spec") and hasattr(torch, scalar_op.nfunc_spec[0]):
def check_special_scipy(func_name):
if "scipy." not in func_name:
return False

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

View check run for this annotation

Codecov / codecov/patch

pytensor/link/pytorch/dispatch/elemwise.py#L18

Added line #L18 was not covered by tests
loc = func_name.split(".")[1:]
try:
mod = importlib.import_module(".".join(loc[:-1]), "torch")
return getattr(mod, loc[-1], False)

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

View check run for this annotation

Codecov / codecov/patch

pytensor/link/pytorch/dispatch/elemwise.py#L22

Added line #L22 was not covered by tests
except ImportError:
return False

if hasattr(scalar_op, "nfunc_spec") and (
hasattr(torch, scalar_op.nfunc_spec[0])
or check_special_scipy(scalar_op.nfunc_spec[0])
):
# 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):
Expand Down
17 changes: 15 additions & 2 deletions pytensor/link/pytorch/dispatch/scalar.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import importlib

import torch

from pytensor.link.pytorch.dispatch.basic import pytorch_funcify
from pytensor.scalar.basic import (
Cast,
ScalarOp,
)
from pytensor.scalar.math import Softplus


@pytorch_funcify.register(ScalarOp)
Expand All @@ -19,9 +22,14 @@ def pytorch_funcify_ScalarOp(op, node, **kwargs):
if nfunc_spec is None:
raise NotImplementedError(f"Dispatch not implemented for Scalar Op {op}")

func_name = nfunc_spec[0]
func_name = nfunc_spec[0].replace("scipy.", "")
Ch0ronomato marked this conversation as resolved.
Show resolved Hide resolved

pytorch_func = getattr(torch, func_name)
if "." in func_name:
loc = func_name.split(".")
mod = importlib.import_module(".".join(["torch", *loc[:-1]]))
pytorch_func = getattr(mod, loc[-1])
else:
pytorch_func = getattr(torch, func_name)

if len(node.inputs) > op.nfunc_spec[1]:
# Some Scalar Ops accept multiple number of inputs, behaving as a variadic function,
Expand Down Expand Up @@ -49,3 +57,8 @@ def cast(x):
return x.to(dtype=dtype)

return cast


@pytorch_funcify.register(Softplus)
def pytorch_funcify_Softplus(op, node, **kwargs):
return torch.nn.Softplus()
16 changes: 15 additions & 1 deletion tests/link/pytorch/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pytensor.ifelse import ifelse
from pytensor.link.pytorch.linker import PytorchLinker
from pytensor.raise_op import CheckAndRaise
from pytensor.tensor import alloc, arange, as_tensor, empty, eye
from pytensor.tensor import alloc, arange, as_tensor, empty, expit, eye, softplus
from pytensor.tensor.type import matrices, matrix, scalar, vector


Expand Down Expand Up @@ -374,3 +374,17 @@ def inner_fn(x):
f = function([x], out, mode="PYTORCH")
f(torch.ones(3))
assert "inner_fn" not in dir(m), "function call reference leaked"


def test_pytorch_scipy():
x = vector("a", shape=(3,))
out = expit(x)
f = FunctionGraph([x], [out])
compare_pytorch_and_py(f, [np.random.rand(3)])


def test_pytorch_softplus():
x = vector("a", shape=(3,))
out = softplus(x)
f = FunctionGraph([x], [out])
compare_pytorch_and_py(f, [np.random.rand(3)])
Loading