Repository navigation
[Auto-Parallel] add fully_shard #76868
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
Changes from all commits
5162f7c
ebef81a
d95531c
16f2d34
1ad282e
f333c6f
f80b2cd
b4b19c7
ecfbe57
422551f
86ffebc
d570846
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from .fully_shard import fully_shard | ||
|
|
||
| __all__ = ["fully_shard"] |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,155 @@ | ||||||||||
| # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||||||||||
| # | ||||||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||
| # you may not use this file except in compliance with the License. | ||||||||||
| # You may obtain a copy of the License at | ||||||||||
| # | ||||||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||
| # | ||||||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||
| # See the License for the specific language governing permissions and | ||||||||||
| # limitations under the License. | ||||||||||
| from __future__ import annotations | ||||||||||
|
|
||||||||||
| from typing import TYPE_CHECKING, Callable | ||||||||||
|
|
||||||||||
| if TYPE_CHECKING: | ||||||||||
| from collections.abc import Iterable | ||||||||||
|
|
||||||||||
| import paddle | ||||||||||
| import paddle.distributed as dist | ||||||||||
| import paddle | ||||||||||
| from paddle.distributed.auto_parallel.fully_shard import FullyShardAuto | ||||||||||
| from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_fully_shard import ( | ||||||||||
| FullyShard, | ||||||||||
| ) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def in_auto_parallel_mode() -> bool: | ||||||||||
| return getattr( | ||||||||||
| paddle.base.framework.global_var, '_in_auto_parallel_', False | ||||||||||
| ) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| # @dataclass | ||||||||||
|
Collaborator
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. 这个注释能删掉吗? |
||||||||||
| class MixedPrecisionPolicy: | ||||||||||
| param_dtype: paddle.dtype | None = None | ||||||||||
| reduce_dtype: paddle.dtype | None = None | ||||||||||
| output_dtype: paddle.dtype | None = None | ||||||||||
| cast_forward_inputs: bool = True | ||||||||||
|
|
||||||||||
|
|
||||||||||
| # @dataclass | ||||||||||
|
Collaborator
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. 同上 |
||||||||||
| class OffloadPolicy: | ||||||||||
| pin_memory: bool = True | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def _fully_shard_manual_parallel( | ||||||||||
| module, | ||||||||||
| mesh, | ||||||||||
| reshard_after_forward, | ||||||||||
| shard_placement_fn, | ||||||||||
| mp_policy, | ||||||||||
| offload_policy, | ||||||||||
| ignored_params, | ||||||||||
| ): | ||||||||||
| return FullyShard(module) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def _fully_shard_auto_parallel( | ||||||||||
| module, | ||||||||||
| mesh, | ||||||||||
| reshard_after_forward, | ||||||||||
| shard_placement_fn, | ||||||||||
| mp_policy, | ||||||||||
| offload_policy, | ||||||||||
| ignored_params, | ||||||||||
| ): | ||||||||||
| FullyShardAuto(module, mesh) | ||||||||||
|
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. 这里需要返回 model
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. 已修改,感谢! |
||||||||||
| return module | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def fully_shard( | ||||||||||
| module: paddle.nn.Layer, | ||||||||||
| *, | ||||||||||
| mesh: dist.ProcessMesh = None, | ||||||||||
| reshard_after_forward: bool | int | None = None, | ||||||||||
| shard_placement_fn: Callable[[paddle.Tensor], dist.Shard | None] | ||||||||||
| | None = None, | ||||||||||
|
Comment on lines
+79
to
+80
Collaborator
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.
Suggested change
这里格式化的优点难看,试试包一个括号再重新格式化呢? |
||||||||||
| mp_policy: MixedPrecisionPolicy | None = None, | ||||||||||
| offload_policy: OffloadPolicy | None = None, | ||||||||||
| ignored_params: Iterable[paddle.Tensor] | None = None, | ||||||||||
| ) -> paddle.nn.Layer: | ||||||||||
|
Comment on lines
+74
to
+84
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. 注意增加文档
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. 已补充文档,感谢 |
||||||||||
| """ | ||||||||||
| Apply fully sharded data parallel (FSDP) to the given module. | ||||||||||
|
|
||||||||||
| This function wraps the input module with fully sharded data parallelism, which shards | ||||||||||
| model parameters, gradients, and optimizer states across multiple devices. It supports | ||||||||||
| both auto_parallel mode and manual_parallel mode. | ||||||||||
|
|
||||||||||
| Args: | ||||||||||
| module (Layer): The neural network module to be wrapped with fully sharded data parallelism. | ||||||||||
| mesh (dist.ProcessMesh, optional): The process mesh defining the device arrangement for sharding. | ||||||||||
| Defaults to None, which uses the default mesh. | ||||||||||
| reshard_after_forward (bool | int | None, optional): Controls when to reshard the parameters after forward pass. | ||||||||||
| If True or 1, reshard after each forward pass. If False or 0, keep sharded. | ||||||||||
| If None, use default strategy. Defaults to None. | ||||||||||
| shard_placement_fn (Callable[[paddle.Tensor], dist.Shard | None] | None, optional): | ||||||||||
| A function that determines how each tensor should be sharded. Takes a tensor as input | ||||||||||
| and returns a Shard placement or None. If None, uses default sharding strategy. | ||||||||||
| Defaults to None. | ||||||||||
| mp_policy (MixedPrecisionPolicy | None, optional): Mixed precision policy configuration. | ||||||||||
| If None, creates a default MixedPrecisionPolicy. Defaults to None. | ||||||||||
| offload_policy (OffloadPolicy | None, optional): Offload policy configuration for CPU offloading. | ||||||||||
| If None, creates a default OffloadPolicy. Defaults to None. | ||||||||||
| ignored_params (Iterable[paddle.Tensor] | None, optional): Parameters that should not be sharded. | ||||||||||
| These parameters will be kept in full precision and not distributed. Defaults to None. | ||||||||||
|
|
||||||||||
| Returns: | ||||||||||
| module: A wrapper module that applies FSDP to the input module. | ||||||||||
|
|
||||||||||
| Examples: | ||||||||||
| .. code-block:: python | ||||||||||
|
|
||||||||||
| >>> # type: ignore | ||||||||||
|
Collaborator
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. 去掉
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. 好的,感谢 |
||||||||||
| >>> # doctest: +REQUIRES(env:DISTRIBUTED) | ||||||||||
| >>> # python -m paddle.distributed.launch --device=0,1 train.py | ||||||||||
| >>> import paddle | ||||||||||
| >>> import paddle.distributed as dist | ||||||||||
| >>> from paddle.distributed.fsdp import fully_shard | ||||||||||
|
|
||||||||||
| >>> mesh = dist.ProcessMesh([0, 1], dim_names=["x"]) | ||||||||||
| >>> model = paddle.nn.Linear(10, 10) | ||||||||||
| >>> inputs = paddle.rand(shape=[10, 10]) | ||||||||||
| >>> inputs = dist.shard_tensor(inputs, mesh, [dist.Shard(0)]) | ||||||||||
| >>> opt = paddle.optimizer.AdamW(parameters=model.parameters()) | ||||||||||
| >>> model = fully_shard(model, mesh) | ||||||||||
| >>> tr_loss = model(inputs) | ||||||||||
| >>> tr_loss.backward() | ||||||||||
| >>> opt.step() | ||||||||||
| >>> opt.clear_grad() | ||||||||||
| """ | ||||||||||
| if mp_policy is None: | ||||||||||
| mp_policy = MixedPrecisionPolicy() | ||||||||||
| if offload_policy is None: | ||||||||||
| offload_policy = OffloadPolicy() | ||||||||||
| ignored_params_set: set[paddle.Tensor] = ( | ||||||||||
| set(ignored_params) if ignored_params else set() | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| args = ( | ||||||||||
| module, | ||||||||||
| mesh, | ||||||||||
| reshard_after_forward, | ||||||||||
| shard_placement_fn, | ||||||||||
| mp_policy, | ||||||||||
| offload_policy, | ||||||||||
| ignored_params_set, | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| if in_auto_parallel_mode(): | ||||||||||
| return _fully_shard_auto_parallel(*args) | ||||||||||
| else: | ||||||||||
| return _fully_shard_manual_parallel(*args) | ||||||||||
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.
放在 27 行之后,不要让
TYPE_CHECKING把 import 分隔开