[Dev] Refactor MoE loss logging - #2569
Merged
Merged
Conversation
yanring
marked this pull request as draft
December 5, 2025 14:27
Contributor
Author
|
/ok to test 9a8e019 |
yanring
force-pushed
the
zijiey/add_imbalance_logging
branch
from
January 12, 2026 08:00
9a8e019 to
71b3d1f
Compare
Contributor
Author
|
/ok to test 1a18019 |
buptzyb
reviewed
Feb 27, 2026
buptzyb
reviewed
Feb 27, 2026
Co-authored-by: Robin Zhang <robinz@nvidia.com>
Co-authored-by: Robin Zhang <robinz@nvidia.com>
Co-authored-by: Robin Zhang <robinz@nvidia.com>
Contributor
Author
|
/ok to test 26a5025 |
buptzyb
approved these changes
Feb 27, 2026
Contributor
Author
|
/ok to test 5bf244f |
yanring
enabled auto-merge
March 2, 2026 14:11
This reverts commit 6d31336.
jiemingz
reviewed
Mar 3, 2026
Contributor
Author
|
/ok to test aa234a3 |
6 tasks
|
/ok to test 25fb25d |
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/22707774878 |
yanring
added a commit
to yanring/Megatron-LM
that referenced
this pull request
Mar 12, 2026
Co-authored-by: Robin Zhang <robinz@nvidia.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do ?
main pr #3431
PR Design / Implementation Doc
TL;DR
Refactors MoE metric logging from ad-hoc global state into
MoEMetricsTracker-- a structured, per-config tracker that centralizes metric collection, distributed reduction, aggregation, and logging. The singleton default preserves existing behavior; multi-model workloads can opt into isolation by assigning separate tracker instances to eachTransformerConfig.Problem Statement
MoE logging logic was spread across mutable global dictionaries and standalone utility functions (
save_to_aux_losses_tracker,reduce_aux_losses_tracker_across_ranks,track_moe_metrics). The reduction semantics (which groups to reduce over, whether to DP-average) were implicit and easy to get wrong when adding new metrics. The global dict also made multi-model isolation impossible.Goals
needs_dp_avg.TransformerConfigwithout changing training-level call sites.save_to_aux_losses_tracker,track_moe_metrics) as deprecated wrappers.Non-Goals
High-Level Design
Ownership model:
megatron/core/(router)self.config.moe_metrics_trackermegatron/training/(training loop)MoEMetricsTracker.get_instance()TransformerConfig.__post_init__setsmoe_metrics_trackerto the global singleton. All paths point to the same instance -- behavior is identical to before.MoEMetricsTracker()instances and assigns them to each config.Per-Metric Metadata (
MetricEntry)Each metric name maps to a
MetricEntrydataclass:valuestorch.Tensornum_layers)reduce_groupProcessGroup?avg_groupProcessGroup?needs_dp_avgboolThis replaces the old
reduce_group_has_dpflag (inverted semantics:needs_dp_avg = not reduce_group_has_dp).Reduction Semantics
For each metric,
_sync_metricsapplies reductions in this fixed order:reduce_groupall-reduce (sum, optional) -- e.g. TP+CP groupavg_groupall-reduce (avg, optional)needs_dp_avg=True) -- average across data parallel ranksExample:
global_load_balancing_lossusesreduce_group=tp_dp_cp_groupwhich already includes DP, soneeds_dp_avg=Falseto avoid double-averaging.Key File Changes
moe_logging.py(new)MoEMetricsTrackerclass withrecord(),report(),clear(), and private sync/aggregate/log helperstransformer_config.pymoe_metrics_trackerfield (defaults to singleton via lazy init in__post_init__)router.pyself.config.moe_metrics_tracker.record(...)instead ofsave_to_aux_losses_tracker(...)training.pyMoEMetricsTracker.get_instance().report(...)instead oftrack_moe_metrics(...)cuda_graphs.pyMoEMetricsTracker.get_instance().clear()instead ofclear_aux_losses_tracker()moe_utils.pysave_to_aux_losses_tracker,track_moe_metricsretained as deprecated wrappers forwarding to the tracker