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_adjust_pass_unittest.cc 6.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <gtest/gtest.h>
  17. #define protected public
  18. #define private public
  19. #include "graph/passes/dimension_adjust_pass.h"
  20. #include "common/debug/log.h"
  21. #include "common/debug/memory_dumper.h"
  22. #include "common/ge_inner_error_codes.h"
  23. #include "common/op/ge_op_utils.h"
  24. #include "common/types.h"
  25. #include "graph/types.h"
  26. #include "graph/utils/graph_utils.h"
  27. #include "graph/utils/op_desc_utils.h"
  28. #include "inc/kernel_factory.h"
  29. #undef protected
  30. #undef private
  31. using namespace std;
  32. using namespace testing;
  33. namespace ge {
  34. class UtestGraphPassesDimensionAdjustPass : public testing::Test {
  35. protected:
  36. void SetUp() {}
  37. void TearDown() {}
  38. };
  39. TEST_F(UtestGraphPassesDimensionAdjustPass, succ) {
  40. ge::ComputeGraphPtr graph = std::make_shared<ge::ComputeGraph>("default");
  41. ge::OpDescPtr data_op_desc = make_shared<ge::OpDesc>("data", CONSTANTOP);
  42. int64_t dims_size = 1;
  43. vector<int64_t> data_vec = {1, 2, 3};
  44. for_each(data_vec.begin(), data_vec.end(), [&](int64_t &data) { dims_size *= data; });
  45. vector<int32_t> data_value_vec(dims_size, 1);
  46. GeTensorDesc data_tensor_desc(GeShape(data_vec), FORMAT_NCHW, DT_INT32);
  47. GeTensorPtr data_tensor = make_shared<GeTensor>(data_tensor_desc, (uint8_t *)data_value_vec.data(),
  48. data_value_vec.size() * sizeof(int32_t));
  49. OpDescUtils::SetWeights(data_op_desc, data_tensor);
  50. data_op_desc->AddOutputDesc(data_tensor_desc);
  51. NodePtr data_node = graph->AddNode(data_op_desc);
  52. data_node->Init();
  53. // add dim node
  54. ge::OpDescPtr dim_op_desc = make_shared<ge::OpDesc>("dim", CONSTANTOP);
  55. vector<int32_t> dim_value_vec = {0};
  56. GeTensorDesc dim_tensor_desc(ge::GeShape(), FORMAT_NCHW, DT_INT32);
  57. GeTensorPtr dim_tensor =
  58. make_shared<GeTensor>(dim_tensor_desc, (uint8_t *)dim_value_vec.data(), dim_value_vec.size() * sizeof(int32_t));
  59. OpDescUtils::SetWeights(dim_op_desc, dim_tensor);
  60. dim_op_desc->AddOutputDesc(dim_tensor_desc);
  61. NodePtr dim_node = graph->AddNode(dim_op_desc);
  62. dim_node->Init();
  63. // add expanddims node
  64. OpDescPtr expanddims_op_desc = std::make_shared<OpDesc>("Expanddims", EXPANDDIMS);
  65. vector<int64_t> expanddims_vec = {1, 1, 2, 3};
  66. GeTensorDesc expanddims_tensor_desc(ge::GeShape(expanddims_vec), FORMAT_NCHW, DT_INT32);
  67. GeTensorPtr expanddims_tensor = make_shared<GeTensor>(expanddims_tensor_desc, (uint8_t *)data_value_vec.data(),
  68. data_value_vec.size() * sizeof(int32_t));
  69. OpDescUtils::SetWeights(expanddims_op_desc, expanddims_tensor);
  70. expanddims_op_desc->AddInputDesc(data_tensor_desc);
  71. expanddims_op_desc->AddInputDesc(dim_tensor_desc);
  72. expanddims_op_desc->AddOutputDesc(expanddims_tensor_desc);
  73. NodePtr op_node = graph->AddNode(expanddims_op_desc);
  74. op_node->Init();
  75. // add output node
  76. OpDescPtr netoutput_op_desc = std::make_shared<OpDesc>("NetOutput", "NetOutput");
  77. netoutput_op_desc->AddInputDesc(expanddims_tensor_desc);
  78. NodePtr netoutput_node = graph->AddNode(netoutput_op_desc);
  79. netoutput_node->Init();
  80. // add edge
  81. GraphUtils::AddEdge(data_node->GetOutDataAnchor(0), op_node->GetInDataAnchor(0));
  82. GraphUtils::AddEdge(dim_node->GetOutDataAnchor(0), op_node->GetInDataAnchor(1));
  83. GraphUtils::AddEdge(op_node->GetOutDataAnchor(0), netoutput_node->GetInDataAnchor(0));
  84. std::shared_ptr<DimensionAdjustPass> pass = make_shared<DimensionAdjustPass>();
  85. ge::Status ret = pass->Run(op_node);
  86. EXPECT_EQ(SUCCESS, ret);
  87. }
  88. TEST_F(UtestGraphPassesDimensionAdjustPass, input_node_is_nullptr) {
  89. std::shared_ptr<DimensionAdjustPass> pass = make_shared<DimensionAdjustPass>();
  90. ge::NodePtr node = nullptr;
  91. ge::Status ret = pass->Run(node);
  92. EXPECT_EQ(PARAM_INVALID, ret);
  93. }
  94. TEST_F(UtestGraphPassesDimensionAdjustPass, node_op_desc_is_nullptr) {
  95. NodePtr op_node = make_shared<Node>(nullptr, nullptr);
  96. std::shared_ptr<DimensionAdjustPass> pass = make_shared<DimensionAdjustPass>();
  97. ge::Status ret = pass->Run(op_node);
  98. EXPECT_EQ(PARAM_INVALID, ret);
  99. }
  100. TEST_F(UtestGraphPassesDimensionAdjustPass, node_get_original_type_failed) {
  101. ge::ComputeGraphPtr graph = std::make_shared<ge::ComputeGraph>("default");
  102. OpDescPtr expanddim_op_desc = std::make_shared<OpDesc>("Expanddims", FRAMEWORKOP);
  103. NodePtr op_node = make_shared<Node>(expanddim_op_desc, graph);
  104. std::shared_ptr<DimensionAdjustPass> pass = make_shared<DimensionAdjustPass>();
  105. ge::Status ret = pass->Run(op_node);
  106. }
  107. TEST_F(UtestGraphPassesDimensionAdjustPass, node_not_register_op) {
  108. ge::ComputeGraphPtr graph = std::make_shared<ge::ComputeGraph>("default");
  109. OpDescPtr expanddim_op_desc = std::make_shared<OpDesc>("Expanddims", FRAMEWORKOP);
  110. AttrUtils::SetStr(expanddim_op_desc, ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, "expanddims_fake");
  111. NodePtr op_node = make_shared<Node>(expanddim_op_desc, graph);
  112. std::shared_ptr<DimensionAdjustPass> pass = make_shared<DimensionAdjustPass>();
  113. ge::Status ret = pass->Run(op_node);
  114. EXPECT_EQ(SUCCESS, ret);
  115. }
  116. TEST_F(UtestGraphPassesDimensionAdjustPass, node_compute_failed) {
  117. ge::ComputeGraphPtr graph = std::make_shared<ge::ComputeGraph>("default");
  118. OpDescPtr expanddim_op_desc = std::make_shared<OpDesc>("Expanddims", EXPANDDIMS);
  119. NodePtr op_node = make_shared<Node>(expanddim_op_desc, graph);
  120. std::shared_ptr<DimensionAdjustPass> pass = make_shared<DimensionAdjustPass>();
  121. ge::Status ret = pass->Run(op_node);
  122. EXPECT_EQ(SUCCESS, ret);
  123. }
  124. } // namespace ge

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