MCPcopy Create free account
hub / github.com/apple/axlearn / restore

Method restore

axlearn/common/checkpointer_orbax.py:531–608  ·  view source on GitHub ↗

See `BaseCheckpointer.restore` for details.

(
        self,
        *,
        step: Optional[int] = None,
        state: Union[Nested[Tensor], Nested[TensorSpec]],
    )

Source from the content-addressed store, hash-verified

529 # https://orbax.readthedocs.io/en/latest/preemption_checkpointing.html
530 if self._manager.reached_preemption(step):
531 self._manager.wait_until_finished()
532 raise SystemExit(f"Exiting after saving checkpoint at {step=} due to pre-emption.")
533 finally:
534 self._eval_summaries = None
535
536 def restore(
537 self,
538 *,
539 step: Optional[int] = None,
540 state: Union[Nested[Tensor], Nested[TensorSpec]],
541 ) -> Tuple[Optional[int], Nested[Tensor]]:
542 """See `BaseCheckpointer.restore` for details."""
543
544 cfg: OrbaxCheckpointer.Config = self.config
545
546 def _restore_args(x: Any) -> ocp.RestoreArgs:
547 if isinstance(x, (Tensor, TensorSpec)):
548 arg = ocp.checkpoint_utils.construct_restore_args(
549 jax.ShapeDtypeStruct(shape=x.shape, dtype=x.dtype, sharding=x.sharding)
550 )
551 if cfg.enable_single_replica_ckpt_restoring and isinstance(
552 arg, ocp.type_handlers.ArrayRestoreArgs
553 ):
554 mesh = x.sharding.mesh
555 arg = ocp.type_handlers.SingleReplicaArrayRestoreArgs(
556 restore_type=arg.restore_type,
557 dtype=arg.dtype,
558 mesh=arg.mesh,
559 mesh_axes=arg.mesh_axes,
560 sharding=arg.sharding,
561 global_shape=arg.global_shape,
562 shape=arg.shape,
563 strict=arg.strict,
564 single_replica_sharding=jax.sharding.NamedSharding(
565 jax.sharding.Mesh(
566 _replica_devices(mesh.devices, cfg.replica_axis_index),
567 mesh.axis_names,
568 ),
569 x.sharding.spec,
570 ),
571 )
572 return arg
573 elif isinstance(x, tf.data.Iterator):
574 return _TfIteratorHandler.RestoreArgs(item=x)
575 elif _GRAIN_INSTALLED and isinstance(x, _GrainIterator):
576 return _PythonSavableHandler.RestoreArgs(item=x)
577 elif isinstance(x, PythonSavable):
578 return _PythonSavableHandler.RestoreArgs(item=x)
579 else:
580 return None
581
582 restore_args = jax.tree.map(_restore_args, state)
583
584 try:
585 composite_state = self._manager.restore(
586 step,
587 args=ocp.args.Composite(
588 index=ocp.args.JsonRestore(None),

Callers

nothing calls this directly

Calls 3

_get_specMethod · 0.95
check_state_structureFunction · 0.90
mapMethod · 0.80

Tested by

no test coverage detected