|
| 1 | +# Copyright 2023–2025 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 | +"""Utility functions for Elastic Training.""" |
| 16 | + |
| 17 | +import functools |
| 18 | +import re |
| 19 | +import subprocess |
| 20 | + |
| 21 | +import maxtext.utils.max_logging as max_logging |
| 22 | +import pathwaysutils |
| 23 | +from pathwaysutils.elastic import manager |
| 24 | + |
| 25 | + |
| 26 | +elastic_manager: manager.Manager | None = None |
| 27 | + |
| 28 | + |
| 29 | +def elastic_mode_enabled(config) -> bool: |
| 30 | + """Returns whether elastic mode is enabled.""" |
| 31 | + return (pathwaysutils.is_pathways_backend_used() and |
| 32 | + config.elastic_pause_resume) |
| 33 | + |
| 34 | + |
| 35 | +def clean_up_checkpoints(checkpoint_dir: str): |
| 36 | + """Cleans up incomplete checkpoints after an elastic event.""" |
| 37 | + max_logging.log(f"Elastic utils: Checking for incomplete checkpoint after an elastic event...") |
| 38 | + checkpoint_dir = f"{checkpoint_dir}" |
| 39 | + |
| 40 | + # 1. List the directory |
| 41 | + result = subprocess.run(['gsutil', 'ls', checkpoint_dir], capture_output=True, text=True) |
| 42 | + |
| 43 | + if result.returncode != 0: |
| 44 | + max_logging.log("Failed to inspect checkpoint dir. Continuing") |
| 45 | + return |
| 46 | + |
| 47 | + # 2. Filter for directories ending in numbers/ (equivalent to your grep and sort) |
| 48 | + checkpoints = [line for line in result.stdout.splitlines() if re.search(r'/\d+/$', line)] |
| 49 | + |
| 50 | + if not checkpoints: |
| 51 | + max_logging.log("Found no existing checkpoints. Continuing") |
| 52 | + return |
| 53 | + |
| 54 | + # Sort naturally (Version sort) and get the last one |
| 55 | + checkpoints.sort(key=lambda x: [int(c) if c.isdigit() else c for c in re.split(r'(\d+)', x)]) |
| 56 | + latest_checkpoint = checkpoints[-1] |
| 57 | + |
| 58 | + max_logging.log(f"Checking latest checkpoint: {latest_checkpoint}") |
| 59 | + |
| 60 | + # 3. Check for commit_success file |
| 61 | + # gsutil -q stat returns 0 if found, non-zero if not |
| 62 | + stat_check = subprocess.run(['gsutil', '-q', 'stat', f"{latest_checkpoint}commit_success*"]) |
| 63 | + |
| 64 | + if stat_check.returncode != 0: |
| 65 | + max_logging.log(f"No commit_success file found. Deleting {latest_checkpoint}...") |
| 66 | + subprocess.run(['gsutil', '-m', 'rm', '-rf', latest_checkpoint]) |
| 67 | + else: |
| 68 | + max_logging.log(f"Found commit_success file. Keeping {latest_checkpoint}.") |
| 69 | + |
| 70 | + |
| 71 | +def elastic_pause_resume(config, callback_fn=None): |
| 72 | + """Pauses and resumes elastic training.""" |
| 73 | + cleanup_partial = functools.partial( |
| 74 | + clean_up_checkpoints, config.checkpoint_dir |
| 75 | + ) |
| 76 | + callback_fn = cleanup_partial if callback_fn is None else callback_fn |
| 77 | + return elastic_manager.elastic_retry( |
| 78 | + max_retries=10, |
| 79 | + poll_interval=10, |
| 80 | + timeout=config.elastic_timeout, |
| 81 | + on_elastic_event_callback=callback_fn, |
| 82 | + ) |
| 83 | + |
| 84 | + |
0 commit comments