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.

dimension_compute_pass_unittest.cc 4.9 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/dimension_compute_pass.h"
  17. #include <string>
  18. #include <vector>
  19. #include <gtest/gtest.h>
  20. #include "common/types.h"
  21. #include "graph/passes/base_pass.h"
  22. #include "graph_builder_utils.h"
  23. #include "inc/kernel.h"
  24. #include "inc/kernel_factory.h"
  25. using namespace domi;
  26. namespace ge {
  27. namespace {
  28. const char *AddNYes = "AddNYes";
  29. const char *AddNNo = "AddNNo";
  30. const char *HuberLossYes = "HuberLossYes";
  31. const char *ShapeNo = "ShapeNo";
  32. const char *ShapeYes = "ShapeYes";
  33. const char *DataNo = "dataNo";
  34. } // namespace
  35. class TestShapeYesKernel : public Kernel {
  36. public:
  37. Status Compute(const NodePtr &node, std::vector<GeTensorPtr> &v_output) override {
  38. auto output = std::make_shared<GeTensor>();
  39. std::vector<uint8_t> data{1, 2, 3};
  40. std::vector<int64_t> shape{3};
  41. output->MutableTensorDesc().SetShape(GeShape(shape));
  42. output->SetData(data);
  43. output->MutableTensorDesc().SetDataType(DT_UINT8);
  44. v_output.push_back(output);
  45. return SUCCESS;
  46. }
  47. };
  48. REGISTER_KERNEL(ShapeYes, TestShapeYesKernel);
  49. class UTEST_graph_passes_dimension_adjust_pass : public testing::Test {
  50. protected:
  51. UTEST_graph_passes_dimension_adjust_pass() = default;
  52. };
  53. namespace {
  54. /// netoutput1
  55. /// |
  56. /// shapeNo1
  57. /// |
  58. /// addnNo1
  59. /// / \
  60. /// / \
  61. /// const1 const2
  62. ComputeGraphPtr BuildGraph8() {
  63. auto builder = ut::GraphBuilder("test");
  64. auto const1 = builder.AddNode("const1", CONSTANT, 0, 1);
  65. auto const2 = builder.AddNode("const2", CONSTANT, 0, 1);
  66. auto addn1 = builder.AddNode("addn1", AddNNo, 2, 1);
  67. auto shape1 = builder.AddNode("shape1", ShapeNo, 1, 1);
  68. auto netoutput1 = builder.AddNode("netoutput", NETOUTPUT, 1, 0);
  69. builder.AddDataEdge(const1, 0, addn1, 0);
  70. builder.AddDataEdge(const2, 0, addn1, 1);
  71. builder.AddDataEdge(addn1, 0, shape1, 0);
  72. builder.AddDataEdge(shape1, 0, netoutput1, 0);
  73. return builder.GetGraph();
  74. }
  75. /// netoutput1
  76. /// |
  77. /// shapeNo1
  78. /// |
  79. /// addnYes1
  80. /// / \
  81. /// / \
  82. ///const1 data1
  83. ComputeGraphPtr BuildGraph9() {
  84. auto builder = ut::GraphBuilder("test");
  85. auto const1 = builder.AddNode("const1", CONSTANT, 0, 1);
  86. auto data1 = builder.AddNode("data1", DataNo, 0, 1);
  87. auto addn1 = builder.AddNode("addn1", AddNYes, 2, 1);
  88. auto shape1 = builder.AddNode("shape1", ShapeNo, 1, 1);
  89. auto netoutput1 = builder.AddNode("netoutput", NETOUTPUT, 1, 0);
  90. builder.AddDataEdge(const1, 0, addn1, 0);
  91. builder.AddDataEdge(data1, 0, addn1, 1);
  92. builder.AddDataEdge(addn1, 0, shape1, 0);
  93. builder.AddDataEdge(shape1, 0, netoutput1, 0);
  94. return builder.GetGraph();
  95. }
  96. /// netoutput1
  97. /// |
  98. /// shapeYes1
  99. /// |
  100. /// addnNo1
  101. ComputeGraphPtr BuildGraph1() {
  102. auto builder = ut::GraphBuilder("test");
  103. auto addnNo1 = builder.AddNode("addnNo1", AddNNo, 2, 1);
  104. auto shapeYes1 = builder.AddNode("shapeYes1", ShapeYes, 1, 1);
  105. auto netoutput1 = builder.AddNode("netoutput1", NETOUTPUT, 1, 0);
  106. builder.AddDataEdge(addnNo1, 0, shapeYes1, 0);
  107. builder.AddDataEdge(shapeYes1, 0, netoutput1, 0);
  108. return builder.GetGraph();
  109. }
  110. } // namespace
  111. TEST_F(UTEST_graph_passes_dimension_adjust_pass, NotChangedNoKernel) {
  112. auto graph = BuildGraph8();
  113. NamesToPass names_to_pass;
  114. names_to_pass.push_back({"Test", new DimensionComputePass});
  115. GEPass pass(graph);
  116. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  117. EXPECT_EQ(graph->GetAllNodes().size(), 5);
  118. for (auto &name_to_pass : names_to_pass) {
  119. delete name_to_pass.second;
  120. }
  121. }
  122. TEST_F(UTEST_graph_passes_dimension_adjust_pass, NotChangedNoComputeKernel) {
  123. auto graph = BuildGraph9();
  124. NamesToPass names_to_pass;
  125. names_to_pass.push_back({"Test", new DimensionComputePass});
  126. GEPass pass(graph);
  127. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  128. EXPECT_EQ(graph->GetAllNodes().size(), 5);
  129. for (auto &name_to_pass : names_to_pass) {
  130. delete name_to_pass.second;
  131. }
  132. }
  133. TEST_F(UTEST_graph_passes_dimension_adjust_pass, Success) {
  134. auto graph = BuildGraph1();
  135. NamesToPass names_to_pass;
  136. names_to_pass.push_back({"Test", new DimensionComputePass});
  137. GEPass pass(graph);
  138. EXPECT_EQ(pass.Run(names_to_pass), SUCCESS);
  139. EXPECT_EQ(graph->GetAllNodes().size(), 2);
  140. for (auto &name_to_pass : names_to_pass) {
  141. delete name_to_pass.second;
  142. }
  143. }
  144. } // namespace ge

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

Contributors (1)