Skip to content
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
49 changes: 49 additions & 0 deletions pathwaysutils/jax/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Pathways JAX abstractions.

This introduces an abstrction layer some JAX APIs that have changed over
`pathwaysutils`'s compatibility window.
"""

from typing import Any

try:
# jax>=0.7.0
from jax.extend import backend # pylint: disable=g-import-not-at-top

register_backend_cache = backend.register_backend_cache

del backend
except AttributeError:
# jax<0.7.0
from jax._src import util # pylint: disable=g-import-not-at-top

def register_backend_cache(cache: Any, name: str, util=util): # pylint: disable=unused-argument
return util.cache_clearing_funs.add(cache.cache_clear)

del util

try:
# jax>0.7.0
from jax.extend import backend # pylint: disable=g-import-not-at-top

ifrt_proxy = backend.ifrt_proxy
del backend
except AttributeError:
# jax<=0.7.0
from jax.lib import xla_extension # pylint: disable=g-import-not-at-top

ifrt_proxy = xla_extension.ifrt_proxy
del xla_extension
4 changes: 2 additions & 2 deletions pathwaysutils/lru_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import functools
from typing import Any, Callable

from jax.extend import backend
from pathwaysutils import jax as pw_jax


def lru_cache(
Expand All @@ -38,7 +38,7 @@ def wrap(f):

wrapper.cache_clear = cached.cache_clear
wrapper.cache_info = cached.cache_info
backend.register_backend_cache(wrapper, "Pathways LRU cache")
pw_jax.register_backend_cache(wrapper, "Pathways LRU cache")
return wrapper

return wrap
5 changes: 3 additions & 2 deletions pathwaysutils/proxy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@

import jax
from jax.extend import backend
from pathwaysutils import jax as pw_jax


def register_backend_factory():
backend.register_backend_factory(
"proxy",
lambda: backend.ifrt_proxy.get_client(
lambda: pw_jax.ifrt_proxy.get_client(
jax.config.read("jax_backend_target"),
backend.ifrt_proxy.ClientConnectionOptions(),
pw_jax.ifrt_proxy.ClientConnectionOptions(),
),
priority=-1,
)
4 changes: 2 additions & 2 deletions pathwaysutils/test/proxy_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

import jax
from jax.extend import backend
from pathwaysutils import jax as pw_jax
from pathwaysutils import proxy_backend


from absl.testing import absltest


Expand All @@ -38,7 +38,7 @@ def test_no_proxy_backend_registration_raises_error(self):
def test_proxy_backend_registration(self):
self.enter_context(
mock.patch.object(
backend.ifrt_proxy,
pw_jax.ifrt_proxy,
"get_client",
return_value=mock.MagicMock(),
)
Expand Down