forked from jax-ml/jax
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompilation_cache_test.py
617 lines (536 loc) · 24.8 KB
/
compilation_cache_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# Copyright 2021 The JAX Authors.
#
# 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.
from __future__ import annotations
from collections import Counter
from functools import partial
import logging
import math
import os
import platform
import unittest
from unittest import mock
from unittest import SkipTest
from absl.testing import absltest
from absl.testing import parameterized
import jax
from jax import jit
from jax import lax
from jax import numpy as jnp
from jax import pmap
from jax._src import compilation_cache as cc
from jax._src import compiler
from jax._src import config
from jax._src import distributed
from jax._src import monitoring
from jax._src import path as pathlib
from jax._src import test_util as jtu
from jax._src import test_warning_util
from jax._src import xla_bridge
from jax._src.compilation_cache_interface import CacheInterface
from jax._src.lib import xla_client as xc
from jax.experimental.pjit import pjit
from jax.sharding import PartitionSpec as P
import numpy as np
config.parse_flags_with_absl()
FAKE_COMPILE_TIME = 10
_counts = Counter() # Map event name to count
def increment_event_count(event):
_counts[event] += 1
monitoring.register_event_listener(increment_event_count)
def msg_exists_in_logs(msg: str, records: list[logging.LogRecord],
level: int | None = None) -> bool:
return any(msg in record.getMessage() for record in records
if level is None or level == record.levelno)
class InMemoryCache(CacheInterface):
"""An in-memory cache for testing purposes."""
# not used, but required by `CacheInterface`
_path = pathlib.Path()
def __init__(self):
self._cache: dict[str, bytes] = {}
def get(self, key: str) -> bytes | None:
return self._cache.get(key)
def put(self, key: str, value: bytes) -> None:
self._cache[key] = value
def clear(self) -> None:
self._cache.clear()
def __len__(self) -> int:
return len(self._cache)
def count_cache_items() -> int:
return 0 if cc._cache is None else len(cc._cache)
def clear_cache() -> None:
if cc._cache is not None:
cc._cache.clear()
class CompilationCacheTestCase(jtu.JaxTestCase):
def setUp(self):
super().setUp()
cc.reset_cache()
cc._cache = InMemoryCache()
def tearDown(self):
cc.reset_cache()
super().tearDown()
@jtu.with_config(
jax_enable_compilation_cache=True,
jax_raise_persistent_cache_errors=True,
jax_persistent_cache_min_compile_time_secs=0,
jax_persistent_cache_min_entry_size_bytes=0,
)
class CompilationCacheTest(CompilationCacheTestCase):
def setUp(self):
super().setUp()
supported_platforms = ["tpu", "gpu", "cpu"]
if not jtu.test_device_matches(supported_platforms):
raise SkipTest(
"serialize executable only works on " + ",".join(supported_platforms)
)
def test_get_no_executable(self):
computation = jax.jit(lambda x, y: x + y).lower(1, 1).compiler_ir()
devices = np.array([[jax.local_devices()[0]]])
compile_options = compiler.get_compile_options(
num_replicas=1, num_partitions=1
)
backend = xla_bridge.get_backend()
key = cc.get_cache_key(computation, devices, compile_options, backend)
executable, compile_time = cc.get_executable_and_time(
key, compile_options, backend)
self.assertIsNone(executable)
self.assertIsNone(compile_time)
def test_diff_executables(self):
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())
compile_options = compiler.get_compile_options(
num_replicas=1, num_partitions=1
)
backend = xla_bridge.get_backend()
executable1 = backend.compile(computation1, compile_options)
executable2 = backend.compile(computation2, compile_options)
cc.put_executable_and_time(
"key1", "computation1", executable1, backend, FAKE_COMPILE_TIME)
cc.put_executable_and_time(
"key2", "computation2", executable2, backend, FAKE_COMPILE_TIME)
self.assertNotEqual(
cc.get_executable_and_time("key1", compile_options, backend)[0],
cc.get_executable_and_time("key2", compile_options, backend)[0]
)
def test_put_executable(self):
computation = (
jax.jit(lambda x, y: x + y)
.lower(np.int32(1), np.int32(1))
.compiler_ir()
)
devices = np.array([[jax.local_devices()[0]]])
compile_options = compiler.get_compile_options(
num_replicas=1, num_partitions=1
)
backend = xla_bridge.get_backend()
executable = backend.compile(str(computation), compile_options)
key = cc.get_cache_key(computation, devices, compile_options, backend)
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)
inputs_to_executable = (
jnp.array(1, dtype=np.int32),
jnp.array(2, dtype=np.int32),
)
expected = executable.execute(inputs_to_executable)
actual = executable_retrieved.execute(inputs_to_executable)
self.assertEqual(expected, actual)
self.assertEqual(FAKE_COMPILE_TIME, compile_time_retrieved)
def test_pmap(self):
f = pmap(lambda x: x - lax.psum(x, "i"), axis_name="i")
x = np.arange(jax.device_count(), dtype=np.int64)
f(x)
self.assertEqual(count_cache_items(), 1)
x = np.arange(jax.device_count(), dtype=np.float32)
f(x)
self.assertEqual(count_cache_items(), 2)
# TODO: create a test for calling pmap with the same input more than once
def test_jit(self):
f = jit(lambda x: x * x)
f(1)
self.assertEqual(count_cache_items(), 1)
f(1.0)
self.assertEqual(count_cache_items(), 2)
def test_xla_autofdo_profile_version(self):
original_profile_version = config.jax_xla_profile_version.value
with config.jax_xla_profile_version(original_profile_version + 1):
f = jit(lambda x: x * x)
f(1)
self.assertEqual(count_cache_items(), 1)
# Clear the cache directory, then update the profile version and execute
# again. The in-memory caches should be invalidated and a new persistent
# cache entry created.
clear_cache()
with config.jax_xla_profile_version(original_profile_version + 2):
f(1)
self.assertEqual(count_cache_items(), 1)
@jtu.with_mesh([("x", 2)])
def test_pjit(self):
@partial(pjit, in_shardings=(P("x"), P("x")), out_shardings=None)
def f(x, y):
return x + y
shape = (8, 8)
x = np.arange(math.prod(shape), dtype=np.int64).reshape(shape)
f(x, x + 1)
self.assertEqual(count_cache_items(), 1)
x = np.arange(math.prod(shape), dtype=np.float32).reshape(shape)
f(x, x + 1)
self.assertEqual(count_cache_items(), 2)
def test_cache_write_warning(self):
f = jit(lambda x: x * x)
backend = xla_bridge.get_backend()
with (
config.raise_persistent_cache_errors(False),
mock.patch.object(cc._get_cache(backend).__class__, "put") as mock_put,
test_warning_util.record_warnings() as w,
):
mock_put.side_effect = RuntimeError("test error")
self.assertEqual(f(2).item(), 4)
if len(w) != 1:
print("Warnings:", [str(w_) for w_ in w], flush=True)
self.assertLen(w, 1)
self.assertIn(
(
"Error writing persistent compilation cache entry "
"for 'jit__lambda_': RuntimeError: test error"
),
str(w[0].message),
)
def test_cache_read_warning(self):
f = jit(lambda x: x * x)
backend = xla_bridge.get_backend()
with (
config.raise_persistent_cache_errors(False),
mock.patch.object(cc._get_cache(backend).__class__, "get") as mock_get,
test_warning_util.record_warnings() as w,
):
mock_get.side_effect = RuntimeError("test error")
# Calling assertEqual with the jitted f will generate two PJIT
# executables: Equal and the lambda function itself.
self.assertEqual(f(2).item(), 4)
if len(w) != 1:
print("Warnings:", [str(w_) for w_ in w], flush=True)
self.assertLen(w, 1)
self.assertIn(
(
"Error reading persistent compilation cache entry "
"for 'jit__lambda_': RuntimeError: test error"
),
str(w[0].message),
)
def test_min_entry_size(self):
with (
config.persistent_cache_min_compile_time_secs(0),
config.persistent_cache_min_entry_size_bytes(1048576), # 1MiB
):
jit(lambda x: x + 1)(1)
self.assertEqual(count_cache_items(), 0)
def test_min_compile_time(self):
with (
config.persistent_cache_min_compile_time_secs(2),
config.persistent_cache_min_entry_size_bytes(0),
):
# Mock time to progress in small intervals so compilation time is small.
with mock.patch("time.monotonic", side_effect=np.arange(0, 10, 0.1)):
jit(lambda x: x + 1)(1)
self.assertEqual(count_cache_items(), 0)
# 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)
self.assertEqual(count_cache_items(), 1)
# This is perhaps related to mocking time.monotonic?
@unittest.skipIf(platform.system() == "Windows", "Test fails on Windows")
def test_cache_saving_metric(self):
with (
config.persistent_cache_min_compile_time_secs(2),
config.persistent_cache_min_entry_size_bytes(0),
):
durations = Counter() # Map metric name to time duration.
def append_metric_duration(metric, duration):
durations[metric] += duration
with jtu.register_event_duration_listener(append_metric_duration):
# Mock time to create a short compilation time, no cache saved, no cache
# hit, no metric recorded.
with mock.patch("time.monotonic", side_effect=np.arange(0, 1, 0.1)):
jit(lambda x: x + 1)(1)
jit(lambda x: x + 1)(1)
self.assertNotIn(
"/jax/compilation_cache/cache_retrieval_time_sec", durations)
self.assertNotIn(
"/jax/compilation_cache/compile_time_saved_sec", durations)
# Mock time to create a long compilation time, metrics incremented with
# a cache hit.
with mock.patch("time.monotonic", side_effect=np.arange(0, 100, 10)):
jit(lambda x: x + 2)(1)
jit(lambda x: x + 2)(1)
self.assertGreater(
durations["/jax/compilation_cache/cache_retrieval_time_sec"], 0)
self.assertGreater(
durations["/jax/compilation_cache/compile_time_saved_sec"], 0)
def test_task_using_cache_metric(self):
count_before_first_use = _counts[
"/jax/compilation_cache/tasks_using_cache"]
jit(lambda x: x + 1)(1)
count_after_first_use = _counts[
"/jax/compilation_cache/tasks_using_cache"]
self.assertEqual(count_after_first_use, count_before_first_use + 1)
# Verify that the count is incremented only once per task.
jit(lambda x: x + 3)(3)
count_after_second_use = _counts[
"/jax/compilation_cache/tasks_using_cache"]
self.assertEqual(count_after_second_use, count_after_first_use)
def test_compile_requests_use_cache_metric(self):
previous_counts = Counter(_counts)
jit(lambda x: x + 1)(1)
jit(lambda x: x + 2)(1)
jit(lambda x: x + 1)(1)
self.assertEqual(
_counts["/jax/compilation_cache/compile_requests_use_cache"]
- previous_counts["/jax/compilation_cache/compile_requests_use_cache"],
3)
@parameterized.parameters(0, 1048576) # 0 byte, 1 MiB
def test_cache_misses_metric(self, min_entry_size):
previous_counts = Counter(_counts)
with (
config.persistent_cache_min_compile_time_secs(2),
config.persistent_cache_min_entry_size_bytes(min_entry_size),
):
# Mock time to create a long compilation time and make cache misses.
with mock.patch("time.monotonic", side_effect=np.arange(0, 100, 10)):
jit(lambda x: x + 1)(1)
jit(lambda x: x + 2)(1)
if min_entry_size <= 0:
self.assertEqual(
_counts["/jax/compilation_cache/cache_misses"]
- previous_counts["/jax/compilation_cache/cache_misses"],
2)
else:
self.assertEqual(
_counts["/jax/compilation_cache/cache_misses"]
- previous_counts["/jax/compilation_cache/cache_misses"],
0)
def test_cache_hits_metric(self):
previous_counts = Counter(_counts)
with (
config.persistent_cache_min_compile_time_secs(2),
config.persistent_cache_min_entry_size_bytes(0),
):
# Mock time to create a long compilation time, cache saved.
with mock.patch("time.monotonic", side_effect=np.arange(0, 100, 10)):
jit(lambda x: x + 1)(1)
jit(lambda x: x + 1)(1)
self.assertEqual(
_counts["/jax/compilation_cache/cache_hits"]
- previous_counts["/jax/compilation_cache/cache_hits"],
1)
def test_persistent_cache_hit_logging(self):
jit(lambda x: x + 1)(1)
msg = "Persistent compilation cache hit"
# cache hits with `log_compiles` on should be in WARNING when enabled
with config.log_compiles(True):
with self.assertLogs(level="WARNING") as log:
jit(lambda x: x + 1)(1)
self.assertTrue(msg_exists_in_logs(msg, log.records, logging.WARNING))
def test_persistent_cache_hit_no_logging(self):
jit(lambda x: x + 1)(1)
msg = "Persistent compilation cache hit"
# cache hits with `log_compiles` off should NOT be in WARNING
with config.log_compiles(False):
with self.assertLogs(level="DEBUG") as log:
jit(lambda x: x + 1)(1)
self.assertFalse(msg_exists_in_logs(msg, log.records, logging.WARNING))
def test_persistent_cache_miss_logging_with_explain(self):
if config.use_shardy_partitioner.value:
self.skipTest("TODO(b/364547005): pure callbacks not supported by Shardy yet")
with (config.explain_cache_misses(True),
config.compilation_cache_dir("jax-cache")):
# omitting writing to cache because compilation is too fast
pure_fn = lambda a: jnp.array(1, dtype=jnp.int32)
with config.persistent_cache_min_compile_time_secs(1e5):
with self.assertLogs(level="DEBUG") as log:
jit(lambda x: x +
jax.pure_callback(pure_fn, jax.ShapeDtypeStruct((), jnp.int32), x)
)(1)
msg1 = "Not writing persistent cache entry"
msg2 = "because it uses host callbacks"
self.assertTrue(msg_exists_in_logs(msg1, log.records, logging.WARNING))
self.assertTrue(msg_exists_in_logs(msg2, log.records, logging.WARNING))
# omitting writing to cache because host callback is present
pure_fn = lambda a: jnp.array(1, dtype=jnp.int32)
with self.assertLogs(level="DEBUG") as log:
jit(lambda x: x +
jax.pure_callback(pure_fn, jax.ShapeDtypeStruct((), jnp.int32), x)
)(1)
msg1 = "Not writing persistent cache entry"
msg2 = "because it uses host callbacks"
self.assertTrue(msg_exists_in_logs(msg1, log.records, logging.WARNING))
self.assertTrue(msg_exists_in_logs(msg2, log.records, logging.WARNING))
# omitting writing to cache because binary is too small
with config.persistent_cache_min_entry_size_bytes(int(1e9)):
with self.assertLogs(level="DEBUG") as log:
jit(lambda x: x + 2)(1)
msg1 = "Not writing persistent cache entry"
msg2 = "is less than threshold"
self.assertTrue(msg_exists_in_logs(msg1, log.records, logging.WARNING))
self.assertTrue(msg_exists_in_logs(msg2, log.records, logging.WARNING))
# successful cache write
with config.persistent_cache_min_entry_size_bytes(1):
with self.assertLogs(level="DEBUG") as log:
jit(lambda x: x ** 2)(1)
msg = "to persistent compilation cache with key"
self.assertTrue(msg_exists_in_logs(msg, log.records, logging.WARNING))
def test_persistent_cache_miss_logging_with_no_explain(self):
# test that cache failure messages do not get logged in WARNING
if config.use_shardy_partitioner.value:
self.skipTest("TODO(b/364547005): pure callbacks not supported by Shardy yet")
with (config.explain_cache_misses(False),
config.compilation_cache_dir("jax-cache")):
# omitting writing to cache because compilation is too fast
with config.persistent_cache_min_compile_time_secs(1e3):
with self.assertLogs(level="DEBUG") as log:
jit(lambda x: x + 1)(1)
msg1, msg2 = "Not writing persistent cache entry", "because it took <"
self.assertFalse(msg_exists_in_logs(msg1, log.records, logging.WARNING))
self.assertFalse(msg_exists_in_logs(msg2, log.records, logging.WARNING))
# omitting writing to cache because host callback is present
pure_fn = lambda a: jnp.array(1, dtype=jnp.int32)
with self.assertLogs(level="DEBUG") as log:
jit(lambda x: x +
jax.pure_callback(pure_fn, jax.ShapeDtypeStruct((), jnp.int32), x)
)(1)
msg1 = "Not writing persistent cache entry"
msg2 = "because it uses host callbacks"
self.assertFalse(msg_exists_in_logs(msg1, log.records, logging.WARNING))
self.assertFalse(msg_exists_in_logs(msg2, log.records, logging.WARNING))
# omitting writing to cache because binary is too small
with config.persistent_cache_min_entry_size_bytes(int(1e9)):
with self.assertLogs(level="DEBUG") as log:
jit(lambda x: x + 2)(1)
msg1 = "Not writing persistent cache entry"
msg2 = "is less than threshold"
self.assertFalse(msg_exists_in_logs(msg1, log.records, logging.WARNING))
self.assertFalse(msg_exists_in_logs(msg2, log.records, logging.WARNING))
# successful cache write
with config.persistent_cache_min_entry_size_bytes(1):
with self.assertLogs(level="DEBUG") as log:
jit(lambda x: x ** 2)(1)
msg = "to persistent compilation cache with key"
self.assertFalse(msg_exists_in_logs(msg, log.records, logging.WARNING))
@parameterized.parameters(0, 1)
def test_cache_write_with_process_restriction(self, process_id):
with (
config.persistent_cache_min_compile_time_secs(0),
config.persistent_cache_min_entry_size_bytes(0),
mock.patch.object(distributed.global_state, "process_id", process_id),
):
jit(lambda x: x + 1)(1)
files_in_directory = count_cache_items()
if process_id == 0:
self.assertEqual(files_in_directory, 1)
elif process_id == 1:
self.assertEqual(files_in_directory, 0)
def test_backend_serialization_deserialization(self):
backend = xla_bridge.get_backend()
executable = (
jax.jit(lambda x, y: x + y)
.lower(np.array(1.), np.array(1.))
.compile()
.runtime_executable()
)
serialized_executable = backend.serialize_executable(executable)
deserialized_executable = backend.deserialize_executable(
serialized_executable, None)
self.assertEqual(
executable.fingerprint, deserialized_executable.fingerprint)
def test_persistent_cache_enable_xla_caches(self):
if jtu.jaxlib_version() <= (0, 4, 35):
self.skipTest("Test requires AutotuneCacheMode bindings")
s = os.sep
with config.compilation_cache_dir("jax-cache"):
with config.persistent_cache_enable_xla_caches("none"):
compile_options = compiler.get_compile_options(
num_replicas=1, num_partitions=1
)
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_kernel_cache_file, "")
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_enable_llvm_module_compilation_parallelism, False)
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_per_fusion_autotune_cache_dir, "")
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_experimental_autotune_cache_mode, xc.AutotuneCacheMode.UPDATE)
with config.persistent_cache_enable_xla_caches("all"):
compile_options = compiler.get_compile_options(
num_replicas=1, num_partitions=1
)
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_kernel_cache_file, f"jax-cache{s}xla_gpu_kernel_cache_file")
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_enable_llvm_module_compilation_parallelism, True)
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_per_fusion_autotune_cache_dir, f"jax-cache{s}xla_gpu_per_fusion_autotune_cache_dir")
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_experimental_autotune_cache_mode, xc.AutotuneCacheMode.UPDATE)
with config.persistent_cache_enable_xla_caches("xla_gpu_kernel_cache_file"):
compile_options = compiler.get_compile_options(
num_replicas=1, num_partitions=1
)
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_kernel_cache_file, f"jax-cache{s}xla_gpu_kernel_cache_file")
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_enable_llvm_module_compilation_parallelism, True)
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_per_fusion_autotune_cache_dir, "")
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_experimental_autotune_cache_mode, xc.AutotuneCacheMode.UPDATE)
with config.persistent_cache_enable_xla_caches("xla_gpu_per_fusion_autotune_cache_dir"):
compile_options = compiler.get_compile_options(
num_replicas=1, num_partitions=1
)
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_kernel_cache_file, "")
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_enable_llvm_module_compilation_parallelism, False)
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_per_fusion_autotune_cache_dir, f"jax-cache{s}xla_gpu_per_fusion_autotune_cache_dir")
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_experimental_autotune_cache_mode, xc.AutotuneCacheMode.UPDATE)
@jtu.with_config(
jax_enable_compilation_cache=False,
jax_persistent_cache_min_compile_time_secs=0,
jax_persistent_cache_min_entry_size_bytes=0,
)
class CompilationCacheDisabledTest(CompilationCacheTestCase):
# If the cache is disabled, there should be no files in the cache directory.
# A call to set_cache_dir() does not affect this.
def test_jit(self):
# Sequence of flag settings for config.jax_enable_compilation_cache:
# 1. Flag is disabled by @jtu.with_config() above.
# 2. Flag is enabled by JaxTestCase for some test configs
# (see test_util.py).
# We need the flag disabled for this test, so disable it below.
with config.enable_compilation_cache(False):
f = jit(lambda x: x * x)
f(1)
self.assertEqual(count_cache_items(), 0)
def test_tasks_disable_cache_metric(self):
with config.enable_compilation_cache(False):
count_before_first_use = _counts[
"/jax/compilation_cache/task_disabled_cache"]
jit(lambda x: x + 1)(1)
count_after_first_use = _counts[
"/jax/compilation_cache/task_disabled_cache"]
self.assertEqual(count_after_first_use - count_before_first_use, 1)
# Verify that the count is incremented only once per task.
jit(lambda x: x + 3)(3)
count_after_second_use = _counts[
"/jax/compilation_cache/task_disabled_cache"]
self.assertEqual(count_after_second_use, count_after_first_use)
def test_persistent_cache_enable_xla_caches_disabled(self):
if jtu.jaxlib_version() <= (0, 4, 35):
self.skipTest("Test requires AutotuneCacheMode bindings")
with config.enable_compilation_cache(False):
compile_options = compiler.get_compile_options(
num_replicas=1, num_partitions=1
)
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_kernel_cache_file, "")
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_enable_llvm_module_compilation_parallelism, False)
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_per_fusion_autotune_cache_dir, "")
self.assertEqual(compile_options.executable_build_options.debug_options.xla_gpu_experimental_autotune_cache_mode, xc.AutotuneCacheMode.UPDATE)
if __name__ == "__main__":
absltest.main(testLoader=jtu.JaxTestLoader())