-
Notifications
You must be signed in to change notification settings - Fork 4.4k
[draft] support async param gather in layer-wise optimizer #2787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,8 @@ def __init__( | |
| config: OptimizerConfig, | ||
| pg_collection: Optional[ProcessGroupCollection] = None, | ||
| init_state_fn_list: Optional[List[Callable]] = None, | ||
| model_chunks: Optional[List] = None, | ||
| async_allgather: Optional[bool] = False, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reuse "overlap_param_gather" if the flag indicates the same thing, no need to introduce new names. |
||
| ) -> None: | ||
| """ | ||
| Initialize LayerWiseDistributedOptimizer. | ||
|
|
@@ -58,6 +60,13 @@ def __init__( | |
|
|
||
| self.pg_collection = pg_collection | ||
| self.shard_params(optimizers) | ||
|
|
||
| # set bucket lw params list for async allgather | ||
| self.async_allgather = async_allgather | ||
| if self.async_allgather: | ||
| assert model_chunks is not None, "model_chunks must be provided if async_allgather is True" | ||
| self.set_bucket_lw_params_list(model_chunks) | ||
|
|
||
| if init_state_fn_list: | ||
| assert len(init_state_fn_list) == len( | ||
| optimizers | ||
|
|
@@ -143,6 +152,28 @@ def shard_params(self, optimizers): | |
| if expt_dp_size == 1 or len(self.expt_dp_params_list[0]) == 0: | ||
| self.expt_dp_params_list = None | ||
|
|
||
| def set_bucket_lw_params_list(self, model_chunks: List[MegatronModule]): | ||
| for model_chunk in model_chunks: | ||
| for group in model_chunk.bucket_groups: | ||
| for bucket in group: | ||
| # find all params that belong to this bucket and keep their sharding structure | ||
| bucket_params_list = [[] for _ in range(get_pg_size(self.pg_collection.dp_cp))] | ||
| for bucket_list, full_params_list in zip(bucket_params_list, self.dp_cp_params_list): | ||
| for param in full_params_list: | ||
| if param in bucket.params: | ||
| bucket_list.append(param) | ||
| bucket.set_lw_params_list(bucket_params_list) | ||
| # do the same for expert parallel | ||
| for group in model_chunk.expert_parallel_bucket_groups: | ||
| for bucket in group: | ||
| # find all params that belong to this bucket and keep their sharding structure | ||
| bucket_params_list = [[] for _ in range(get_pg_size(self.pg_collection.expt_dp))] | ||
| for bucket_list, full_params_list in zip(bucket_params_list, self.expt_dp_params_list): | ||
| for param in full_params_list: | ||
| if param in bucket.params: | ||
| bucket_list.append(param) | ||
| bucket.set_lw_params_list(bucket_params_list) | ||
|
|
||
| @torch.no_grad() | ||
| def allgather_params(self) -> None: | ||
| """All-gather updated params from all ranks.""" | ||
|
|
@@ -223,8 +254,9 @@ def step(self): # type: ignore[no-untyped-def] | |
| """step function for layer-wise optimizer.""" | ||
| update_successful, grad_norm, num_zeros_in_grad = super().step() | ||
|
|
||
| # All gather updated params. | ||
| self.allgather_params() | ||
| # All gather updated params. If async_allgather is True, the allgather is done in the forward pre-hook. | ||
| if not self.async_allgather: | ||
| self.allgather_params() | ||
|
|
||
| return update_successful, grad_norm, num_zeros_in_grad | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does async allgather with layerwise still require use-distributed-optimizer? I think yes, to let DDP make the buckets right?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for simplicity to demo the idea, I didn't touch that part. currently if use-distributed-optimizer is off, then all async related functionality will be turned off and code errors out.
But technically this is not required, we just need to change those check from if use-distributed-optimizer to if (use-distributed-optimizer or use-layer-wise)