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

Add docs on implementing Pytorch Ops (and CumOp) #837

Merged
merged 8 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions pytensor/link/pytorch/dispatch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
# # Load dispatch specializations
import pytensor.link.pytorch.dispatch.scalar
import pytensor.link.pytorch.dispatch.elemwise
import pytensor.link.pytorch.dispatch.extra_ops
# isort: on
21 changes: 21 additions & 0 deletions pytensor/link/pytorch/dispatch/extra_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import torch

from pytensor.link.pytorch.dispatch.basic import pytorch_funcify
from pytensor.tensor.extra_ops import CumOp


@pytorch_funcify.register(CumOp)
def pytorch_funcify_Cumop(op, **kwargs):
dim = op.axis
mode = op.mode

def cumop(x, dim=dim):
Copy link
Member

Choose a reason for hiding this comment

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

Looks good, just no need for any kwargs. The function will only ever receive the node inputs

Suggested change
def cumop(x, dim=dim):
def cumop(x):

if dim is None:
x = x.reshape(-1)
dim = 0

Check warning on line 15 in pytensor/link/pytorch/dispatch/extra_ops.py

View check run for this annotation

Codecov / codecov/patch

pytensor/link/pytorch/dispatch/extra_ops.py#L14-L15

Added lines #L14 - L15 were not covered by tests
if mode == "add":
return torch.cumsum(x, dim=dim)

Check warning on line 17 in pytensor/link/pytorch/dispatch/extra_ops.py

View check run for this annotation

Codecov / codecov/patch

pytensor/link/pytorch/dispatch/extra_ops.py#L17

Added line #L17 was not covered by tests
else:
return torch.cumprod(x, dim=dim)

Check warning on line 19 in pytensor/link/pytorch/dispatch/extra_ops.py

View check run for this annotation

Codecov / codecov/patch

pytensor/link/pytorch/dispatch/extra_ops.py#L19

Added line #L19 was not covered by tests

return cumop
38 changes: 38 additions & 0 deletions tests/link/pytorch/test_extra_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import numpy as np
import pytest

import pytensor.tensor as pt
from pytensor.configdefaults import config
from pytensor.graph import FunctionGraph
from tests.link.pytorch.test_basic import compare_pytorch_and_py


@pytest.mark.parametrize(
"axis",
[
None,
1,
],
)
def test_pytorch_CumOp(axis):
"""Test PyTorch conversion of the `CumOp` `Op`."""

# Create a symbolic input for the first input of `CumOp`
a = pt.matrix("a")

# Create test value tag for a
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Create test value tag for a
# Create test value

test_value = np.arange(9, dtype=config.floatX).reshape((3, 3))

# Create the output variable
out = pt.cumsum(a, axis=axis)

# Create a PyTensor `FunctionGraph`
fgraph = FunctionGraph([a], [out])

# Pass the graph and inputs to the testing function
compare_pytorch_and_py(fgraph, [test_value])

# For the second mode of CumOp
out = pt.cumprod(a, axis=axis)
fgraph = FunctionGraph([a], [out])
compare_pytorch_and_py(fgraph, [test_value])
Loading