Dsv4 pm - #4751
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for a tiny DeepSeek-V4 model configuration (deepseek4-tiny) and refactors the DeepSeek-V4 parameter mapping and hook functions to align MaxText and Hugging Face parameters. It also updates Tid2EidVar to subclass nnx.Param and injects static hash tables during checkpoint conversion. The review feedback highlights several critical issues: a potential NameError when saving adapter-only checkpoints, a failure to respect the scan_layers parameter in the parameter hook function, an incorrect configuration lookup for first_num_hash_layers, and a complex inline condition in utils.py that should be simplified for readability.
| save_weights_to_checkpoint( | ||
| output_directory, | ||
| jax_weights, | ||
| state_params, |
There was a problem hiding this comment.
If is_adapter_only is True, state_params is never defined because its definition is inside the else block of if is_adapter_only:. This will result in a NameError: name 'state_params' is not defined when attempting to save an adapter-only checkpoint.
We should pass state_params if we are not in adapter-only mode, and fall back to jax_weights otherwise.
| state_params, | |
| state_params if not is_adapter_only else jax_weights, |
|
|
||
| mapping = {} | ||
| for i in range(n_layers): | ||
| prefix = f"params-decoder-layers_{i}" |
There was a problem hiding this comment.
The DEEPSEEK_V4_MAXTEXT_TO_HF_PARAM_HOOK_FN function ignores the scan_layers parameter and always uses the un-scanned prefix params-decoder-layers_{i}. When scan_layers is enabled, the parameter mapping keys will use params-decoder-scanned_blocks-layers_{i}, causing a mismatch where no hook functions (such as transposes and MHC reshapes) are applied to the scanned layers.
| prefix = f"params-decoder-layers_{i}" | |
| if scan_layers: | |
| prefix = f"params-decoder-scanned_blocks-layers_{i}" | |
| else: | |
| prefix = f"params-decoder-layers_{i}" |
| num_experts = 8 | ||
|
|
||
| mapping[f"{prefix}-mlp-MoeBlock_0-gate-kernel"] = f"layers.{i}.ffn.gate.weight" | ||
| num_hash_layers = config.get("first_num_hash_layers", 3) |
There was a problem hiding this comment.
The parameter mapping function attempts to retrieve first_num_hash_layers from config (which is the Hugging Face config dictionary). However, first_num_hash_layers is a MaxText-specific configuration parameter and is not present in the Hugging Face config (which instead uses num_hash_layers). This causes the function to always fall back to the default value of 3, ignoring any custom user configuration.
We should retrieve this value from maxtext_config using getattr.
| num_hash_layers = config.get("first_num_hash_layers", 3) | |
| num_hash_layers = getattr(maxtext_config, "first_num_hash_layers", 3) |
|
|
||
| state_new = train_state.TrainState( | ||
| step=step_number_to_save_new_ckpt, apply_fn=None, params={"params": jax_weights}, tx=None, opt_state={} # type: ignore | ||
| step=step_number_to_save_new_ckpt, apply_fn=None, params=jax_weights if ("params" in jax_weights.keys() if isinstance(jax_weights, dict) else False) else {"params": jax_weights}, tx=None, opt_state={} # type: ignore |
There was a problem hiding this comment.
The nested inline if-else condition used to determine the params argument is difficult to read and can be simplified using standard logical operators.
| step=step_number_to_save_new_ckpt, apply_fn=None, params=jax_weights if ("params" in jax_weights.keys() if isinstance(jax_weights, dict) else False) else {"params": jax_weights}, tx=None, opt_state={} # type: ignore | |
| step=step_number_to_save_new_ckpt, apply_fn=None, params=jax_weights if (isinstance(jax_weights, dict) and "params" in jax_weights) else {"params": jax_weights}, tx=None, opt_state={} # type: ignore |
Description
Start with a short description of what the PR does and how this is a change from
the past.
The rest of the description includes relevant details and context, examples:
If the change fixes a bug or a Github issue, please include a link, e.g.,:
FIXES: b/123456
FIXES: #123456
You can also provide a comma-separated list. If you don't want to close a bug but
simply to reference it, use BUGS, e.g.:
BUGS: b/123456
Notice 1: Once all tests pass, the "pull ready" label will automatically be assigned.
This label is used for administrative purposes. Please do not add it manually.
Notice 2: For external contributions, our settings currently require an approval from a MaxText maintainer to trigger CI tests.
Tests
Please describe how you tested this change, and include any instructions and/or
commands to reproduce.
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.