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.8 kB

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

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