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.

label_allocator.cc 3.2 kB

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "label_allocator.h"
  17. #include "framework/common/types.h"
  18. #include "common/util.h"
  19. #include "common/ge_inner_error_codes.h"
  20. #include "graph/debug/ge_attr_define.h"
  21. #include "graph/utils/graph_utils.h"
  22. #include "graph/label/label_maker.h"
  23. namespace ge {
  24. LabelAllocator::LabelAllocator(const ComputeGraphPtr &graph) : compute_graph_(graph) {}
  25. Status LabelAllocator::AssignFunctionalLabels() {
  26. if (compute_graph_ == nullptr) {
  27. GELOGE(INTERNAL_ERROR, "ComputeGraph not set, Assign labels failed.");
  28. return INTERNAL_ERROR;
  29. }
  30. // Add label task for sub graph.
  31. GELOGD("AssignFunctionalLabels start: %s.", compute_graph_->GetName().c_str());
  32. std::set<NodePtr> functional_nodes;
  33. for (auto graph : compute_graph_->GetAllSubgraphs()) {
  34. if (!CollectFunctionalNode(graph, functional_nodes)) {
  35. return INTERNAL_ERROR;
  36. }
  37. }
  38. // Add label for functional op.
  39. uint32_t label_index = 0;
  40. for (auto node : functional_nodes) {
  41. LabelMakerPtr maker = LabelMakerFactory::Instance().Create(node->GetType(), compute_graph_, node);
  42. if (maker == nullptr) {
  43. GELOGE(INTERNAL_ERROR, "Node: %s label maker not registed.", node->GetType().c_str());
  44. return INTERNAL_ERROR;
  45. }
  46. if (maker->Run(label_index) != SUCCESS) {
  47. GELOGE(INTERNAL_ERROR, "Node: %s run label maker failed.", node->GetType().c_str());
  48. return INTERNAL_ERROR;
  49. }
  50. }
  51. (void)AttrUtils::SetInt(*compute_graph_, ATTR_MODEL_LABEL_NUM, label_index);
  52. GELOGI("AssignFunctionalLabels success, Num: %u.", label_index);
  53. return SUCCESS;
  54. }
  55. bool LabelAllocator::CollectFunctionalNode(ComputeGraphPtr &graph, std::set<NodePtr> &functional_nodes) {
  56. if (graph == nullptr) {
  57. GELOGE(INTERNAL_ERROR, "Sub ComputeGraph is null.");
  58. return false;
  59. }
  60. if (graph->GetGraphUnknownFlag()) {
  61. GELOGD("Graph[%s] is unknown graph, skip label allocator.", graph->GetName().c_str());
  62. return true;
  63. }
  64. NodePtr func_node = graph->GetParentNode();
  65. if (func_node == nullptr) {
  66. GELOGE(INTERNAL_ERROR, "Parent functional node not set: %s.", graph->GetName().c_str());
  67. return false;
  68. }
  69. ComputeGraphPtr owner_graph = func_node->GetOwnerComputeGraph();
  70. if (owner_graph == nullptr) {
  71. GELOGE(INTERNAL_ERROR, "ComputeGraph owner not set: %s.", func_node->GetName().c_str());
  72. return false;
  73. }
  74. if (owner_graph->GetGraphUnknownFlag()) {
  75. GELOGD("Graph[%s] is unknown graph, skip label allocator.", owner_graph->GetName().c_str());
  76. return true;
  77. }
  78. (void)functional_nodes.insert(func_node); // unique functional node.
  79. return true;
  80. }
  81. } // namespace ge

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