Skip to content

fix(training): count expert parameter norms with TP and EP - #4998

Merged
yaoyu-33 merged 5 commits into
mainfrom
yuya/mcore-5916-bridge-param-norm
Jul 29, 2026
Merged

fix(training): count expert parameter norms with TP and EP#4998
yaoyu-33 merged 5 commits into
mainfrom
yuya/mcore-5916-bridge-param-norm

Conversation

@yaoyu-33

@yaoyu-33 yaoyu-33 commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Pass the model-attached regular TP and expert-TP process groups to MCore's parameter-duplicate filter.
  • Keep dense and expert distributed-optimizer main-parameter shards in separate norm buckets.
  • Reduce dense shards over DP/CP and expert shards over expert DP.
  • Add outcome-based regressions for logical expert-parameter counting and group-correct sharded norm aggregation.

Root cause and impact

Bridge maintains an independent calc_params_l2_norm utility for parameter-norm reporting. It filtered every parameter with the regular tensor-parallel topology and accumulated all distributed-optimizer main-parameter shards over ordinary DP/CP.

For parameters with allreduce=False, duplicate filtering must use expert TP. Their sharded FP32 main parameters must likewise be reduced over expert DP. The old behavior could omit a logical expert parameter or aggregate its shard over the wrong domain, producing an undercounted parameter L2 norm in training logs.

Why the Bridge diff differs from the final MCore diff

MCore #5916 originally implemented the same separate expert-sharded bucket in commit 2629ec4c3ae700d0fd67a4ef4399ad9e2d9c6461. A later GTP refactor absorbed that bucket into MCore's generalized norm/reduction structure, so the final MCore PR diff is smaller.

Bridge still owns this independent training-log calculation and uses model-attached process groups (pg_collection.tp, pg_collection.expt_tp, pg_collection.dp_cp, and pg_collection.expt_dp) instead of MCore's global MPU accessors. Bridge already delegates optimizer gradient norm, clipping, and synchronization to MCore, so this PR does not duplicate any optimizer, TE, DDP, or grouped-linear code.

Upstream status

NVIDIA/Megatron-LM#5916 is merged. The current Bridge main pin is its exact merge commit:

  • cd4afffa648426a959dc7cb1e24b5ce7d0c3ff54

The previous upstream blocker is therefore resolved, and this PR does not change the MCore submodule pointer or dependency metadata.

Test-first validation

Both contracts were executed against current Bridge main production code before applying this PR's implementation, using the same tests and the pinned MCore merge commit.

RED: current main without the Bridge fix

  • Logical expert parameter with regular TP rank 1 and expert-TP rank 0:
    • obtained 0.0
    • expected 2.0
  • BF16 distributed-optimizer shards with deterministic group-aware reductions:
    • obtained 5.0
    • expected sqrt(41) = 6.403124...
  • Result: 2 failed

GREEN: same tests with the Bridge fix

  • Exact same two nodes: 2 passed
  • Complete TestCalcParamsL2Norm class: 25 passed
  • uv run pre-commit run --all-files: passed
  • git diff --check: passed

The sharded numeric test assigns independent remote contributions to DP/CP and expert DP and asserts the final norm, so it validates the observable result rather than only checking mocked call arguments.

@copy-pr-bot

copy-pr-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@yaoyu-33 yaoyu-33 added area:training Training loop, callbacks, and runtime integration blocked Work cannot move forward until an external dependency is cleared bug Something isn't working labels Jul 21, 2026
yaoyu-33 added 3 commits July 28, 2026 16:04
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
@yaoyu-33
yaoyu-33 force-pushed the yuya/mcore-5916-bridge-param-norm branch from c57cd8f to 482bf93 Compare July 28, 2026 23:12
@yaoyu-33
yaoyu-33 marked this pull request as ready for review July 28, 2026 23:13
@claude

claude Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review

LGTM. Correct, well-scoped bug fix in calc_params_l2_norm.

What it fixes

Expert (MoE) params with sharded distributed-optimizer main params were appended to sharded_params_data and reduced over the regular DP+CP group (pg_collection.dp_cp). But the distributed optimizer shards expert main params over the expert DP group, so the norm was summed over the wrong process group -- producing an incorrect global norm whenever EP is combined with the distributed optimizer in bf16. The fix routes them into a dedicated sharded_moe_params_data list reduced over pg_collection.expt_dp.

Correctness notes

  • The new expt_dp all_reduce is issued unconditionally (even when empty), mirroring the dense sharded_norm_2 path. Right call -- a conditional collective would hang ranks with uneven expert-param counts.
  • param_is_not_tensor_parallel_duplicate now receives explicit tp_group/expert_tp_group, so expert params de-duplicate against the expert-TP rank. Correct for EP != TP layouts.
  • Hoisting pg_collection to the top is a harmless single-call reuse.

Test coverage

Strong. New/updated unit tests assert dense-vs-expert reductions land on matching DP groups, the duplicate filter receives both TP groups, and an end-to-end mixed dense+expert norm (sqrt(41)) validates accumulation.

Suggested test cases

No perf tests impacted (only train_utils.py and its unit test changed; no scripts/performance/configs/ entries touched). Relevant unit cases:

  • test_bf16_mode_with_sharded_main_param
  • test_bf16_sharded_dense_and_expert_norm_uses_matching_dp_groups
  • test_duplicate_filter_receives_tp_and_expert_tp_groups
  • test_moe_param_norm_counts_logical_parameter_when_tp_ranks_differ
  • test_moe_params_bf16_with_sharded_main_param

Comment on lines +436 to +445
if len(sharded_moe_params_data) > 0:
sharded_moe_norm, _ = multi_tensor_applier(
multi_tensor_l2norm,
dummy_overflow_buf,
[sharded_moe_params_data],
False, # no per-parameter norm.
)
sharded_moe_norm_2 = sharded_moe_norm * sharded_moe_norm
else:
sharded_moe_norm_2 = torch.zeros((1,), dtype=torch.float32, device="cuda")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The expert-sharded all_reduce on pg_collection.expt_dp is issued unconditionally (even for empty sharded_moe_params_data), which correctly mirrors the dense sharded_norm_2 reduction and prevents NCCL hangs when ranks have uneven expert-param counts. Nice — just confirming this collective is always reached on every rank in expt_dp, since a conditional collective here would deadlock.

Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
@yaoyu-33 yaoyu-33 added r0.6.0 Auto-cherrypick to release branch. Apply before merge; cherrypick happens after merge. needs-review PR is ready for code review and waiting on a reviewer and removed blocked Work cannot move forward until an external dependency is cleared labels Jul 29, 2026
The existing calc_params_l2_norm tests all mock
param_is_not_tensor_parallel_duplicate, so they would pass even against an
MCore build that dropped the expert_tp_group kwarg -- silently undercounting
expert parameter norms. Add a test that exercises the real MCore function so a
pinned MCore regressing below this contract fails loudly.

Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test fcc2c82

@yaoyu-33
yaoyu-33 merged commit 0cff82d into main Jul 29, 2026
84 checks passed
@yaoyu-33
yaoyu-33 deleted the yuya/mcore-5916-bridge-param-norm branch July 29, 2026 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:training Training loop, callbacks, and runtime integration bug Something isn't working needs-review PR is ready for code review and waiting on a reviewer r0.6.0 Auto-cherrypick to release branch. Apply before merge; cherrypick happens after merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant