|
| 1 | +# Copyright 2026 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 | + |
| 15 | +"""Tests for JAX compilation cache hits in train.py. |
| 16 | +
|
| 17 | +This test ensures that the `train_step` function is compiled only once. |
| 18 | +It verifies that the Ahead-Of-Time (AOT) compilation signature (which uses |
| 19 | +a dummy `shaped_batch` constructed in `train.py`) matches the runtime |
| 20 | +compilation signature (which uses the actual `example_batch` from the data pipeline). |
| 21 | +
|
| 22 | +If this test fails, it likely means a regression was introduced where the AOT |
| 23 | +batch sharding/shape does not match the runtime batch sharding/shape. This causes |
| 24 | +JAX to recompile `train_step` at step 0, leading to a "double compilation" |
| 25 | +and a very slow first step. |
| 26 | +
|
| 27 | +To debug: |
| 28 | +1. Verify that `maxtext_utils.get_shaped_batch` in `train.py` is called with the |
| 29 | + correct `sharding` argument (matching the data pipeline sharding). |
| 30 | +2. Check if there are differences in shapes or dtypes between the AOT dummy batch |
| 31 | + and the runtime batch. |
| 32 | +""" |
| 33 | + |
| 34 | +import os |
| 35 | +import tempfile |
| 36 | +import shutil |
| 37 | +import pytest |
| 38 | +import subprocess |
| 39 | +import sys |
| 40 | + |
| 41 | +from tests.utils.test_helpers import ( |
| 42 | + get_test_config_path, |
| 43 | + get_test_base_output_directory, |
| 44 | +) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.cpu_only |
| 48 | +def test_train_step_cache_hit(): |
| 49 | + temp_dir = tempfile.mkdtemp() |
| 50 | + _base_output_directory = get_test_base_output_directory() |
| 51 | + |
| 52 | + try: |
| 53 | + small_model_overrides = [ |
| 54 | + "base_emb_dim=16", |
| 55 | + "base_num_query_heads=4", |
| 56 | + "base_num_kv_heads=4", |
| 57 | + "base_mlp_dim=16", |
| 58 | + "base_num_decoder_layers=1", |
| 59 | + "head_dim=64", |
| 60 | + "max_target_length=64", |
| 61 | + "vocab_size=32", |
| 62 | + "sharding_tolerance=0.1", |
| 63 | + ] |
| 64 | + |
| 65 | + cmd = [ |
| 66 | + sys.executable, |
| 67 | + "-m", |
| 68 | + "maxtext.trainers.pre_train.train", |
| 69 | + get_test_config_path(), |
| 70 | + f"base_output_directory={_base_output_directory}", |
| 71 | + "run_name=compile_cache_test_cpu", |
| 72 | + "steps=2", |
| 73 | + "enable_checkpointing=False", |
| 74 | + "enable_goodput_recording=False", |
| 75 | + "dataset_type=synthetic", |
| 76 | + "hardware=cpu", |
| 77 | + "skip_jax_distributed_system=True", |
| 78 | + f"jax_cache_dir={temp_dir}", |
| 79 | + ] + small_model_overrides |
| 80 | + |
| 81 | + env = os.environ.copy() |
| 82 | + env["JAX_PLATFORMS"] = "cpu" |
| 83 | + env["JAX_ENABLE_COMPILATION_CACHE"] = "true" |
| 84 | + env["JAX_COMPILATION_CACHE_DIR"] = temp_dir |
| 85 | + env["JAX_LOG_COMPILES"] = "1" |
| 86 | + |
| 87 | + print("Running CPU training subprocess:", " ".join(cmd)) |
| 88 | + result = subprocess.run(cmd, env=env, capture_output=True, text=True, check=True) |
| 89 | + |
| 90 | + captured_logs = result.stderr |
| 91 | + |
| 92 | + # Print captured logs for debugging (will be shown by pytest if assert fails) |
| 93 | + print("=== Captured Subprocess Stderr ===") |
| 94 | + print(captured_logs) |
| 95 | + print("===================================") |
| 96 | + |
| 97 | + # Check if cache dir has files |
| 98 | + cache_files = os.listdir(temp_dir) |
| 99 | + print("=== Cache Directory Content ===") |
| 100 | + print(f"Path: {temp_dir}") |
| 101 | + print(f"Files: {cache_files}") |
| 102 | + print("===============================") |
| 103 | + |
| 104 | + assert len(cache_files) > 0, ( |
| 105 | + "JAX compilation cache directory is empty. This suggests the compilation " |
| 106 | + "cache was not writeable or the JAX cache configuration was ignored." |
| 107 | + ) |
| 108 | + |
| 109 | + assert len(cache_files) == 1, ( |
| 110 | + f"Expected exactly 1 JAX compilation cache file, but found {len(cache_files)}: {cache_files}. " |
| 111 | + "This indicates a cache miss where AOT compilation and runtime execution generated different keys, " |
| 112 | + "causing train_step to be compiled twice (double-compilation regression)." |
| 113 | + ) |
| 114 | + |
| 115 | + assert "Persistent compilation cache hit for 'jit_train_step'" in captured_logs, ( |
| 116 | + "Did not find 'Persistent compilation cache hit for 'jit_train_step'' in logs. " |
| 117 | + "This means the runtime execution of train_step did not hit the cache populated by the AOT compilation. " |
| 118 | + "Check if the AOT input batch signature (shape/dtype/sharding) matches the runtime input batch." |
| 119 | + ) |
| 120 | + |
| 121 | + finally: |
| 122 | + if os.path.exists(temp_dir): |
| 123 | + shutil.rmtree(temp_dir) |
0 commit comments