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