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.

base_mds_kernel.cc 7.2 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "./base_mds_kernel.h"
  17. namespace ge {
  18. namespace mds_cut_pass {
  19. shared_ptr<DeploySchedulerKernel> GetKernelByType(const NodePtr &node) {
  20. if (node == nullptr) {
  21. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  22. GELOGE(FAILED, "[Check][Param] parameter node is nullptr.");
  23. return nullptr;
  24. }
  25. KernelFactory &factory = KernelFactory::Instance();
  26. string type = node->GetType();
  27. if (type == FRAMEWORKOP) {
  28. if (!ge::AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, type)) {
  29. REPORT_CALL_ERROR("E19999", "Get Attr:%s from op:%s(%s) failed",
  30. ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE.c_str(),
  31. node->GetName().c_str(), node->GetType().c_str());
  32. return nullptr;
  33. }
  34. }
  35. return factory.Create(type);
  36. }
  37. }
  38. shared_ptr<DeploySchedulerKernel> DeploySchedulerKernel::Instance() {
  39. static const std::shared_ptr<DeploySchedulerKernel> instance_ptr =
  40. shared_ptr<DeploySchedulerKernel>(new(std::nothrow) DeploySchedulerKernel());
  41. return instance_ptr;
  42. }
  43. Status DeploySchedulerKernel::CutN(const ge::NodePtr &node) {
  44. GE_CHECK_NOTNULL(node);
  45. auto op_desc = node->GetOpDesc();
  46. GE_CHECK_NOTNULL(op_desc);
  47. for (auto &in_anchor : node->GetAllInDataAnchors()) {
  48. GE_CHECK_NOTNULL(in_anchor);
  49. auto src_anchor = in_anchor->GetPeerOutAnchor();
  50. if (src_anchor == nullptr) {
  51. continue;
  52. }
  53. auto tensor_desc = op_desc->MutableInputDesc(in_anchor->GetIdx());
  54. auto src_node = src_anchor->GetOwnerNode();
  55. GE_CHECK_NOTNULL(src_node);
  56. auto src_op_desc = src_node->GetOpDesc();
  57. auto src_tensor_desc = src_op_desc->MutableOutputDesc(src_anchor->GetIdx());
  58. GE_CHECK_NOTNULL(src_tensor_desc);
  59. // peer out shape is cutted already
  60. if (MdsUtils::IsDistributedDeploySupported(src_tensor_desc, kCutN)) {
  61. if (MdsUtils::IsDistributedDeploySupported(tensor_desc, kCutN)) {
  62. tensor_desc->SetShape(src_tensor_desc->GetShape());
  63. } else {
  64. MDS_REQUIRE_SUCCESS(MdsUtils::DataGather(src_anchor, in_anchor),
  65. "[CutN] failed to gather between node[%s][%d] to node[%s][%d]",
  66. src_op_desc->GetName().c_str(),
  67. src_anchor->GetIdx(),
  68. op_desc->GetName().c_str(),
  69. in_anchor->GetIdx());
  70. }
  71. } else {
  72. if (MdsUtils::IsDistributedDeploySupported(tensor_desc, kCutN)) {
  73. MDS_REQUIRE_SUCCESS(MdsUtils::DataSlice(src_anchor, in_anchor, input_node_),
  74. "[CutN] failed to slice between node[%s][%d] to node[%s][%d]",
  75. src_op_desc->GetName().c_str(),
  76. src_anchor->GetIdx(),
  77. op_desc->GetName().c_str(),
  78. in_anchor->GetIdx());
  79. } else {
  80. tensor_desc->SetShape(src_tensor_desc->GetShape());
  81. }
  82. }
  83. // insert hcomallreduce for cutn
  84. bool is_grad_compute_node = false;
  85. if (ge::AttrUtils::GetBool(src_node->GetOpDesc(), ATTR_NAME_GRADIENT_NODE, is_grad_compute_node)
  86. && is_grad_compute_node) {
  87. MDS_REQUIRE_SUCCESS(MdsUtils::DataReduce(src_anchor, in_anchor),
  88. "[CutN] failed to reduce between node[%s][%d] to node[%s][%d]",
  89. src_op_desc->GetName().c_str(),
  90. src_anchor->GetIdx(),
  91. op_desc->GetName().c_str(),
  92. in_anchor->GetIdx());
  93. }
  94. }
  95. // call infer shape, update output shape
  96. MDS_REQUIRE_SUCCESS(node->InferShapeAndType(), "[CutN] %s call infershape failed", node->GetName().c_str());
  97. return SUCCESS;
  98. }
  99. Status DeploySchedulerKernel::CutH(const ge::NodePtr &node) {
  100. GE_CHECK_NOTNULL(node);
  101. auto op_desc = node->GetOpDesc();
  102. GE_CHECK_NOTNULL(op_desc);
  103. for (auto &in_anchor : node->GetAllInDataAnchors()) {
  104. GE_CHECK_NOTNULL(in_anchor);
  105. auto src_anchor = in_anchor->GetPeerOutAnchor();
  106. if (src_anchor == nullptr) {
  107. continue;
  108. }
  109. auto tensor_desc = op_desc->MutableInputDesc(in_anchor->GetIdx());
  110. auto src_node = src_anchor->GetOwnerNode();
  111. GE_CHECK_NOTNULL(src_node);
  112. auto src_op_desc = src_node->GetOpDesc();
  113. auto src_tensor_desc = src_op_desc->MutableOutputDesc(src_anchor->GetIdx());
  114. GE_CHECK_NOTNULL(src_tensor_desc);
  115. // peer out shape is cutted already
  116. if (MdsUtils::IsDistributedDeploySupported(src_tensor_desc, kCutH)) {
  117. if (MdsUtils::IsDistributedDeploySupported(tensor_desc, kCutH)) {
  118. MDS_REQUIRE_SUCCESS(HaloExchangeProcess(node, in_anchor->GetIdx()),
  119. "[CutH] failed to do overlap between node[%s][%d] to node[%s][%d]",
  120. src_op_desc->GetName().c_str(),
  121. src_anchor->GetIdx(),
  122. op_desc->GetName().c_str(),
  123. in_anchor->GetIdx());
  124. } else {
  125. MDS_REQUIRE_SUCCESS(MdsUtils::DataGather(src_anchor, in_anchor),
  126. "[CutH] failed to gather between node[%s][%d] to node[%s][%d]",
  127. src_op_desc->GetName().c_str(),
  128. src_anchor->GetIdx(),
  129. op_desc->GetName().c_str(),
  130. in_anchor->GetIdx());
  131. }
  132. } else {
  133. if (MdsUtils::IsDistributedDeploySupported(tensor_desc, kCutH)) {
  134. MDS_REQUIRE_SUCCESS(MdsUtils::DataSlice(src_anchor, in_anchor, input_node_),
  135. "[CutH] failed to slice between node[%s][%d] to node[%s][%d]",
  136. src_op_desc->GetName().c_str(),
  137. src_anchor->GetIdx(),
  138. op_desc->GetName().c_str(),
  139. in_anchor->GetIdx());
  140. } else {
  141. MDS_REQUIRE_SUCCESS(HaloExchangeProcess(node, in_anchor->GetIdx(), true),
  142. "[CutH] failed to do overlap between node[%s][%d] to node[%s][%d]",
  143. src_op_desc->GetName().c_str(),
  144. src_anchor->GetIdx(),
  145. op_desc->GetName().c_str(),
  146. in_anchor->GetIdx());
  147. }
  148. }
  149. }
  150. // call infer shape, update output shape
  151. MDS_REQUIRE_SUCCESS(node->InferShapeAndType(), "[CutH] call infer shape failed", node->GetName().c_str());
  152. return SUCCESS;
  153. }
  154. }

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示