You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

parallel_pass.cc 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Copyright 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. #include "tools/optimizer/parallel/parallel_pass.h"
  17. #include "include/errorcode.h"
  18. #include "ir/tensor.h"
  19. #include "tools/optimizer/parallel/operator_info_register.h"
  20. #include "ops/fusion/conv2d_fusion.h"
  21. namespace mindspore {
  22. namespace opt {
  23. namespace {
  24. constexpr auto kAnfPrimitiveIndex = 0;
  25. }
  26. bool ParallelPass::IsParallelCareNode(const AnfNodePtr &node) {
  27. auto c_node = node->cast<CNodePtr>();
  28. auto prim = GetValueNode<PrimitivePtr>(c_node->input(kAnfPrimitiveIndex));
  29. // depth_wise can not be splited in conv_info, we deal with in depthwise_conv_info
  30. is_depth_wise_ = prim->GetAttr(ops::kIsDepthWise) != nullptr && GetValue<bool>(prim->GetAttr(ops::kIsDepthWise));
  31. type_name_.clear();
  32. return std::any_of(kParallelOpNames.begin(), kParallelOpNames.end(), [this, &node](auto &prim_item) {
  33. if (CheckPrimitiveType(node, prim_item.first.first) && is_depth_wise_ == prim_item.first.second) {
  34. type_name_ = prim_item.second;
  35. }
  36. return !type_name_.empty();
  37. });
  38. }
  39. bool ParallelPass::SetParallelOpName(const AnfNodePtr &node, std::string *parallel_name) {
  40. if (!utils::isa<CNode>(node)) {
  41. return false;
  42. }
  43. auto cnode = node->cast<CNodePtr>();
  44. std::string cnode_name = cnode->fullname_with_scope();
  45. if (cnode_name.find(PARALLEL_NAME_SUFFIX) != std::string::npos) {
  46. MS_LOG(DEBUG) << " : Skip splited cnode " << cnode_name;
  47. return false;
  48. }
  49. // find operator name first, then operator type name.
  50. if (split_strategys_.find(*parallel_name) == split_strategys_.end()) {
  51. *parallel_name = type_name_;
  52. }
  53. MS_LOG(DEBUG) << " : Reached a parallel care node: " << cnode_name;
  54. if (split_strategys_.find(*parallel_name) == split_strategys_.end()) {
  55. MS_LOG(DEBUG) << *parallel_name << " : No split strategy for the current CNode.";
  56. return false;
  57. }
  58. cnode->set_fullname_with_scope(cnode_name + PARALLEL_NAME_SUFFIX);
  59. return true;
  60. }
  61. OperatorInfoPtr ParallelPass::CreateParallelOperator(const AnfNodePtr &node, const std::string &scope_name,
  62. const std::string &parallel_op_name) {
  63. // foreach kernel_list && data_type
  64. auto cnode = node->cast<CNodePtr>();
  65. auto node_prim = cnode->input(kParallelPrimitiveIndex);
  66. auto prim = GetValueNode<PrimitivePtr>(node_prim);
  67. for (const auto &schmea_id : kParallelSchemaId) {
  68. if (!CheckPrimitiveType(node, schmea_id.first)) {
  69. continue;
  70. }
  71. auto split_key_pair = kParallelSchemaId.find(schmea_id.first);
  72. auto split_schema_id = split_key_pair->second.first;
  73. auto split_type_id = split_key_pair->second.second;
  74. SplitOpKey op_key = SplitOpKey(split_schema_id, split_type_id, is_depth_wise_);
  75. auto op_create_func = OperatorInfoFactory::GeInstance()->FindOperatorInfo(op_key);
  76. if (op_create_func == nullptr) {
  77. return nullptr;
  78. }
  79. OperatorInfoPtr op = op_create_func(scope_name, split_strategys_[parallel_op_name]);
  80. return op;
  81. }
  82. return nullptr;
  83. }
  84. AnfNodePtr ParallelPass::Run(const FuncGraphPtr &func_graph, const AnfNodePtr &node) {
  85. if (CheckIfFuncGraphIsNull(func_graph) != RET_OK || CheckIfAnfNodeIsNull(node) != RET_OK) {
  86. return node;
  87. }
  88. if (!utils::isa<CNode>(node)) {
  89. return node;
  90. }
  91. if (!IsParallelCareNode(node)) {
  92. return node;
  93. }
  94. // if current conv2d node has two output nodes ,we do not split it;
  95. auto manager = func_graph->manager();
  96. auto iter = manager->node_users().find(node);
  97. if (iter == manager->node_users().end()) {
  98. MS_LOG(ERROR) << "node : " << node->fullname_with_scope() << "has no output";
  99. }
  100. auto output_info_list = iter->second;
  101. if (output_info_list.size() > kDefaultBatch) {
  102. return node;
  103. }
  104. auto cnode = node->cast<CNodePtr>();
  105. if (CheckIfCNodeIsNull(cnode) != RET_OK) {
  106. return node;
  107. }
  108. std::string parallel_op_name = cnode->fullname_with_scope();
  109. if (!SetParallelOpName(node, &parallel_op_name)) {
  110. return node;
  111. }
  112. std::string cnode_name = cnode->fullname_with_scope();
  113. OperatorInfoPtr parallel_operator = CreateParallelOperator(node, cnode_name, parallel_op_name);
  114. if (parallel_operator == nullptr) {
  115. MS_LOG(ERROR) << "Failure: Create " << parallel_op_name << " OperatorInstance failed";
  116. return node;
  117. }
  118. parallel_operator->Init(func_graph, cnode, fmk_type_);
  119. if (parallel_operator->DoSplit() == RET_ERROR) {
  120. MS_LOG(ERROR) << "Failure: operator " << parallel_op_name << " init failed";
  121. return node;
  122. }
  123. return parallel_operator->replace_op();
  124. }
  125. } // namespace opt
  126. } // namespace mindspore