|
25 | 25 |
|
26 | 26 | from absl import app |
27 | 27 | from flax import nnx |
28 | | -from flax.linen import partitioning as nn_partitioning |
29 | 28 | import jax |
30 | 29 | from jax import numpy as jnp |
31 | 30 | from maxtext.configs import pyconfig |
@@ -69,44 +68,34 @@ def init_state_fn(): |
69 | 68 | mesh = model.mesh |
70 | 69 | _, tx = train_utils.create_training_optimizer(config, model) |
71 | 70 | init_state_fn = partial(maxtext_utils.init_initial_state, model, tx, config, True, init_rng) |
| 71 | + |
72 | 72 | checkpoint_manager = train_utils.create_checkpoint_manager(config, mesh, init_state_fn) |
73 | 73 |
|
74 | | - unboxed_abstract_state, _, _ = maxtext_utils.get_abstract_state(config, mesh, init_state_fn, is_training=True) |
75 | 74 | # A barrier to sync all hosts before starting to restore checkpoint |
76 | 75 | jax.experimental.multihost_utils.sync_global_devices("Barrier before load") |
77 | | - checkpoint_load_start = datetime.datetime.now() |
78 | | - with nn_partitioning.axis_rules(config.logical_axis_rules): |
79 | | - state, _ = checkpointing.load_state_if_possible( |
80 | | - checkpoint_manager, |
81 | | - None, |
82 | | - config.load_parameters_path, |
83 | | - config.load_full_state_path, |
84 | | - config.checkpoint_storage_concurrent_gb, |
85 | | - unboxed_abstract_state, |
86 | | - use_ocdbt=config.checkpoint_storage_use_ocdbt, |
87 | | - use_zarr3=config.checkpoint_storage_use_zarr3, |
88 | | - ) |
89 | | - if state: |
90 | | - state = state["items"] |
91 | 76 |
|
| 77 | + checkpoint_load_start = datetime.datetime.now() |
| 78 | + # Delegate checkpoint restoration or state initialization to setup_training_state |
| 79 | + state, _, _, _, was_restored = maxtext_utils.setup_training_state(None, config, mesh, checkpoint_manager, init_state_fn) |
92 | 80 | jax.block_until_ready(state) |
93 | 81 | checkpoint_load_end = datetime.datetime.now() |
94 | | - if state is not None: # Checkpoint was available for restore |
| 82 | + |
| 83 | + if was_restored: |
95 | 84 | if jax.process_index() == 0: |
96 | 85 | max_logging.log( |
97 | 86 | "STANDALONE CHECKPOINTER : Checkpoint restored in :" f" {checkpoint_load_end - checkpoint_load_start}" |
98 | 87 | ) |
99 | | - else: # Checkpoint was unavailable, state needs to be initialized |
100 | | - state, _, _, _ = maxtext_utils.setup_training_state(None, config, mesh, checkpoint_manager, init_state_fn) |
101 | | - state = add_entropy_to_checkpoint(state) |
| 88 | + else: # Checkpoint was unavailable, fresh state needs to be perturbed with entropy |
| 89 | + state = add_entropy_to_checkpoint(state) |
102 | 90 |
|
103 | 91 | start_step = get_first_step(model, state) # this is the start_step for training |
104 | 92 | for step in np.arange(start_step, config.steps): |
105 | 93 | if checkpoint_manager is not None: |
106 | 94 | start_time = datetime.datetime.now() |
107 | 95 | # A barrier to sync all hosts before starting to save checkpoint |
108 | 96 | jax.experimental.multihost_utils.sync_global_devices("Barrier before save") |
109 | | - if checkpointing.save_checkpoint(checkpoint_manager, int(step), state): |
| 97 | + state_to_save = train_state_nnx.to_linen_checkpoint_dict(state.to_pure_dict()) if config.pure_nnx else state |
| 98 | + if checkpointing.save_checkpoint(checkpoint_manager, int(step), state_to_save): |
110 | 99 | checkpoint_manager.wait_until_finished() |
111 | 100 | end_time = datetime.datetime.now() |
112 | 101 | if jax.process_index() == 0: |
|
0 commit comments