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.

variable_format_pass.cc 5.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/variable_format_pass.h"
  17. #include <map>
  18. #include <set>
  19. #include <string>
  20. #include "framework/common/debug/ge_log.h"
  21. namespace ge {
  22. Status VariableFormatPass::Run(ge::ComputeGraphPtr graph) {
  23. GE_CHECK_NOTNULL(graph);
  24. for (auto &node : graph->GetDirectNode()) {
  25. GE_IF_BOOL_EXEC(node->GetOpDesc() == nullptr, continue);
  26. GE_IF_BOOL_EXEC(node->GetOpDesc()->GetType() != VARIABLE, continue);
  27. ge::NodePtr use_node = nullptr;
  28. if (GetApplyMomentumOpByVariableInput(node, use_node)) {
  29. GE_CHK_STATUS_RET(UpdateVariableOutFormat(node, use_node), "update variable out format failed");
  30. GE_CHK_STATUS_RET(UpdateApplyMomentumInputFormat(use_node), "update apply momentum input format failed");
  31. }
  32. }
  33. return domi::SUCCESS;
  34. }
  35. bool VariableFormatPass::GetApplyMomentumOpByVariableInput(const ge::NodePtr &var_node, ge::NodePtr &use_node) {
  36. GE_IF_BOOL_EXEC(var_node == nullptr, return false);
  37. std::map<std::string, std::set<int>> confirm_ops = {{"ApplyMomentum", {1}}};
  38. for (auto &out_anchor : var_node->GetAllOutDataAnchors()) {
  39. for (auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  40. GE_IF_BOOL_EXEC(ConfirmUseOpAndIndexByAnchor(in_anchor, confirm_ops, use_node), return true);
  41. }
  42. }
  43. return false;
  44. }
  45. bool VariableFormatPass::ConfirmUseOpAndIndexByAnchor(const ge::InDataAnchorPtr &in_anchor,
  46. const map<string, std::set<int>> &confirm_ops,
  47. ge::NodePtr &use_node) {
  48. GE_IF_BOOL_EXEC(in_anchor == nullptr, return false);
  49. ge::NodePtr dst_node = in_anchor->GetOwnerNode();
  50. ge::OpDescPtr dst_op_desc = dst_node->GetOpDesc();
  51. GE_IF_BOOL_EXEC(dst_op_desc == nullptr, return false);
  52. const string &dst_type = dst_op_desc->GetType();
  53. int input_index = in_anchor->GetIdx();
  54. GELOGD("ConfirmUseOpAndIndex, var name %s, dst_type = %s, input index %d", dst_node->GetName().c_str(),
  55. dst_type.c_str(), input_index);
  56. GE_IF_BOOL_EXEC(confirm_ops.count(dst_type) > 0,
  57. GE_IF_BOOL_EXEC(confirm_ops.at(dst_type).count(input_index) > 0, use_node = dst_node; return true););
  58. return false;
  59. }
  60. Status VariableFormatPass::UpdateVariableOutFormat(const ge::NodePtr &var_node, ge::NodePtr &use_node) {
  61. GE_CHECK_NOTNULL(var_node);
  62. GE_CHECK_NOTNULL(use_node);
  63. ge::OpDescPtr op_desc_ptr = use_node->GetOpDesc();
  64. GE_CHECK_NOTNULL(op_desc_ptr);
  65. GE_CHECK_NOTNULL(use_node->GetInDataAnchor(0));
  66. GE_CHECK_NOTNULL(use_node->GetInDataAnchor(0)->GetPeerOutAnchor());
  67. NodePtr in_node = use_node->GetInDataAnchor(0)->GetPeerOutAnchor()->GetOwnerNode();
  68. if (in_node != nullptr) {
  69. string in_op_type = in_node->GetType();
  70. if ((in_op_type == VARIABLE) && (in_node->GetOpDesc() != nullptr) &&
  71. (in_node->GetOpDesc()->MutableOutputDesc(0) != nullptr)) {
  72. ge::Format format = in_node->GetOpDesc()->MutableOutputDesc(0)->GetFormat();
  73. ge::OpDescPtr cur_op_desc_ptr = var_node->GetOpDesc();
  74. if (cur_op_desc_ptr != nullptr) {
  75. cur_op_desc_ptr->MutableOutputDesc(0)->SetFormat(format);
  76. cur_op_desc_ptr->MutableOutputDesc(0)->SetOriginFormat(format);
  77. }
  78. }
  79. }
  80. return domi::SUCCESS;
  81. }
  82. Status VariableFormatPass::UpdateApplyMomentumInputFormat(const ge::NodePtr &node) {
  83. GE_CHECK_NOTNULL(node);
  84. ge::OpDescPtr op_desc_ptr = node->GetOpDesc();
  85. GE_CHECK_NOTNULL(op_desc_ptr);
  86. GE_CHECK_NOTNULL(node->GetInDataAnchor(0));
  87. GE_CHECK_NOTNULL(node->GetInDataAnchor(0)->GetPeerOutAnchor());
  88. GE_CHECK_NOTNULL(op_desc_ptr->MutableInputDesc(0));
  89. GE_CHECK_NOTNULL(op_desc_ptr->MutableInputDesc(1));
  90. GE_CHECK_NOTNULL(op_desc_ptr->MutableOutputDesc(0));
  91. NodePtr in_node = node->GetInDataAnchor(0)->GetPeerOutAnchor()->GetOwnerNode();
  92. if (in_node != nullptr) {
  93. string in_op_type = in_node->GetType();
  94. if ((in_op_type == VARIABLE) && (in_node->GetOpDesc() != nullptr)) {
  95. ge::Format format = in_node->GetOpDesc()->MutableOutputDesc(0)->GetFormat();
  96. op_desc_ptr->MutableInputDesc(0)->SetFormat(format);
  97. op_desc_ptr->MutableInputDesc(0)->SetOriginFormat(format);
  98. op_desc_ptr->MutableInputDesc(1)->SetFormat(format);
  99. op_desc_ptr->MutableInputDesc(1)->SetOriginFormat(format);
  100. op_desc_ptr->MutableOutputDesc(0)->SetFormat(format);
  101. op_desc_ptr->MutableOutputDesc(0)->SetOriginFormat(format);
  102. }
  103. }
  104. return domi::SUCCESS;
  105. }
  106. } // namespace ge

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