1 /**
2 * Copyright 2020-2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <set>
18 #include <string>
19
20 #include "utils/parallel_node_check.h"
21 #include "base/core_ops.h"
22
23 namespace mindspore {
24 // clang-format off
25 #ifndef ENABLE_SECURITY
26 static const std::set<std::string> PARALLEL_BLACK_LIST_ = {prim::kTupleGetItem, "J", "list_getitem",
27 "array_getitem", "tuple_setitem", "Depend", "list_setitem", "array_setitem", "dict_getitem",
28 "list_append", "list_map", "list_reduce", "tuple_reversed", "tile_shape", "tuple_div", "tuple_to_array",
29 "make_dict", "make_slice", "make_record", "string_equal", "VirtualLoss", "Return", "env_getitem",
30 "identity", "partial", "env_setitem", "env_getitem", "env_add", "MakeRefKey", "make_ref", "get_ref_key",
31 "get_ref_value", "get_ref_origin", "dot", "im2col", "col2im", "im2col_v1", "state_setitem", "ScalarSummary",
32 "ImageSummary", "TensorSummary", "Debug", "HistogramSummary", "col2im_v1", "resolve", "BroadcastGradientArgs",
33 "InvertPermutation", "DropoutGenMask", "embed", "create_instance", "RefToEmbed",
34 "stop_gradient", "UpdateState", "Load", "Switch", "Print"};
35 #else
36 static const std::set<std::string> PARALLEL_BLACK_LIST_ = {prim::kTupleGetItem, "J", "list_getitem",
37 "array_getitem", "tuple_setitem", "Depend", "list_setitem", "array_setitem", "dict_getitem",
38 "list_append", "list_map", "list_reduce", "tuple_reversed", "tile_shape", "tuple_div", "tuple_to_array",
39 "make_dict", "make_slice", "make_record", "string_equal", "VirtualLoss", "Return", "env_getitem",
40 "identity", "partial", "env_setitem", "env_getitem", "env_add", "MakeRefKey", "make_ref", "get_ref_key",
41 "get_ref_value", "get_ref_origin", "dot", "im2col", "col2im", "im2col_v1", "state_setitem", "Debug", "col2im_v1",
42 "resolve", "BroadcastGradientArgs", "InvertPermutation", "DropoutGenMask", "embed", "create_instance", "RefToEmbed",
43 "stop_gradient", "UpdateState", "Load", "Switch", "Print"};
44 #endif
45 static const std::set<PrimitivePtr> ALLGATHER_NODE_LIST_ = {prim::kPrimAllGather, prim::kPrimMiniStepAllGather,
46 prim::kPrimMicroStepAllGather};
47 static const std::set<PrimitivePtr> TRIVIAL_NODE_LIST_ = {prim::kPrimCast, prim::kPrimDepend};
48 // clang-format on
49
IsInParallelBlackList(const PrimitivePtr & prim)50 bool IsInParallelBlackList(const PrimitivePtr &prim) {
51 MS_EXCEPTION_IF_NULL(prim);
52 return (PARALLEL_BLACK_LIST_.find(prim->name()) != PARALLEL_BLACK_LIST_.end());
53 }
54
IsInAllGatherNodeList(const CNodePtr & cnode)55 bool IsInAllGatherNodeList(const CNodePtr &cnode) {
56 for (auto &value : ALLGATHER_NODE_LIST_) {
57 if (IsPrimitiveCNode(cnode, value)) {
58 return true;
59 }
60 }
61 return false;
62 }
63
IsInTrivialNodeList(const CNodePtr & cnode)64 bool IsInTrivialNodeList(const CNodePtr &cnode) {
65 for (auto &value : TRIVIAL_NODE_LIST_) {
66 if (IsPrimitiveCNode(cnode, value)) {
67 return true;
68 }
69 }
70 return false;
71 }
72
IsParallelConsiderCNode(const CNodePtr & cnode)73 bool IsParallelConsiderCNode(const CNodePtr &cnode) {
74 if (cnode == nullptr || cnode->size() == 0) {
75 return false;
76 }
77 const auto &prim_node = cnode->input(0)->cast<ValueNodePtr>();
78 if (prim_node == nullptr) {
79 return false;
80 }
81 const auto &prim = prim_node->value()->cast<PrimitivePtr>();
82 if (prim == nullptr) {
83 return false;
84 }
85 return !IsInParallelBlackList(prim);
86 }
87 } // namespace mindspore
88