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.

replace_with_empty_const_pass.cc 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/replace_with_empty_const_pass.h"
  17. #include <sstream>
  18. #include <string>
  19. #include "common/ge/ge_util.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "framework/common/ge_inner_error_codes.h"
  22. #include "graph/utils/graph_utils.h"
  23. namespace ge {
  24. Status ReplaceWithEmptyConstPass::Run(NodePtr &node) {
  25. GELOGD("ReplaceWithEmptyConstPass in.");
  26. if (node == nullptr) {
  27. GELOGE(PARAM_INVALID, "Parameter is null.");
  28. return PARAM_INVALID;
  29. }
  30. if (node->GetOpDesc() == nullptr) {
  31. GELOGE(PARAM_INVALID, "Param [opDesc] must not be null.");
  32. return PARAM_INVALID;
  33. }
  34. if (node->GetType() == CONSTANT || node->GetType() == CONSTANTOP) {
  35. GELOGI("Node %s is const. Ignore current pass.", node->GetName().c_str());
  36. return SUCCESS;
  37. }
  38. // Node like no op, it has no output
  39. if (node->GetOpDesc()->GetAllOutputsDescPtr().empty()) {
  40. GELOGI("Node %s has no output desc. Ignore current pass.", node->GetName().c_str());
  41. return SUCCESS;
  42. }
  43. // If outputs of current node are all empty, replace it with empty const
  44. bool is_all_output_empty = true;
  45. for (const auto &output_desc_ptr : node->GetOpDesc()->GetAllOutputsDescPtr()) {
  46. if (output_desc_ptr == nullptr) {
  47. GELOGI("Node %s Got empty output_desc_ptr, ignore current pass.", node->GetName().c_str());
  48. return SUCCESS;
  49. }
  50. if (!IsEmptyTenor(output_desc_ptr->GetShape())) {
  51. is_all_output_empty = false;
  52. break;
  53. }
  54. }
  55. if (is_all_output_empty) {
  56. GELOGI("Node %s has empty tensor output. It will be replaced by empty const.", node->GetName().c_str());
  57. // Replace op which all output is empty with empty const
  58. vector<GeTensorPtr> outputs;
  59. Status ret = GetOutputsOfCurrNode(node, outputs);
  60. if (ret != SUCCESS) {
  61. // If replace failed, it should not break whole process, so still return success
  62. GELOGW("Failed to get outputs of node %s.", node->GetName().c_str());
  63. }
  64. else {
  65. ret = Folding(node, outputs);
  66. if (ret != SUCCESS) {
  67. // If replace failed, it should not break whole process, so still return success
  68. GELOGW("Failed to repalce node %s with empty const.", node->GetName().c_str());
  69. }
  70. }
  71. }
  72. GELOGD("ReplaceWithEmptyConstPass end.");
  73. return SUCCESS;
  74. }
  75. Status ReplaceWithEmptyConstPass::GetOutputsOfCurrNode(const NodePtr &node_to_replace, vector<GeTensorPtr> &outputs) {
  76. for (const auto &out_anchor : node_to_replace->GetAllOutDataAnchors()) {
  77. GE_CHECK_NOTNULL(node_to_replace->GetOpDesc());
  78. auto out_desc = node_to_replace->GetOpDesc()->GetOutputDesc(out_anchor->GetIdx());
  79. GeTensorPtr empty_tensor = MakeShared<ge::GeTensor>(out_desc);
  80. GE_CHECK_NOTNULL(empty_tensor);
  81. outputs.emplace_back(empty_tensor);
  82. }
  83. return SUCCESS;
  84. }
  85. bool ReplaceWithEmptyConstPass::IsEmptyTenor(const GeShape &shape) const {
  86. for (auto dim : shape.GetDims()) {
  87. if (dim == 0) {
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. } // namespace ge

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