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 3.9 kB

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

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