-
Notifications
You must be signed in to change notification settings - Fork 786
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
Support nd sbp dim reduce #7230
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1243742
support_symmetric_cyclic_nd_sbp_boxing
clackhan dc9f49b
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow into …
clackhan b179fce
rename func
clackhan ec19236
minor fix
clackhan bc6d616
solve comment
clackhan 7d6b72a
minor fix
clackhan e215690
support_nd_sbp_dim_reduce
clackhan 01537b5
fix_typo
clackhan 5be37bb
add test case
clackhan 01232d1
fix bug
clackhan c9d4232
fix bug
clackhan d7c96a1
refine
clackhan af14288
fix dead loop error
clackhan c33989c
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow into …
clackhan d499760
Merge branch 'master' into support_nd_sbp_dim_reduce
oneflow-ci-bot 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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
Copyright 2020 The OneFlow 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 "oneflow/core/boxing/eager_boxing_interpreter_mgr.h" | ||
#include "oneflow/core/framework/nd_sbp.h" | ||
#include "oneflow/core/framework/device.h" | ||
#include "oneflow/core/functional/functional.h" | ||
#include "oneflow/core/graph/boxing/hierarchical_sub_task_graph_builder_impl.h" | ||
#include "oneflow/core/common/decorator.h" | ||
|
||
namespace oneflow { | ||
|
||
namespace { | ||
|
||
Maybe<std::tuple<Symbol<PlacedNdSbp>, Symbol<PlacedNdSbp>>> RawInOutPlacedNdSbpDimReduce( | ||
Symbol<PlacedNdSbp> in, Symbol<PlacedNdSbp> out) { | ||
// reduce hierarchy | ||
ParallelDesc reduced_in_placement = *in->placement(); | ||
ParallelDesc reduced_out_placement = *out->placement(); | ||
cfg::NdSbp reduced_in_nd_sbp; | ||
cfg::NdSbp reduced_out_nd_sbp; | ||
InOutParallelDimReduce(*in->placement(), *out->placement(), *in->nd_sbp(), *out->nd_sbp(), | ||
&reduced_in_placement, &reduced_out_placement, &reduced_in_nd_sbp, | ||
&reduced_out_nd_sbp); | ||
return std::make_tuple( | ||
JUST(PlacedNdSbp::New(SymbolOf(reduced_in_nd_sbp), SymbolOf(reduced_in_placement))), | ||
JUST(PlacedNdSbp::New(SymbolOf(reduced_out_nd_sbp), SymbolOf(reduced_out_placement)))); | ||
} | ||
|
||
constexpr auto* InOutPlacedNdSbpDimReduce = DECORATE(&RawInOutPlacedNdSbpDimReduce, ThreadLocal); | ||
|
||
Maybe<void> RawCheckParallelDimReduce(Symbol<PlacedNdSbp> in, Symbol<PlacedNdSbp> out, | ||
const Shape& logical_shape) { | ||
CHECK_OR_RETURN(in->nd_sbp()->sbp_parallel_size() > 1 || out->nd_sbp()->sbp_parallel_size() > 1); | ||
CHECK_EQ_OR_RETURN(in->placement()->device_tag(), out->placement()->device_tag()); | ||
Symbol<PlacedNdSbp> reduced_in; | ||
Symbol<PlacedNdSbp> reduced_out; | ||
std::tie(reduced_in, reduced_out) = *JUST(InOutPlacedNdSbpDimReduce(in, out)); | ||
|
||
if (reduced_in->nd_sbp()->sbp_parallel_size() == 1 | ||
&& reduced_out->nd_sbp()->sbp_parallel_size() == 1) { | ||
return Maybe<void>::Ok(); | ||
} | ||
if ((reduced_in->placement() != in->placement() || reduced_out->placement() != out->placement()) | ||
&& reduced_in->placement() == reduced_out->placement()) { | ||
return Maybe<void>::Ok(); | ||
} | ||
return Error::CheckFailedError(); | ||
} | ||
|
||
static constexpr auto* CheckParallelDimReduce = | ||
DECORATE(&RawCheckParallelDimReduce, ThreadLocalCopiable); | ||
|
||
} // namespace | ||
|
||
Maybe<one::Tensor> ParallelDimReduce(const std::shared_ptr<one::Tensor>& tensor, | ||
Symbol<PlacedNdSbp> in, Symbol<PlacedNdSbp> out) { | ||
const auto& tensor_nd_sbp = JUST(tensor->nd_sbp()); | ||
CHECK_OR_RETURN(tensor_nd_sbp == in->nd_sbp()); | ||
const auto& tensor_placement = JUST(tensor->parallel_desc()); | ||
CHECK_OR_RETURN(tensor_placement == in->placement()); | ||
|
||
Symbol<PlacedNdSbp> reduced_in; | ||
Symbol<PlacedNdSbp> reduced_out; | ||
std::tie(reduced_in, reduced_out) = *JUST(InOutPlacedNdSbpDimReduce(in, out)); | ||
|
||
const std::shared_ptr<one::Tensor>& local_tensor = JUST(tensor->cur_rank_phy_tensor()); | ||
|
||
std::shared_ptr<one::Tensor> reduced_in_tensor = JUST(one::functional::LocalToConsistent( | ||
local_tensor, reduced_in->placement(), *JUST(GetSbpList(reduced_in->nd_sbp())), | ||
*tensor->shape(), tensor->dtype())); | ||
|
||
const auto& boxing_interpreter = | ||
JUST(Global<EagerBoxingInterpreterManager>::Get()->GetEagerBoxingInterpreter( | ||
reduced_in->nd_sbp(), reduced_out->nd_sbp(), reduced_in->placement(), | ||
reduced_out->placement(), *tensor->shape())); | ||
std::shared_ptr<one::Tensor> reduced_out_tensor = JUST( | ||
boxing_interpreter->Interpret(reduced_in_tensor, reduced_in->nd_sbp(), reduced_out->nd_sbp(), | ||
reduced_in->placement(), reduced_out->placement())); | ||
|
||
const std::shared_ptr<one::Tensor>& reduced_out_local_tensor = | ||
JUST(reduced_out_tensor->cur_rank_phy_tensor()); | ||
|
||
return JUST(one::functional::LocalToConsistent(reduced_out_local_tensor, out->placement(), | ||
*JUST(GetSbpList(out->nd_sbp())), *tensor->shape(), | ||
tensor->dtype())); | ||
} | ||
|
||
COMMAND(RegisterBoxingFunction("nd-sbp-dim-reduce", CheckParallelDimReduce, &ParallelDimReduce)); | ||
|
||
} // namespace oneflow |
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
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.
优先选择能够通过规约降低维度的boxing 路径