fix(mfsdp): support fsdp v2 zero-1 and zero-2 - #10
Conversation
… develop/mfsdp-refactor-main-stage2 # Conflicts: # megatron/core/distributed/fsdp/src/megatron_fsdp/v2/param_group.py
shjwudp
left a comment
There was a problem hiding this comment.
This PR fixes several existing issues.
I'd prefer a more complete solution that addresses both optim and optim_grads in a single PR. We should ensure that both shard strategies are fully validated with the expected behaviors:
optim: performs one all-gather (AG) and one reduce-scatter (RS) per iteration
optim_grads: performs one AG and num_GA reduce-scatter (RS) operations
Please also include unit tests. These strategies are relatively complex, so a corresponding design doc would be highly valuable—particularly to clarify how the optim and optim_grads sharding strategies are implemented within the current code framework and pitfalls.
| + main_shard_meta.size | ||
| ] | ||
| ) | ||
| sync_replicated_model_buffer(model_weight_buffer) |
There was a problem hiding this comment.
This unshard can be executed during the forward pass, which is more favorable for overlap. The communication workflow from ZeRO-3 can be reused (only first micro-batch), and for optim and optim_grads sharding strategy, it is not necessary to register post-forward or post-backward hooks.
Additionally, how are optim_grads and optim gradients handle distinguished?
| TracePoolAllocator() if enable_trace_pool else StorageFreeingBucketAllocator() | ||
| use_trace_pool = enable_trace_pool and sharding_strategy in ( | ||
| "optim_grads", | ||
| "optim_grads_params", |
There was a problem hiding this comment.
Curious, why "optim" can not use trace pool allocator?
There was a problem hiding this comment.
optim does not use temporary unshard/reduce-scatter buckets in the current flow: weights and grads used by compute are replicated, grad reduction isin-place all-reduce, and replicated weight refresh writes directly into the persistent model weight buffer. Therefore TracePoolAllocator has no allocation trace to plan and can hit the empty-trace assertion
There was a problem hiding this comment.
I will fix this case
| start_offsets.append(start_offset) | ||
| model_param_shards.append((model_shard, transpose_shard)) | ||
|
|
||
| quantize_main_weights_to_fp8( |
There was a problem hiding this comment.
Is fp8 param need special handle for optim and optim_grads sharding strategy?
shjwudp
left a comment
There was a problem hiding this comment.
Great to see ZeRO-1/2 introduced here (along with critically important unit tests) — this will be a key step toward unlocking Hybrid FSDP. That said, the implementation details will need several rounds of discussion to finalize. Please refer to my review comments. Thank you!
| # --- Reduce-grad overlap tracking --- | ||
| reduce_grad_buckets: Dict[int, List[Tuple[torch.cuda.Event, ParameterGroup]]] | ||
| # --- Reduce-scatter grad overlap tracking --- | ||
| reduce_scatter_grad_buckets: Dict[int, List[Tuple[torch.cuda.Event, ParameterGroup]]] |
There was a problem hiding this comment.
We need potentially support no_shard, so reduce_grad make more sense here.
| module.reduce_grad(async_op=ctx.enable_async_reduce_grad) | ||
| module.reduce_scatter_grad( | ||
| async_op=ctx.enable_async_reduce_grad, | ||
| allowed_sharding_strategies=("optim_grads", "optim_grads_params"), |
There was a problem hiding this comment.
Can we externalize allowed_sharding_strategies to reduce the complexity of the reduce_scatter_grad function?
|
|
||
| @torch.no_grad() | ||
| def finish_grad_sync(self, force_all_reduce: Optional[bool] = False): | ||
| """Finish optimizer-facing gradient synchronization for this iteration.""" |
There was a problem hiding this comment.
Moving the ZeRO-1's gradient handling to the end looks good for now, but we may need to revisit this in the future if ZeRO-1 overlap-grad-reduce support is required.
| """ | ||
| work = None | ||
| if not bwd_pass and self._needs_replicated_weight_buffer_refresh: | ||
| sync_replicated_buffer_from_shard( |
There was a problem hiding this comment.
Would be better merge sync_replicated_buffer_from_shard into L220 weight_buffer.unshard.
bf91245 to
acd7241
Compare
acd7241 to
00507fd
Compare
c70e5b0 to
123d7f9
Compare
What does this PR do ?
Contribution process
flowchart LR A[Pre-checks] --> B[PR Tests] subgraph Code Review/Approval C1[Expert Review] --> C2[Final Review] end B --> C1 C2 --> D[Merge]Pre-checks
Core 0.8)Code review
The following process is enforced via the CODEOWNERS file for changes into
megatron/core. For changes outside ofmegatron/core, it is up to the PR author whether or not to tag the Final Reviewer team.For MRs into `main` branch
Feel free to message or comment the @mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!
(Step 1): Add PR label
Expert Review(Step 2): Collect the expert reviewers reviews
Expert Reviewlabel when your PR is ready for review.Final Review might get declined if these requirements are not fulfilled.
(Step 3): Final Review
Final Reviewlabel(Optional Step 4): Cherry-pick into release branch
If this PR also needs to be merged into
core_r*release branches, after this PR has been merged, selectCherry-pickto open a new PR into the release branch.For MRs into `dev` branch
The proposed review process for `dev` branch is under active discussion.MRs are mergable after one approval by either
eharper@nvidia.comorzijiey@nvidia.com.Merging your PR
Any member of core-adlr and
core-nemowill be able to merge your PR.