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.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 map<string, pair<uint64_t, uint64_t>> &ConstantFoldingPass::GetGeConstantFoldingPerfStatistic() const {
  48. return statistic_of_ge_constant_folding_;
  49. }
  50. const map<string, pair<uint64_t, uint64_t>> &ConstantFoldingPass::GetOpConstantFoldingPerfStatistic() const {
  51. return statistic_of_op_constant_folding_;
  52. }
  53. Status ConstantFoldingPass::Run(ge::NodePtr &node) {
  54. GE_CHECK_NOTNULL(node);
  55. GELOGD("Begin to run constant folding on node %s", node->GetName().c_str());
  56. if (folding_pass::IsNoNeedConstantFolding(node)) {
  57. return SUCCESS;
  58. }
  59. OpDescPtr node_desc = node->GetOpDesc();
  60. DataType data_type = node_desc->GetOutputDesc(0).GetDataType();
  61. Format format = node_desc->GetOutputDesc(0).GetFormat();
  62. GELOGD("Current [node:%s, type:%s] info: format: %s, datatype:%s", node->GetName().c_str(), node->GetType().c_str(),
  63. TypeUtils::FormatToSerialString(format).c_str(), TypeUtils::DataTypeToSerialString(data_type).c_str());
  64. auto input_nodes = OpDescUtils::GetConstInputNode(*node);
  65. if (input_nodes.empty() || input_nodes.size() != node_desc->GetInputsSize()) {
  66. GELOGD("Node:%s, const input nodes size is %zu, and nodeDesc inputsSize is %zu.", node->GetName().c_str(),
  67. input_nodes.size(), node_desc->GetInputsSize());
  68. return SUCCESS;
  69. }
  70. auto inputs = OpDescUtils::GetInputData(input_nodes);
  71. vector<GeTensorPtr> outputs;
  72. // Statistic of ge constant folding kernel
  73. uint64_t start_time = GetCurrentTimestamp();
  74. auto ret = RunOpKernelWithCheck(node, inputs, outputs);
  75. if (ret != SUCCESS) {
  76. auto op_kernel = folding_pass::GetKernelByType(node);
  77. if (op_kernel == nullptr) {
  78. GELOGD("No op kernel for node %s type %s, skip the constant folding", node->GetName().c_str(),
  79. node->GetType().c_str());
  80. return SUCCESS;
  81. }
  82. // Statistic of op and fe constant folding kernel
  83. start_time = GetCurrentTimestamp();
  84. ret = op_kernel->Compute(node_desc, inputs, outputs);
  85. uint64_t cost_time = GetCurrentTimestamp() - start_time;
  86. if (statistic_of_ge_constant_folding_.find(node->GetType()) != statistic_of_ge_constant_folding_.end()) {
  87. uint64_t &cnt = statistic_of_ge_constant_folding_[node->GetType()].first;
  88. uint64_t &cur_cost_time = statistic_of_ge_constant_folding_[node->GetType()].second;
  89. cnt++;
  90. cur_cost_time += cost_time;
  91. } else {
  92. statistic_of_ge_constant_folding_[node->GetType()] = std::pair<uint64_t, uint64_t>(kStartCallNum, cost_time);
  93. }
  94. if (ret != SUCCESS) {
  95. if (ret == NOT_CHANGED) {
  96. GELOGD("Node %s type %s, compute terminates and exits the constant folding.", node->GetName().c_str(),
  97. node->GetType().c_str());
  98. return SUCCESS;
  99. }
  100. GELOGE(INTERNAL_ERROR, "Calculate for node %s failed in constant folding", node->GetName().c_str());
  101. return ret;
  102. }
  103. GELOGI("Node %s type %s, constant folding compute success.", node->GetName().c_str(), node->GetType().c_str());
  104. } else {
  105. if (statistic_of_op_constant_folding_.find(node->GetType()) != statistic_of_op_constant_folding_.end()) {
  106. uint64_t &cnt = statistic_of_op_constant_folding_[node->GetType()].first;
  107. uint64_t &cost_time = statistic_of_op_constant_folding_[node->GetType()].second;
  108. cnt++;
  109. cost_time += GetCurrentTimestamp() - start_time;
  110. } else {
  111. statistic_of_op_constant_folding_[node->GetType()] =
  112. std::pair<uint64_t, uint64_t>(kStartCallNum, GetCurrentTimestamp() - start_time);
  113. }
  114. }
  115. if (outputs.empty()) {
  116. GELOGE(INTERNAL_ERROR,
  117. "Failed to constant folding on node %s,"
  118. " no output weight",
  119. node->GetName().c_str());
  120. return INTERNAL_ERROR;
  121. }
  122. return Folding(node, outputs);
  123. }
  124. } // namespace ge

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