-
Notifications
You must be signed in to change notification settings - Fork 802
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
Supplement ND SBP signatures for reshape op #9858
Merged
Merged
Changes from 31 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
663cf30
EnumerateNdSbpSignatures
leaves-zwx dccb762
ReshapeOp::EnumerateNdSbpSignatures
leaves-zwx 7cd2dc1
fix algo
leaves-zwx 1eed7aa
fix
leaves-zwx 3dc723b
cpp test
leaves-zwx cbf7261
fix
leaves-zwx a440e8d
rename
leaves-zwx 6998d52
refine UserOp::GetNdSbpSignatureList
leaves-zwx 94870a6
ReshapeOp::EnumerateNdSbpSignatures
leaves-zwx bff5183
py test
leaves-zwx 2db180a
revert changes
leaves-zwx 84b3acf
new ReshapeOp::EnumerateNdSbpSignatures
leaves-zwx ad4a227
add test case
leaves-zwx 7c22e1a
refine algorithm
leaves-zwx c51364a
update test and fix bug
leaves-zwx db658eb
rm comment
leaves-zwx 278f577
rm redundant condition
leaves-zwx c500114
DeduplicateNdSbpSignatureList
leaves-zwx a4ddd55
refine GenRankMeshSubset
leaves-zwx 508cc6f
add comments
leaves-zwx e5451c2
suppress warning
leaves-zwx b3c9ca9
Merge branch 'master' into enlarge_reshape_sbp
leaves-zwx 66d3923
suppress warning
leaves-zwx 5e4aebc
suppress warning
leaves-zwx 670eca3
refine sort algorithm
leaves-zwx 351083e
refine sort algorithm and test
leaves-zwx e6b579b
rm calling FilterNdSbpIn2OutSignatures in EnumerateNdSbpSignatures
leaves-zwx ca9eb24
change example
leaves-zwx abe42db
Update oneflow/user/ops/reshape_user_op_util.cpp
leaves-zwx 4836822
Merge branch 'master' into enlarge_reshape_sbp
leaves-zwx 7bacbcf
refine sort algorithm
leaves-zwx f578fcf
Merge branch 'master' into enlarge_reshape_sbp
leaves-zwx d3cf09d
change sort order
leaves-zwx 13dd1be
use FilterNdSbpByLogicalShape
leaves-zwx 70ae77e
Merge branch 'master' into enlarge_reshape_sbp
leaves-zwx 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 |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
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/framework/sbp_infer_util.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace oneflow { | ||
namespace test { | ||
|
||
namespace { | ||
|
||
bool ParseNdSbpSignatureFromString(const std::string& nd_sbp_signature_str, | ||
leaves-zwx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
NdSbpSignature& nd_sbp_signature) { | ||
auto* bn2nd_sbp = nd_sbp_signature.mutable_bn_in_op2nd_sbp(); | ||
std::string arg_name = "in"; | ||
bool meet_nd_sbp_group = false; | ||
bool meet_split = false; | ||
int nd_sbp_group_id = 0; | ||
std::vector<std::string> nd_sbp_str_group; | ||
size_t pos = 0; | ||
while (pos < nd_sbp_signature_str.size()) { | ||
const char& c = nd_sbp_signature_str[pos]; | ||
pos++; | ||
if (c == ' ') { | ||
continue; | ||
} else if (c == '(') { | ||
if (!meet_nd_sbp_group) { | ||
// enter a nd-sbp group | ||
meet_nd_sbp_group = true; | ||
nd_sbp_str_group.emplace_back(); | ||
continue; | ||
} else { | ||
// meet left parentheses of S(x) | ||
meet_split = true; | ||
} | ||
} else if (c == ')') { | ||
if (meet_split) { | ||
// meet right parentheses of S(x) | ||
meet_split = false; | ||
} else if (meet_nd_sbp_group) { | ||
// leave a nd-sbp group | ||
meet_nd_sbp_group = false; | ||
std::string bn = arg_name + "_" + std::to_string(nd_sbp_group_id); | ||
if (!ParseNdSbpFromStringList(nd_sbp_str_group, &(*bn2nd_sbp)[bn])) { return false; } | ||
nd_sbp_str_group.clear(); | ||
continue; | ||
} else { | ||
return false; | ||
} | ||
} else if (c == ',') { | ||
if (meet_nd_sbp_group) { | ||
nd_sbp_str_group.emplace_back(); | ||
} else { | ||
nd_sbp_group_id += 1; | ||
} | ||
continue; | ||
} else if (c == '-') { | ||
if (pos < nd_sbp_signature_str.size() && nd_sbp_signature_str[pos] == '>') { | ||
// in args parsing has finished, parse out args | ||
arg_name = "out"; | ||
nd_sbp_group_id = 0; | ||
// skip '>' in substr '->' | ||
pos++; | ||
continue; | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
// do nothing | ||
} | ||
nd_sbp_str_group.back() += c; | ||
} | ||
return true; | ||
} | ||
|
||
void TestDeduplicateNdSbpSignature(const std::vector<std::string>& nd_sbp_signature_str_list, | ||
const std::vector<std::string>& bns) { | ||
std::vector<NdSbpSignature> nd_sbp_sig_list; | ||
nd_sbp_sig_list.reserve(nd_sbp_signature_str_list.size()); | ||
for (const auto& nd_sbp_signature_str : nd_sbp_signature_str_list) { | ||
nd_sbp_sig_list.emplace_back(); | ||
ASSERT_TRUE(ParseNdSbpSignatureFromString(nd_sbp_signature_str, nd_sbp_sig_list.back())); | ||
} | ||
std::random_device rd; | ||
std::mt19937 gen(rd()); | ||
std::shuffle(nd_sbp_sig_list.begin(), nd_sbp_sig_list.end(), gen); | ||
nd_sbp_sig_list.reserve(nd_sbp_sig_list.size() + nd_sbp_sig_list.size() / 2); | ||
std::copy_n(nd_sbp_sig_list.begin(), nd_sbp_sig_list.size() / 2, | ||
std::back_inserter(nd_sbp_sig_list)); | ||
std::shuffle(nd_sbp_sig_list.begin(), nd_sbp_sig_list.end(), gen); | ||
DeduplicateNdSbpSignatureList(&nd_sbp_sig_list, bns); | ||
} | ||
|
||
} // namespace | ||
|
||
TEST(SbpInferUtil, DeduplicateNdSbpSignatureList) { | ||
TestDeduplicateNdSbpSignature( | ||
{ | ||
"(S(0), S(0)) -> (S(0), S(0))", | ||
"(S(0), S(1)) -> (S(0), S(1))", | ||
"(S(0), S(3)) -> (S(0), S(2))", | ||
"(S(0), B) -> (S(0), B)", | ||
"(S(0), P) -> (S(0), P)", | ||
"(S(1), S(0)) -> (S(1), S(0))", | ||
"(S(1), S(1)) -> (S(1), S(1))", | ||
"(S(1), S(3)) -> (S(1), S(2))", | ||
"(S(1), B) -> (S(1), B)", | ||
"(S(1), P) -> (S(1), P)", | ||
"(S(3), S(0)) -> (S(2), S(0))", | ||
"(S(3), S(1)) -> (S(2), S(1))", | ||
"(S(3), S(3)) -> (S(2), S(2))", | ||
"(S(3), B) -> (S(2), B)", | ||
"(S(3), P) -> (S(2), P)", | ||
"(B, S(0)) -> (B, S(0))", | ||
"(B, S(1)) -> (B, S(1))", | ||
"(B, S(3)) -> (B, S(2))", | ||
"(B, B) -> (B, B)", | ||
"(B, P) -> (B, P)", | ||
"(P, S(0)) -> (P, S(0))", | ||
"(P, S(1)) -> (P, S(1))", | ||
"(P, S(3)) -> (P, S(2))", | ||
"(P, B) -> (P, B)", | ||
"(P, P) -> (P, P)", | ||
}, | ||
{"in_0", "out_0"}); | ||
} | ||
|
||
} // namespace test | ||
} // 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
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
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
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.
别的地方没什么问题了。
这里我想了一下,上次我们开会时是讨论到说用97作为不同blob的进位对吧,如果说NdSbp的数字不超过96,是不会有任何风险的。
那现在把B映射到9,P映射到10,而进位是11,这样(B, B)就会是9*11+9 = 108,是超过了97的。
而B是出现很频繁的SBP,也就是风险会出现得频繁一些。
但是如果把B映射到1,P映射到2,Si -> i+3,这样要超过96起码是 88+9,也就是 (S5, S6),(S5, S7)或者 (S6, 任意SBP) 才有可能有风险。
实际中S5少见,更不用说 (S5, S6)了,甚至 (S5, S5) 都是没有问题的。要出问题,至少有一个S6或者 S7,也就是张量起码要有7维。
所以把B,P映射的数字前调能够很有效地避免大部分的风险。(当然即使有风险也不一定会出问题,素数能有效地规避掉一些,但是如果能够避免大部分的风险,还是避免的好)