Skip to content

[DeepSeek] Add all_to_all_single impl; Enable backward - #989

Closed
kwen2501 wants to merge 3 commits into
gh/kwen2501/11/basefrom
gh/kwen2501/11/head
Closed

kwen2501 wants to merge 3 commits into
gh/kwen2501/11/basefrom
gh/kwen2501/11/head

Conversation

@kwen2501

@kwen2501 kwen2501 commented Mar 19, 2025 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Adding dist.all_to_all_single as token shuffle method.
This is made the default option for better support of backward pass.

The alternative shuffle method --on_device_all_to_all_v -- will be enabled only when user calls
model.setup_symm_mem()
and is limited to inference only.

# TODO: don't use `received`
gathered_tokens = MoE.token_gather_buf[:received]
else: # "torch_all_to_all"
# Prepare input ans output splits

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.

Typo: and

also, the point of this else branch is just to offer EP>8 support for now, right? and perhaps also benchmark between the two on EP=8

return reordered_past

# Setup Symmetric Memory for MoE token shuffle.
# Supports inference currently.

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.

doesn't support training you mean?
there was a comment on the first PR in the stack that points out a bug in backward formula for all2all. Otoh I thought training was supported.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I put a comment in the code:

For training, this is disabled for now. Reason is that autograd requires tensors not be modified a second time, this conflicts with our wish of sharing the symm mem across layers and/or PP microbatches.

Till we find a workaround, this is disabled for backward.

@EugenHotaj EugenHotaj Mar 20, 2025 •

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.

We noticed this as well when debugging the backward pass / the OOM mentioned in #979.

One way we were able to get around both issues is by using the symmetric memories purely as buffers and passing them directly to on_device_all_to_all_v and calling e.g. token_gather_buffer.copy_(tensor) inside the OnDeviceAllToAllV.forward function and changing OnDeviceAllToAllV.backward accordingly.

But tbh I'm not sure this resulted in correct gradients...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EugenHotaj Yeah, we may need to go the way you said.

Adding `dist.all_to_all_single` as token shuffle method. 
This is made the default option for better support of backward pass.

The alternative shuffle method --`on_device_all_to_all_v` -- will be enabled only when user calls
`model.setup_symm_mem()`
and is limited to inference only.

[ghstack-poisoned]
@kwen2501 kwen2501 changed the title [DeepSeek] Add all_to_all_single impl [DeepSeek] Add all_to_all_single impl; Enable backward Mar 20, 2025
Adding `dist.all_to_all_single` as token shuffle method. 
This is made the default option for better support of backward pass.

The alternative shuffle method --`on_device_all_to_all_v` -- will be enabled only when user calls
`model.setup_symm_mem()`
and is limited to inference only.

[ghstack-poisoned]
@EugenHotaj

Copy link
Copy Markdown
Contributor

Noob q: do we need to allreduce the gradients for the non-MoE parameters or is this automatically handled somehow?

@kwen2501

Copy link
Copy Markdown
Contributor Author

Great great great question!
In PR #956 we apply DP differently -- for non-shared experts, we apply FSDP; for all other parts (including shared experts), we apply HSDP. Meaning, for the "all other parts", they are replicates in the EP dimension, hence they need DDP in that dimension. DDP + FSDP = HSDP.

@EugenHotaj

Copy link
Copy Markdown
Contributor

@kwen2501 if I understand you correctly, the HSDP wrapping in #956 will automatically handle allreducing the grads for the non-EP parts?

@kwen2501

Copy link
Copy Markdown
Contributor Author

Yep.

processed_tokens = torch.empty_like(gathered_tokens)

# This part processes the tokens routed to the local experts.
# TODO: can we use group GEMM here?

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.

yes, will integrate my MG group GEMM here. (#967)

returned_tokens = torch.empty_like(gathered_tokens)
returned_tokens[idxs] = gathered_tokens
output_tokens = torch.empty_like(returned_tokens)
output_tokens[idxs] = returned_tokens

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.

approx, in here we can add an un-permute kernel to handle and speedup the returned token re-ordering.

@lessw2020 lessw2020 left a comment

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.

lgtm.
Stamping to get this landed!

kwen2501 added a commit that referenced this pull request Mar 21, 2025
ghstack-source-id: 137f661
Pull Request resolved: #989
kwen2501 added a commit that referenced this pull request Mar 21, 2025
Reland #989

Adding dist.all_to_all_single as token shuffle method.
This is made the default option for better support of backward pass.

The alternative shuffle method --on_device_all_to_all_v -- will be
enabled only when user calls
model.setup_symm_mem()
and is limited to inference only.
@kwen2501

Copy link
Copy Markdown
Contributor Author

Closing via #1002

@kwen2501 kwen2501 closed this Mar 21, 2025
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
Reland pytorch#989

Adding dist.all_to_all_single as token shuffle method.
This is made the default option for better support of backward pass.

The alternative shuffle method --on_device_all_to_all_v -- will be
enabled only when user calls
model.setup_symm_mem()
and is limited to inference only.
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
Reland pytorch#989

Adding dist.all_to_all_single as token shuffle method.
This is made the default option for better support of backward pass.

The alternative shuffle method --on_device_all_to_all_v -- will be
enabled only when user calls
model.setup_symm_mem()
and is limited to inference only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

5 participants