File size: 14,069 Bytes
00c55c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import asyncio
import concurrent.futures as futures
import dataclasses
import logging
import pathlib
import shutil
import time
from typing import Protocol

from etils import epath
import jax
import jax.experimental.multihost_utils as multihost_utils
import numpy as np
import orbax.checkpoint as ocp
import orbax.checkpoint.future as future

from openpi.shared import array_typing as at
import openpi.shared.normalize as _normalize
import openpi.training.data_loader as _data_loader
import openpi.training.utils as training_utils


_CHECKPOINT_SUCCESS_FILE = "_SUCCESS"


def _success_marker(step_dir: pathlib.Path) -> pathlib.Path:
    return step_dir / _CHECKPOINT_SUCCESS_FILE


def _wait_for_success_marker(step_dir: pathlib.Path) -> None:
    marker = _success_marker(step_dir)
    logging.info(f"[rank {jax.process_index()}] Waiting for checkpoint marker {marker}")
    while not marker.exists():
        time.sleep(5)
    logging.info(f"[rank {jax.process_index()}] Observed checkpoint marker {marker}")


def _is_checkpoint_step_dir(path: pathlib.Path) -> bool:
    if not path.is_dir() or not path.name.isdigit():
        return False
    if _success_marker(path).exists():
        return True
    # Backward compatibility for legacy Orbax checkpoints written before the
    # success marker was introduced.
    return (path / "params").exists() and (path / "train_state").exists()


def checkpoint_steps(checkpoint_dir: epath.Path | pathlib.Path | str) -> list[int]:
    checkpoint_dir = pathlib.Path(str(checkpoint_dir))
    if not checkpoint_dir.exists():
        return []
    return sorted(int(d.name) for d in checkpoint_dir.iterdir() if _is_checkpoint_step_dir(d))


def initialize_checkpoint_dir(
    checkpoint_dir: epath.Path | str, *, keep_period: int | None, overwrite: bool, resume: bool
) -> tuple[ocp.CheckpointManager, bool]:
    checkpoint_dir = epath.Path(checkpoint_dir).resolve()
    resuming = False
    # if checkpoint_dir.exists():
    #     if overwrite:
    #         checkpoint_dir.rmtree()
    #         checkpoint_dir.mkdir(parents=True, exist_ok=True)
    #         logging.info(f"Wiped checkpoint directory {checkpoint_dir}")
    #     elif resume:
    #         resuming = True
    #     else:
    #         raise FileExistsError(
    #             f"Checkpoint directory {checkpoint_dir} already exists. Use --overwrite or --resume "
    #             "to indicate how to handle it."
    #         )
    checkpoint_dir.mkdir(parents=True, exist_ok=True)

    if resume is True:
        resuming = True

    if jax.process_index() == 0:
        if checkpoint_dir.exists():
            if overwrite:
                checkpoint_dir.rmtree()
                checkpoint_dir.mkdir(parents=True, exist_ok=True)
                logging.info(f"Wiped checkpoint directory {checkpoint_dir}")
            elif resume:
                logging.info(f"Resuming training from checkpoint directory {checkpoint_dir}")
                resuming = True
            else:
                raise FileExistsError(
                    f"Checkpoint directory {checkpoint_dir} already exists. Use --overwrite or --resume "
                    "to indicate how to handle it."
                )
                pass

    # Clean up any leftover Orbax temp directories from previous failed saves.
    if jax.process_index() == 0:
        for tmp_dir in checkpoint_dir.glob("*.orbax-checkpoint-tmp-*"):
            if tmp_dir.is_dir():
                logging.warning(f"Removing leftover temp checkpoint dir: {tmp_dir}")
                tmp_dir.rmtree()

    mngr = ocp.CheckpointManager(
        checkpoint_dir,
        item_handlers={
            "train_state": ocp.PyTreeCheckpointHandler(),
            "params": ocp.PyTreeCheckpointHandler(),
        },
        options=ocp.CheckpointManagerOptions(
            max_to_keep=1,
            keep_period=keep_period,
            create=True,
        ),
    )

    # Special case: the checkpoint directory exists and the user requests to resume training, but the training run did
    # not get to the first checkpoint saved. In this case, we don't actually want the train script to try and restore a
    # checkpoint, since it will fail.
    if resuming and tuple(mngr.all_steps()) in [(), (0,)]:
        logging.info("Checkpoint directory exists, but does not contain any checkpoints. Aborting resume.")
        resuming = False

    return mngr, resuming


