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 4.8 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Copyright 2019-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 "common/debug/log.h"
  19. #include "common/types.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "graph/operator_factory.h"
  22. #include "graph/utils/attr_utils.h"
  23. #include "graph/utils/node_utils.h"
  24. #include "graph/utils/op_desc_utils.h"
  25. #include "graph/utils/type_utils.h"
  26. #include "inc/kernel.h"
  27. namespace ge {
  28. const int64_t kStartCallNum = 1;
  29. const std::unordered_map<std::string, std::pair<std::uint64_t, uint64_t>>
  30. &ConstantFoldingPass::GetGeConstantFoldingPerfStatistic() const {
  31. return statistic_of_ge_constant_folding_;
  32. }
  33. const std::unordered_map<std::string, std::pair<std::uint64_t, uint64_t>>
  34. &ConstantFoldingPass::GetOpConstantFoldingPerfStatistic() const {
  35. return statistic_of_op_constant_folding_;
  36. }
  37. Status ConstantFoldingPass::Run(ge::NodePtr &node) {
  38. GE_CHECK_NOTNULL(node);
  39. GELOGD("Begin to run constant folding on node %s", node->GetName().c_str());
  40. if (folding_pass::IsNoNeedConstantFolding(node)) {
  41. return SUCCESS;
  42. }
  43. OpDescPtr node_desc = node->GetOpDesc();
  44. DataType data_type = node_desc->GetOutputDesc(0).GetDataType();
  45. Format format = node_desc->GetOutputDesc(0).GetFormat();
  46. GELOGD("Current [node:%s, type:%s] info: format: %s, datatype:%s", node->GetName().c_str(), node->GetType().c_str(),
  47. TypeUtils::FormatToSerialString(format).c_str(), TypeUtils::DataTypeToSerialString(data_type).c_str());
  48. auto input_nodes = OpDescUtils::GetConstInputNode(*node);
  49. if (input_nodes.empty() || input_nodes.size() != node_desc->GetInputsSize()) {
  50. GELOGD("Node:%s, const input nodes size is %zu, and nodeDesc inputsSize is %zu.", node->GetName().c_str(),
  51. input_nodes.size(), node_desc->GetInputsSize());
  52. return SUCCESS;
  53. }
  54. auto inputs = OpDescUtils::GetInputData(input_nodes);
  55. vector<GeTensorPtr> outputs;
  56. // Statistic of ge constant folding kernel
  57. uint64_t start_time = GetCurrentTimestap();
  58. auto ret = RunOpKernel(node, inputs, outputs);
  59. if (ret != SUCCESS) {
  60. auto op_kernel = folding_pass::GetKernelByType(node);
  61. if (op_kernel == nullptr) {
  62. GELOGD("No op kernel for node %s type %s, skip the constant folding", node->GetName().c_str(),
  63. node->GetType().c_str());
  64. return SUCCESS;
  65. }
  66. // Statistic of op and fe constant folding kernel
  67. start_time = GetCurrentTimestap();
  68. ret = op_kernel->Compute(node_desc, inputs, outputs);
  69. uint64_t cost_time = GetCurrentTimestap() - start_time;
  70. if (statistic_of_ge_constant_folding_.find(node->GetType()) != statistic_of_ge_constant_folding_.end()) {
  71. uint64_t &cnt = statistic_of_ge_constant_folding_[node->GetType()].first;
  72. uint64_t &cur_cost_time = statistic_of_ge_constant_folding_[node->GetType()].second;
  73. cnt++;
  74. cur_cost_time += cost_time;
  75. } else {
  76. statistic_of_ge_constant_folding_[node->GetType()] = std::pair<uint64_t, uint64_t>(kStartCallNum, cost_time);
  77. }
  78. if (ret != SUCCESS) {
  79. if (ret == NOT_CHANGED) {
  80. GELOGD("Node %s type %s, compute terminates and exits the constant folding.", node->GetName().c_str(),
  81. node->GetType().c_str());
  82. return SUCCESS;
  83. }
  84. GELOGE(INTERNAL_ERROR, "Calculate for node %s failed in constant folding", node->GetName().c_str());
  85. return ret;
  86. }
  87. GELOGI("Node %s type %s, constant folding compute success.", node->GetName().c_str(), node->GetType().c_str());
  88. } else {
  89. if (statistic_of_op_constant_folding_.find(node->GetType()) != statistic_of_op_constant_folding_.end()) {
  90. uint64_t &cnt = statistic_of_op_constant_folding_[node->GetType()].first;
  91. uint64_t &cost_time = statistic_of_op_constant_folding_[node->GetType()].second;
  92. cnt++;
  93. cost_time += GetCurrentTimestap() - start_time;
  94. } else {
  95. statistic_of_op_constant_folding_[node->GetType()] =
  96. std::pair<uint64_t, uint64_t>(kStartCallNum, GetCurrentTimestap() - start_time);
  97. }
  98. }
  99. if (outputs.empty()) {
  100. GELOGE(INTERNAL_ERROR,
  101. "Failed to constant folding on node %s,"
  102. " no output weight",
  103. node->GetName().c_str());
  104. return INTERNAL_ERROR;
  105. }
  106. return Folding(node, outputs);
  107. }
  108. } // namespace ge

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