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 6 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 doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"sphinx.ext.napoleon",
"sphinx.ext.linkcode",
"sphinx.ext.mathjax",
"sphinx_design"
]

needs_sphinx = "3"
Expand Down
1 change: 1 addition & 0 deletions doc/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
- mock
- pillow
- pymc-sphinx-theme
- sphinx-design
- pip
- pip:
- -e ..
572 changes: 468 additions & 104 deletions doc/extending/creating_a_numba_jax_op.rst

Large diffs are not rendered by default.

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
23 changes: 23 additions & 0 deletions pytensor/link/pytorch/dispatch/extra_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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):
axis = op.axis
mode = op.mode

def cumop(x):
if axis 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
else:
dim = axis

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
if mode == "add":
return torch.cumsum(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
else:
return torch.cumprod(x, dim=dim)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L21 was not covered by tests

return cumop
2 changes: 2 additions & 0 deletions pytensor/tensor/extra_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ class CumOp(COp):
def __init__(self, axis: int | None = None, mode="add"):
if mode not in ("add", "mul"):
raise ValueError(f'{type(self).__name__}: Unknown mode "{mode}"')
if not (isinstance(axis, int) or axis is None):
raise TypeError("axis must be an integer or None.")
self.axis = axis
self.mode = mode

Expand Down
40 changes: 40 additions & 0 deletions tests/link/pytorch/test_extra_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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, (0,)],
)
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
test_value = np.arange(9, dtype=config.floatX).reshape((3, 3))

# Create the output variable
if isinstance(axis, tuple):
with pytest.raises(TypeError, match="axis must be an integer or None."):
out = pt.cumsum(a, axis=axis)
with pytest.raises(TypeError, match="axis must be an integer or None."):
out = pt.cumprod(a, axis=axis)
else:
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