Skip to content

Add global batch-wise MoE auxiliary loss - #4772

Open
pianpwk wants to merge 2 commits into
gh/pianpwk/103/basefrom
gh/pianpwk/103/head
Open

pianpwk wants to merge 2 commits into
gh/pianpwk/103/basefrom
gh/pianpwk/103/head

Conversation

@pianpwk

@pianpwk pianpwk commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

microbatching supported with algo from https://arxiv.org/abs/2501.11873

Stack from ghstack (oldest at bottom):

[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 17, 2026
pianpwk added a commit that referenced this pull request Sep 17, 2026
ghstack-source-id: a21e261
Pull Request resolved: #4772
[ghstack-poisoned]
pianpwk added a commit that referenced this pull request Sep 17, 2026
ghstack-source-id: 961706c
Pull Request resolved: #4772
@pianpwk
pianpwk marked this pull request as ready for review September 18, 2026 22:27
@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Sep 18, 2026
hidden_dim=hidden_dim,
num_experts=8,
top_k=4,
load_balance_coeff=1e-3,

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.

did you find out which load balancing scheme gpt-oss use from literature?

comm_backend=moe_comm_backend,
non_blocking_capacity_factor=non_blocking_capacity_factor,
),
aux_loss_coeff=1e-3,

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.

I think we also need to make this None for qwen
https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/common/config_utils.py#L316

Actually default should be None to avoid mistake in the future.


def _post_optimizer_build_fn(optimizers, model_parts, parallel_dims):
"""Register step pre-hooks for load balancing and aux-loss accumulators."""
register_moe_load_balancing_hook(optimizers, model_parts, parallel_dims)

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.

don't need this?

Comment on lines +326 to +327
elif aux_loss_type == "global_batch_wise":
aux_loss = GlobalBatchWiseLoadBalanceLoss.Config(coeff=aux_loss_coeff)

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.

sounds unambiguous to just

Suggested change
elif aux_loss_type == "global_batch_wise":
aux_loss = GlobalBatchWiseLoadBalanceLoss.Config(coeff=aux_loss_coeff)
elif aux_loss_type == "batch_wise":
aux_loss = BatchWiseLoadBalanceLoss.Config(coeff=aux_loss_coeff)

)


def _reduce_token_partials(

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.

does it make sense to put them under AuxLoss for now?

Comment on lines +689 to +692
if self._cumulative_expert_counts_E is None:
self._cumulative_expert_counts_E = torch.zeros_like(
global_counts_E, dtype=torch.float32
)

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.

may not be cudagraph friendly if we reuse cudagraph for every spmd (outer) grad accum? But I think we probably will move to all spmd grad accum steps captured in one big cudagraph anyway. cc @fegin

That said, any reason we don't allocate memory for the final shape upfront?

Comment on lines +37 to +40
"GlobalBatchWiseLoadBalanceLoss with multiple microbatches is "
"incompatible with activation checkpointing because recomputation "
"would advance its rolling expert counts. Disable activation "
"checkpointing or use MicrobatchWiseLoadBalanceLoss."

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.

Sounds too big of a limitation, as models using this loss cannot be trained with AC anymore.

On the other hand, I believe torch_remat can work around this? @anijain2305

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

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl 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