|
| 1 | +""" |
| 2 | +A DAG to run MaxText multi-tier and emergency checkpointing tests. |
| 3 | +
|
| 4 | +This DAG performs a series of tests to save and validate checkpoints |
| 5 | +for the MaxText model. It runs tests in two modes: one with the replicator |
| 6 | +service enabled (Multi-tier Checkpointing) and one without |
| 7 | +(Emergency Checkpointing). The tests are executed on a TPU multi-pod cluster. |
| 8 | +
|
| 9 | +Variables: |
| 10 | + SCHEDULE (str): The cron schedule. |
| 11 | + DAG_TEST_NAME (str): Unique DAG name. |
| 12 | + BASE_OUTPUT_DIRECTORY (str): GCS path for saved checkpoints. |
| 13 | + DOCKER_IMAGES (list): Tuples of setup mode and Docker image. |
| 14 | + RAM_DISK (str): Local directory mounted on each machine. |
| 15 | + TEST_CONFIGS (dict): Accelerator type and slice count. |
| 16 | + CLUSTERS (dict): Maps accelerator to TPU cluster configs. |
| 17 | + STEP (int): Total training steps. |
| 18 | + LOCAL_CHECKPOINT_PERIOD (int): Frequency (in steps) for local checkpoints. |
| 19 | + REPLICATOR_BACKUP_INTERVAL_MINUTES (int): Interval (in minutes) for replicator. |
| 20 | + NAME_PREFIX (str): Prefix for test run names. |
| 21 | + MODEL_NAME (str): Name of the model under test. |
| 22 | +""" |
| 23 | + |
| 24 | +import datetime |
| 25 | +from dataclasses import asdict |
| 26 | + |
| 27 | +from airflow import models |
| 28 | +from airflow.utils.task_group import TaskGroup |
| 29 | + |
| 30 | +from dags import composer_env, gcs_bucket |
| 31 | +from dags.common import test_owner |
| 32 | +from dags.common.vm_resource import DockerImage, XpkClusters |
| 33 | +from dags.multipod.configs import gke_config |
| 34 | +from dags.multipod.configs.common import SetupMode |
| 35 | +from dags.orbax.util import logging_mtc as log |
| 36 | +from dags.orbax.util import multi_tier_checkpoint_util as mtc |
| 37 | +from xlml.utils.xpk import BRANCH_ABHINAV_MTC |
| 38 | +from absl import logging |
| 39 | + |
| 40 | +SCHEDULE = "0 10 * * *" if composer_env.is_prod_env() else None |
| 41 | +DAG_TEST_NAME = "maxtext_emc_and_mtc_orbax_save_local" |
| 42 | +BASE_OUTPUT_DIRECTORY = ( |
| 43 | + f"{gcs_bucket.MTC_AUTOMATION_BUCKET}/" |
| 44 | +) |
| 45 | +DOCKER_IMAGES = [( |
| 46 | + SetupMode.NIGHTLY, |
| 47 | + DockerImage.MAXTEXT_TPU_JAX_NIGHTLY, |
| 48 | +)] |
| 49 | +RAM_DISK = "/local" |
| 50 | +TEST_CONFIGS = {"v5p-128": [2]} |
| 51 | +CLUSTERS = {"v5p-128": XpkClusters.TPU_V5P_128_CLUSTER_ORBAX} |
| 52 | +STEP = 100 |
| 53 | +LOCAL_CHECKPOINT_PERIOD = 20 |
| 54 | +REPLICATOR_BACKUP_INTERVAL_MINUTES = 30 |
| 55 | +NAME_PREFIX = "max-sv" |
| 56 | +MODEL_NAME = "llama2-7b" |
| 57 | + |
| 58 | +with models.DAG( |
| 59 | + dag_id=DAG_TEST_NAME, |
| 60 | + schedule_interval=SCHEDULE, |
| 61 | + tags=[ |
| 62 | + "multipod_team", |
| 63 | + "maxtext", |
| 64 | + "emergency_checkpoint_manager", |
| 65 | + "multitier_checkpointing", |
| 66 | + "nightly", |
| 67 | + "orbax", |
| 68 | + ], |
| 69 | + start_date=datetime.datetime(2025, 6, 12), |
| 70 | + catchup=False, |
| 71 | + concurrency=2, |
| 72 | +) as dag: |
| 73 | + tests_to_run_seq = [] |
| 74 | + for use_replicator_flag in [True, False]: |
| 75 | + |
| 76 | + # These are the two test cases we are testing |
| 77 | + # use_replicator = True --> Multi-tier Checkpointing |
| 78 | + # use_replicator = False --> Emergency Checkpointing |
| 79 | + prefix_replicator = "mtc" if use_replicator_flag else "emc" |
| 80 | + folder = f"maxtext_{prefix_replicator}_orbax_save_local" |
| 81 | + BASE_OUTPUT_DIRECTORY = f"{gcs_bucket.MTC_AUTOMATION_BUCKET}/{folder}" |
| 82 | + with TaskGroup( |
| 83 | + group_id=f"maxtext_{prefix_replicator}_orbax_save_local" |
| 84 | + ) as task: |
| 85 | + for mode, image in DOCKER_IMAGES: |
| 86 | + for accelerator, slices in TEST_CONFIGS.items(): |
| 87 | + for slice_num in slices: |
| 88 | + |
| 89 | + # Multi-tier Checkpointing Configuration |
| 90 | + # TODO(Camilo): Make this configurable based on the accelerator type. |
| 91 | + # Currently, it is hardcoded for v5p. |
| 92 | + cpc_conf = ( |
| 93 | + CLUSTERS[accelerator].project, |
| 94 | + CLUSTERS[accelerator].zone[:-2], |
| 95 | + CLUSTERS[accelerator].name, |
| 96 | + gcs_bucket.MTC_AUTOMATION_BUCKET.split("gs://")[1], |
| 97 | + "ct5p-hightpu-4t", |
| 98 | + "google.com/tpu", |
| 99 | + "800000Mi", |
| 100 | + ) |
| 101 | + |
| 102 | + # We conditionally set the trigger_rule on the first task. |
| 103 | + # If first task group failed the next one can execute. |
| 104 | + init_delete_cpc = mtc.initiate_cpc_deletion(*cpc_conf) |
| 105 | + wait_delete_cpc = mtc.wait_for_cpc_deletion.override(trigger_rule="all_done")(*cpc_conf) |
| 106 | + apply_cpc = mtc.apply_cpc(*cpc_conf) |
| 107 | + run_time = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M") |
| 108 | + run_name = f"{NAME_PREFIX}-{prefix_replicator}-{slice_num}x-{accelerator}-{run_time}" |
| 109 | + workload_command = ( |
| 110 | + "export TPU_PREMAPPED_BUFFER_SIZE=52428800000 && " |
| 111 | + "export TPU_PREMAPPED_BUFFER_TRANSFER_THRESHOLD_BYTES=52428800000 && " |
| 112 | + "python3 -m MaxText.train MaxText/configs/base.yml remat_policy=full " |
| 113 | + f"global_parameter_scale=1 base_output_directory={BASE_OUTPUT_DIRECTORY} " |
| 114 | + f"dataset_type=synthetic steps={STEP} per_device_batch_size=1 " |
| 115 | + f"max_target_length=256 model_name={MODEL_NAME} per_device_batch_size=2 " |
| 116 | + "reuse_example_batch=1 enable_emergency_checkpoint=true " |
| 117 | + f"local_checkpoint_directory={RAM_DISK} local_checkpoint_period={LOCAL_CHECKPOINT_PERIOD} " |
| 118 | + f"use_replicator_service={use_replicator_flag} replicator_backup_interval_minutes={REPLICATOR_BACKUP_INTERVAL_MINUTES} " |
| 119 | + f"run_name={run_name}", |
| 120 | + ) |
| 121 | + |
| 122 | + start_time = log.generate_timestamp() |
| 123 | + |
| 124 | + maxtext_chkpt_run_test = gke_config.get_gke_config( |
| 125 | + num_slices=slice_num, |
| 126 | + cluster=CLUSTERS[accelerator], |
| 127 | + time_out_in_min=60, |
| 128 | + test_name=f"{NAME_PREFIX}-{prefix_replicator}", |
| 129 | + run_model_cmds=workload_command, |
| 130 | + docker_image=image.value, |
| 131 | + test_owner=test_owner.CAMILO, |
| 132 | + ).run( |
| 133 | + ramdisk_directory=RAM_DISK, |
| 134 | + mtc_enabled=True, |
| 135 | + xpk_branch=BRANCH_ABHINAV_MTC, |
| 136 | + skip_post_process=True, |
| 137 | + ) |
| 138 | + |
| 139 | + cleanup_command = (f"rm -rf {RAM_DISK}/*",) |
| 140 | + ram_disk_cleanup = gke_config.get_gke_config( |
| 141 | + num_slices=slice_num, |
| 142 | + cluster=CLUSTERS[accelerator], |
| 143 | + time_out_in_min=60, |
| 144 | + test_name=f"{NAME_PREFIX}-cl", |
| 145 | + run_model_cmds=cleanup_command, |
| 146 | + docker_image=image.value, |
| 147 | + test_owner=test_owner.CAMILO, |
| 148 | + ).run( |
| 149 | + ramdisk_directory=RAM_DISK, |
| 150 | + mtc_enabled=True, |
| 151 | + xpk_branch=BRANCH_ABHINAV_MTC, |
| 152 | + skip_post_process=True, |
| 153 | + ) |
| 154 | + |
| 155 | + end_time = log.generate_timestamp() |
| 156 | + vali_step = STEP- 1 |
| 157 | + vali_step_list = [ |
| 158 | + i for i in range(0, vali_step, LOCAL_CHECKPOINT_PERIOD) |
| 159 | + ] |
| 160 | + vali_step_list.append(vali_step) |
| 161 | + |
| 162 | + # Here we are looking for the string '(blocking + background)'. |
| 163 | + # We will compare expected steps with the ones we found when query this regex. Should be the same |
| 164 | + # If for some reason the restore start from 0 this task will fail because len(valid_step_list) != len(founded_steps) |
| 165 | + validate_log = log.validate_log_with_step( |
| 166 | + project_id=CLUSTERS[accelerator].project, |
| 167 | + location=CLUSTERS[accelerator].zone[:-2], |
| 168 | + cluster_name=CLUSTERS[accelerator].name, |
| 169 | + text_filter="(blocking + background).", |
| 170 | + start_time=start_time, |
| 171 | + end_time=end_time, |
| 172 | + vali_step_list=vali_step_list, |
| 173 | + ) |
| 174 | + |
| 175 | + ( |
| 176 | + init_delete_cpc |
| 177 | + >> wait_delete_cpc |
| 178 | + >> apply_cpc |
| 179 | + >> start_time |
| 180 | + >> maxtext_chkpt_run_test |
| 181 | + >> ram_disk_cleanup |
| 182 | + >> end_time |
| 183 | + >> validate_log |
| 184 | + ) |
| 185 | + # Add to a global list of test to be run in a sequential way |
| 186 | + tests_to_run_seq.append(task) |
| 187 | + |
| 188 | + # Chain the task groups sequentially |
| 189 | + for idx_test in range(len(tests_to_run_seq) - 1): |
| 190 | + tests_to_run_seq[idx_test] >> tests_to_run_seq[idx_test + 1] |
0 commit comments