Repository navigation
[DeepEP] Fix silently wrong MoE gradients under activation checkpointing - #5115
Conversation
|
|
|
The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:
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. |
|
Thanks, I'm also seeing a related PR #5123 just so you know. @anijain2305 will review both. |
|
@francesco-bertolotti Thanks for the contribution. There are a few developments happening in parallel that should resolve the issue this PR is targetting
The benefit of this PR is that it actually allows you to recompute dispatch and therefore save memory. However, Currently, my take is that we should just always save dispatch and combine for DeepEP for simplicity. But I am happy to reconsider. Do you have any real runs where recomputing dispatch really saves you considerable memory that it is a must have? Update - After talking to @tianyu-l , I think as a framework, we should provde the option to recompute. Can you please do a followup
|
|
Copying the modified update from last message to avoid notification suppression. After talking to @tianyu-l , I think as a framework, we should provide the option to recompute
|
| num_topk=num_topk, | ||
| use_fp8_dispatch=use_fp8_dispatch, | ||
| deterministic=torch.are_deterministic_algorithms_enabled(), | ||
| deterministic=deterministic, |
There was a problem hiding this comment.
concretely, could we determine this flag somewhere in this file, where we have access to module.remat_should_recompute("dispatch")
0dc9867 to
85d31be
Compare
| """Whether RegionAC replays dispatch/combine during backward.""" | ||
| # Without RegionAC the remat regions have no fqn and do not affect execution | ||
| # (remat_should_recompute is then True, since nothing is saved). | ||
| return bool(self._remat_module_fqn) and self.remat_should_recompute( |
There was a problem hiding this comment.
Is bool(self._remat_module_fqn) for FullAC, which is not migrated to remat yet?
anijain2305
left a comment
There was a problem hiding this comment.
Looks great! One question.
Why
deepep.dispatchreturns anEPHandlethat records the layout of the received rows: which row came from which source token on which rank, in what order..When activation checkpointing replays the DeepEP ops, this breaks:
ctx. The tensors saved for backward are dropped, to be recomputed.ElasticBuffer(deterministic=False), two identical dispatches do not return rows in the same order, so the recomputed tensors (received tokens, expert activations) are in order B.ctxattributes are not recomputed, so the original nodes map order-B gradients with the order-A layout.The forward output is correct, so the loss looks normal. But every gradient through the MoE is wrong: the router, the expert weights, and the block input.
When this happens:
FullACSelectiveAC, default save setdeepep.dispatch/combineare incomm_ops)SelectiveACsubclass overridingget_save_ops()RegionACsave_regionsmatches*.token_dispatcher.ep_communication(default[])What
get_buffer(..., deterministic: bool | None = None)(torchtitan/distributed/deepep/deepep.py)Nonekeeps the current behavior and followstorch.are_deterministic_algorithms_enabled().ValueErrorinstead of rebuilding. All dispatchers share one process-global buffer, so a rebuild would silently change the setting for dispatchers built earlier.DeepEPTokenDispatcher.Config.deterministic: bool | None = Noneset_determinism, and passed toget_bufferbyinit_buffer.TokenDispatcherTransform(dispatcher=DeepEPTokenDispatcher, kwargs={"deterministic": True}).deterministic=True._check_deepep_replay(torchtitan/distributed/activation_checkpoint.py)ValueErrorwhen a policy would replay the dispatch or combine of a DeepEP dispatcher withdeterministic=False._wrap_block, so it sees the save set the policy actually uses for that block (someSelectiveACsubclasses varyget_save_ops()per block).FullAC: always replays.SelectiveAC: replays unless bothdeepep.dispatchanddeepep.combineare inget_save_ops().RegionAC:dispatcher.remat_should_recompute("ep_communication").deterministic=True, which adds a sort to every dispatch;save_regionspattern.No behavior change for configurations that were already correct. The default
SelectiveACsaves the DeepEP ops, anddebug.deterministic=Truealready gave a deterministic buffer through theNonepath.Numerics
Measured on one GH200 node, EP=4, D=6144, F=1792, 96 experts per rank, top-8. The block is
h + moe(rms_norm(h))with an fp32 router, and gradients are compared against the same block without checkpointing (relative L2 error):aten.topk, default bufferaten.topk,deterministic=TrueCost of
deterministic=Trueat that shape:deterministic=Truetopk-only SACThe alternative, saving the DeepEP ops, keeps the received-token tensors per layer (about 3 GiB per layer at this shape).
Tests
tests/unit_tests/cpu/test_deepep_activation_checkpoint.py(needsdeep_epimportable, no GPU). Checks that the guard:FullAC,topk-only SAC, dispatch-only and combine-only SAC, andRegionACwith no save regions whendeterministic=False;deterministic=True;RegionACsavingep_communication;torch.use_deterministic_algorithmswhendeterministic=None.tests/unit_tests/gpu/test_deepep_activation_checkpoint.py(multi_gpu, EP=min(4, GPUs)). Requires output, input grad and all parameter grads to be bitwise equal to no checkpointing underFullAC,topk-only SAC andRegionAC, withdeterministic=True.deterministic=False) that must fail.tests/unit_tests/cpu/test_{inference_moe,transforms,moe}.py,tests/unit_tests/gpu/test_{activation_checkpoint,remat}.py.pre-commitis clean on the changed files, except pyrefly errors in untouched code (_set_lru_cache,F.scaled_addmm_,MixedPrecisionPolicy(param_dtype_override_fn=...), dist_moe runtime), which come from the torch nightly API.Notes for users
FullAC, a customSelectiveAC.get_save_ops(), orRegionACwithoutep_communicationinsave_regionshad wrong MoE gradients. Throughput numbers from such runs are valid; loss curves are not.deterministic=Trueon the DeepEP dispatcher, or save both DeepEP ops.