Skip to content

Commit d19f29b

Browse files
authored
[AutoParallel]Refine ShardOptimizer (#62933)
* fix * fix * fix * fix * refine * fix * fix * fix
1 parent 8213876 commit d19f29b

2 files changed

Lines changed: 39 additions & 33 deletions

File tree

paddle/fluid/pybind/tensor.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,12 @@ void BindTensor(pybind11::module &m) { // NOLINT
10771077
[](DistTensor &self, const DistTensor &src) {
10781078
self.unsafe_set_dims(src.dims());
10791079
self.unsafe_set_dist_attr(src.dist_attr());
1080-
self.unsafe_mutable_value()->ShareDataWith(src.value());
1080+
if (!IsCurRankInMesh(self.process_mesh()) &&
1081+
!IsCurRankInMesh(src.dist_attr().process_mesh())) {
1082+
self.unsafe_mutable_value()->ShareDataNoCheckWith(src.value());
1083+
} else {
1084+
self.unsafe_mutable_value()->ShareDataWith(src.value());
1085+
}
10811086
return self;
10821087
})
10831088
.def("_clear", &DistTensor::clear);

python/paddle/distributed/auto_parallel/api.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,9 @@ def __init__(self, optimizer, shard_fn=None):
657657
self._shard_fn._shard_parameter(param)
658658

659659
def _set_and_check_sharding_prop_from_param(self):
660-
if len(self._shard_fn._mesh._shape) == 1:
660+
if (self._shard_fn._mesh is not None) and (
661+
len(self._shard_fn._mesh._shape) == 1
662+
):
661663
self._sharding_degree = self._shard_fn._mesh.get_dim_size(0)
662664
self._sharding_mesh_axis = 0
663665
else:
@@ -684,16 +686,12 @@ def _set_and_check_sharding_prop_from_param(self):
684686
assert isinstance(
685687
placements[self._sharding_mesh_axis], dist.Replicate
686688
), "The placement on sharding_mesh_axis should be Replicate"
689+
687690
# check the sharding degree since it has already been set
688-
if any(
689-
isinstance(placement, dist.Shard)
690-
for placement in placements
691-
):
692-
for idx, placement in enumerate(placements):
693-
if isinstance(placement, dist.Replicate):
694-
assert (
695-
mesh.dim_size(idx) == self._sharding_degree
696-
), "The sharding degree of all parameters must be equal currently."
691+
assert (
692+
mesh.dim_size(self._sharding_mesh_axis)
693+
== self._sharding_degree
694+
), "The sharding degree of all parameters must be equal currently."
697695

698696
assert (
699697
self._sharding_degree is not None
@@ -889,7 +887,7 @@ class ShardingStage1(_ShardingStageBase):
889887
A builtin shard_fn for shard_optimizer interface, users can pass it to shard_optimizer to implement sharding optimization with stage 1.
890888
891889
Args:
892-
mesh(paddle.distributed.ProcessMesh): The `ProcessMesh` object describes the Cartesian topology of the used processes.
890+
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.
893891
894892
Examples:
895893
.. code-block:: python
@@ -922,7 +920,7 @@ class ShardingStage1(_ShardingStageBase):
922920
>>> # python -m paddle.distributed.launch --gpus=0,1 {test_case}.py
923921
"""
924922

925-
def __init__(self, mesh):
923+
def __init__(self, mesh=None):
926924
super().__init__(mesh)
927925

928926
def __call__(self, key, param, accumulator):
@@ -950,7 +948,7 @@ class ShardingStage2(_ShardingStageBase):
950948
A builtin shard_fn for shard_optimizer interface, users can pass it to shard_optimizer to implement sharding optimization with stage 2.
951949
952950
Args:
953-
mesh(paddle.distributed.ProcessMesh): The `ProcessMesh` object describes the Cartesian topology of the used processes.
951+
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.
954952
955953
Examples:
956954
.. code-block:: python
@@ -983,7 +981,7 @@ class ShardingStage2(_ShardingStageBase):
983981
>>> # python -m paddle.distributed.launch --gpus=0,1 {test_case}.py
984982
"""
985983

986-
def __init__(self, mesh):
984+
def __init__(self, mesh=None):
987985
super().__init__(mesh)
988986

989987
def __call__(self, key, param, accumulator):
@@ -1022,21 +1020,21 @@ def _grad_hook(grad):
10221020
return grad
10231021

10241022
def _register_hook_for_param_grad(self, param):
1025-
if param.is_dense():
1023+
if param.is_dense() and self._mesh is not None:
10261024
placements = []
10271025
for _ in range(len(self._mesh.shape)):
10281026
placements.append(dist.Replicate())
10291027
param._to_dist_(placements, self._mesh)
1030-
1031-
param.register_hook(ShardingStage2._grad_hook)
1028+
if param.is_dist():
1029+
param.register_hook(ShardingStage2._grad_hook)
10321030

10331031

10341032
class ShardingStage3(_ShardingStageBase):
10351033
"""
10361034
A builtin shard_fn for shard_optimizer interface, users can pass it to shard_optimizer to implement sharding optimization with stage 3.
10371035
10381036
Args:
1039-
mesh(paddle.distributed.ProcessMesh): The `ProcessMesh` object describes the Cartesian topology of the used processes.
1037+
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.
10401038
10411039
Examples:
10421040
.. code-block:: python
@@ -1069,30 +1067,33 @@ class ShardingStage3(_ShardingStageBase):
10691067
>>> # python -m paddle.distributed.launch --gpus=0,1 {test_case}.py
10701068
"""
10711069

1072-
def __init__(self, mesh):
1070+
def __init__(self, mesh=None):
10731071
super().__init__(mesh)
10741072

10751073
def _shard_parameter(self, param):
1076-
if param.is_dense():
1074+
if param.is_dense() and self._mesh is not None:
10771075
placements = []
10781076
for _ in range(len(self._mesh.shape)):
10791077
placements.append(dist.Replicate())
10801078
param._to_dist_(placements, self._mesh)
1081-
1082-
new_placements = get_placement_with_sharding(
1083-
param, self._sharding_mesh_axis
1084-
)
1085-
shard_param = dist.reshard(param, param.process_mesh, new_placements)
1086-
# change the holder of param to new shard_param
1087-
param.get_tensor()._share_data_with(shard_param.get_tensor())
1079+
if param.is_dist():
1080+
new_placements = get_placement_with_sharding(
1081+
param, self._sharding_mesh_axis
1082+
)
1083+
shard_param = dist.reshard(
1084+
param, param.process_mesh, new_placements
1085+
)
1086+
# change the holder of param to new shard_param
1087+
param.get_tensor()._share_data_with(shard_param.get_tensor())
10881088

10891089
def _unshard_parameter(self, param):
1090-
new_placements = param.placements
1091-
if isinstance(new_placements[self._sharding_mesh_axis], dist.Shard):
1092-
new_placements[self._sharding_mesh_axis] = dist.Replicate()
1090+
if param.is_dist():
1091+
new_placements = param.placements
1092+
if isinstance(new_placements[self._sharding_mesh_axis], dist.Shard):
1093+
new_placements[self._sharding_mesh_axis] = dist.Replicate()
10931094

1094-
new_param = dist.reshard(param, param.process_mesh, new_placements)
1095-
param.get_tensor()._share_data_with(new_param.get_tensor())
1095+
new_param = dist.reshard(param, param.process_mesh, new_placements)
1096+
param.get_tensor()._share_data_with(new_param.get_tensor())
10961097

10971098
def __call__(self, key, param, accumulator):
10981099
if param.is_dist():

0 commit comments

Comments
 (0)