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 6.7 kB

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

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