-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[AutoParallel]Refine ShardOptimizer #62933
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
Merged
zhangbo9674
merged 8 commits into
PaddlePaddle:develop
from
zhangbo9674:dev/fix_shard_opt
Mar 29, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -657,7 +657,9 @@ def __init__(self, optimizer, shard_fn=None): | |
| self._shard_fn._shard_parameter(param) | ||
|
|
||
| def _set_and_check_sharding_prop_from_param(self): | ||
| if len(self._shard_fn._mesh._shape) == 1: | ||
| if (self._shard_fn._mesh is not None) and ( | ||
| len(self._shard_fn._mesh._shape) == 1 | ||
| ): | ||
| self._sharding_degree = self._shard_fn._mesh.get_dim_size(0) | ||
| self._sharding_mesh_axis = 0 | ||
| else: | ||
|
|
@@ -684,16 +686,12 @@ def _set_and_check_sharding_prop_from_param(self): | |
| assert isinstance( | ||
| placements[self._sharding_mesh_axis], dist.Replicate | ||
| ), "The placement on sharding_mesh_axis should be Replicate" | ||
|
|
||
| # check the sharding degree since it has already been set | ||
| if any( | ||
| isinstance(placement, dist.Shard) | ||
| for placement in placements | ||
| ): | ||
| for idx, placement in enumerate(placements): | ||
| if isinstance(placement, dist.Replicate): | ||
| assert ( | ||
| mesh.dim_size(idx) == self._sharding_degree | ||
| ), "The sharding degree of all parameters must be equal currently." | ||
| assert ( | ||
| mesh.dim_size(self._sharding_mesh_axis) | ||
| == self._sharding_degree | ||
| ), "The sharding degree of all parameters must be equal currently." | ||
|
|
||
| assert ( | ||
| self._sharding_degree is not None | ||
|
|
@@ -889,7 +887,7 @@ class ShardingStage1(_ShardingStageBase): | |
| A builtin shard_fn for shard_optimizer interface, users can pass it to shard_optimizer to implement sharding optimization with stage 1. | ||
|
|
||
| Args: | ||
| mesh(paddle.distributed.ProcessMesh): The `ProcessMesh` object describes the Cartesian topology of the used processes. | ||
| mesh(None|paddle.distributed.ProcessMesh): If mesh is not None, the `ProcessMesh` object describes the Cartesian topology of the used processes for dense type parameters. Note: Currently, only one mesh configuration is supported for all dense parameters. If there is a need for multiple mesh configurations, please configure them yourself in the upper layer networking code. | ||
|
|
||
| Examples: | ||
| .. code-block:: python | ||
|
|
@@ -922,7 +920,7 @@ class ShardingStage1(_ShardingStageBase): | |
| >>> # python -m paddle.distributed.launch --gpus=0,1 {test_case}.py | ||
| """ | ||
|
|
||
| def __init__(self, mesh): | ||
| def __init__(self, mesh=None): | ||
| super().__init__(mesh) | ||
|
|
||
| def __call__(self, key, param, accumulator): | ||
|
|
@@ -950,7 +948,7 @@ class ShardingStage2(_ShardingStageBase): | |
| A builtin shard_fn for shard_optimizer interface, users can pass it to shard_optimizer to implement sharding optimization with stage 2. | ||
|
|
||
| Args: | ||
| mesh(paddle.distributed.ProcessMesh): The `ProcessMesh` object describes the Cartesian topology of the used processes. | ||
| mesh(None|paddle.distributed.ProcessMesh): If mesh is not None, the `ProcessMesh` object describes the Cartesian topology of the used processes for dense type parameters. Note: Currently, only one mesh configuration is supported for all dense parameters. If there is a need for multiple mesh configurations, please configure them yourself in the upper layer networking code. | ||
|
|
||
| Examples: | ||
| .. code-block:: python | ||
|
|
@@ -983,7 +981,7 @@ class ShardingStage2(_ShardingStageBase): | |
| >>> # python -m paddle.distributed.launch --gpus=0,1 {test_case}.py | ||
| """ | ||
|
|
||
| def __init__(self, mesh): | ||
| def __init__(self, mesh=None): | ||
| super().__init__(mesh) | ||
|
|
||
| def __call__(self, key, param, accumulator): | ||
|
|
@@ -1022,21 +1020,21 @@ def _grad_hook(grad): | |
| return grad | ||
|
|
||
| def _register_hook_for_param_grad(self, param): | ||
| if param.is_dense(): | ||
| if param.is_dense() and self._mesh is not None: | ||
| placements = [] | ||
| for _ in range(len(self._mesh.shape)): | ||
| placements.append(dist.Replicate()) | ||
| param._to_dist_(placements, self._mesh) | ||
|
|
||
| param.register_hook(ShardingStage2._grad_hook) | ||
| if param.is_dist(): | ||
| param.register_hook(ShardingStage2._grad_hook) | ||
|
|
||
|
|
||
| class ShardingStage3(_ShardingStageBase): | ||
| """ | ||
| A builtin shard_fn for shard_optimizer interface, users can pass it to shard_optimizer to implement sharding optimization with stage 3. | ||
|
|
||
| Args: | ||
| mesh(paddle.distributed.ProcessMesh): The `ProcessMesh` object describes the Cartesian topology of the used processes. | ||
| mesh(None|paddle.distributed.ProcessMesh): If mesh is not None, the `ProcessMesh` object describes the Cartesian topology of the used processes for dense type parameters. Note: Currently, only one mesh configuration is supported for all dense parameters. If there is a need for multiple mesh configurations, please configure them yourself in the upper layer networking code. | ||
|
|
||
| Examples: | ||
| .. code-block:: python | ||
|
|
@@ -1069,30 +1067,33 @@ class ShardingStage3(_ShardingStageBase): | |
| >>> # python -m paddle.distributed.launch --gpus=0,1 {test_case}.py | ||
| """ | ||
|
|
||
| def __init__(self, mesh): | ||
| def __init__(self, mesh=None): | ||
| super().__init__(mesh) | ||
|
|
||
| def _shard_parameter(self, param): | ||
| if param.is_dense(): | ||
| if param.is_dense() and self._mesh is not None: | ||
|
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. else: raise error
Contributor
Author
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. 不指定 mesh 的情况下,不允许 param 是 dense 么?当前测试的 llama2-13B 动静统一组网下就是存在 param 为 dense 的情况。 |
||
| placements = [] | ||
| for _ in range(len(self._mesh.shape)): | ||
| placements.append(dist.Replicate()) | ||
| param._to_dist_(placements, self._mesh) | ||
|
|
||
| new_placements = get_placement_with_sharding( | ||
| param, self._sharding_mesh_axis | ||
| ) | ||
| shard_param = dist.reshard(param, param.process_mesh, new_placements) | ||
| # change the holder of param to new shard_param | ||
| param.get_tensor()._share_data_with(shard_param.get_tensor()) | ||
| if param.is_dist(): | ||
| new_placements = get_placement_with_sharding( | ||
| param, self._sharding_mesh_axis | ||
| ) | ||
| shard_param = dist.reshard( | ||
| param, param.process_mesh, new_placements | ||
| ) | ||
| # change the holder of param to new shard_param | ||
| param.get_tensor()._share_data_with(shard_param.get_tensor()) | ||
|
|
||
| def _unshard_parameter(self, param): | ||
| new_placements = param.placements | ||
| if isinstance(new_placements[self._sharding_mesh_axis], dist.Shard): | ||
| new_placements[self._sharding_mesh_axis] = dist.Replicate() | ||
| if param.is_dist(): | ||
| new_placements = param.placements | ||
| if isinstance(new_placements[self._sharding_mesh_axis], dist.Shard): | ||
| new_placements[self._sharding_mesh_axis] = dist.Replicate() | ||
|
|
||
| new_param = dist.reshard(param, param.process_mesh, new_placements) | ||
| param.get_tensor()._share_data_with(new_param.get_tensor()) | ||
| new_param = dist.reshard(param, param.process_mesh, new_placements) | ||
| param.get_tensor()._share_data_with(new_param.get_tensor()) | ||
|
|
||
| def __call__(self, key, param, accumulator): | ||
| if param.is_dist(): | ||
|
|
||
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.
默认值为 None 时的的情况也描述一下吧