-
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
[Reshard] Implement replicated to split with same placement #55552
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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 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 |
---|---|---|
@@ -1,10 +1,23 @@ | ||
proto_library(auto_parallel_proto SRCS auto_parallel.proto) | ||
|
||
set(DISTRIBUTED_SRCS "") | ||
|
||
if(WITH_DISTRIBUTE) | ||
list( | ||
APPEND | ||
DISTRIBUTED_SRCS | ||
dist_tensor.cc | ||
reshard_function.cc | ||
reshard_split_functor.cc | ||
reshard_utils.cc | ||
r_to_s_reshard_function.cc) | ||
endif() | ||
|
||
collect_srcs( | ||
core_srcs | ||
SRCS | ||
device_mesh.cc | ||
process_mesh.cc | ||
dist_attr.cc | ||
dist_mapper.cc | ||
dist_tensor.cc) | ||
${DISTRIBUTED_SRCS}) |
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
100 changes: 100 additions & 0 deletions
100
paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.cc
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,100 @@ | ||
// 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. | ||
|
||
#include "paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.h" | ||
|
||
#include "glog/logging.h" | ||
#include "paddle/phi/api/lib/kernel_dispatch.h" | ||
#include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" | ||
#include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" | ||
#include "paddle/phi/core/distributed/auto_parallel/reshard_split_functor.h" | ||
#include "paddle/phi/core/distributed/auto_parallel/reshard_utils.h" | ||
#include "paddle/phi/core/kernel_factory.h" | ||
|
||
namespace phi { | ||
namespace distributed { | ||
|
||
bool RToSReshardFunction::IsSuitable( | ||
const DistTensor& in, | ||
const std::shared_ptr<TensorDistAttr>& out_dist_attr) { | ||
bool flag = true; | ||
const auto& in_dist_attr = in.dist_attr(); | ||
|
||
const auto& in_dims_mapping = in_dist_attr->dims_mapping(); | ||
const auto& out_dims_mapping = out_dist_attr->dims_mapping(); | ||
|
||
flag &= IsDimsMappingReplicated(in_dims_mapping); | ||
flag &= IsDimsMappingShard(out_dims_mapping); | ||
|
||
const auto& in_process_mesh = in_dist_attr->process_mesh(); | ||
const auto& out_process_mesh = out_dist_attr->process_mesh(); | ||
|
||
flag &= (in_process_mesh.ndim() == 1); | ||
flag &= (out_process_mesh.ndim() == 1); | ||
flag &= (in_process_mesh == out_process_mesh); | ||
|
||
return flag; | ||
} | ||
|
||
std::shared_ptr<DistTensor> RToSReshardFunction::Eval( | ||
const phi::DeviceContext& dev_ctx, | ||
const DistTensor& in, | ||
const std::shared_ptr<TensorDistAttr>& out_dist_attr) { | ||
const auto& out_dims_mapping = out_dist_attr->dims_mapping(); | ||
const auto& out_process_mesh = out_dist_attr->process_mesh(); | ||
const DenseTensor& in_physical_tensor_cur_rank = in.value(); | ||
|
||
DenseTensor out_physical_tensor_cur_rank; | ||
|
||
std::map<int64_t, int64_t> split_axis_to_mesh_axis = | ||
GetSplitAxisWithDimsMapping(out_dims_mapping); | ||
std::vector<int64_t> coord_in_mesh = GetCurRankCoordInMesh(out_process_mesh); | ||
|
||
int64_t split_axis = split_axis_to_mesh_axis.begin()->first; | ||
int64_t mesh_axis = split_axis_to_mesh_axis.begin()->second; | ||
|
||
PADDLE_ENFORCE_LT( | ||
mesh_axis, | ||
out_process_mesh.ndim(), | ||
phi::errors::OutOfRange( | ||
"The mesh axis %lld exceed the size of process mesh %lld.", | ||
mesh_axis, | ||
out_process_mesh.ndim())); | ||
|
||
int64_t num_of_process = out_process_mesh.shape()[mesh_axis]; | ||
VLOG(3) << "RToSReshard: Tensor will be split on axis " << split_axis | ||
<< ". Split will use axis " << mesh_axis << " of process_mesh." | ||
<< " There will have " << num_of_process | ||
<< " process participate in."; | ||
|
||
// TODO(liyurui): Consider the tensor can not be balanced split, | ||
// for example, the shape of tensor is {6} but want to split it by 4 | ||
// process. | ||
IntArray sections(std::vector<int64_t>( | ||
num_of_process, in.dims()[split_axis] / num_of_process)); | ||
|
||
std::vector<DenseTensor> split_out_vec = ReshardSplitFunctor( | ||
dev_ctx, in_physical_tensor_cur_rank, sections, split_axis); | ||
|
||
VLOG(3) << "The current process will remain the idx " | ||
<< coord_in_mesh[mesh_axis] << " piece of tensor"; | ||
out_physical_tensor_cur_rank = split_out_vec[coord_in_mesh[mesh_axis]]; | ||
|
||
return std::make_shared<DistTensor>( | ||
std::make_shared<DenseTensor>(out_physical_tensor_cur_rank), | ||
out_dist_attr); | ||
} | ||
|
||
} // namespace distributed | ||
} // namespace phi |
38 changes: 38 additions & 0 deletions
38
paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.h
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,38 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include "paddle/phi/core/distributed/auto_parallel/reshard_function.h" | ||
|
||
namespace phi { | ||
namespace distributed { | ||
|
||
class RToSReshardFunction final : public ReshardFunction { | ||
public: | ||
RToSReshardFunction() = default; | ||
~RToSReshardFunction() = default; | ||
|
||
bool IsSuitable( | ||
const DistTensor& in, | ||
const std::shared_ptr<TensorDistAttr>& out_dist_attr) override; | ||
|
||
std::shared_ptr<DistTensor> Eval( | ||
const DeviceContext& dev_ctx, | ||
const DistTensor& in, | ||
const std::shared_ptr<TensorDistAttr>& out_dist_attr) override; | ||
}; | ||
|
||
} // namespace distributed | ||
} // namespace phi |
22 changes: 22 additions & 0 deletions
22
paddle/phi/core/distributed/auto_parallel/reshard_function.cc
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,22 @@ | ||
// 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. | ||
|
||
#include "paddle/phi/core/distributed/auto_parallel/reshard_function.h" | ||
#include "paddle/phi/core/device_context.h" | ||
#include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" | ||
#include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" | ||
|
||
namespace phi { | ||
namespace distributed {} // namespace distributed | ||
} // namespace phi |
45 changes: 45 additions & 0 deletions
45
paddle/phi/core/distributed/auto_parallel/reshard_function.h
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. | ||
|
||
#pragma once | ||
#include <memory> | ||
|
||
namespace phi { | ||
class DeviceContext; | ||
|
||
namespace distributed { | ||
namespace auto_parallel { | ||
class TensorDistAttr; | ||
} // namespace auto_parallel | ||
|
||
class DistTensor; | ||
using auto_parallel::TensorDistAttr; | ||
|
||
class ReshardFunction { | ||
public: | ||
ReshardFunction() = default; | ||
virtual ~ReshardFunction() = default; | ||
|
||
virtual bool IsSuitable( | ||
const DistTensor& in, | ||
const std::shared_ptr<TensorDistAttr>& out_dist_attr) = 0; | ||
|
||
virtual std::shared_ptr<DistTensor> Eval( | ||
const DeviceContext& dev_ctx, | ||
const DistTensor& in, | ||
const std::shared_ptr<TensorDistAttr>& out_dist_attr) = 0; | ||
}; | ||
|
||
} // namespace distributed | ||
} // namespace phi |
78 changes: 78 additions & 0 deletions
78
paddle/phi/core/distributed/auto_parallel/reshard_split_functor.cc
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,78 @@ | ||
// 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. | ||
|
||
#include "paddle/phi/core/distributed/auto_parallel/reshard_split_functor.h" | ||
LiYuRio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#include "paddle/phi/backends/all_context.h" | ||
#include "paddle/phi/core/dense_tensor.h" | ||
#include "paddle/phi/core/device_context.h" | ||
#include "paddle/phi/core/visit_type.h" | ||
#include "paddle/phi/kernels/split_kernel.h" | ||
|
||
namespace phi { | ||
namespace distributed { | ||
|
||
std::vector<DenseTensor> ReshardSplitFunctor(const DeviceContext& dev_ctx, | ||
const DenseTensor& input, | ||
const IntArray& sections, | ||
int64_t axis) { | ||
size_t out_number = sections.size(); | ||
std::vector<DenseTensor> result(out_number); | ||
|
||
std::vector<MetaTensor> out_meta; | ||
std::vector<MetaTensor*> out_meta_ptr; | ||
|
||
out_meta.reserve(out_number); | ||
out_meta_ptr.reserve(out_number); | ||
for (size_t i = 0; i < out_number; ++i) { | ||
out_meta.emplace_back(result[i]); | ||
out_meta_ptr.emplace_back(&out_meta.back()); | ||
} | ||
SplitInferMeta(phi::MetaTensor(input), sections, axis, out_meta_ptr); | ||
|
||
std::vector<DenseTensor*> outs; | ||
for (size_t i = 0; i < out_number; ++i) { | ||
outs.emplace_back(&result[i]); | ||
} | ||
|
||
if (phi::CPUContext::classof(&dev_ctx)) { | ||
PD_VISIT_ALL_TYPES(input.dtype(), "SplitKernel", ([&] { | ||
SplitKernel<data_t>( | ||
static_cast<const CPUContext&>(dev_ctx), | ||
input, | ||
sections, | ||
axis, | ||
outs); | ||
})); | ||
return result; | ||
} | ||
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) | ||
if (phi::GPUContext::classof(&dev_ctx)) { | ||
PD_VISIT_ALL_TYPES(input.dtype(), "SplitKernel", ([&] { | ||
SplitKernel<data_t>( | ||
static_cast<const GPUContext&>(dev_ctx), | ||
input, | ||
sections, | ||
axis, | ||
outs); | ||
})); | ||
return result; | ||
} | ||
#endif | ||
PADDLE_THROW(phi::errors::Unimplemented( | ||
"The split in reshard only supported on CPU and GPU for now.")); | ||
} | ||
|
||
} // namespace distributed | ||
} // namespace phi |
32 changes: 32 additions & 0 deletions
32
paddle/phi/core/distributed/auto_parallel/reshard_split_functor.h
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,32 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include <map> | ||
#include <vector> | ||
#include "paddle/phi/common/int_array.h" | ||
|
||
namespace phi { | ||
class DeviceContext; | ||
class DenseTensor; | ||
|
||
namespace distributed { | ||
std::vector<DenseTensor> ReshardSplitFunctor(const DeviceContext& dev_ctx, | ||
const DenseTensor& input, | ||
const IntArray& sections, | ||
int64_t axis); | ||
|
||
} // namespace distributed | ||
} // namespace phi |
Oops, something went wrong.
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.
What does "Eval" mean?