2022-09-22 12:26:48 -07:00
|
|
|
# Copyright 2021 The JAX Authors.
|
2021-03-05 14:57:36 -08:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2023-03-09 13:09:20 -08:00
|
|
|
running_in_cloud_tpu_vm: bool = False
|
2022-04-18 16:39:01 -07:00
|
|
|
|
2023-01-25 09:50:56 -08:00
|
|
|
|
|
|
|
def maybe_import_libtpu():
|
|
|
|
try:
|
|
|
|
# pylint: disable=import-outside-toplevel
|
|
|
|
# pytype: disable=import-error
|
|
|
|
import libtpu
|
|
|
|
|
|
|
|
# pytype: enable=import-error
|
|
|
|
# pylint: enable=import-outside-toplevel
|
|
|
|
except ImportError:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return libtpu
|
|
|
|
|
|
|
|
|
|
|
|
def jax_force_tpu_init() -> bool:
|
|
|
|
return 'JAX_FORCE_TPU_INIT' in os.environ
|
|
|
|
|
|
|
|
|
2023-03-09 13:09:20 -08:00
|
|
|
def cloud_tpu_init() -> None:
|
Changes to make jax[tpu] work better in a docker container.
1. In cloud_tpu_init.py, check whether we're on a Cloud TPU VM by
looking for the libtpu Python package, instead of /lib/libtpu.so
(which isn't necessarily present in a docker container). JAX now
relies on the libtpu package instead of the system libtpu.so, so
this makes more sense either way. This means we'll try/catch an
ImportError in all non-TPU environments when importing jax, which
hopefully isn't noticeably slow.
2. Add requests as a jax[tpu] dependency, since it's needed by
cloud_tpu_init.py. This comes pre-installed on Cloud TPU VMs, but
may not be installed in docker containers, virtualenvs, etc.
I manually tested by creating the following Dockerfile on a Cloud TPU VM:
```
FROM ubuntu:18.04
RUN apt update && apt install git python3-pip -y
RUN git clone https://github.com/skye/jax && cd jax && git checkout tpu_docker
WORKDIR jax
RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install .[tpu] -f https://storage.googleapis.com/jax-releases/libtpu_releases.html
CMD ["python3", "-c", "import jax; print(jax.device_count())"]
```
And then running the following commands:
```
$ sudo docker build -t jax-test .
$ sudo docker run --privileged jax-test
8
```
Note the `--privileged` flags is necessary to let the container access
the TPU devices in /dev.
2021-07-12 16:57:45 -07:00
|
|
|
"""Automatically sets Cloud TPU topology and other env vars.
|
2021-03-05 14:57:36 -08:00
|
|
|
|
|
|
|
**This must be called before the TPU runtime is loaded, which happens as soon
|
|
|
|
as JAX's C++ backend is loaded! I.e. call this before xla_bridge or xla_client
|
|
|
|
is imported.**
|
|
|
|
|
Changes to make jax[tpu] work better in a docker container.
1. In cloud_tpu_init.py, check whether we're on a Cloud TPU VM by
looking for the libtpu Python package, instead of /lib/libtpu.so
(which isn't necessarily present in a docker container). JAX now
relies on the libtpu package instead of the system libtpu.so, so
this makes more sense either way. This means we'll try/catch an
ImportError in all non-TPU environments when importing jax, which
hopefully isn't noticeably slow.
2. Add requests as a jax[tpu] dependency, since it's needed by
cloud_tpu_init.py. This comes pre-installed on Cloud TPU VMs, but
may not be installed in docker containers, virtualenvs, etc.
I manually tested by creating the following Dockerfile on a Cloud TPU VM:
```
FROM ubuntu:18.04
RUN apt update && apt install git python3-pip -y
RUN git clone https://github.com/skye/jax && cd jax && git checkout tpu_docker
WORKDIR jax
RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install .[tpu] -f https://storage.googleapis.com/jax-releases/libtpu_releases.html
CMD ["python3", "-c", "import jax; print(jax.device_count())"]
```
And then running the following commands:
```
$ sudo docker build -t jax-test .
$ sudo docker run --privileged jax-test
8
```
Note the `--privileged` flags is necessary to let the container access
the TPU devices in /dev.
2021-07-12 16:57:45 -07:00
|
|
|
Safe to call in non-Cloud TPU environments.
|
|
|
|
|
2021-06-22 23:31:12 +00:00
|
|
|
Some of these environment variables are used to tell the TPU runtime what kind
|
|
|
|
of mesh topology to use. It assumes a single-host topology by default, so we
|
|
|
|
manually set them here to default to the full pod slice if applicable.
|
2021-03-05 14:57:36 -08:00
|
|
|
|
|
|
|
This will not set any env vars if a single topology-related env var is already
|
|
|
|
set.
|
|
|
|
"""
|
2022-05-17 10:53:17 -07:00
|
|
|
global running_in_cloud_tpu_vm
|
2023-01-25 09:50:56 -08:00
|
|
|
|
|
|
|
# We assume we are in a correctly-configured Cloud TPU environment
|
|
|
|
# if the following hold: a) libtpu is installed b) JAX_FORCE_TPU_INIT is set
|
|
|
|
# Exit early if we're not running on Cloud TPU.
|
|
|
|
libtpu_module = maybe_import_libtpu()
|
|
|
|
if libtpu_module is not None:
|
|
|
|
libtpu_module.configure_library_path()
|
|
|
|
elif not jax_force_tpu_init():
|
Changes to make jax[tpu] work better in a docker container.
1. In cloud_tpu_init.py, check whether we're on a Cloud TPU VM by
looking for the libtpu Python package, instead of /lib/libtpu.so
(which isn't necessarily present in a docker container). JAX now
relies on the libtpu package instead of the system libtpu.so, so
this makes more sense either way. This means we'll try/catch an
ImportError in all non-TPU environments when importing jax, which
hopefully isn't noticeably slow.
2. Add requests as a jax[tpu] dependency, since it's needed by
cloud_tpu_init.py. This comes pre-installed on Cloud TPU VMs, but
may not be installed in docker containers, virtualenvs, etc.
I manually tested by creating the following Dockerfile on a Cloud TPU VM:
```
FROM ubuntu:18.04
RUN apt update && apt install git python3-pip -y
RUN git clone https://github.com/skye/jax && cd jax && git checkout tpu_docker
WORKDIR jax
RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install .[tpu] -f https://storage.googleapis.com/jax-releases/libtpu_releases.html
CMD ["python3", "-c", "import jax; print(jax.device_count())"]
```
And then running the following commands:
```
$ sudo docker build -t jax-test .
$ sudo docker run --privileged jax-test
8
```
Note the `--privileged` flags is necessary to let the container access
the TPU devices in /dev.
2021-07-12 16:57:45 -07:00
|
|
|
return
|
2021-06-22 23:31:12 +00:00
|
|
|
|
2022-05-17 10:53:17 -07:00
|
|
|
running_in_cloud_tpu_vm = True
|
|
|
|
|
2021-03-11 22:52:54 +00:00
|
|
|
os.environ.setdefault('GRPC_VERBOSITY', 'ERROR')
|
2022-10-03 22:52:03 +00:00
|
|
|
os.environ.setdefault('JAX_PLATFORMS', 'tpu,cpu')
|
2022-06-14 21:21:34 +00:00
|
|
|
os.environ['TPU_ML_PLATFORM'] = 'JAX'
|
2023-03-28 13:42:51 -07:00
|
|
|
|
|
|
|
if 'JAX_USE_PJRT_C_API_ON_TPU' not in os.environ:
|
|
|
|
os.environ['JAX_USE_PJRT_C_API_ON_TPU'] = 'true'
|