def save_state(
    checkpoint_manager: ocp.CheckpointManager,
    state: training_utils.TrainState,
    data_loader: _data_loader.DataLoader,
    step: int,
):
    # Split params that can be used for inference into a separate item.
    with at.disable_typechecking():
        train_state, params = _split_params(state)
    items = {
        "train_state": train_state,
        "params": {"params": params},
    }
    checkpoint_manager.save(step, items)
    checkpoint_manager.wait_until_finished()

    # Save assets directly after checkpoint is written (same as save_state_multihost).
    if jax.process_index() == 0:
        data_config = data_loader.data_config()
        norm_stats = data_config.norm_stats
        if norm_stats is not None and data_config.asset_id is not None:
            step_dir = pathlib.Path(str(checkpoint_manager.directory)) / str(step)
            assets_save_path = step_dir / "assets" / data_config.asset_id
            try:
                _normalize.save(assets_save_path, norm_stats)
                logging.info(f"Saved assets to {assets_save_path}")
            except Exception as e:
                logging.error(f"Failed to save assets: {e}")
        else:
            logging.warning(f"Skipping assets save: norm_stats={'None' if norm_stats is None else 'ok'}, asset_id={data_config.asset_id}")


def restore_state(
    checkpoint_manager: ocp.CheckpointManager,
    state: training_utils.TrainState,
    data_loader: _data_loader.DataLoader,
    step: int | None = None,
) -> training_utils.TrainState:
    del data_loader

    with at.disable_typechecking():
        # Split params that can be used for inference into a separate item.
        train_state, params = _split_params(state)
        restored = checkpoint_manager.restore(
            step,
            items={
                "train_state": train_state,
                "params": {"params": params},
            },
        )
    return _merge_params(restored["train_state"], restored["params"])


def load_norm_stats(assets_dir: epath.Path | str, asset_id: str) -> dict[str, _normalize.NormStats] | None:
    norm_stats_dir = epath.Path(assets_dir) / asset_id
    norm_stats = _normalize.load(norm_stats_dir)
    logging.info(f"Loaded norm stats from {norm_stats_dir}")
    return norm_stats


class Callback(Protocol):
    def __call__(self, directory: epath.Path) -> None: ...


class CallbackHandler(ocp.AsyncCheckpointHandler):
    """A CheckpointHandler for calling an arbitrary function asynchronously. Only for saving, not for restoring."""

    def __init__(self):
        self._executor = futures.ThreadPoolExecutor(max_workers=1)

    def close(self):
        self._executor.shutdown()

    def save(self, directory: epath.Path, args: CallbackSave):
        if jax.process_index() == 0:
            args.callback(directory)

    async def async_save(self, directory: epath.Path, args: CallbackSave) -> list[futures.Future]:
        return [self._executor.submit(self.save, directory, args)]

    def restore(self, *args, **kwargs):
        raise NotImplementedError("CallbackHandler does not support restore")


@ocp.args.register_with_handler(CallbackHandler, for_save=True)
@dataclasses.dataclass
class CallbackSave(ocp.args.CheckpointArgs):
    callback: Callback


@ocp.args.register_with_handler(CallbackHandler, for_restore=True)
class CallbackRestore(ocp.args.CheckpointArgs): ...


def _split_params(state: training_utils.TrainState) -> tuple[training_utils.TrainState, at.Params]:
    if state.ema_params is not None:
        params = state.ema_params
        train_state = dataclasses.replace(state, ema_params=None)
    else:
        params = state.params
        train_state = dataclasses.replace(state, params={})
    return train_state, params


def _merge_params(train_state: training_utils.TrainState, params: dict[str, at.Params]) -> training_utils.TrainState:
    # Revert the logic inside `_split_params`. Assumes that existence of `params` means that EMA params were used during the split.
    if train_state.params:
        return dataclasses.replace(train_state, ema_params=params["params"])
    return dataclasses.replace(train_state, params=params["params"])


# ============================================================
# Multi-host checkpoint utilities
# ============================================================


def _cleanup_old_checkpoints(
    checkpoint_dir: epath.Path, max_to_keep: int, keep_period: int | None
):
    """Remove old checkpoint directories, respecting max_to_keep and keep_period."""
    checkpoint_dir = epath.Path(str(checkpoint_dir))
    all_steps = checkpoint_steps(checkpoint_dir)
    steps_to_keep = set(all_steps[-max_to_keep:])
    if keep_period:
        steps_to_keep.update(s for s in all_steps if s % keep_period == 0)
    for s in all_steps:
        if s not in steps_to_keep:
            (checkpoint_dir / str(s)).rmtree()


