Reconfigures the workload onto the currently available slices. This is called by the elastic manager's maybe_reshard_up/down functions and is responsible for creating a new mesh, reinitializing the state and any objects that depend on the mesh. It returns all of the reinitialized objects.
(
config: pyconfig.HyperParameters,
elastic_manager,
checkpoint_manager,
recorder,
)
| 89 | |
| 90 | @timing.timeit |
| 91 | def elastic_handler( |
| 92 | config: pyconfig.HyperParameters, |
| 93 | elastic_manager, |
| 94 | checkpoint_manager, |
| 95 | recorder, |
| 96 | ): |
| 97 | """Reconfigures the workload onto the currently available slices. |
| 98 | |
| 99 | This is called by the elastic manager's maybe_reshard_up/down |
| 100 | functions and is responsible for creating a new mesh, |
| 101 | reinitializing the state and any objects that depend on the mesh. |
| 102 | |
| 103 | It returns all of the reinitialized objects. |
| 104 | |
| 105 | maybe_reshard_up/down take this function and its arguments and if |
| 106 | there is an elastic event, those functions will call this function |
| 107 | and return its returns. |
| 108 | """ |
| 109 | # We use train_utils.create_training_tools because it contains most of the |
| 110 | # reconfiguration. Depending on the configuration, the checkpoint |
| 111 | # manager depends on the mesh and must be recreated. Therefore, we |
| 112 | # close the previous checkpoint manager and get a new checkpoint |
| 113 | # manager from create_training_tools. |
| 114 | if checkpoint_manager is not None: |
| 115 | checkpoint_manager.close() |
| 116 | |
| 117 | with jax.default_device(elastic_manager.default_device): |
| 118 | ( |
| 119 | init_rng, |
| 120 | checkpoint_manager, |
| 121 | state_mesh_shardings, |
| 122 | model, |
| 123 | mesh, |
| 124 | learning_rate_schedule, |
| 125 | data_iterator, |
| 126 | _, |
| 127 | _, |
| 128 | _, |
| 129 | state, |
| 130 | ) = setup_train_loop(config, recorder, elastic_manager.good_devices) |
| 131 | |
| 132 | p_train_step, _ = train_utils.jit_train_and_eval_step(config, model, mesh, state, state_mesh_shardings, train_step) |
| 133 | |
| 134 | step, snapshot_jax_arrays, _ = elastic_manager.get_resharded_snapshot(mesh) |
| 135 | state = state.replace(**snapshot_jax_arrays) |
| 136 | state = state.replace(step=state.step.at[None].set(step)) |
| 137 | jax.block_until_ready(state) |
| 138 | |
| 139 | # We do not want to restore from the previous checkpoint but instead |
| 140 | # restore from the host offloaded snapshot. |
| 141 | if checkpoint_manager is not None: |
| 142 | latest_step = checkpoint_manager.latest_step() |
| 143 | |
| 144 | # If we checkpointed after the latest snapshot, the checkpoint manager |
| 145 | # will try to take another checkpoint and fail because it already |
| 146 | # exists. Therefore, we delete the checkpoint and let the checkpoint |
| 147 | # manager re-take the checkpoint. |
| 148 | if latest_step is not None and latest_step >= step: |
nothing calls this directly
no test coverage detected