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

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

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