1 /**
2 * Copyright 2020-2021 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 #ifndef MINDSPORE_LITE_TOOLS_OPTIMIZER_COMMON_GLLO_UTILS_H_
18 #define MINDSPORE_LITE_TOOLS_OPTIMIZER_COMMON_GLLO_UTILS_H_
19
20 #include <memory>
21 #include <string>
22 #include <vector>
23 #include "ops/primitive_c.h"
24 #include "ir/anf.h"
25 #include "ir/func_graph.h"
26 #include "src/common/utils.h"
27 #include "backend/optimizer/common/pattern_engine.h"
28 #include "ops/fusion/conv2d_backprop_input_fusion.h"
29 #include "schema/inner/model_generated.h"
30 #include "tools/converter/converter_context.h"
31
32 using PrimitiveCPtr = std::shared_ptr<mindspore::ops::PrimitiveC>;
33 using mindspore::lite::RET_ERROR;
34 using mindspore::lite::RET_OK;
35 using mindspore::lite::STATUS;
36 namespace mindspore {
37 namespace opt {
38 inline constexpr int kInputIndexTwo = 2;
39 inline constexpr int kInputIndexThree = 3;
40 inline constexpr int kInputIndexFour = 4;
41 inline constexpr int kInputIndexFive = 5;
42 inline constexpr size_t kInputSizeTwo = 2;
43 inline constexpr size_t kInputSizeThree = 3;
44 inline constexpr size_t kInputSizeFour = 4;
45 inline constexpr size_t kInputSizeFive = 5;
46 inline const std::vector<int> kNH2NC = {0, 3, 1, 2};
47 inline const std::vector<int> kNC2NH = {0, 2, 3, 1};
48 inline const PrimitivePtr kPrimMakeTupleV2 = std::make_shared<Primitive>("make_tuple");
49 inline const PrimitivePtr kPrimIdentity = std::make_shared<Primitive>("Identity");
50 inline const PrimitivePtr kPrimConv2DBackpropInputFusion =
51 std::make_shared<Primitive>(ops::kNameConv2DBackpropInputFusion);
52
53 std::vector<int> CastToInt(const ValuePtr &value);
54
55 std::vector<std::vector<int>> CastToVec2DInt(const ValuePtr &value);
56
57 std::vector<float> CastToFloat(const ValuePtr &value);
58
59 bool CheckPrimitiveType(const AnfNodePtr &node, const PrimitivePtr &primitive_type);
60
61 bool IsRealCNodeKernel(const AnfNodePtr &node);
62
63 bool IsGraphKernel(const AnfNodePtr &node);
64
65 bool CheckInputs(const CNodePtr &cnode);
66
67 ParameterPtr AddNewBiasNode(float *bias_data, const FuncGraphPtr &func_graph, int kernel_num, TypeId type_id);
68
69 bool IsParamNode(const BaseRef &n);
70
71 bool IsParamOrValueNodeWithData(const BaseRef &n);
72
73 bool IsParallelSplitConvNode(const BaseRef &n);
74
75 bool IsConvNode(const BaseRef &n);
76
77 bool IsOpType(const BaseRef &n, const PrimitivePtr &prim);
78
79 bool CheckIsAllInputsParam(const AnfNodePtr &node);
80
81 size_t GetOutputTensorNum(const AnfNodePtr &node);
82
83 bool IsMultiOutputTensors(const FuncGraphPtr &graph, const AnfNodePtr &node);
84
85 size_t GetTupleGetItemOutIndex(const CNodePtr &tuple_get_item);
86
87 tensor::TensorPtr GetTensorInfo(const AnfNodePtr &node);
88
89 AbstractBasePtr GetCNodeInputAbstract(const CNodePtr &cnode, size_t index);
90
91 STATUS TransFilterFormat(const tensor::TensorPtr &tensor, schema::Format src_format, schema::Format dst_format);
92
93 ParameterPtr BuildParameterNode(const FuncGraphPtr &func_graph, const AnfNodePtr &node,
94 const tensor::TensorPtr &tensor_info);
95
96 ParameterPtr BuildIntValueParameterNode(const FuncGraphPtr &func_graph, const int32_t &data,
97 const std::string &node_name);
98
99 ParameterPtr BuildIntVecParameterNode(const FuncGraphPtr &func_graph, const std::vector<int32_t> &data,
100 const std::string &node_name);
101
102 ParameterPtr BuildIntVec2DParameterNode(const FuncGraphPtr &func_graph, const std::vector<std::vector<int32_t>> &data,
103 const std::string &node_name);
104
105 ParameterPtr BuildFloatValueParameterNode(const FuncGraphPtr &func_graph, const float &data,
106 const std::string &node_name);
107
108 ParameterPtr BuildFloatVecParameterNode(const FuncGraphPtr &func_graph, const std::vector<float> &data,
109 const std::string &node_name);
110
111 CNodePtr GenTransposeNode(const FuncGraphPtr &func_graph, const AnfNodePtr &input_node, const std::vector<int> &perm,
112 const std::string &cnode_name);
113
114 CNodePtr GenGatherNode(const FuncGraphPtr &func_graph, const AnfNodePtr &input_node, const std::vector<int> &indices,
115 const std::string &cnode_name);
116
117 CNodePtr GenTupleGetItemNode(const FuncGraphPtr &func_graph, const CNodePtr &input, size_t index);
118
119 STATUS FetchShapeFromAbstract(const abstract::AbstractBasePtr &abstract, ShapeVector *shape);
120
121 STATUS GetTensorInfoFromAbstract(tensor::TensorPtr *tensor_info, const CNodePtr &cnode, size_t index);
122
123 template <const PrimitivePtr *prim = nullptr>
IsSpecifiedNode(const BaseRef & n)124 inline bool IsSpecifiedNode(const BaseRef &n) {
125 if (utils::isa<AnfNodePtr>(n)) {
126 auto anf_node = utils::cast<AnfNodePtr>(n);
127 return CheckPrimitiveType(anf_node, *prim);
128 }
129 return false;
130 }
131
132 bool IsTrainOp(const CNodePtr &cnode);
133
134 bool IsMarkedTrainOp(const CNodePtr &cnode);
135
136 int GetDataTypeFromAnfNode(const AnfNodePtr &anf_node, TypeId *type_id);
137 } // namespace opt
138 } // namespace mindspore
139 #endif // MINDSPORE_LITE_TOOLS_OPTIMIZER_COMMON_GLLO_UTILS_H_
140