|
| 1 | +# Copyright 2026 The Orbax Authors. |
| 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 | +# http://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 | +"""ProcessMetadataCheckpointHandler class. |
| 16 | +
|
| 17 | +Implementation of CheckpointHandler interface. |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import dataclasses |
| 23 | +from typing import Callable, List, Tuple |
| 24 | + |
| 25 | +from absl import logging |
| 26 | +from etils import epath |
| 27 | +import jax |
| 28 | +from orbax.checkpoint import checkpoint_args |
| 29 | +from orbax.checkpoint import options as options_lib |
| 30 | +from orbax.checkpoint._src import asyncio_utils |
| 31 | +from orbax.checkpoint._src.futures import future |
| 32 | +from orbax.checkpoint._src.handlers import async_checkpoint_handler |
| 33 | +from orbax.checkpoint._src.multihost import multihost |
| 34 | +from orbax.checkpoint.experimental.emergency import mesh_consistency |
| 35 | + |
| 36 | + |
| 37 | +CheckpointArgs = checkpoint_args.CheckpointArgs |
| 38 | +PreviousDistributedToDeviceIds = List[List[int]] |
| 39 | +PreviousDeviceIds = List[int] |
| 40 | +register_with_handler = checkpoint_args.register_with_handler |
| 41 | + |
| 42 | + |
| 43 | +class ProcessMetadataCheckpointHandler( |
| 44 | + async_checkpoint_handler.AsyncCheckpointHandler |
| 45 | +): |
| 46 | + """Saves process metadata.""" |
| 47 | + |
| 48 | + def __init__( |
| 49 | + self, |
| 50 | + multiprocessing_options: options_lib.MultiprocessingOptions = ( |
| 51 | + options_lib.MultiprocessingOptions() |
| 52 | + ), |
| 53 | + distributed_to_device_ids_fn: Callable[[], List[List[int]]] | None = None, |
| 54 | + ) -> None: |
| 55 | + """Initializes ProcessMetadataCheckpointHandler.""" |
| 56 | + self._distributed_to_device_ids_fn = ( |
| 57 | + distributed_to_device_ids_fn or multihost.distributed_to_device_ids |
| 58 | + ) |
| 59 | + |
| 60 | + async def async_save( |
| 61 | + self, |
| 62 | + directory: epath.Path, |
| 63 | + args: ProcessMetadataSaveArgs, |
| 64 | + ) -> List[future.Future] | None: |
| 65 | + """Saves the given process metadata. |
| 66 | +
|
| 67 | + Args: |
| 68 | + directory: save location directory. |
| 69 | + args: ProcessMetadataSaveArgs (see below). |
| 70 | +
|
| 71 | + Returns: |
| 72 | + A list of commit futures. |
| 73 | + """ |
| 74 | + logging.info('Saving process metadata to %s', directory) |
| 75 | + return [ |
| 76 | + future.CommitFutureAwaitingContractedSignals( |
| 77 | + mesh_consistency.save_process_metadata( |
| 78 | + directory, |
| 79 | + args.global_mesh, |
| 80 | + self._distributed_to_device_ids_fn(), |
| 81 | + ), |
| 82 | + name='process_metadata_ch_save', |
| 83 | + ) |
| 84 | + ] |
| 85 | + |
| 86 | + def save( |
| 87 | + self, |
| 88 | + directory: epath.Path, |
| 89 | + args: ProcessMetadataSaveArgs, |
| 90 | + ) -> None: |
| 91 | + async def async_save( |
| 92 | + directory: epath.Path, |
| 93 | + args: ProcessMetadataSaveArgs, |
| 94 | + ) -> None: |
| 95 | + commit_futures = await self.async_save(directory, args) |
| 96 | + if commit_futures: |
| 97 | + for f in commit_futures: |
| 98 | + f.result() |
| 99 | + |
| 100 | + asyncio_utils.run_sync(async_save(directory, args)) |
| 101 | + |
| 102 | + def restore( |
| 103 | + self, |
| 104 | + directory: epath.Path, |
| 105 | + args: ProcessMetadataRestoreArgs, |
| 106 | + ) -> Tuple[PreviousDistributedToDeviceIds, PreviousDeviceIds]: |
| 107 | + """Restores metadata from directory. |
| 108 | +
|
| 109 | + Args: |
| 110 | + directory: restore location directory. |
| 111 | + args: ProcessMetadataRestoreArgs (see below). |
| 112 | +
|
| 113 | + Returns: |
| 114 | + A tuple of previous distributed to device ids and previous device ids. |
| 115 | + """ |
| 116 | + logging.info('Restoring process metadata from %s', directory) |
| 117 | + return mesh_consistency.read_process_metadata(directory) |
| 118 | + |
| 119 | + |
| 120 | +@register_with_handler(ProcessMetadataCheckpointHandler, for_save=True) |
| 121 | +@dataclasses.dataclass |
| 122 | +class ProcessMetadataSaveArgs(CheckpointArgs): |
| 123 | + """Parameters for saving process metadata. |
| 124 | +
|
| 125 | + Attributes: |
| 126 | + global_mesh: the global mesh. |
| 127 | + """ |
| 128 | + |
| 129 | + global_mesh: jax.sharding.Mesh |
| 130 | + |
| 131 | + |
| 132 | +@register_with_handler(ProcessMetadataCheckpointHandler, for_restore=True) |
| 133 | +@dataclasses.dataclass |
| 134 | +class ProcessMetadataRestoreArgs(CheckpointArgs): |
| 135 | + """Parameters for restoring process metadata.""" |
0 commit comments