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.

hybrid_model.cc 4.0 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "hybrid_model.h"
  17. #include <vector>
  18. #include "graph/debug/ge_attr_define.h"
  19. #include "graph/load/new_model_manager/model_utils.h"
  20. #include "graph/utils/graph_utils.h"
  21. #include "graph/utils/node_utils.h"
  22. #include "graph/utils/tensor_utils.h"
  23. #include "hybrid/common/npu_memory_allocator.h"
  24. #include "hybrid/model/hybrid_model_builder.h"
  25. #include "hybrid/node_executor/node_executor.h"
  26. namespace ge {
  27. namespace hybrid {
  28. HybridModel::HybridModel(GeRootModelPtr ge_model) : ge_root_model_(std::move(ge_model)) {
  29. }
  30. HybridModel::~HybridModel() {
  31. GELOGD("[%s] HybridModel destroyed.", model_name_.c_str());
  32. }
  33. Status HybridModel::Init() {
  34. GELOGD("Start to init hybrid model.");
  35. GE_CHK_STATUS_RET(HybridModelBuilder(*this).Build(), "Failed to build hybrid model.");
  36. GELOGD("HybridModel initialized successfully.");
  37. return SUCCESS;
  38. }
  39. TensorValue* HybridModel::GetVariable(const string &name) const {
  40. auto it = variable_tensors_.find(name);
  41. if (it == variable_tensors_.end()) {
  42. GELOGD("Failed to get variable tensor. var name = [%s]", name.c_str());
  43. return nullptr;
  44. }
  45. GELOGD("Got variable tensor. var name = [%s], tensor = %s", name.c_str(), it->second->DebugString().c_str());
  46. return it->second.get();
  47. }
  48. NodePtr HybridModel::GetVariableNode(const string &name) const {
  49. auto it = device_variable_nodes_.find(name);
  50. if (it != device_variable_nodes_.end()) {
  51. return it->second;
  52. }
  53. auto host_find = host_variable_nodes_.find(name);
  54. if (host_find != host_variable_nodes_.end()) {
  55. return host_find->second;
  56. }
  57. GELOGD("Failed to get variable node by name = [%s]", name.c_str());
  58. return nullptr;
  59. }
  60. const std::vector<domi::TaskDef> *HybridModel::GetTaskDefs(const NodePtr &node) const {
  61. auto it = task_defs_.find(node);
  62. if (it == task_defs_.end()) {
  63. return nullptr;
  64. }
  65. return &it->second;
  66. }
  67. NodeItem *HybridModel::MutableNodeItem(const NodePtr &node) {
  68. auto it = node_items_.find(node);
  69. if (it == node_items_.end()) {
  70. return nullptr;
  71. }
  72. return it->second.get();
  73. }
  74. const NodeItem *HybridModel::GetNodeItem(const NodePtr &node) const {
  75. auto it = node_items_.find(node);
  76. if (it == node_items_.end()) {
  77. return nullptr;
  78. }
  79. return it->second.get();
  80. }
  81. GeModelPtr HybridModel::GetGeModel(const NodePtr &node) const {
  82. auto it = known_shape_sub_models_.find(node);
  83. if (it == known_shape_sub_models_.end()) {
  84. GELOGE(INTERNAL_ERROR, "[%s] Failed to get GeModel for subgraph node.", node->GetName().c_str());
  85. return nullptr;
  86. }
  87. return it->second;
  88. }
  89. const GraphItem* HybridModel::GetRootGraphItem() const {
  90. return root_graph_item_.get();
  91. }
  92. const GraphItem *HybridModel::GetSubgraphItem(const std::string &graph_name) const {
  93. GELOGD("To find subgraph item by name = %s", graph_name.c_str());
  94. auto it = subgraph_items_.find(graph_name);
  95. if (it == subgraph_items_.end()) {
  96. GELOGD("Subgraph item not found by node = %s", graph_name.c_str());
  97. return nullptr;
  98. }
  99. return it->second.get();
  100. }
  101. const GraphItem *HybridModel::GetSubgraphItem(const ComputeGraphPtr &subgraph) const {
  102. if (subgraph == nullptr) {
  103. GELOGE(PARAM_INVALID, "subgraph is nullptr");
  104. return nullptr;
  105. }
  106. auto subgraph_name = subgraph->GetName();
  107. return GetSubgraphItem(subgraph_name);
  108. }
  109. const string &HybridModel::GetModelName() const {
  110. return model_name_;
  111. }
  112. } // namespace hybrid
  113. } // namespace ge

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