Skip to content

Implement @as_jax_op to wrap a JAX function for use in PyTensor #1120

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

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"jax": ("https://jax.readthedocs.io/en/latest", None),
"numpy": ("https://numpy.org/doc/stable", None),
"torch": ("https://pytorch.org/docs/stable", None),
"equinox": ("https://docs.kidger.site/equinox/", None),
}

needs_sphinx = "3"
Expand Down
2 changes: 1 addition & 1 deletion doc/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ dependencies:
- pip
- pip:
- sphinx_sitemap
- -e ..
- -e ..[jax]
Copy link
Member

Choose a reason for hiding this comment

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

should be reverted?

7 changes: 7 additions & 0 deletions doc/library/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ Convert to Variable

.. autofunction:: pytensor.as_symbolic(...)

Wrap JAX functions
Copy link
Member

Choose a reason for hiding this comment

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

Reminds me, may want to add something on defining jax/numba/pytorch ops page

==================

.. autofunction:: as_jax_op(...)

Alias for :func:`pytensor.link.jax.ops.as_jax_op`

Debug
=====

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ tests = [
"pytest-sphinx",
]
rtd = ["sphinx>=5.1.0,<6", "pygments", "pydot", "pydot2", "pydot-ng"]
jax = ["jax", "jaxlib"]
jax = ["jax", "jaxlib", "equinox"]
numba = ["numba>=0.57", "llvmlite"]

[tool.setuptools.packages.find]
Expand Down
12 changes: 12 additions & 0 deletions pytensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ def get_underlying_scalar_constant(v):
from pytensor.scan.views import foldl, foldr, map, reduce
from pytensor.compile.builders import OpFromGraph

try:
import pytensor.link.jax.ops
from pytensor.link.jax.ops import as_jax_op
Comment on lines +170 to +172
Copy link
Member

Choose a reason for hiding this comment

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

Do not force eager import of JAX. You can make it inside the as_jax_op?

We did some effort to reduce import times of the library

except ImportError as e:
import_error_as_jax_op = e

def as_jax_op(*args, **kwargs):
raise ImportError(
"JAX and/or equinox are not installed. Install them"
" to use this function: pip install pytensor[jax]"
) from import_error_as_jax_op

# isort: on


Expand Down
Loading