Repository navigation
Add global batch-wise MoE auxiliary loss - #4772
Conversation
[ghstack-poisoned]
| hidden_dim=hidden_dim, | ||
| num_experts=8, | ||
| top_k=4, | ||
| load_balance_coeff=1e-3, |
There was a problem hiding this comment.
did you find out which load balancing scheme gpt-oss use from literature?
There was a problem hiding this comment.
not from the paper, this follows transformers https://github.com/huggingface/transformers/blob/f339035b986aaf719bc6f5ea92342f73c498cb0e/src/transformers/models/gpt_oss/configuration_gpt_oss.py#L61
the GPT-OSS repo also doesn't have load-balancing loss, we could remove this https://github.com/openai/gpt-oss/blob/main/gpt_oss/torch/model.py
| comm_backend=moe_comm_backend, | ||
| non_blocking_capacity_factor=non_blocking_capacity_factor, | ||
| ), | ||
| aux_loss_coeff=1e-3, |
There was a problem hiding this comment.
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) |
| elif aux_loss_type == "global_batch_wise": | ||
| aux_loss = GlobalBatchWiseLoadBalanceLoss.Config(coeff=aux_loss_coeff) |
There was a problem hiding this comment.
sounds unambiguous to just
| 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( |
There was a problem hiding this comment.
does it make sense to put them under AuxLoss for now?
| if self._cumulative_expert_counts_E is None: | ||
| self._cumulative_expert_counts_E = torch.zeros_like( | ||
| global_counts_E, dtype=torch.float32 | ||
| ) |
There was a problem hiding this comment.
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?
| "GlobalBatchWiseLoadBalanceLoss with multiple microbatches is " | ||
| "incompatible with activation checkpointing because recomputation " | ||
| "would advance its rolling expert counts. Disable activation " | ||
| "checkpointing or use MicrobatchWiseLoadBalanceLoss." |
There was a problem hiding this comment.
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
[ghstack-poisoned]
| "would read rolling expert counts from later microbatches. Disable " | ||
| "activation checkpointing, use one pipeline microbatch, or use " | ||
| "MicrobatchWiseLoadBalanceLoss." | ||
| ) |
There was a problem hiding this comment.
i am looking into this, but seems like it's a bug that actually helps modeling, because this is mutable state?
say you have pp schedule like
f0 f1 f2 f3 b0 b1 b2 b3
with no AC, b0 will read the stats buffer saved from counts(f0), so it's just the first batch
b1 reads counts(f0) + counts(f1), so it's the rolling approximate
with AC, it seems the buffer is not snapshotted, but used when b0 recomputes f0. but now f0-f3 have accumulated into the buffer, so it actually reads the global stat counts(f0) + counts(f1) + counts(f2) + counts(f3), which is not approximate but exact lol
but also the exact numerics will depend on PP schedule
@soulitzer in case this makes sense to you, maybe i'm doing something wrong
There was a problem hiding this comment.
oh, sounds subtle
- the accumulation idea itself is approximation
- the only problem with PP is that different schedules don't get the same numerics
- but I guess it's hard for future features to be developed when comparing numerics -- people might have forgot that the aux loss can change "drastically" when changing schedules, so I'm OK with erroring out
- in particular, the original paper only mentions grad accum, not PP at all
|
@tianyu-l just updated, should now follow flow of
|
tianyu-l
left a comment
There was a problem hiding this comment.
Please address remaining comments before landing.
| validate_batch_wise_aux_loss( | ||
| model_config, | ||
| num_pp_microbatches=self.num_pp_microbatches, | ||
| activation_checkpoint_enabled=config.activation_checkpoint is not None, | ||
| ) |
There was a problem hiding this comment.
this can be put in TrainingEngine.Config.__post_init__()?
| if torch.compiler.is_compiling(): | ||
| # No eager replay under compile; the partitioner rematerializes instead. | ||
| return False |
There was a problem hiding this comment.
I think we can remove this because we recently deprecated per-block compilation.
| if torch.compiler.is_compiling(): | ||
| # No eager replay under compile; the partitioner rematerializes instead. | ||
| return False | ||
| return remat.is_recomputing() or torch._C._current_graph_task_id() != -1 |
| "would read rolling expert counts from later microbatches. Disable " | ||
| "activation checkpointing, use one pipeline microbatch, or use " | ||
| "MicrobatchWiseLoadBalanceLoss." | ||
| ) |
There was a problem hiding this comment.
oh, sounds subtle
- the accumulation idea itself is approximation
- the only problem with PP is that different schedules don't get the same numerics
- but I guess it's hard for future features to be developed when comparing numerics -- people might have forgot that the aux loss can change "drastically" when changing schedules, so I'm OK with erroring out
- in particular, the original paper only mentions grad accum, not PP at all
[ghstack-poisoned]
| ), | ||
| checkpointer=None, | ||
| activation_checkpoint=SelectiveAC.Config(), | ||
| activation_checkpoint=None, |
There was a problem hiding this comment.
it should work with torch_remat right? cc @anijain2305
[ghstack-poisoned]
|
Warning: Unknown label
Please add the new label to .github/pytorch-probot.yml |
[ghstack-poisoned]
| for user in node.users: | ||
| if not _is_backward_node(user) and "recompute" in user.meta: | ||
| user.meta["recompute"] = CheckpointPolicy.MUST_SAVE | ||
|
|
There was a problem hiding this comment.
@IvanKobzarev checking if this is ok, we have a stats buffer we add_ into, and don't want to double count in AC. We have a context to detect this in eager, not sure if graph trainer has a mechanism that's not policy level
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0) (oldest at bottom): * __->__ #5368 #4772 exported its A10G goldens on a base that predated #5250 (in-place bf16 Linear WGRAD accumulation), which itself re-baselined every A10G golden. The final ghstack rebase onto #5250 kept #4772's stale values, so the committed real_pg goldens never matched the landed code: step-1 grad_norm already differs (the WGRAD accumulation change), and the divergence grows from step 2. The PR's real-PG CI only ran the required subset, which excludes these tests; its fake-PG job failed on both. These tests intentionally verify the new batch-wise aux-loss policy (the expert-bias policy is kept only for gpt_oss_pp via _use_expert_bias_load_balancing), so this refreshes values without changing configs. Provenance (torch 2.16.0.dev20261011+cu132, A10G, seed checkpoint per the test definitions): - real_pg: bit-identical across two independent main runs -- job 114675094754 at 8b4a130 and job 115529499679 at 50474ad. - fake_pg: bit-identical across four independent ciflow/fake-pg runs on PRs based on post-#4772 main (runs 38459347990, 38460226133, 38460414111, 38466591432) that do not touch core model code.
microbatching supported with algo from https://arxiv.org/abs/2501.11873
Stack from ghstack (oldest at bottom):