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.

helper.h 8.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * Copyright 2019 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. #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_HELPER_H_
  17. #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_HELPER_H_
  18. #include <vector>
  19. #include <memory>
  20. #include <utility>
  21. #include <string>
  22. #include <set>
  23. #include <unordered_set>
  24. #include "ir/func_graph.h"
  25. #include "backend/session/kernel_graph.h"
  26. #include "utils/ms_utils.h"
  27. #include "backend/optimizer/common/pattern_engine.h"
  28. namespace mindspore {
  29. namespace opt {
  30. constexpr size_t kTransOpInputNum = 2;
  31. constexpr size_t kCastInputNum = 2;
  32. constexpr size_t kDependInputNum = 3;
  33. constexpr size_t kReluInputNum = 2;
  34. constexpr size_t kReluGradInputNum = 3;
  35. constexpr size_t kAddInputNum = 3;
  36. constexpr size_t kAddNInputNum = 3;
  37. constexpr size_t kTupleGetitemInputNum = 3;
  38. constexpr size_t kConvInputNum = 3;
  39. constexpr size_t kRealDivInputNum = 3;
  40. constexpr size_t kSqrtInputNum = 2;
  41. constexpr size_t kMulInputNum = 3;
  42. constexpr size_t kRsqrtInputNum = 2;
  43. constexpr size_t kSubInputNum = 3;
  44. constexpr size_t kAssignSubInputNum = 3;
  45. constexpr size_t kConvBn1OutputNum = 3;
  46. constexpr size_t kBn2ReluOutputNum = 4;
  47. constexpr size_t kBnInputNum = 6;
  48. constexpr size_t kBnOutputNum = 5;
  49. constexpr size_t kBatchNormInputNum = 5;
  50. constexpr size_t kBatchNormOutputNum = 5;
  51. constexpr size_t kBN1OutputNum = 2;
  52. constexpr size_t kBN2OutputNum = 3;
  53. constexpr size_t kBN3OutputNum = 1;
  54. constexpr size_t kBNGradInputNum = 6;
  55. constexpr size_t kBNGradOutputNum = 3;
  56. constexpr size_t kBNGrad1OutputNum = 3;
  57. constexpr size_t kBNGrad2OutputNum = 5;
  58. constexpr size_t kBNGrad3OutputNum = 1;
  59. constexpr size_t kBNTrainingReduceOutputNum = 2;
  60. constexpr size_t kBNTrainingUpdateOutputNum = 5;
  61. constexpr size_t kBNTrainingUpdateV2OutputNum = 3;
  62. constexpr size_t kBNTrainingUpdateV3OutputNum = 5;
  63. constexpr size_t kBNTrainingUpdateGradOutputNum = 2;
  64. constexpr size_t kSingleOutputNum = 1;
  65. constexpr size_t kSumNodeInputNum = 2;
  66. constexpr size_t kSquareNodeInputNum = 2;
  67. constexpr size_t kSquareSumv2OutputNum = 2;
  68. constexpr size_t kMinimumInputNum = 3;
  69. constexpr size_t kLambNextMVWithDecayInputNum = 7;
  70. constexpr size_t kLambNextMVWithDecayConstantMulInputNum = 5;
  71. constexpr size_t kLambNextMVWithDecayOutputNum = 4;
  72. constexpr size_t kLambNextMVWithDecayV1OutputNum = 4;
  73. constexpr size_t kLambNextRightOutputNum = 2;
  74. constexpr size_t kLambUpdateWithLrV2InputNum = 8;
  75. constexpr size_t kLambNextMVRuleInputNum = 14;
  76. constexpr size_t kLambNextMVRuleOutputNum = 4;
  77. constexpr size_t kBackendReshapeInputNum = 2;
  78. constexpr size_t kBackendTransposeInputNum = 2;
  79. constexpr size_t kAdamApplyOneWithDecayOutputNum = 3;
  80. constexpr size_t kLayerNormBetaGammaBackpropInputNum = 5;
  81. constexpr size_t kLayerNormBetaGammaBackpropOutputNum = 2;
  82. constexpr size_t kLayerNormGradInputNum = 6;
  83. constexpr size_t kAdamApplyOneOutputNum = 3;
  84. constexpr size_t kBackendTransDataInputNum = 2;
  85. constexpr size_t kApplyMomentumInputNum = 6;
  86. constexpr size_t kBiasAddInputNum = 3;
  87. constexpr size_t kTopkInputNum = 3;
  88. constexpr size_t kLarsV2InputNum = 5;
  89. constexpr size_t kFusedMulApplyMomentumOutputNum = 2;
  90. constexpr size_t kSplitInputNum = 2;
  91. constexpr size_t kUnsortedSegmentSumInputNum = 2;
  92. enum FusedBatchNormInput {
  93. kX = 1,
  94. kVariance = 5,
  95. };
  96. enum FusedBatchNormOutput {
  97. kY = 0,
  98. kRunningMean,
  99. kRunningVariance,
  100. kSaveMean,
  101. kSaveInvVariance,
  102. };
  103. enum ConvBn1Output {
  104. kData = 0,
  105. kVarPart,
  106. kMean,
  107. };
  108. std::vector<int> Convert2Int(const std::vector<size_t> &v);
  109. // check whether node depends on either of nodes or not
  110. bool IsDepend(const FuncGraph &graph, const AnfNodePtr &node, const std::vector<AnfNodePtr> &nodes);
  111. bool UnVisited(const BaseRef &n);
  112. bool Visited(const BaseRef &n);
  113. // check if the input node is CNode, then check it's input_size, if meet condition above, return true, otherwise return
  114. // false. cnode can only be used when return true.
  115. bool CheckIfCNodeAndInputSize(const AnfNodePtr &node, int input_size, CNodePtr *cnode);
  116. // check if the input node is CNode, then check it's input_size, return CNodePtr if check success.
  117. CNodePtr CheckAnfNodeIfCNodeAndInputSize(const AnfNodePtr &node, int input_size);
  118. void CheckCNodeInputSize(const CNodePtr &cnode, size_t input_size);
  119. bool HasSymmetricalKernelInfo(const AnfNodePtr &node_x, const AnfNodePtr &node_y);
  120. const AnfNodePtr EliminateDependTransop(const FuncGraphPtr &func_graph, const AnfNodePtr &node);
  121. void CreateOutputsOfConvBn1(const FuncGraphPtr &func_graph, const CNodePtr &conv_cnode, const CNodePtr &bn_cnode,
  122. std::vector<AnfNodePtr> *conv_bn1_outputs);
  123. void CreateOutputsOfFusedBn2(const FuncGraphPtr &graph, const std::vector<AnfNodePtr> &fused_bn1_outputs,
  124. const CNodePtr &bn_node, std::vector<AnfNodePtr> *fused_bn2_outputs);
  125. void CreateOutputsOfFusedBn3(const FuncGraphPtr &graph, const AnfNodePtr &data_input,
  126. const std::vector<AnfNodePtr> &fused_bn1_outputs,
  127. const std::vector<AnfNodePtr> &fused_bn2_outputs, const CNodePtr &bn_node,
  128. std::vector<AnfNodePtr> *fused_bn3_outputs);
  129. void CreateMultipleOutputsOfAnfNode(const FuncGraphPtr &kernel_graph, const AnfNodePtr &anf_node_ptr, size_t output_num,
  130. std::vector<AnfNodePtr> *outputs);
  131. tensor::TensorPtr CreateTensorWithValueTuple(const ValueTuplePtr &value_tuple_ptr, const TypePtr &type_ptr,
  132. size_t data_length);
  133. tensor::TensorPtr CreateTupleTensor(const ValueTuplePtr &value_tuple);
  134. bool IsAllNopNode(const session::KernelGraph *const graph);
  135. bool IsNopNode(const AnfNodePtr &node);
  136. void HideNopNode(session::KernelGraph *const graph);
  137. void RemoveNopNode(session::KernelGraph *const graph);
  138. AnfNodePtr CreatTupleGetItemNode(const FuncGraphPtr &func_graph, const AnfNodePtr &node, size_t output_idx);
  139. bool IsUsedByOthers(const FuncGraphPtr &graph, const AnfNodePtr &node);
  140. std::shared_ptr<std::vector<std::pair<AnfNodePtr, int>>> GetRealNodeUsedList(const FuncGraphPtr &graph,
  141. const AnfNodePtr &node);
  142. size_t GetRealNodeNum(const FuncGraphPtr &graph, const AnfNodePtr &node);
  143. std::shared_ptr<std::vector<std::pair<AnfNodePtr, int>>> GetRealNodeUsedListByOutputIdx(const FuncGraphPtr &graph,
  144. const AnfNodePtr &node,
  145. size_t output_index);
  146. bool IsNotRealUsedByOthers(const FuncGraphPtr &graph, const AnfNodePtr &node);
  147. void ConstInputToAttr(const CNodePtr &cnode, const std::unordered_set<size_t> &input_attrs);
  148. bool AnfEqual(const BaseRef &a, const BaseRef &b);
  149. bool CNodeTypeEqual(const BaseRef &a, const BaseRef &b);
  150. AnfNodePtr SexpToNode(const BaseRef &sexp, const BaseRef &graph, PrimitiveVarMap *primitive_vars,
  151. bool multigraph = false);
  152. // Check var_node in two equivs is the same node
  153. bool IsSameNode(const EquivPtr &equiv1, const EquivPtr &equiv2, const VarPtr &var_node);
  154. // Get anf_node from equiv by var_node
  155. AnfNodePtr GetAnfNodeByVar(const EquivPtr &equiv, const VarPtr &var_node);
  156. // Compare tuple getitem's index, return bool[n1's index < n2's index]
  157. bool CompareTupleGetitem(const AnfNodePtr &n1, const AnfNodePtr &n2);
  158. // Get attr which is bool from cnode
  159. bool GetBoolAttr(const AnfNodePtr &node, const std::string &attr_name);
  160. // Check node's data type is in supported data type set
  161. bool CheckSupportDataType(const AnfNodePtr &node, const std::set<TypeId> &supported_data_type_set);
  162. // Create a new value node of func graph,not kernel graph
  163. ValueNodePtr MakeValueNode(const ValueNodePtr &value_node);
  164. // Transfer depend or control_depend to the new node
  165. void TransferDepend(const CNodePtr &old_node, const FuncGraphPtr &graph, const CNodePtr &new_node);
  166. } // namespace opt
  167. } // namespace mindspore
  168. #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_HELPER_H_