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.

resource_pair_add_control_pass.cc 4.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/resource_pair_add_control_pass.h"
  17. #include <map>
  18. #include <memory>
  19. #include <set>
  20. #include <string>
  21. #include "framework/common/debug/ge_log.h"
  22. #include "common/ge_inner_error_codes.h"
  23. #include "common/types.h"
  24. #include "common/util.h"
  25. #include "graph/utils/attr_utils.h"
  26. #include "graph/utils/tensor_adapter.h"
  27. namespace {
  28. const std::map<std::string, std::string> kResourcePairType = {{"StackPush", "StackPop"}};
  29. const std::set<std::string> kResourceTypes = {"StackPush", "StackPop"};
  30. } // namespace
  31. namespace ge {
  32. Status ResourcePairAddControlPass::Run(ComputeGraphPtr graph) {
  33. GE_CHECK_NOTNULL(graph);
  34. GELOGD("ResourcePairAddControlPass pass start.");
  35. std::map<std::string, std::map<std::string, NodePtr>> prefix_2_node_per_type;
  36. // find all node of condition type, store with type and scope prefix key
  37. for (auto &node : graph->GetDirectNode()) {
  38. GE_CHECK_NOTNULL(node);
  39. auto node_type = node->GetType();
  40. if (kResourceTypes.find(node_type) != kResourceTypes.end()) {
  41. std::string node_name = node->GetName();
  42. std::string node_key(node_name);
  43. std::size_t found = node_name.rfind(node_type);
  44. if (found != std::string::npos) {
  45. node_key.replace(found, node_type.size(), "");
  46. }
  47. prefix_2_node_per_type[node_type][node_key] = node;
  48. GELOGD("ResourcePairAddControlPass insert node_key:%s, op_name:%s, op_type:%s", node_key.c_str(),
  49. node_name.c_str(), node->GetType().c_str());
  50. }
  51. }
  52. // according type pair, find same prefix node, add control edge
  53. for (auto &resource_type_pair : kResourcePairType) {
  54. auto from_item_prefix_2_node = prefix_2_node_per_type.find(resource_type_pair.first);
  55. if (from_item_prefix_2_node != prefix_2_node_per_type.end()) {
  56. for (auto &prefix_2_node : from_item_prefix_2_node->second) {
  57. const std::string &prefix = prefix_2_node.first;
  58. NodePtr from_node = prefix_2_node.second;
  59. GE_CHECK_NOTNULL(from_node);
  60. auto to_item_prefix_2_node = prefix_2_node_per_type.find(resource_type_pair.second);
  61. if (to_item_prefix_2_node == prefix_2_node_per_type.end()) {
  62. GELOGE(PARAM_INVALID, "find peer type node fail, suffix:%s, from_type:%s, to_type:%s", prefix.c_str(),
  63. resource_type_pair.first.c_str(), resource_type_pair.second.c_str());
  64. return PARAM_INVALID;
  65. }
  66. auto to_prefix_2_node = to_item_prefix_2_node->second.find(prefix);
  67. if (to_prefix_2_node == to_item_prefix_2_node->second.end()) {
  68. GELOGE(PARAM_INVALID, "find peer prefix node fail, suffix:%s, from_type:%s, to_type:%s", prefix.c_str(),
  69. resource_type_pair.first.c_str(), resource_type_pair.second.c_str());
  70. return PARAM_INVALID;
  71. }
  72. NodePtr to_node = to_prefix_2_node->second;
  73. GE_CHECK_NOTNULL(to_node);
  74. auto from_anchor = from_node->GetOutControlAnchor();
  75. auto to_anchor = to_node->GetInControlAnchor();
  76. GE_CHECK_NOTNULL(from_anchor);
  77. GE_CHECK_NOTNULL(to_anchor);
  78. graphStatus ret = from_anchor->LinkTo(to_anchor);
  79. if (ret != GRAPH_SUCCESS) {
  80. GELOGE(PARAM_INVALID, "link fail, from_node:%s, to_node:%s, from_type:%s, to_type:%s",
  81. from_node->GetName().c_str(), to_node->GetName().c_str(), resource_type_pair.first.c_str(),
  82. resource_type_pair.second.c_str());
  83. return PARAM_INVALID;
  84. }
  85. GELOGD("link success, from_node:%s, to_node:%s, from_type:%s, to_type:%s", from_node->GetName().c_str(),
  86. to_node->GetName().c_str(), resource_type_pair.first.c_str(), resource_type_pair.second.c_str());
  87. }
  88. }
  89. }
  90. return SUCCESS;
  91. }
  92. } // namespace ge

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