Skip to content

[WIP] [CUDA] fsdp - #3768

Merged
nastya236 merged 3 commits into
mainfrom
fsdp2
Jul 21, 2026
Merged

nastya236 merged 3 commits into
mainfrom
fsdp2

Conversation

@nastya236

@nastya236 nastya236 commented Jun 25, 2026 •

Copy link
Copy Markdown
Collaborator

Fully sharded data parallel.

dtype = mx.bfloat16
for i, layer in enumerate(model.layers):
    model.layers[i] = fully_shard(layer, group=groups["fsdp"], compute_dtype=dtype)
model.embed_tokens = fully_shard(model.embed_tokens, group=groups["fsdp"], compute_dtype =dtype)

It is incomplete, because bf16 weights that are gathered during forward are not resharded and used in backward. So we can cut another ~6GB by resharding and gathering back during backward.
image

Peak memory difference between sharded optimizer and this PR is 14 GB [4B * 4 = 16GB -- master copy weights, therefore each rank holds only 16GB / 8 =2 GB 2 instead of 16GB] as expected.

@nastya236
nastya236 marked this pull request as ready for review July 12, 2026 17:55

@angeloskath angeloskath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks really good!

I am curious about how you are thinking to approach the re-gathering the weights in the backward. I see a few ways none of which I like.

Either way we should definitely merge this as is. Really nice!

@nastya236

Copy link
Copy Markdown
Collaborator Author

Thank you! General high level thought was to checkpoint gather so the forward weights don't stay in the graph, but you probably though about it more than me..

@nastya236
nastya236 merged commit 8f64abc into main Jul 21, 2026
28 checks passed
@nastya236
nastya236 deleted the fsdp2 branch July 21, 2026 21:10
@BrewTestBot BrewTestBot mentioned this pull request Aug 18, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants