Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def init_state_fn():
state_map = {
"['optimizer']['step'].value": ("step", None),
"['optimizer']['opt_state'][0]['count'].value": ("opt_states_0.no_prefix_0.count", None),
"['optimizer']['opt_state']['count'].value": ("opt_states_0.no_prefix_0.count", None),
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of handle the NNX state mapping, can we convert NNX state to Linen layout here, so all the linen mapping can be used directly?

if cfg.pure_nnx:
      state = train_state_nnx.to_checkpoint_dict(state)

else:
state_map = {
Expand All @@ -237,10 +238,18 @@ def get_layer_prefix(keystr_pax):
f"opt_states_0.{prefix_pax_opt_state}.m{keystr_pax}",
transform_fn,
)
state_map[f"['optimizer']['opt_state']['mu']{keystr_maxtext}.value"] = (
f"opt_states_0.{prefix_pax_opt_state}.m{keystr_pax}",
transform_fn,
)
state_map[f"['optimizer']['opt_state'][0]['nu']{keystr_maxtext}.value"] = (
f"opt_states_0.{prefix_pax_opt_state}.v{keystr_pax}",
transform_fn,
)
state_map[f"['optimizer']['opt_state']['nu']{keystr_maxtext}.value"] = (
f"opt_states_0.{prefix_pax_opt_state}.v{keystr_pax}",
transform_fn,
)
else:
state_map[f".params['params']{keystr_maxtext}"] = (f"mdl_vars{keystr_pax}", transform_fn)
state_map[f".opt_state.mu['params']{keystr_maxtext}"] = (
Expand All @@ -254,6 +263,8 @@ def get_layer_prefix(keystr_pax):

def verify_fn(key_path, _):
keystr = jax.tree_util.keystr(key_path)
if "['rngs']" in keystr:
return

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'rngs' should have been filtered out, if we convert to linen format first. See above comments.

assert keystr in state_map, f"{keystr} not found"

jax.tree_util.tree_map_with_path(verify_fn, state)
Expand All @@ -264,6 +275,8 @@ def verify_fn(key_path, _):

def map_fn(key_path, value):
key_path_str = jax.tree_util.keystr(key_path)
if "['rngs']" in key_path_str:
return value

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

file_path, transform_fn = state_map[key_path_str]
full_path = os.path.join(paxml_ckpt_prefix, file_path)
spec = {"driver": "zarr", "metadata_key": ".zarray", "kvstore": {}}
Expand Down Expand Up @@ -303,6 +316,8 @@ def map_fn(key_path, value):
max_utils.print_mem_stats("converted state finished")

step_value = int(converted_state.optimizer.step.value) if cfg.pure_nnx else converted_state.step
if cfg.pure_nnx:
converted_state = train_state_nnx.to_checkpoint_dict(converted_state)
if checkpointing.save_checkpoint(checkpoint_manager, step_value, converted_state):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is the similar conversion in maybe_save_checkpoint in src/maxtext/common/checkpointing.py:

  if config.pure_nnx:
    # Save in the Linen on-disk layout so pure_nnx and Linen checkpoints are interchangeable.
    if config.enable_diloco:
      # DiLoCoTrainState: persist the synchronized global model (outer params).
      # The per-replica inner optimizer / outer-momentum state is not checkpointed.
      step_value = state.step.get_value() if hasattr(state.step, "get_value") else state.step
      state = train_state_nnx.to_linen_checkpoint_dict({"model": state.params, "optimizer": {"step": step_value}})
    else:
      # rngs/dropout/batch-stats are packed under items/nnx_aux so the RNG/dropout
      # stream continues across resumes instead of resetting to a base key.
      state = train_state_nnx.to_checkpoint_dict(state)

Let's move this conversion from maybe_save_checkpoint to save_checkpoint so we don't have to do the conversion in all the callers.

max_logging.log(f"saved a checkpoint at step {step_value}")
# Upon preemption, exit when and only when all ongoing saves are complete.
Expand Down
Loading