Skip to content

[RFC] Add sequence-wise MoE load-balance auxiliary loss (DeepSeek-V3)#3864

Open
sdmyzlp wants to merge 4 commits into
pytorch:mainfrom
sdmyzlp:br_aux_loss
Open

[RFC] Add sequence-wise MoE load-balance auxiliary loss (DeepSeek-V3)#3864
sdmyzlp wants to merge 4 commits into
pytorch:mainfrom
sdmyzlp:br_aux_loss

Conversation

@sdmyzlp

@sdmyzlp sdmyzlp commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Add a universal auxiliary loss framework. with MoE sequence-wise load balancing as the first example consumer. This work follows the approach from #3000 (gradient injection via autograd.Function for PP compatibility).

With several modification:

  1. spmd_types-native reduction. This PR instead uses ShardingConfig with spmd.P → spmd.I placement on _SeqwiseCounts, letting the spmd_types framework handle the all-reduce at the module boundary instead of hand-rolled collectives.

  2. LoggedAuxLoss as a generic base class. Extracts aux loss as a reusable LoggedAuxLoss that provides both gradient injection and per-microbatch metric accumulation with PP-safe collection via collect_aux_loss_metrics. Future losses could subclass it.

  3. Built-in logging. Includes an logging attempt out of the box — aux losses appear in the console log line behind metrics.console_log_extra_metrics and are collected automatically across all PP stages.

  4. @spmd.register_autograd_function. instead of a raw torch.autograd.Function, for native spmd_types compatibility.

Tested configurations

  • PP2
  • TP2
  • EP2
  • TP2 + EP2
  • Compile

Side fixes

  • Move global_batch_size resolution before update_from_config() so LoggedAuxLoss.Config.global_batch_size is available during model config traversal.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 5, 2026
@pytorch-bot

pytorch-bot Bot commented Jul 5, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/8gpu

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@tianyu-l

tianyu-l commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Haven't looked into details of this PR, but is it covering the same feature as #3000, which is being reviewed. If so shall we move conversation over there?

cc @pianpwk @shuhuayu

@sdmyzlp

sdmyzlp commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Haven't looked into details of this PR, but is it covering the same feature as #3000, which is being reviewed. If so shall we move conversation over there?

Thanks @tianyu-l for the pointer, and cc @pianpwk @shuhuayu.

We have carefully studied #3000 and this PR is built on top of that existing work, with the main modifications summarized in the updated description — the key shift being a universal LoggedAuxLoss design rather than an MoE-specific one, which we hope will also be useful for the implementation of indexer loss feature.

We understand that @pianpwk may currently be focusing on the spmd_types migration, and wonder if we could keep this PR open for further discussion. Would love to hear your thoughts.

@sdmyzlp sdmyzlp force-pushed the br_aux_loss branch 2 times, most recently from 42ce061 to 6307ed2 Compare July 11, 2026 06:19
sdmyzlp added 4 commits July 11, 2026 15:06
config.training.global_batch_size defaults to -1 and was independently
resolved by each consumer. This caused update_from_config (which runs
before the resolution) to read -1 and propagate it into sub-configs.

Fix: resolve global_batch_size in-place on the config object right after
batch_degree / dp_degree is known, before model_config.update_from_config
or any downstream consumer reads it. All later -1 checks are now dead
code because the value is always resolved.
…losses

Auxiliary objectives computed inside the model (MoE load-balance, DSA
indexer KL, ...) cannot reach the trainer's loss function under pipeline
parallelism, where each stage output is a single tensor. The gradient
injection decouples the gradient from the scalar output, and the logging
framework accumulates per-step values with PP-safe collection.
Per-sequence f_i * P_i load-balance loss penalizes expert imbalance
independently of batch composition, making gradients correct under
PP microbatching and gradient accumulation.
When enabled, extra_metrics (e.g. aux losses) are printed to the
console with per-key color cycling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants