2022-09-22 12:26:48 -07:00
|
|
|
# Copyright 2021 The JAX Authors.
|
2021-06-04 19:22:57 +00: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.
|
|
|
|
|
2021-07-20 16:16:21 +00:00
|
|
|
from functools import partial
|
2023-02-28 12:40:30 -08:00
|
|
|
import math
|
2021-09-23 06:33:25 -07:00
|
|
|
import os
|
|
|
|
import tempfile
|
2023-07-27 23:00:26 -07:00
|
|
|
from unittest import mock
|
|
|
|
from unittest import SkipTest
|
2022-09-27 20:59:08 +00:00
|
|
|
import warnings
|
2021-09-23 06:33:25 -07:00
|
|
|
|
|
|
|
from absl.testing import absltest
|
2021-06-04 19:22:57 +00:00
|
|
|
import jax
|
2023-07-11 14:47:04 -07:00
|
|
|
from jax import config
|
2023-07-27 23:00:26 -07:00
|
|
|
from jax import jit
|
|
|
|
from jax import lax
|
|
|
|
from jax import pmap
|
2023-04-17 08:35:26 -07:00
|
|
|
from jax._src import compilation_cache as cc
|
|
|
|
from jax._src import test_util as jtu
|
2023-02-28 07:01:14 -08:00
|
|
|
from jax._src import xla_bridge
|
2023-07-27 23:00:26 -07:00
|
|
|
from jax._src.config import persistent_cache_min_compile_time_secs
|
|
|
|
from jax._src.config import raise_persistent_cache_errors
|
2022-08-23 09:36:12 -07:00
|
|
|
from jax._src.lib import xla_client
|
2023-07-11 14:47:04 -07:00
|
|
|
from jax.experimental.maps import xmap
|
|
|
|
from jax.experimental.pjit import pjit
|
|
|
|
from jax.sharding import PartitionSpec as P
|
2021-06-04 19:22:57 +00:00
|
|
|
import numpy as np
|
|
|
|
|
2023-07-27 23:00:26 -07:00
|
|
|
|
2021-07-14 11:25:37 -07:00
|
|
|
config.parse_flags_with_absl()
|
|
|
|
FLAGS = config.FLAGS
|
|
|
|
|
2023-07-19 15:17:04 -07:00
|
|
|
FAKE_COMPILE_TIME = 10
|
|
|
|
|
2023-04-19 13:26:24 -07:00
|
|
|
|
|
|
|
@jtu.with_config(
|
|
|
|
jax_raise_persistent_cache_errors=True,
|
|
|
|
jax_persistent_cache_min_compile_time_secs=0,
|
|
|
|
)
|
2021-06-04 19:22:57 +00:00
|
|
|
class CompilationCacheTest(jtu.JaxTestCase):
|
|
|
|
|
2021-07-08 16:11:39 +00:00
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
2023-02-24 15:05:12 -08:00
|
|
|
supported_platforms = ["tpu", "gpu"]
|
2023-02-01 16:52:40 -08:00
|
|
|
|
2022-10-25 14:24:38 -07:00
|
|
|
if "--xla_cpu_use_xla_runtime=true" in os.environ.get("XLA_FLAGS", ""):
|
|
|
|
supported_platforms.append("cpu")
|
2023-02-01 16:52:40 -08:00
|
|
|
|
2022-09-08 11:50:01 -07:00
|
|
|
if jtu.device_under_test() not in supported_platforms:
|
2023-04-19 13:26:24 -07:00
|
|
|
raise SkipTest(
|
|
|
|
"serialize executable only works on " + ",".join(supported_platforms)
|
|
|
|
)
|
2021-07-08 16:11:39 +00:00
|
|
|
|
2023-02-07 15:14:53 -08:00
|
|
|
# Reset cache if already initialized by JaxTestCase
|
|
|
|
if cc.is_initialized():
|
|
|
|
cc.reset_cache()
|
|
|
|
|
2021-07-19 21:53:27 +00:00
|
|
|
def tearDown(self):
|
2023-02-07 15:14:53 -08:00
|
|
|
if cc.is_initialized():
|
|
|
|
cc.reset_cache()
|
|
|
|
super().tearDown()
|
2021-07-19 21:53:27 +00:00
|
|
|
|
2021-07-07 21:42:34 +00:00
|
|
|
def test_get_no_executable(self):
|
2021-10-04 13:52:23 -07:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
cc.initialize_cache(tmpdir)
|
2023-04-19 13:26:24 -07:00
|
|
|
computation = jax.jit(lambda x, y: x + y).lower(1, 1).compiler_ir()
|
2023-04-20 06:16:12 -07:00
|
|
|
devices = np.array([[jax.local_devices()[0]]])
|
2022-08-23 09:36:12 -07:00
|
|
|
compile_options = xla_bridge.get_compile_options(
|
2023-04-19 13:26:24 -07:00
|
|
|
num_replicas=1, num_partitions=1
|
|
|
|
)
|
2022-08-23 09:36:12 -07:00
|
|
|
backend = xla_bridge.get_backend()
|
2023-04-20 06:16:12 -07:00
|
|
|
key = cc.get_cache_key(computation, devices, compile_options, backend)
|
2023-07-19 15:17:04 -07:00
|
|
|
executable, compile_time = cc.get_executable_and_time(
|
|
|
|
key, compile_options, backend)
|
|
|
|
self.assertIsNone(executable)
|
|
|
|
self.assertIsNone(compile_time)
|
2021-07-07 21:42:34 +00:00
|
|
|
|
|
|
|
def test_diff_executables(self):
|
2021-10-04 13:52:23 -07:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
cc.initialize_cache(tmpdir)
|
2023-04-19 13:26:24 -07:00
|
|
|
computation1 = str(jax.jit(lambda x, y: x + y).lower(1, 1).compiler_ir())
|
|
|
|
computation2 = str(jax.jit(lambda x, y: x * y).lower(2, 2).compiler_ir())
|
2022-08-23 09:36:12 -07:00
|
|
|
compile_options = xla_bridge.get_compile_options(
|
2023-04-19 13:26:24 -07:00
|
|
|
num_replicas=1, num_partitions=1
|
|
|
|
)
|
2022-08-23 09:36:12 -07:00
|
|
|
backend = xla_bridge.get_backend()
|
2021-10-04 13:52:23 -07:00
|
|
|
executable1 = backend.compile(computation1, compile_options)
|
|
|
|
executable2 = backend.compile(computation2, compile_options)
|
2023-07-19 15:17:04 -07:00
|
|
|
cc.put_executable_and_time(
|
|
|
|
"key1", "computation1", executable1, backend, FAKE_COMPILE_TIME)
|
|
|
|
cc.put_executable_and_time(
|
|
|
|
"key2", "computation2", executable2, backend, FAKE_COMPILE_TIME)
|
2023-04-19 13:26:24 -07:00
|
|
|
self.assertNotEqual(
|
2023-07-19 15:17:04 -07:00
|
|
|
cc.get_executable_and_time("key1", compile_options, backend)[0],
|
|
|
|
cc.get_executable_and_time("key2", compile_options, backend)[0]
|
2023-04-19 13:26:24 -07:00
|
|
|
)
|
2021-07-07 21:42:34 +00:00
|
|
|
|
|
|
|
def test_put_executable(self):
|
2021-10-04 13:52:23 -07:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
cc.initialize_cache(tmpdir)
|
2023-04-19 13:26:24 -07:00
|
|
|
computation = (
|
|
|
|
jax.jit(lambda x, y: x + y)
|
|
|
|
.lower(np.int32(1), np.int32(1))
|
|
|
|
.compiler_ir()
|
|
|
|
)
|
2023-04-20 06:16:12 -07:00
|
|
|
devices = np.array([[jax.local_devices()[0]]])
|
2022-08-23 09:36:12 -07:00
|
|
|
compile_options = xla_bridge.get_compile_options(
|
2023-04-19 13:26:24 -07:00
|
|
|
num_replicas=1, num_partitions=1
|
|
|
|
)
|
2022-08-23 09:36:12 -07:00
|
|
|
backend = xla_bridge.get_backend()
|
2023-04-19 13:26:24 -07:00
|
|
|
executable = backend.compile(str(computation), compile_options)
|
2023-04-20 06:16:12 -07:00
|
|
|
key = cc.get_cache_key(computation, devices, compile_options, backend)
|
2023-07-19 15:17:04 -07:00
|
|
|
cc.put_executable_and_time(
|
|
|
|
key, "alambda", executable, backend, FAKE_COMPILE_TIME)
|
|
|
|
executable_retrieved, compile_time_retrieved = cc.get_executable_and_time(
|
|
|
|
key, compile_options, backend)
|
2023-04-19 13:26:24 -07:00
|
|
|
inputs_to_executable = (
|
|
|
|
np.array(1, dtype=np.int32),
|
|
|
|
np.array(2, dtype=np.int32),
|
|
|
|
)
|
|
|
|
expected = xla_client.execute_with_python_values(
|
|
|
|
executable, inputs_to_executable, backend
|
|
|
|
)
|
|
|
|
actual = xla_client.execute_with_python_values(
|
2023-07-19 15:17:04 -07:00
|
|
|
executable_retrieved, inputs_to_executable, backend
|
2023-04-19 13:26:24 -07:00
|
|
|
)
|
2021-10-04 13:52:23 -07:00
|
|
|
self.assertEqual(expected, actual)
|
2023-07-19 15:17:04 -07:00
|
|
|
self.assertEqual(FAKE_COMPILE_TIME, compile_time_retrieved)
|
2021-07-07 21:42:34 +00:00
|
|
|
|
2021-07-08 16:11:39 +00:00
|
|
|
def test_pmap(self):
|
2021-10-04 13:52:23 -07:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
cc.initialize_cache(tmpdir)
|
2023-04-19 13:26:24 -07:00
|
|
|
f = pmap(lambda x: x - lax.psum(x, "i"), axis_name="i")
|
2021-10-04 13:52:23 -07:00
|
|
|
x = np.arange(jax.device_count(), dtype=np.int64)
|
|
|
|
f(x)
|
|
|
|
files_in_directory = len(os.listdir(tmpdir))
|
|
|
|
self.assertEqual(files_in_directory, 1)
|
|
|
|
x = np.arange(jax.device_count(), dtype=np.float32)
|
|
|
|
f(x)
|
|
|
|
files_in_directory = len(os.listdir(tmpdir))
|
|
|
|
self.assertEqual(files_in_directory, 2)
|
2023-04-19 13:26:24 -07:00
|
|
|
# TODO: create a test for calling pmap with the same input more than once
|
2021-07-08 16:11:39 +00:00
|
|
|
|
2021-07-19 19:10:16 +00:00
|
|
|
def test_jit(self):
|
2021-10-04 13:52:23 -07:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
cc.initialize_cache(tmpdir)
|
2023-04-19 13:26:24 -07:00
|
|
|
f = jit(lambda x: x * x)
|
2021-10-04 13:52:23 -07:00
|
|
|
f(1)
|
|
|
|
files_in_directory = len(os.listdir(tmpdir))
|
|
|
|
self.assertEqual(files_in_directory, 1)
|
|
|
|
f(1.0)
|
|
|
|
files_in_directory = len(os.listdir(tmpdir))
|
|
|
|
self.assertEqual(files_in_directory, 2)
|
2021-07-19 19:10:16 +00:00
|
|
|
|
2023-04-19 13:26:24 -07:00
|
|
|
@jtu.with_mesh([("x", 2)])
|
2021-07-20 16:16:21 +00:00
|
|
|
def test_pjit(self):
|
2021-10-04 13:52:23 -07:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
cc.initialize_cache(tmpdir)
|
2023-04-19 13:26:24 -07:00
|
|
|
|
|
|
|
@partial(pjit, in_shardings=(P("x"), P("x")), out_shardings=None)
|
2021-10-04 13:52:23 -07:00
|
|
|
def f(x, y):
|
|
|
|
return x + y
|
2021-07-20 16:16:21 +00:00
|
|
|
|
2021-10-04 13:52:23 -07:00
|
|
|
shape = (8, 8)
|
2023-02-28 12:40:30 -08:00
|
|
|
x = np.arange(math.prod(shape), dtype=np.int64).reshape(shape)
|
2021-10-04 13:52:23 -07:00
|
|
|
f(x, x + 1)
|
|
|
|
files_in_directory = len(os.listdir(tmpdir))
|
|
|
|
self.assertEqual(files_in_directory, 1)
|
2023-02-28 12:40:30 -08:00
|
|
|
x = np.arange(math.prod(shape), dtype=np.float32).reshape(shape)
|
2021-10-04 13:52:23 -07:00
|
|
|
f(x, x + 1)
|
|
|
|
files_in_directory = len(os.listdir(tmpdir))
|
|
|
|
self.assertEqual(files_in_directory, 2)
|
2021-07-20 16:16:21 +00:00
|
|
|
|
2023-04-19 13:26:24 -07:00
|
|
|
@jtu.with_mesh([("x", 2)])
|
2021-07-20 16:16:21 +00:00
|
|
|
def test_xmap(self):
|
2021-10-04 13:52:23 -07:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
cc.initialize_cache(tmpdir)
|
2023-04-19 13:26:24 -07:00
|
|
|
|
2021-10-04 13:52:23 -07:00
|
|
|
def f(x):
|
|
|
|
return x * 2
|
2023-04-19 13:26:24 -07:00
|
|
|
|
2021-10-04 13:52:23 -07:00
|
|
|
devices = np.array(jax.local_devices()[:2])
|
|
|
|
if devices.size < 2:
|
|
|
|
raise SkipTest("Test requires 2 devices")
|
|
|
|
x = np.arange(8, dtype=np.int64).reshape((2, 2, 2))
|
2023-04-19 13:26:24 -07:00
|
|
|
xmap(
|
|
|
|
f, in_axes=["a", ...], out_axes=["a", ...], axis_resources={"a": "x"}
|
|
|
|
)(x)
|
2021-10-04 13:52:23 -07:00
|
|
|
files_in_directory = len(os.listdir(tmpdir))
|
|
|
|
self.assertEqual(files_in_directory, 1)
|
|
|
|
x = np.arange(8, dtype=np.float32).reshape((2, 2, 2))
|
2023-04-19 13:26:24 -07:00
|
|
|
xmap(
|
|
|
|
f, in_axes=["a", ...], out_axes=["a", ...], axis_resources={"a": "x"}
|
|
|
|
)(x)
|
2021-10-04 13:52:23 -07:00
|
|
|
files_in_directory = len(os.listdir(tmpdir))
|
|
|
|
self.assertEqual(files_in_directory, 2)
|
2021-07-20 16:16:21 +00:00
|
|
|
|
2022-09-27 20:59:08 +00:00
|
|
|
def test_cache_write_warning(self):
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
cc.initialize_cache(tmpdir)
|
2023-04-19 13:26:24 -07:00
|
|
|
f = jit(lambda x: x * x)
|
2022-09-27 20:59:08 +00:00
|
|
|
|
2023-04-19 13:26:24 -07:00
|
|
|
with raise_persistent_cache_errors(False), mock.patch.object(
|
|
|
|
cc._cache.__class__, "put"
|
|
|
|
) as mock_put, warnings.catch_warnings(record=True) as w:
|
2022-09-27 20:59:08 +00:00
|
|
|
mock_put.side_effect = RuntimeError("test error")
|
|
|
|
self.assertEqual(f(2), 4)
|
|
|
|
self.assertLen(w, 1)
|
|
|
|
self.assertIn(
|
2023-04-19 13:26:24 -07:00
|
|
|
(
|
|
|
|
"Error writing persistent compilation cache entry "
|
|
|
|
"for 'jit__lambda_': RuntimeError: test error"
|
|
|
|
),
|
|
|
|
str(w[0].message),
|
|
|
|
)
|
2022-09-27 20:59:08 +00:00
|
|
|
|
|
|
|
def test_cache_read_warning(self):
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
cc.initialize_cache(tmpdir)
|
2023-04-19 13:26:24 -07:00
|
|
|
f = jit(lambda x: x * x)
|
2022-09-27 20:59:08 +00:00
|
|
|
|
2023-04-19 13:26:24 -07:00
|
|
|
with raise_persistent_cache_errors(False), mock.patch.object(
|
|
|
|
cc._cache.__class__, "get"
|
|
|
|
) as mock_get, warnings.catch_warnings(record=True) as w:
|
2022-09-27 20:59:08 +00:00
|
|
|
mock_get.side_effect = RuntimeError("test error")
|
|
|
|
self.assertEqual(f(2), 4)
|
2023-03-22 21:43:51 +00:00
|
|
|
if len(w) > 1:
|
|
|
|
print("Warnings:", [str(w_) for w_ in w], flush=True)
|
2023-03-28 12:16:04 -07:00
|
|
|
self.assertLen(w, 1)
|
2022-09-27 20:59:08 +00:00
|
|
|
self.assertIn(
|
2023-04-19 13:26:24 -07:00
|
|
|
(
|
|
|
|
"Error reading persistent compilation cache entry "
|
|
|
|
"for 'jit__lambda_': RuntimeError: test error"
|
|
|
|
),
|
|
|
|
str(w[0].message),
|
|
|
|
)
|
2022-09-27 20:59:08 +00:00
|
|
|
|
2022-10-28 23:53:30 +00:00
|
|
|
def test_min_compile_time(self):
|
2023-04-19 13:26:24 -07:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdir, persistent_cache_min_compile_time_secs(
|
|
|
|
2
|
|
|
|
):
|
2022-10-13 23:14:49 +00:00
|
|
|
cc.initialize_cache(tmpdir)
|
|
|
|
|
2022-10-28 23:53:30 +00:00
|
|
|
# Mock time to progress in small intervals so compilation time is small.
|
2023-04-19 13:26:24 -07:00
|
|
|
with mock.patch("time.monotonic", side_effect=np.arange(0, 10, 0.1)):
|
2022-10-28 23:53:30 +00:00
|
|
|
jit(lambda x: x + 1)(1)
|
2022-10-13 23:14:49 +00:00
|
|
|
files_in_cache = len(os.listdir(tmpdir))
|
|
|
|
self.assertEqual(files_in_cache, 0)
|
|
|
|
|
2022-10-28 23:53:30 +00:00
|
|
|
# Mock time to progress in large intervals so compilation time is large.
|
|
|
|
with mock.patch("time.monotonic", side_effect=np.arange(0, 100, 10)):
|
|
|
|
jit(lambda x: x + 2)(1)
|
2022-10-13 23:14:49 +00:00
|
|
|
files_in_cache = len(os.listdir(tmpdir))
|
|
|
|
self.assertEqual(files_in_cache, 1)
|
|
|
|
|
2023-04-19 13:26:24 -07:00
|
|
|
|
2021-06-04 19:22:57 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
absltest.main(testLoader=jtu.JaxTestLoader())
|