2018-11-17 18:03:33 -08:00
|
|
|
# Copyright 2018 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.
|
2019-07-29 15:06:05 -04:00
|
|
|
|
|
|
|
# This module is largely a wrapper around `jaxlib` that performs version
|
|
|
|
# checking on import.
|
|
|
|
|
2021-07-13 09:24:48 -04:00
|
|
|
import platform
|
2021-04-28 11:43:50 -04:00
|
|
|
import os
|
2021-07-13 09:24:48 -04:00
|
|
|
import warnings
|
2021-04-28 11:43:50 -04:00
|
|
|
from typing import Optional
|
|
|
|
|
2020-06-04 15:27:48 -07:00
|
|
|
__all__ = [
|
2021-03-31 18:35:15 -07:00
|
|
|
'cuda_linalg', 'cuda_prng', 'cusolver', 'rocsolver', 'jaxlib', 'lapack',
|
2021-02-04 15:13:39 -08:00
|
|
|
'pocketfft', 'pytree', 'tpu_client', 'version', 'xla_client'
|
2020-06-04 15:27:48 -07:00
|
|
|
]
|
|
|
|
|
2021-07-13 09:24:48 -04:00
|
|
|
# First, before attempting to import jaxlib, warn about experimental machine
|
|
|
|
# configurations.
|
|
|
|
if platform.system() == "Darwin" and platform.machine() == "arm64":
|
|
|
|
warnings.warn("JAX on Mac ARM machines is experimental and minimally tested. "
|
|
|
|
"Please see https://github.com/google/jax/issues/5501 in the "
|
|
|
|
"event of problems.")
|
|
|
|
|
2021-03-15 21:07:59 -04:00
|
|
|
try:
|
|
|
|
import jaxlib
|
|
|
|
except ModuleNotFoundError as err:
|
|
|
|
raise ModuleNotFoundError(
|
|
|
|
'jax requires jaxlib to be installed. See '
|
|
|
|
'https://github.com/google/jax#installation for installation instructions.'
|
|
|
|
) from err
|
2019-07-29 15:06:05 -04:00
|
|
|
|
2021-03-17 10:46:38 -07:00
|
|
|
from jax.version import _minimum_jaxlib_version as _minimum_jaxlib_version_str
|
2019-07-29 15:06:05 -04:00
|
|
|
try:
|
|
|
|
from jaxlib import version as jaxlib_version
|
2020-03-09 22:06:12 +02:00
|
|
|
except Exception as err:
|
2019-07-29 15:06:05 -04:00
|
|
|
# jaxlib is too old to have version number.
|
2021-03-17 10:46:38 -07:00
|
|
|
msg = f'This version of jax requires jaxlib version >= {_minimum_jaxlib_version_str}.'
|
|
|
|
raise ImportError(msg) from err
|
2019-07-29 15:06:05 -04:00
|
|
|
|
2019-09-04 16:24:32 -04:00
|
|
|
version = tuple(int(x) for x in jaxlib_version.__version__.split('.'))
|
2021-03-17 10:46:38 -07:00
|
|
|
_minimum_jaxlib_version = tuple(int(x) for x in _minimum_jaxlib_version_str.split('.'))
|
2019-07-29 15:06:05 -04:00
|
|
|
|
|
|
|
# Check the jaxlib version before importing anything else from jaxlib.
|
|
|
|
def _check_jaxlib_version():
|
|
|
|
if version < _minimum_jaxlib_version:
|
2021-03-17 10:46:38 -07:00
|
|
|
msg = (f'jaxlib is version {jaxlib_version.__version__}, '
|
|
|
|
f'but this version of jax requires version {_minimum_jaxlib_version_str}.')
|
2019-10-15 08:52:21 -04:00
|
|
|
|
|
|
|
if version == (0, 1, 23):
|
2020-08-19 18:39:25 +02:00
|
|
|
msg += ('\n\nA common cause of this error is that you installed jaxlib '
|
|
|
|
'using pip, but your version of pip is too old to support '
|
|
|
|
'manylinux2010 wheels. Try running:\n\n'
|
|
|
|
'pip install --upgrade pip\n'
|
|
|
|
'pip install --upgrade jax jaxlib\n')
|
2021-03-17 10:46:38 -07:00
|
|
|
raise ValueError(msg)
|
2019-07-29 15:06:05 -04:00
|
|
|
|
|
|
|
_check_jaxlib_version()
|
|
|
|
|
2021-07-15 16:39:18 -04:00
|
|
|
from jaxlib import cpu_feature_guard
|
|
|
|
cpu_feature_guard.check_cpu_features()
|
2021-05-19 10:58:05 -07:00
|
|
|
|
2019-07-29 15:06:05 -04:00
|
|
|
from jaxlib import xla_client
|
|
|
|
from jaxlib import lapack
|
2021-02-04 15:13:39 -08:00
|
|
|
from jaxlib import pocketfft
|
2021-01-14 14:21:01 -05:00
|
|
|
|
2021-01-20 12:43:00 -08:00
|
|
|
xla_extension = xla_client._xla
|
2021-01-14 14:21:01 -05:00
|
|
|
pytree = xla_client._xla.pytree
|
|
|
|
jax_jit = xla_client._xla.jax_jit
|
2021-01-16 17:57:39 +01:00
|
|
|
pmap_lib = xla_client._xla.pmap_lib
|
2020-12-05 00:07:04 +01:00
|
|
|
|
|
|
|
try:
|
2021-01-13 13:02:53 -08:00
|
|
|
from jaxlib import cusolver # pytype: disable=import-error
|
2020-12-05 00:07:04 +01:00
|
|
|
except ImportError:
|
|
|
|
cusolver = None
|
|
|
|
|
2021-04-15 10:10:40 -07:00
|
|
|
try:
|
|
|
|
from jaxlib import cusparse # pytype: disable=import-error
|
|
|
|
except ImportError:
|
|
|
|
cusparse = None
|
|
|
|
|
2020-12-05 00:07:04 +01:00
|
|
|
try:
|
2020-12-17 14:01:04 -08:00
|
|
|
from jaxlib import rocsolver # pytype: disable=import-error
|
2020-12-05 00:07:04 +01:00
|
|
|
except ImportError:
|
|
|
|
rocsolver = None
|
|
|
|
|
2019-11-24 13:06:23 -05:00
|
|
|
try:
|
2021-01-13 13:02:53 -08:00
|
|
|
from jaxlib import cuda_prng # pytype: disable=import-error
|
2019-11-24 13:06:23 -05:00
|
|
|
except ImportError:
|
|
|
|
cuda_prng = None
|
2020-05-12 11:05:03 -07:00
|
|
|
|
2021-03-31 18:35:15 -07:00
|
|
|
try:
|
|
|
|
from jaxlib import cuda_linalg # pytype: disable=import-error
|
|
|
|
except ImportError:
|
|
|
|
cuda_linalg = None
|
|
|
|
|
2021-01-05 13:23:57 -08:00
|
|
|
# Jaxlib code is split between the Jax and the Tensorflow repositories.
|
|
|
|
# Only for the internal usage of the JAX developers, we expose a version
|
2021-06-18 08:55:08 +03:00
|
|
|
# number that can be used to perform changes without breaking the main
|
2021-01-05 13:23:57 -08:00
|
|
|
# branch on the Jax github.
|
|
|
|
_xla_extension_version = getattr(xla_client, '_version', 0)
|
|
|
|
|
2020-05-12 11:05:03 -07:00
|
|
|
try:
|
2021-06-01 11:43:54 -07:00
|
|
|
from jaxlib import tpu_client as tpu_driver_client # pytype: disable=import-error
|
2020-05-12 11:05:03 -07:00
|
|
|
except:
|
2021-06-24 10:51:06 -04:00
|
|
|
tpu_driver_client = None # type: ignore
|
2021-04-28 11:43:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
cuda_path: Optional[str]
|
|
|
|
cuda_path = os.path.join(os.path.dirname(jaxlib.__file__), "cuda")
|
|
|
|
if not os.path.isdir(cuda_path):
|
|
|
|
cuda_path = None
|