-
Notifications
You must be signed in to change notification settings - Fork 566
fix(nnx): fix gpt3 paxml checkpoint conversion state map and layout #4555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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), | ||
| } | ||
| else: | ||
| state_map = { | ||
|
|
@@ -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}"] = ( | ||
|
|
@@ -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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
@@ -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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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": {}} | ||
|
|
@@ -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): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is the similar conversion in Let's move this conversion from |
||
| max_logging.log(f"saved a checkpoint at step {step_value}") | ||
| # Upon preemption, exit when and only when all ongoing saves are complete. | ||
|
|
||
There was a problem hiding this comment.
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?