Adding prefetching of first shards to train script when fsdp enabled#1955
Closed
chelsea0x3b wants to merge 2 commits intopytorch:mainfrom
chelsea0x3b:fsdp-prefetching
Closed
Adding prefetching of first shards to train script when fsdp enabled#1955chelsea0x3b wants to merge 2 commits intopytorch:mainfrom chelsea0x3b:fsdp-prefetching
chelsea0x3b wants to merge 2 commits intopytorch:mainfrom
chelsea0x3b:fsdp-prefetching
Conversation
tianyu-l
requested changes
Oct 28, 2025
Comment on lines
+507
to
+509
| if self.parallel_dims.fsdp_enabled: | ||
| # NOTE: prefetches the model | ||
| self.model_parts[0].unshard(async_op=True) |
Contributor
There was a problem hiding this comment.
Curious how much benefit do we get from this? Could you show some traces?
I have the following concerns:
- When we don't do logging (where getting loss to CPU incurs d2h sync), GPU is ahead of CPU and can already overlap with dataloading, so this is not saving anything.
- In torchtitan there're other FSDP implementation, in in general we should avoid FSDP2-only code. There's a way around by testing if
model_parts[0]is anFSDPModule, but it's not so clean.
Let's see if the benefit justifies the complexity. WDYT?
Contributor
Author
There was a problem hiding this comment.
Ah yeah turns out there is little benefit, you're right! I think i had logging higher when I was noticing an improvement. Will close, ty!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If model is sharded calling
.unshard()will prefetch the first shard. I placed this before the data loader & other preprocessing so it should overlap.Sources:
https://docs.pytorch.org/docs/main/distributed.fsdp.fully_shard.html#torch.distributed.fsdp.FSDPModule.unshard
https://docs.pytorch.org/tutorials/intermediate/FSDP_tutorial.html#forward-backward-with-prefetching