Skip to content

[DeepSeek] Enable data parallel - #956

Closed
kwen2501 wants to merge 4 commits into
gh/kwen2501/9/basefrom
gh/kwen2501/9/head
Closed

kwen2501 wants to merge 4 commits into
gh/kwen2501/9/basefrom
gh/kwen2501/9/head

Conversation

@kwen2501

@kwen2501 kwen2501 commented Mar 12, 2025 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

This PR adds FSDP on the Expert modules. Since before this we are already doing a DP-to-EP shuffle between the Attention and the Experts, we applies HSDP on the Attention, with the replicate dimension being the same as the EP dimension.

In particular, we are using Zero-2 sharding which shards optimizer and gradients, and not sharding the weights, to avoid the expensive re-gather of MoE.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 12, 2025
kwen2501 added a commit that referenced this pull request Mar 12, 2025
ghstack-source-id: fccf923
Pull Request resolved: #956
This PR adds FSDP on the Expert modules. Since before this we are already doing a DP-to-EP shuffle between the Attention and the Experts, we applies HSDP on the Attention, with the replicate dimension being the same as the EP dimension.

[ghstack-poisoned]
kwen2501 added a commit that referenced this pull request Mar 13, 2025
ghstack-source-id: 4d5cf5c
Pull Request resolved: #956

@weifengpy weifengpy 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.

fsdp wrapping looks good to me

This PR adds FSDP on the Expert modules. Since before this we are already doing a DP-to-EP shuffle between the Attention and the Experts, we applies HSDP on the Attention, with the replicate dimension being the same as the EP dimension.

In particular, we are using Zero-2 sharding which shards optimizer and gradients, and not sharding the weights, to avoid the expensive re-gather of MoE.

[ghstack-poisoned]
kwen2501 added a commit that referenced this pull request Mar 20, 2025
ghstack-source-id: e07ab6d
Pull Request resolved: #956
This PR adds FSDP on the Expert modules. Since before this we are already doing a DP-to-EP shuffle between the Attention and the Experts, we applies HSDP on the Attention, with the replicate dimension being the same as the EP dimension.

In particular, we are using Zero-2 sharding which shards optimizer and gradients, and not sharding the weights, to avoid the expensive re-gather of MoE.

[ghstack-poisoned]
kwen2501 added a commit that referenced this pull request Mar 20, 2025
ghstack-source-id: 3dbd063
Pull Request resolved: #956
@EugenHotaj

EugenHotaj commented Mar 20, 2025 •

Copy link
Copy Markdown
Contributor

Another (slightly tangential) noob q: I guess clip_grad_norm_ will now need to explicitly handle the ep rank for the MOE params? MOE params will need to allreduce their total_norm across the ep dim similar to what pp is doing.

@garrett361

Copy link
Copy Markdown
Contributor

Very cool. One question out of curiosity: any particular reason to use the 2D hsdp mesh when sharding the non-routed-expert layers instead of using its 1D flattened version hsdp._flatten()?

That would improve memory savings by a factor of 2 in this case.

@kwen2501

Copy link
Copy Markdown
Contributor Author

Very cool. One question out of curiosity: any particular reason to use the 2D hsdp mesh when sharding the non-routed-expert layers instead of using its 1D flattened version hsdp._flatten()?

That would improve memory savings by a factor of 2 in this case.

@weifengpy any comment you may have?

@kwen2501

Copy link
Copy Markdown
Contributor Author

Another (slightly tangential) noob q: I guess clip_grad_norm_ will now need to explicitly handle the ep rank for the MOE params? Non-MOE params will need to allreduce their total_norm across the ep dim similar to what pp is doing.

I believe @weifengpy may be the best person to answer this.

@weifengpy

weifengpy commented Mar 20, 2025 •

Copy link
Copy Markdown
Contributor

clip_grad_norm_ will now need to explicitly handle the ep rank for the MOE params?

@kwen2501 at grad clipping, would model.parameters() return DTensor with ep mesh? seems like a yes. in that case, norms will be all-reduced during clip_grad_norm_(DTensor). Should be transparent

Non-MOE params will need to allreduce their total_norm across the ep dim similar to what pp is doing

good catch! we need similar things to pp, i.e. all-reduce total_norm explicitly

@weifengpy

Copy link
Copy Markdown
Contributor

Very cool. One question out of curiosity: any particular reason to use the 2D hsdp mesh when sharding the non-routed-expert layers instead of using its 1D flattened version hsdp._flatten()?
That would improve memory savings by a factor of 2 in this case.

@weifengpy any comment you may have?

indeed, applying fully_shard with global mesh on non-expert should bring extra memory savings, at the cost of extra AG

kwen2501 added a commit that referenced this pull request Mar 21, 2025
ghstack-source-id: 3dbd063
Pull Request resolved: #956
kwen2501 added a commit that referenced this pull request Mar 21, 2025
Reland #956

This PR adds FSDP on the Expert modules. Since before this we are
already doing a DP-to-EP shuffle between the Attention and the Experts,
we applies HSDP on the Attention, with the replicate dimension being the
same as the EP dimension.

In particular, we are using Zero-2 sharding which shards optimizer and
gradients, and not sharding the weights, to avoid the expensive
re-gather of MoE.
@kwen2501

Copy link
Copy Markdown
Contributor Author

Landed via #1003

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

This PR adds FSDP on the Expert modules. Since before this we are
already doing a DP-to-EP shuffle between the Attention and the Experts,
we applies HSDP on the Attention, with the replicate dimension being the
same as the EP dimension.

In particular, we are using Zero-2 sharding which shards optimizer and
gradients, and not sharding the weights, to avoid the expensive
re-gather of MoE.
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
Reland pytorch#956

This PR adds FSDP on the Expert modules. Since before this we are
already doing a DP-to-EP shuffle between the Attention and the Experts,
we applies HSDP on the Attention, with the replicate dimension being the
same as the EP dimension.

In particular, we are using Zero-2 sharding which shards optimizer and
gradients, and not sharding the weights, to avoid the expensive
re-gather of MoE.
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.

6 participants