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.

mark_node_unknown_shape_pass.cc 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "graph/passes/mark_node_unknown_shape_pass.h"
  17. #include "graph/utils/node_utils.h"
  18. #include "graph/debug/ge_attr_define.h"
  19. #include "common/local_context.h"
  20. namespace ge {
  21. namespace {
  22. const char *const kEngineNameAiCore = "AIcoreEngine";
  23. const char *const kNeedRefreshShape = "_need_generate";
  24. const char *const kOriginalNode = "_original_node";
  25. const int32_t kDynamicState = -2;
  26. }
  27. Status MarkNodeUnknownShapePass::Run(ComputeGraphPtr graph) {
  28. GE_CHECK_NOTNULL(graph);
  29. if (!GetLocalOmgContext().fuzz_compile_flag) {
  30. return SUCCESS;
  31. }
  32. if (IsAllAicoreSupportDyn(graph)) {
  33. if (UpdateNodeShapeToUnknown(graph) != SUCCESS) {
  34. GELOGE(FAILED, "[Update][Node_Shape]Failed to update node shape to unknown.");
  35. return FAILED;
  36. }
  37. }
  38. return SUCCESS;
  39. }
  40. bool MarkNodeUnknownShapePass::IsAllAicoreSupportDyn(ComputeGraphPtr &graph) {
  41. bool is_all_aicore_support_dyn = false;
  42. for (const auto &node : graph->GetAllNodes()) {
  43. if (node->GetOpDesc() == nullptr) {
  44. continue;
  45. }
  46. if (node->GetOpDesc()->GetOpKernelLibName() != kEngineNameAiCore) {
  47. GELOGD("Kernel of %s is %s.", node->GetName().c_str(), node->GetOpDesc()->GetOpKernelLibName().c_str());
  48. continue;
  49. }
  50. NodePtr original_node = nullptr;
  51. original_node = node->GetOpDesc()->TryGetExtAttr(kOriginalNode, original_node);
  52. if ((original_node == nullptr && AttrUtils::HasAttr(node->GetOpDesc(), ATTR_NAME_FUZZ_BUILD_RES_ATTRS)) ||
  53. (original_node != nullptr && AttrUtils::HasAttr(node->GetOpDesc(), ATTR_NAME_FUZZ_BUILD_RES_ATTRS) &&
  54. !AttrUtils::HasAttr(original_node->GetOpDesc(), kNeedRefreshShape))) {
  55. GELOGD("%s has set ATTR_NAME_FUZZ_BUILD_RES_ATTRS.", node->GetName().c_str());
  56. is_all_aicore_support_dyn = true;
  57. } else {
  58. GELOGD("%s has not set ATTR_NAME_FUZZ_BUILD_RES_ATTRS.", node->GetName().c_str());
  59. is_all_aicore_support_dyn = false;
  60. break;
  61. }
  62. }
  63. return is_all_aicore_support_dyn;
  64. }
  65. Status MarkNodeUnknownShapePass::UpdateNodeShapeToUnknown(ComputeGraphPtr &graph) {
  66. GELOGD("Need to update node shape to dynamic when get fuzz build result.");
  67. for (const auto &node : graph->GetAllNodes()) {
  68. if (NodeUtils::IsConst(*node) || node->GetType() == VARIABLE) {
  69. continue;
  70. }
  71. auto op_desc = node->GetOpDesc();
  72. GE_CHECK_NOTNULL(op_desc);
  73. for (size_t i = 0; i < op_desc->GetAllInputsSize(); ++i) {
  74. auto src_node = NodeUtils::GetInDataNodeByIndex(*node, static_cast<int>(i));
  75. if (src_node != nullptr && (NodeUtils::IsConst(*src_node) || src_node->GetType() == VARIABLE)) {
  76. continue;
  77. }
  78. GELOGD("Update input shape for %s.", node->GetName().c_str());
  79. auto input_desc = op_desc->MutableInputDesc(static_cast<uint32_t>(i));
  80. if (input_desc != nullptr) {
  81. input_desc->SetShape(GeShape({kDynamicState}));
  82. }
  83. }
  84. for (auto &output_desc : op_desc->GetAllOutputsDescPtr()) {
  85. if (output_desc != nullptr) {
  86. GELOGD("Update output shape for %s.", node->GetName().c_str());
  87. output_desc->SetShape(GeShape({kDynamicState}));
  88. }
  89. }
  90. }
  91. return SUCCESS;
  92. }
  93. } // namespace ge

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