def save_state_multihost(
    checkpoint_dir: epath.Path,
    state: training_utils.TrainState,
    data_loader: _data_loader.DataLoader,
    step: int,
    *,
    max_to_keep: int = 1,
    keep_period: int | None = None,
):
    """Save a checkpoint using Orbax's native multi-process JAX array path.

    All processes must call this function. We intentionally keep sharded JAX
    arrays sharded and let Orbax coordinate the write; converting to NumPy first
    makes non-primary ranks finish too early and leaves rank 0 alone in a large
    filesystem write.
    """
    with at.disable_typechecking():
        train_state, params = _split_params(state)

    jax.block_until_ready((train_state, params))

    logging.info(f"[rank {jax.process_index()}] Saving multihost checkpoint at step {step}")

    step_dir = pathlib.Path(str(checkpoint_dir)) / str(step)
    if jax.process_index() == 0:
        if step_dir.exists():
            logging.warning(f"[rank 0] Removing existing checkpoint directory before save: {step_dir}")
            shutil.rmtree(step_dir)
        step_dir.mkdir(parents=True, exist_ok=True)

    multihost_utils.sync_global_devices(f"save_state_multihost_prepare_{step}")

    with ocp.PyTreeCheckpointer(use_ocdbt=True) as ckptr:
        ckptr.save(
            epath.Path(str(step_dir / "params")),
            ocp.args.PyTreeSave({"params": params}),
        )

    if jax.process_index() == 0:
        logging.info(f"[rank 0] Saved params checkpoint to {step_dir / 'params'}")

    with ocp.PyTreeCheckpointer(use_ocdbt=True) as ckptr:
        ckptr.save(
            epath.Path(str(step_dir / "train_state")),
            ocp.args.PyTreeSave(train_state),
        )

    if jax.process_index() == 0:
        logging.info(f"[rank 0] Saved train_state checkpoint to {step_dir / 'train_state'}")

        data_config = data_loader.data_config()
        norm_stats = data_config.norm_stats
        if norm_stats is not None and data_config.asset_id is not None:
            assets_save_path = step_dir / "assets" / data_config.asset_id
            try:
                _normalize.save(assets_save_path, norm_stats)
                logging.info(f"[rank 0] Saved assets to {assets_save_path}")
            except Exception as e:
                logging.error(f"[rank 0] Failed to save assets: {e}")

        _cleanup_old_checkpoints(checkpoint_dir, max_to_keep, keep_period)

        _success_marker(step_dir).write_text(f"step={step}\n")
    else:
        _wait_for_success_marker(step_dir)

    logging.info(f"[rank {jax.process_index()}] Checkpoint saved at step {step}")


def restore_state_multihost(
    checkpoint_dir: epath.Path,
    state_sharding,
    step: int | None = None,
) -> training_utils.TrainState:
    """Restore from a multihost checkpoint and re-shard for multi-node training.

    All processes call this. Each reads the full checkpoint then reshards locally.
    Supports checkpoints saved by the native multi-process Orbax path and legacy Orbax checkpoints.
    """
    all_steps = checkpoint_steps(checkpoint_dir)
    if not all_steps:
        raise FileNotFoundError(f"No checkpoints found in {checkpoint_dir}")
    if step is None:
        step = all_steps[-1]

    step_dir = pathlib.Path(str(checkpoint_dir)) / str(step)
    logging.info(f"Restoring multihost checkpoint from step {step}: {step_dir}")

    with at.disable_typechecking():
        train_state_sharding, params_sharding = _split_params(state_sharding)

    with ocp.PyTreeCheckpointer() as ckptr:
        metadata = ckptr.metadata(epath.Path(str(step_dir / "params")))
        params = ckptr.restore(
            epath.Path(str(step_dir / "params")),
            ocp.args.PyTreeRestore(
                item={"params": metadata["params"]},
                restore_args=jax.tree.map(
                    lambda _: ocp.ArrayRestoreArgs(sharding=None, restore_type=np.ndarray),
                    {"params": metadata["params"]},
                ),
            ),
        )
    with ocp.PyTreeCheckpointer() as ckptr:
        metadata = ckptr.metadata(epath.Path(str(step_dir / "train_state")))
        train_state = ckptr.restore(
            epath.Path(str(step_dir / "train_state")),
            ocp.args.PyTreeRestore(
                item=metadata,
                restore_args=jax.tree.map(
                    lambda _: ocp.ArrayRestoreArgs(sharding=None, restore_type=np.ndarray),
                    metadata,
                ),
            ),
        )

    # Re-shard onto the multi-host mesh.
    train_state = jax.device_put(train_state, train_state_sharding)
    params = jax.device_put(params, {"params": params_sharding})

    return _merge_params(train_state, params)