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.0 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
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. namespace ge {
  22. const int64_t kStartCallNum = 1;
  23. const map<string, pair<uint64_t, uint64_t>> &ConstantFoldingPass::GetGeConstantFoldingPerfStatistic() const {
  24. return statistic_of_ge_constant_folding_;
  25. }
  26. const map<string, pair<uint64_t, uint64_t>> &ConstantFoldingPass::GetOpConstantFoldingPerfStatistic() const {
  27. return statistic_of_op_constant_folding_;
  28. }
  29. Status ConstantFoldingPass::Run(ge::NodePtr &node) {
  30. GE_CHECK_NOTNULL(node);
  31. GELOGD("Begin to run constant folding on node %s", node->GetName().c_str());
  32. if (folding_pass::IsNoNeedConstantFolding(node)) {
  33. return SUCCESS;
  34. }
  35. OpDescPtr node_desc = node->GetOpDesc();
  36. DataType data_type = node_desc->GetOutputDesc(0).GetDataType();
  37. Format format = node_desc->GetOutputDesc(0).GetFormat();
  38. GELOGD("Current [node:%s, type:%s] info: format: %s, datatype:%s", node->GetName().c_str(), node->GetType().c_str(),
  39. TypeUtils::FormatToSerialString(format).c_str(), TypeUtils::DataTypeToSerialString(data_type).c_str());
  40. auto input_nodes = OpDescUtils::GetConstInputNode(*node);
  41. if (input_nodes.empty() || input_nodes.size() != node_desc->GetInputsSize()) {
  42. GELOGD("Node:%s, const input nodes size is %zu, and nodeDesc inputsSize is %zu.", node->GetName().c_str(),
  43. input_nodes.size(), node_desc->GetInputsSize());
  44. return SUCCESS;
  45. }
  46. auto inputs = OpDescUtils::GetInputData(input_nodes);
  47. vector<GeTensorPtr> outputs;
  48. // Statistic of ge constant folding kernel
  49. uint64_t start_time = GetCurrentTimestamp();
  50. auto ret = FoldingPass::RunOpKernelWithCheck(node, inputs, outputs);
  51. if (ret != SUCCESS) {
  52. auto op_kernel = folding_pass::GetKernelByType(node);
  53. if (op_kernel == nullptr) {
  54. GELOGD("No op kernel for node %s type %s, skip the constant folding", node->GetName().c_str(),
  55. node->GetType().c_str());
  56. return SUCCESS;
  57. }
  58. // Statistic of op and fe constant folding kernel
  59. start_time = GetCurrentTimestamp();
  60. ret = op_kernel->Compute(node_desc, inputs, outputs);
  61. uint64_t cost_time = GetCurrentTimestamp() - start_time;
  62. if (statistic_of_ge_constant_folding_.find(node->GetType()) != statistic_of_ge_constant_folding_.end()) {
  63. uint64_t &cnt = statistic_of_ge_constant_folding_[node->GetType()].first;
  64. uint64_t &cur_cost_time = statistic_of_ge_constant_folding_[node->GetType()].second;
  65. cnt++;
  66. cur_cost_time += cost_time;
  67. } else {
  68. statistic_of_ge_constant_folding_[node->GetType()] = std::pair<uint64_t, uint64_t>(kStartCallNum, cost_time);
  69. }
  70. if (ret != SUCCESS) {
  71. if (ret == NOT_CHANGED) {
  72. GELOGD("Node %s type %s, compute terminates and exits the constant folding.", node->GetName().c_str(),
  73. node->GetType().c_str());
  74. return SUCCESS;
  75. }
  76. REPORT_CALL_ERROR("E19999", "Calculate for node %s(%s) failed",
  77. node->GetName().c_str(), node->GetType().c_str());
  78. GELOGE(INTERNAL_ERROR, "[Call][Calculate] for node %s failed in constant folding", node->GetName().c_str());
  79. return ret;
  80. }
  81. GELOGI("Node %s type %s, constant folding compute success.", node->GetName().c_str(), node->GetType().c_str());
  82. } else {
  83. if (statistic_of_op_constant_folding_.find(node->GetType()) != statistic_of_op_constant_folding_.end()) {
  84. uint64_t &cnt = statistic_of_op_constant_folding_[node->GetType()].first;
  85. uint64_t &cost_time = statistic_of_op_constant_folding_[node->GetType()].second;
  86. cnt++;
  87. cost_time += GetCurrentTimestamp() - start_time;
  88. } else {
  89. statistic_of_op_constant_folding_[node->GetType()] =
  90. std::pair<uint64_t, uint64_t>(kStartCallNum, GetCurrentTimestamp() - start_time);
  91. }
  92. }
  93. if (outputs.empty()) {
  94. REPORT_INNER_ERROR("E19999", "After calculate for node %s(%s), output weight is empty, check invalid",
  95. node->GetName().c_str(), node->GetType().c_str());
  96. GELOGE(INTERNAL_ERROR, "[Check][Param] After calculate for node %s(%s), output weight is empty",
  97. node->GetName().c_str(), node->GetType().c_str());
  98. return INTERNAL_ERROR;
  99. }
  100. return Folding(node, outputs);
  101. }
  102. } // namespace ge

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