-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[AutoParallel] Add paddle.distributed.reshard python API. #57293
Merged
GhostScreaming
merged 8 commits into
PaddlePaddle:develop
from
GhostScreaming:add_reshard_api
Sep 19, 2023
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2ef88d6
Add paddle.distributed.reshard API. It supports reshard for DistTensor.
GhostScreaming 50d2740
Polish code with review comments.
GhostScreaming c7973cb
Fix problem of in_dynamic_mode
GhostScreaming 44b8c93
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
GhostScreaming 85a0220
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
GhostScreaming c09c0a8
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
GhostScreaming 32eca46
Fix some problems according to review comments.
GhostScreaming 5c33d9e
Set test_reshard_api as multi-cards testcase. And set its timeout.
GhostScreaming 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 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 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 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 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 |
---|---|---|
|
@@ -186,6 +186,8 @@ if(WITH_DISTRIBUTE AND WITH_GPU) | |
py_test_modules(test_engine_save_load MODULES test_engine_save_load) | ||
py_test_modules(test_rule_based_tuner MODULES test_rule_based_tuner) | ||
py_test_modules(test_dist_tensor MODULES test_dist_tensor) | ||
py_test_modules(test_reshard_api MODULES test_reshard_api) | ||
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. 这个CMakeLists里面对不同性质单测做了划分,reshard是个多卡单测,看看是不是和其他reshard_x_to_x单测放到一起,最好也加个超时控制,多卡单测容易超时 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. Done, thx. |
||
py_test_modules(test_api_dist_branch MODULES test_api_dist_branch) | ||
py_test_modules(test_shard_tensor_api MODULES test_shard_tensor_api) | ||
py_test_modules(test_cost_interface MODULES test_cost_interface) | ||
# End of unittests WITH single card WITHOUT timeout | ||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# Copyright (c) 2023 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. | ||
|
||
import os | ||
|
||
import numpy as np | ||
|
||
import paddle | ||
import paddle.distributed as dist | ||
|
||
|
||
class TestReshardAPI: | ||
def __init__(self): | ||
self._shape = eval(os.getenv("shape")) | ||
self._dtype = os.getenv("dtype") | ||
self._seeds = eval(os.getenv("seeds")) | ||
self._backend = os.getenv("backend") | ||
self._shard = eval(os.getenv("shard")) | ||
self._mesh = dist.ProcessMesh([0, 1], dim_names=["x"]) | ||
|
||
def run_test_cases(self): | ||
if self._backend == "cpu": | ||
paddle.set_device("cpu") | ||
self.test_case_p_to_r() | ||
|
||
def test_case_p_to_r(self): | ||
a = paddle.ones(self._shape) | ||
in_shard_specs = [None for i in range(len(self._shape))] | ||
out_shard_specs = [None for i in range(len(self._shape))] | ||
dist_attr = dist.DistAttr( | ||
mesh=self._mesh, sharding_specs=in_shard_specs | ||
) | ||
dist_attr._set_partial_dims([0]) | ||
out_dist_attr = dist.DistAttr( | ||
mesh=self._mesh, sharding_specs=out_shard_specs | ||
) | ||
|
||
input_tensor = dist.shard_tensor(a, dist_attr=dist_attr) | ||
output_tensor = dist.reshard(input_tensor, dist_attr=out_dist_attr) | ||
|
||
input_tensor = dist.shard_tensor(a, dist_attr=dist_attr) | ||
assert np.equal(output_tensor.shape, input_tensor.shape).all() | ||
np.testing.assert_equal(output_tensor._local_value().numpy(), a.numpy()) | ||
|
||
def test_case_r_to_s(self): | ||
a = paddle.ones(self._shape) | ||
in_shard_specs = [None for i in range(len(self._shape))] | ||
out_shard_specs = [None for i in range(len(self._shape))] | ||
out_shard_specs[self._shard] = "x" | ||
dist_attr = dist.DistAttr( | ||
mesh=self._mesh, sharding_specs=in_shard_specs | ||
) | ||
out_dist_attr = dist.DistAttr( | ||
mesh=self._mesh, sharding_specs=out_shard_specs | ||
) | ||
|
||
input_tensor = dist.shard_tensor(a, dist_attr=dist_attr) | ||
output_tensor = dist.reshard(input_tensor, dist_attr=out_dist_attr) | ||
|
||
out_shape = list(self._shape) | ||
if out_shape[self._shard] % 2 == 0: | ||
out_shape[self._shard] = out_shape[self._shard] // 2 | ||
np.testing.assert_equal(output_tensor.numpy(), input_tensor.numpy()) | ||
else: | ||
out_shape[self._shard] = ( | ||
out_shape[self._shard] // 2 | ||
if dist.get_rank() == 1 | ||
else out_shape[self._shard] // 2 + 1 | ||
) | ||
|
||
assert np.equal(output_tensor.shape, input_tensor.shape).all() | ||
assert np.equal(output_tensor._local_shape, out_shape).all() | ||
|
||
|
||
if __name__ == '__main__': | ||
TestReshardAPI().run_test_cases() |
This file contains 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright (c) 2023 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. | ||
|
||
import unittest | ||
|
||
import collective.test_communication_api_base as test_base | ||
|
||
|
||
class TestReshardAPI(test_base.CommunicationTestDistBase): | ||
def setUp(self): | ||
super().setUp(num_of_devices=2, timeout=120) | ||
self._default_envs = { | ||
"shape": "(10, 20)", | ||
"dtype": "float32", | ||
"seeds": str(self._seeds), | ||
"shard": "0", | ||
} | ||
self._changeable_envs = { | ||
"backend": ["cpu", "gpu"], | ||
} | ||
|
||
def test_reshard_api(self): | ||
envs_list = test_base.gen_product_envs_list( | ||
self._default_envs, self._changeable_envs | ||
) | ||
for envs in envs_list: | ||
self.run_test_case( | ||
"reshard_api.py", | ||
user_defined_envs=envs, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.
这里是不是还需要将reshard加到all list中
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.
Done, thx.