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.

graph_preprocess_unittest.cc 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include <memory>
  18. #include "common/ge_inner_error_codes.h"
  19. #include "common/types.h"
  20. #include "common/util.h"
  21. #include "graph/passes/graph_builder_utils.h"
  22. #include "graph/utils/attr_utils.h"
  23. #include "graph/debug/ge_attr_define.h"
  24. #define private public
  25. #define protected public
  26. #include "graph/preprocess/graph_preprocess.h"
  27. #include "ge/ge_api.h"
  28. #undef private
  29. #undef protected
  30. using namespace std;
  31. namespace ge {
  32. class UtestGraphPreproces : public testing::Test {
  33. protected:
  34. void SetUp() {
  35. }
  36. void TearDown() {
  37. }
  38. };
  39. ComputeGraphPtr BuildGraph1(){
  40. auto builder = ut::GraphBuilder("g1");
  41. auto data1 = builder.AddNode("data1",DATA,1,1);
  42. auto data_opdesc = data1->GetOpDesc();
  43. AttrUtils::SetInt(data_opdesc, ATTR_NAME_INDEX, 0);
  44. data1->UpdateOpDesc(data_opdesc);
  45. return builder.GetGraph();
  46. }
  47. ComputeGraphPtr BuildGraph2() {
  48. auto builder = ut::GraphBuilder("g2");
  49. auto data1 = builder.AddNode("data1", DATA, 1, 1, FORMAT_NCHW, DT_FLOAT, std::vector<int64_t>({22, -1}));
  50. ge::AttrUtils::SetStr(data1->GetOpDesc(), ATTR_ATC_USER_DEFINE_DATATYPE, "DT_INT8");
  51. auto data_opdesc = data1->GetOpDesc();
  52. AttrUtils::SetInt(data_opdesc, ATTR_NAME_INDEX, 0);
  53. data1->UpdateOpDesc(data_opdesc);
  54. return builder.GetGraph();
  55. }
  56. ComputeGraphPtr BuildGraph3() {
  57. auto builder = ut::GraphBuilder("g3");
  58. auto data1 = builder.AddNode("data1", DATA, 1, 1, FORMAT_NCHW, DT_FLOAT);
  59. ge::AttrUtils::SetStr(data1->GetOpDesc(), ATTR_ATC_USER_DEFINE_DATATYPE, "DT_INT8");
  60. auto data_opdesc = data1->GetOpDesc();
  61. AttrUtils::SetInt(data_opdesc, ATTR_NAME_INDEX, 0);
  62. data1->UpdateOpDesc(data_opdesc);
  63. return builder.GetGraph();
  64. }
  65. TEST_F(UtestGraphPreproces, test_dynamic_input_shape_parse) {
  66. ge::GraphPrepare graph_prepare;
  67. graph_prepare.compute_graph_ = BuildGraph1();
  68. // prepare user_input & graph option
  69. ge::GeTensorDesc tensor1;
  70. tensor1.SetFormat(ge::FORMAT_NCHW);
  71. tensor1.SetShape(ge::GeShape({3, 12, 5, 5}));
  72. tensor1.SetDataType(ge::DT_FLOAT);
  73. GeTensor input1(tensor1);
  74. std::vector<GeTensor> user_input = {input1};
  75. std::map<string,string> graph_option = {{"ge.exec.dynamicGraphExecuteMode","dynamic_execute"},
  76. {"ge.exec.dataInputsShapeRange","[3,1~20,2~10,5]"}};
  77. auto ret = graph_prepare.UpdateInput(user_input, graph_option);
  78. EXPECT_EQ(ret, ge::SUCCESS);
  79. // check data node output shape_range and shape
  80. auto data_node = graph_prepare.compute_graph_->FindNode("data1");
  81. auto data_output_desc = data_node->GetOpDesc()->GetOutputDescPtr(0);
  82. vector<int64_t> expect_shape = {3,-1,-1,5};
  83. auto result_shape = data_output_desc->GetShape();
  84. EXPECT_EQ(result_shape.GetDimNum(), expect_shape.size());
  85. for(size_t i =0; i< expect_shape.size(); ++i){
  86. EXPECT_EQ(result_shape.GetDim(i), expect_shape.at(i));
  87. }
  88. }
  89. TEST_F(UtestGraphPreproces, test_check_user_input) {
  90. ge::GraphPrepare graph_prepare;
  91. graph_prepare.compute_graph_ = BuildGraph1();
  92. vector<int64_t> dim = {2, -3};
  93. GeTensor tensor;
  94. tensor.SetTensorDesc(GeTensorDesc(GeShape(dim)));
  95. std::vector<GeTensor> user_input;
  96. user_input.emplace_back(tensor);
  97. Status ret = graph_prepare.CheckUserInput(user_input);
  98. EXPECT_EQ(ret, GE_GRAPH_INIT_FAILED);
  99. }
  100. TEST_F(UtestGraphPreproces, test_update_input_output1) {
  101. ge::GraphPrepare graph_prepare;
  102. graph_prepare.compute_graph_ = BuildGraph3();
  103. Status ret = graph_prepare.UpdateInputOutputByOptions();
  104. EXPECT_EQ(ret, SUCCESS);
  105. }
  106. }

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