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.

cond_branch_v1_unittest.cc 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "graph/passes/merge_input_memcpy_pass.h"
  17. #include "graph/passes/switch_to_stream_switch_pass.h"
  18. #include "graph/passes/merge_to_stream_merge_pass.h"
  19. #include "graph/passes/attach_stream_label_pass.h"
  20. #include <gtest/gtest.h>
  21. #include "graph_builder_utils.h"
  22. namespace ge {
  23. class UtestCondBranchV1Pass : public testing::Test {
  24. protected:
  25. void SetUp() {}
  26. void TearDown() {}
  27. };
  28. namespace {
  29. ///
  30. /// net_output
  31. /// |
  32. /// merge
  33. /// / \.
  34. /// square add
  35. /// F| T/ T\.
  36. /// switch1 switch2
  37. /// / \ / \.
  38. /// var1 var2 var3
  39. ///
  40. ComputeGraphPtr BuildGraph1() {
  41. auto builder = ut::GraphBuilder("g1");
  42. auto var1 = builder.AddNode("var1", VARIABLEV2, 0, 1);
  43. auto var2 = builder.AddNode("var2", VARIABLEV2, 0, 1, FORMAT_ND, DT_BOOL, {});
  44. auto var3 = builder.AddNode("var3", VARIABLEV2, 0, 1);
  45. auto switch1 = builder.AddNode("switch1", REFSWITCH, 2, 2);
  46. auto switch2 = builder.AddNode("switch2", SWITCH, 2, 2);
  47. auto add = builder.AddNode("add", ADD, 2, 1);
  48. auto square = builder.AddNode("square", SQUARE, 1, 1);
  49. auto merge = builder.AddNode("merge", MERGE, 2, 2);
  50. auto net_output = builder.AddNode("net_output", NETOUTPUT, 1, 0);
  51. builder.AddDataEdge(var1, 0, switch1, 0);
  52. builder.AddDataEdge(var2, 0, switch1, 1);
  53. builder.AddDataEdge(var3, 0, switch2, 0);
  54. builder.AddDataEdge(var2, 0, switch2, 1);
  55. builder.AddDataEdge(switch1, 0, square, 0);
  56. builder.AddDataEdge(switch1, 1, add, 0);
  57. builder.AddDataEdge(switch2, 1, add, 1);
  58. builder.AddDataEdge(square, 0, merge, 0);
  59. builder.AddDataEdge(add, 0, merge, 1);
  60. builder.AddDataEdge(merge, 0, net_output, 0);
  61. return builder.GetGraph();
  62. }
  63. } // namespace
  64. TEST_F(UtestCondBranchV1Pass, common_cond_branch_v1) {
  65. auto graph = BuildGraph1();
  66. MergeInputMemcpyPass memcpy_pass;
  67. SwitchToStreamSwitchPass switch_pass;
  68. MergeToStreamMergePass merge_pass;
  69. AttachStreamLabelPass label_pass;
  70. EXPECT_EQ(memcpy_pass.Run(graph), SUCCESS);
  71. EXPECT_EQ(switch_pass.Run(graph), SUCCESS);
  72. EXPECT_EQ(merge_pass.Run(graph), SUCCESS);
  73. EXPECT_EQ(label_pass.Run(graph), SUCCESS);
  74. uint32_t switch_num = 0;
  75. uint32_t merge_num = 0;
  76. uint32_t cast_num = 0;
  77. uint32_t stream_switch_num = 0;
  78. uint32_t memcpy_num = 0;
  79. uint32_t active_num = 0;
  80. uint32_t stream_merge_num = 0;
  81. for (const auto &node : graph->GetAllNodes()) {
  82. const auto &op_desc = node->GetOpDesc();
  83. std::string type = op_desc->GetType();
  84. if (type == SWITCH || type == REFSWITCH) {
  85. switch_num++;
  86. } else if (type == MERGE) {
  87. merge_num++;
  88. } else if (type == CAST) {
  89. cast_num++;
  90. } else if (type == STREAMSWITCH) {
  91. stream_switch_num++;
  92. EXPECT_TRUE(op_desc->HasAttr(ATTR_NAME_STREAM_LABEL));
  93. EXPECT_TRUE(op_desc->HasAttr(ATTR_NAME_ACTIVE_LABEL_LIST));
  94. EXPECT_TRUE(op_desc->HasAttr(ATTR_NAME_SWITCH_DATA_TYPE));
  95. EXPECT_TRUE(op_desc->HasAttr(ATTR_NAME_SWITCH_TRUE_BRANCH_FLAG));
  96. } else if (type == STREAMMERGE) {
  97. stream_merge_num++;
  98. EXPECT_TRUE(op_desc->HasAttr(ATTR_NAME_STREAM_LABEL));
  99. } else if ((type == MEMCPYASYNC) || (type == MEMCPYADDRASYNC)) {
  100. EXPECT_TRUE(op_desc->HasAttr(ATTR_NAME_STREAM_LABEL));
  101. memcpy_num++;
  102. } else if (type == STREAMACTIVE) {
  103. active_num++;
  104. EXPECT_TRUE(op_desc->HasAttr(ATTR_NAME_ACTIVE_LABEL_LIST));
  105. }
  106. }
  107. EXPECT_EQ(switch_num, 0);
  108. EXPECT_EQ(merge_num, 0);
  109. EXPECT_EQ(cast_num, 1);
  110. EXPECT_EQ(stream_switch_num, 2);
  111. EXPECT_EQ(memcpy_num, 2);
  112. EXPECT_EQ(active_num, 3);
  113. EXPECT_EQ(stream_merge_num, 1);
  114. }
  115. } // namespace ge

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