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.

kernel_graph_test.cc 8.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "common/common_test.h"
  17. #include "ir/param_info.h"
  18. #include "frontend/operator/ops.h"
  19. #include "backend/session/kernel_graph.h"
  20. #include "backend/session/anf_runtime_algorithm.h"
  21. #include "mindspore/ccsrc/runtime/device/kernel_info.h"
  22. #include "utils/utils.h"
  23. namespace mindspore {
  24. namespace session {
  25. using device::KernelInfo;
  26. using KernelBuildInfoBuilder = kernel::KernelBuildInfo::KernelBuildInfoBuilder;
  27. class KernelGraphTest : public UT::Common {
  28. public:
  29. KernelGraphTest() = default;
  30. void SetUp() override {}
  31. void TearDown() override {}
  32. };
  33. TEST_F(KernelGraphTest, NewValueNode) {
  34. auto kernel_graph = std::make_shared<KernelGraph>();
  35. auto add_value = NewValueNode(MakeValue(static_cast<int64_t>(0)));
  36. MS_EXCEPTION_IF_NULL(add_value);
  37. std::vector<int64_t> shape = {1};
  38. auto x_abstract = std::make_shared<abstract::AbstractTensor>(kFloat32, shape);
  39. add_value->set_abstract(x_abstract);
  40. add_value->set_kernel_info(std::make_shared<KernelInfo>());
  41. auto mutable_kernel_info = dynamic_cast<device::KernelInfo *>(add_value->kernel_info());
  42. MS_EXCEPTION_IF_NULL(mutable_kernel_info);
  43. std::shared_ptr<KernelBuildInfoBuilder> builder = std::make_shared<KernelBuildInfoBuilder>();
  44. builder->SetOutputsFormat({kOpFormat_FRAC_Z});
  45. builder->SetOutputsDeviceType({kFloat32->type_id()});
  46. mutable_kernel_info->set_select_kernel_build_info(builder->Build());
  47. auto new_value = kernel_graph->NewValueNode(add_value);
  48. EXPECT_NE(new_value, nullptr);
  49. EXPECT_EQ(AnfAlgo::GetOutputInferShape(new_value, 0)[0], 1);
  50. EXPECT_EQ(AnfAlgo::GetOutputInferDataType(new_value, 0), kFloat32->type_id());
  51. EXPECT_EQ(AnfAlgo::GetOutputFormat(new_value, 0), kOpFormat_DEFAULT);
  52. EXPECT_EQ(AnfAlgo::GetOutputDeviceDataType(new_value, 0), kTypeUnknown);
  53. }
  54. TEST_F(KernelGraphTest, NewParameter) {
  55. auto anf_graph = std::make_shared<FuncGraph>();
  56. auto kernel_graph = std::make_shared<KernelGraph>();
  57. // test nullptr as input
  58. auto new_paramter = kernel_graph->NewParameter();
  59. EXPECT_NE(new_paramter, nullptr);
  60. EXPECT_TRUE(new_paramter->isa<Parameter>());
  61. EXPECT_EQ(AnfAlgo::GetOutputFormat(new_paramter, 0), kOpFormat_DEFAULT);
  62. EXPECT_EQ(AnfAlgo::GetOutputDeviceDataType(new_paramter, 0), kMetaTypeNone);
  63. // test non-weight parameter node as input
  64. std::vector<int64_t> shape = {2, 32, 224, 224};
  65. auto x_abstract = std::make_shared<abstract::AbstractTensor>(kFloat32, shape);
  66. auto non_weight_parameter = anf_graph->add_parameter();
  67. MS_EXCEPTION_IF_NULL(non_weight_parameter);
  68. non_weight_parameter->set_abstract(x_abstract);
  69. auto new_non_weight_parameter = kernel_graph->NewParameter(non_weight_parameter);
  70. EXPECT_NE(new_non_weight_parameter, nullptr);
  71. new_non_weight_parameter->set_name("non_weight_parameter");
  72. EXPECT_EQ(AnfAlgo::GetOutputInferShape(new_non_weight_parameter, 0)[1], 32);
  73. EXPECT_EQ(AnfAlgo::GetOutputInferDataType(new_non_weight_parameter, 0), kFloat32->type_id());
  74. EXPECT_EQ(AnfAlgo::GetOutputFormat(new_non_weight_parameter, 0), kOpFormat_DEFAULT);
  75. EXPECT_EQ(AnfAlgo::GetOutputDeviceDataType(new_non_weight_parameter, 0), kFloat32->type_id());
  76. EXPECT_EQ(new_non_weight_parameter->name(), "non_weight_parameter");
  77. // test weight parameter node as input
  78. auto weight_parameter_node = anf_graph->add_parameter();
  79. MS_EXCEPTION_IF_NULL(weight_parameter_node);
  80. auto param_value_new = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, shape);
  81. weight_parameter_node->set_default_param(param_value_new);
  82. weight_parameter_node->set_abstract(x_abstract);
  83. auto new_weight_parameter_node = kernel_graph->NewParameter(weight_parameter_node);
  84. EXPECT_NE(new_weight_parameter_node, nullptr);
  85. EXPECT_TRUE(new_weight_parameter_node->has_default());
  86. EXPECT_EQ(AnfAlgo::GetOutputInferShape(new_weight_parameter_node, 0)[2], 224);
  87. EXPECT_EQ(AnfAlgo::GetOutputInferDataType(new_weight_parameter_node, 0), kFloat32->type_id());
  88. EXPECT_EQ(AnfAlgo::GetOutputFormat(new_weight_parameter_node, 0), kOpFormat_DEFAULT);
  89. EXPECT_EQ(AnfAlgo::GetOutputDeviceDataType(new_weight_parameter_node, 0), kTypeUnknown);
  90. }
  91. TEST_F(KernelGraphTest, NewCNode) {
  92. auto kernel_graph = std::make_shared<KernelGraph>();
  93. auto add_value = NewValueNode(prim::kPrimAdd);
  94. std::vector<AnfNodePtr> inputs = {add_value};
  95. auto new_cnode = kernel_graph->NewCNode(inputs);
  96. EXPECT_NE(new_cnode, nullptr);
  97. EXPECT_EQ(AnfAlgo::GetCNodeName(new_cnode), prim::kPrimAdd->name());
  98. EXPECT_TRUE(AnfAlgo::GetOutputInferShape(new_cnode, 0).empty());
  99. EXPECT_EQ(AnfAlgo::GetOutputInferDataType(new_cnode, 0), kMetaTypeNone);
  100. }
  101. TEST_F(KernelGraphTest, MutableInputs) {
  102. auto kernel_graph = std::make_shared<KernelGraph>();
  103. auto x_parameter = kernel_graph->add_parameter();
  104. MS_EXCEPTION_IF_NULL(x_parameter);
  105. x_parameter->set_name("x_parameter");
  106. auto y_parameter = kernel_graph->add_parameter();
  107. MS_EXCEPTION_IF_NULL(y_parameter);
  108. y_parameter->set_name("y_parameter");
  109. std::vector<AnfNodePtr> inputs = {x_parameter, y_parameter};
  110. auto mutable_inputs = kernel_graph->MutableInputs();
  111. MS_EXCEPTION_IF_NULL(mutable_inputs);
  112. *mutable_inputs = inputs;
  113. auto first_input = kernel_graph->inputs()[0];
  114. MS_EXCEPTION_IF_NULL(first_input);
  115. auto first_parameter = first_input->cast<ParameterPtr>();
  116. MS_EXCEPTION_IF_NULL(first_parameter);
  117. EXPECT_EQ(first_parameter->name(), "x_parameter");
  118. auto second_input = kernel_graph->inputs()[1];
  119. MS_EXCEPTION_IF_NULL(second_input);
  120. auto second_parameter = second_input->cast<ParameterPtr>();
  121. MS_EXCEPTION_IF_NULL(second_parameter);
  122. EXPECT_EQ(second_parameter->name(), "y_parameter");
  123. }
  124. TEST_F(KernelGraphTest, SetExecOrderByDefault) {
  125. /*
  126. * define kernel graph:
  127. * x ----- y
  128. * add ----- z
  129. * mul
  130. * return
  131. */
  132. auto kernel_graph = std::make_shared<KernelGraph>();
  133. std::vector<int64_t> shape = {2, 32, 224, 224};
  134. auto abstract = std::make_shared<abstract::AbstractTensor>(kFloat32, shape);
  135. auto x_parameter = kernel_graph->NewParameter();
  136. MS_EXCEPTION_IF_NULL(x_parameter);
  137. x_parameter->set_name("x_parameter");
  138. x_parameter->set_abstract(abstract);
  139. auto y_parameter = kernel_graph->NewParameter();
  140. MS_EXCEPTION_IF_NULL(y_parameter);
  141. y_parameter->set_name("y_parameter");
  142. y_parameter->set_abstract(abstract);
  143. std::vector<AnfNodePtr> add_inputs = {NewValueNode(prim::kPrimAdd), x_parameter, y_parameter};
  144. auto add = kernel_graph->NewCNode(add_inputs);
  145. MS_EXCEPTION_IF_NULL(add);
  146. add->set_abstract(abstract);
  147. auto z_parameter = kernel_graph->NewParameter();
  148. MS_EXCEPTION_IF_NULL(z_parameter);
  149. z_parameter->set_name("z_parameter");
  150. z_parameter->set_abstract(abstract);
  151. std::vector<AnfNodePtr> mul_inputs = {NewValueNode(prim::kPrimMul), add, z_parameter};
  152. auto mul = kernel_graph->NewCNode(mul_inputs);
  153. MS_EXCEPTION_IF_NULL(mul);
  154. mul->set_abstract(abstract);
  155. std::vector<AnfNodePtr> make_tuple_inputs = {NewValueNode(prim::kPrimMakeTuple), mul};
  156. auto make_tuple = kernel_graph->NewCNode(make_tuple_inputs);
  157. kernel_graph->set_output(make_tuple);
  158. // test outputs() function
  159. auto outputs = kernel_graph->outputs();
  160. EXPECT_EQ(outputs.size(), 1);
  161. EXPECT_EQ(AnfAlgo::GetCNodeName(outputs[0]), prim::kPrimMul->name());
  162. // test SetExecOrderByDefault() function
  163. kernel_graph->SetExecOrderByDefault();
  164. auto execution_order = kernel_graph->execution_order();
  165. EXPECT_EQ(execution_order.size(), 2);
  166. EXPECT_EQ(AnfAlgo::GetCNodeName(execution_order[0]), prim::kPrimAdd->name());
  167. EXPECT_EQ(AnfAlgo::GetCNodeName(execution_order[1]), prim::kPrimMul->name());
  168. // test set_execution_order() function
  169. kernel_graph->set_execution_order({add});
  170. execution_order = kernel_graph->execution_order();
  171. EXPECT_EQ(execution_order.size(), 1);
  172. EXPECT_EQ(AnfAlgo::GetCNodeName(execution_order[0]), prim::kPrimAdd->name());
  173. }
  174. TEST_F(KernelGraphTest, SetGraphId) {
  175. auto kernel_graph = std::make_shared<KernelGraph>();
  176. kernel_graph->set_graph_id(1);
  177. EXPECT_EQ(kernel_graph->graph_id(), 1);
  178. }
  179. } // namespace session
  180. } // namespace mindspore