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.

constant_folding_pass.cc 6.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Copyright 2020 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/constant_folding_pass.h"
  17. #include <vector>
  18. #include "external/graph/operator_factory.h"
  19. #include "graph/utils/node_utils.h"
  20. #include "graph/utils/type_utils.h"
  21. #include "ge_local_engine/engine/host_cpu_engine.h"
  22. #include "init/gelib.h"
  23. namespace ge {
  24. const int64_t kStartCallNum = 1;
  25. const std::string kKernelLibName = "aicpu_tf_kernel";
  26. const std::string kOpsFlagClose = "0";
  27. const map<string, pair<uint64_t, uint64_t>> &ConstantFoldingPass::GetGeConstantFoldingPerfStatistic() const {
  28. return statistic_of_ge_constant_folding_;
  29. }
  30. const map<string, pair<uint64_t, uint64_t>> &ConstantFoldingPass::GetOpConstantFoldingPerfStatistic() const {
  31. return statistic_of_op_constant_folding_;
  32. }
  33. Status ConstantFoldingPass::RunOpKernelWithCheck(NodePtr &node, const vector<ConstGeTensorPtr> &inputs,
  34. std::vector<GeTensorPtr> &outputs) {
  35. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  36. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  37. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Check][Param] GE is not initialized or is finalized.");
  38. return UNSUPPORTED;
  39. }
  40. OpsKernelInfoStorePtr kernel_info = instance_ptr->OpsKernelManagerObj().GetOpsKernelInfoStore(kKernelLibName);
  41. if (kernel_info == nullptr) {
  42. GELOGE(FAILED, "[Get][OpsKernelInfoStore] %s failed", kKernelLibName.c_str());
  43. return UNSUPPORTED;
  44. }
  45. std::string ops_flag;
  46. kernel_info->opsFlagCheck(*node, ops_flag);
  47. if (ops_flag == kOpsFlagClose) {
  48. return UNSUPPORTED;
  49. }
  50. return RunOpKernel(node, inputs, outputs);
  51. }
  52. Status ConstantFoldingPass::RunOpKernel(NodePtr &node,
  53. const vector<ConstGeTensorPtr> &inputs,
  54. std::vector<GeTensorPtr> &outputs) {
  55. return HostCpuEngine::GetInstance().Run(node, inputs, outputs);
  56. }
  57. Status ConstantFoldingPass::Run(ge::NodePtr &node) {
  58. GE_CHECK_NOTNULL(node);
  59. GELOGD("Begin to run constant folding on node %s", node->GetName().c_str());
  60. if (folding_pass::IsNoNeedConstantFolding(node)) {
  61. return SUCCESS;
  62. }
  63. OpDescPtr node_desc = node->GetOpDesc();
  64. DataType data_type = node_desc->GetOutputDesc(0).GetDataType();
  65. Format format = node_desc->GetOutputDesc(0).GetFormat();
  66. GELOGD("Current [node:%s, type:%s] info: format: %s, datatype:%s", node->GetName().c_str(), node->GetType().c_str(),
  67. TypeUtils::FormatToSerialString(format).c_str(), TypeUtils::DataTypeToSerialString(data_type).c_str());
  68. auto input_nodes = OpDescUtils::GetConstInputNode(*node);
  69. if (input_nodes.empty() || input_nodes.size() != node_desc->GetInputsSize()) {
  70. GELOGD("Node:%s, const input nodes size is %zu, and nodeDesc inputsSize is %zu.", node->GetName().c_str(),
  71. input_nodes.size(), node_desc->GetInputsSize());
  72. return SUCCESS;
  73. }
  74. auto inputs = OpDescUtils::GetInputData(input_nodes);
  75. vector<GeTensorPtr> outputs;
  76. // Statistic of ge constant folding kernel
  77. uint64_t start_time = GetCurrentTimestamp();
  78. auto ret = RunOpKernelWithCheck(node, inputs, outputs);
  79. if (ret != SUCCESS) {
  80. auto op_kernel = folding_pass::GetKernelByType(node);
  81. if (op_kernel == nullptr) {
  82. GELOGD("No op kernel for node %s type %s, skip the constant folding", node->GetName().c_str(),
  83. node->GetType().c_str());
  84. return SUCCESS;
  85. }
  86. // Statistic of op and fe constant folding kernel
  87. start_time = GetCurrentTimestamp();
  88. ret = op_kernel->Compute(node_desc, inputs, outputs);
  89. uint64_t cost_time = GetCurrentTimestamp() - start_time;
  90. if (statistic_of_ge_constant_folding_.find(node->GetType()) != statistic_of_ge_constant_folding_.end()) {
  91. uint64_t &cnt = statistic_of_ge_constant_folding_[node->GetType()].first;
  92. uint64_t &cur_cost_time = statistic_of_ge_constant_folding_[node->GetType()].second;
  93. cnt++;
  94. cur_cost_time += cost_time;
  95. } else {
  96. statistic_of_ge_constant_folding_[node->GetType()] = std::pair<uint64_t, uint64_t>(kStartCallNum, cost_time);
  97. }
  98. if (ret != SUCCESS) {
  99. if (ret == NOT_CHANGED) {
  100. GELOGD("Node %s type %s, compute terminates and exits the constant folding.", node->GetName().c_str(),
  101. node->GetType().c_str());
  102. return SUCCESS;
  103. }
  104. REPORT_CALL_ERROR("E19999", "Calculate for node %s(%s) failed",
  105. node->GetName().c_str(), node->GetType().c_str());
  106. GELOGE(INTERNAL_ERROR, "[Call][Calculate] for node %s failed in constant folding", node->GetName().c_str());
  107. return ret;
  108. }
  109. GELOGI("Node %s type %s, constant folding compute success.", node->GetName().c_str(), node->GetType().c_str());
  110. } else {
  111. if (statistic_of_op_constant_folding_.find(node->GetType()) != statistic_of_op_constant_folding_.end()) {
  112. uint64_t &cnt = statistic_of_op_constant_folding_[node->GetType()].first;
  113. uint64_t &cost_time = statistic_of_op_constant_folding_[node->GetType()].second;
  114. cnt++;
  115. cost_time += GetCurrentTimestamp() - start_time;
  116. } else {
  117. statistic_of_op_constant_folding_[node->GetType()] =
  118. std::pair<uint64_t, uint64_t>(kStartCallNum, GetCurrentTimestamp() - start_time);
  119. }
  120. }
  121. if (outputs.empty()) {
  122. REPORT_INNER_ERROR("E19999", "After calculate for node %s(%s), output weight is empty, check invalid",
  123. node->GetName().c_str(), node->GetType().c_str());
  124. GELOGE(INTERNAL_ERROR, "[Check][Param] After calculate for node %s(%s), output weight is empty",
  125. node->GetName().c_str(), node->GetType().c_str());
  126. return INTERNAL_ERROR;
  127. }
  128. return Folding(node, outputs);
  129. }
  130. } // namespace ge

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