|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Pathways JAX abstractions. |
| 15 | +
|
| 16 | +This introduces an abstrction layer some JAX APIs that have changed over |
| 17 | +`pathwaysutils`'s compatibility window. |
| 18 | +""" |
| 19 | + |
| 20 | +from typing import Any |
| 21 | + |
| 22 | +try: |
| 23 | + # jax>=0.7.0 |
| 24 | + from jax.extend import backend # pylint: disable=g-import-not-at-top |
| 25 | + |
| 26 | + register_backend_cache = backend.register_backend_cache |
| 27 | + |
| 28 | + del backend |
| 29 | +except AttributeError: |
| 30 | + # jax<0.7.0 |
| 31 | + from jax._src import util # pylint: disable=g-import-not-at-top |
| 32 | + |
| 33 | + def register_backend_cache(cache: Any, name: str): # pylint: disable=unused-argument |
| 34 | + return util.cache_clearing_funs.add(cache.cache_clear) |
| 35 | + |
| 36 | + del util |
| 37 | + |
| 38 | +try: |
| 39 | + # jax>0.7.0 |
| 40 | + from jax.extend import backend # pylint: disable=g-import-not-at-top |
| 41 | + |
| 42 | + ifrt_proxy = backend.ifrt_proxy |
| 43 | + del backend |
| 44 | +except AttributeError: |
| 45 | + # jax<=0.7.0 |
| 46 | + from jax.lib import xla_extension # pylint: disable=g-import-not-at-top |
| 47 | + |
| 48 | + ifrt_proxy = xla_extension.ifrt_proxy |
| 49 | + del xla_extension |
0 commit comments