Skip to content

Dsv4 pm - #4751

Draft
snehalv2002 wants to merge 1 commit into
mainfrom
dsv4-pm
Draft

Dsv4 pm#4751
snehalv2002 wants to merge 1 commit into
mainfrom
dsv4-pm

Conversation

@snehalv2002

Copy link
Copy Markdown
Collaborator

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:

  • why is this change being made,
  • the problem being solved and any relevant context,
  • why this is a good solution,
  • some information about the specific implementation,
  • shortcomings of the solution and possible future improvements.

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):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

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.

Suggested change
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}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

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.

Suggested change
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

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.

Suggested change
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The nested inline if-else condition used to determine the params argument is difficult to read and can be simplified using standard logical operators.

Suggested change
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant