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 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_PRE_ACTIVATE_COMMON_HELPER_H_
  17. #define MINDSPORE_CCSRC_PRE_ACTIVATE_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 "session/kernel_graph.h"
  26. #include "common/utils.h"
  27. #include "pre_activate/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. enum FusedBatchNormInput {
  92. kX = 1,
  93. kVariance = 5,
  94. };
  95. enum FusedBatchNormOutput {
  96. kY = 0,
  97. kRunningMean,
  98. kRunningVariance,
  99. kSaveMean,
  100. kSaveInvVariance,
  101. };
  102. enum ConvBn1Output {
  103. kData = 0,
  104. kVarPart,
  105. kMean,
  106. };
  107. std::vector<int> Convert2Int(const std::vector<size_t> &v);
  108. // check whether node1 depends on node2 or not
  109. bool IsDepend(const FuncGraphPtr &graph, const AnfNodePtr &node1, const AnfNodePtr &node2);
  110. bool UnVisited(const BaseRef &n);
  111. bool Visited(const BaseRef &n);
  112. // check if the input node is CNode, then check it's input_size, if meet condition above, return true, otherwise return
  113. // false. cnode can only be used when return true.
  114. bool CheckIfCNodeAndInputSize(const AnfNodePtr &node, int input_size, CNodePtr *cnode);
  115. // check if the input node is CNode, then check it's input_size, return CNodePtr if check success.
  116. CNodePtr CheckAnfNodeIfCNodeAndInputSize(const AnfNodePtr &node, int input_size);
  117. void CheckCNodeInputSize(const CNodePtr &cnode, size_t input_size);
  118. bool HasSymmetricalKernelInfo(const AnfNodePtr &node_x, const AnfNodePtr &node_y);
  119. const AnfNodePtr EliminateDependTransop(const FuncGraphPtr &func_graph, const AnfNodePtr &node);
  120. void CreateOutputsOfConvBn1(const FuncGraphPtr &func_graph, const CNodePtr &conv_cnode, const CNodePtr &bn_cnode,
  121. std::vector<AnfNodePtr> *conv_bn1_outputs);
  122. void CreateOutputsOfFusedBn2(const FuncGraphPtr &graph, const std::vector<AnfNodePtr> &fused_bn1_outputs,
  123. const CNodePtr &bn_node, std::vector<AnfNodePtr> *fused_bn2_outputs);
  124. void CreateOutputsOfFusedBn3(const FuncGraphPtr &graph, const AnfNodePtr &data_input,
  125. const std::vector<AnfNodePtr> &fused_bn1_outputs,
  126. const std::vector<AnfNodePtr> &fused_bn2_outputs, const CNodePtr &bn_node,
  127. std::vector<AnfNodePtr> *fused_bn3_outputs);
  128. void CreateMultipleOutputsOfAnfNode(const FuncGraphPtr &kernel_graph, const AnfNodePtr &anf_node_ptr, size_t output_num,
  129. std::vector<AnfNodePtr> *outputs);
  130. tensor::TensorPtr CreateTensorWithValueTuple(const ValueTuplePtr &value_tuple_ptr, const TypePtr &type_ptr,
  131. size_t data_length);
  132. tensor::TensorPtr CreateTupleTensor(const ValueTuplePtr &value_tuple);
  133. bool IsAllNopNode(const session::KernelGraph *const graph);
  134. bool IsNopNode(const AnfNodePtr &node);
  135. void HideNopNode(session::KernelGraph *const graph);
  136. void RemoveNopNode(session::KernelGraph *const graph);
  137. AnfNodePtr CreatTupleGetItemNode(const FuncGraphPtr &func_graph, const AnfNodePtr &node, size_t output_idx);
  138. bool IsUsedByOthers(const FuncGraphPtr &graph, const AnfNodePtr &node);
  139. std::shared_ptr<std::vector<std::pair<AnfNodePtr, int>>> GetRealNodeUsedList(const FuncGraphPtr &graph,
  140. const AnfNodePtr &node);
  141. void ConstInputToAttr(const CNodePtr &cnode, const std::unordered_set<size_t> &input_attrs);
  142. bool AnfEqual(const BaseRef &a, const BaseRef &b);
  143. bool CNodeTypeEqual(const BaseRef &a, const BaseRef &b);
  144. AnfNodePtr SexpToNode(const BaseRef &sexp, const BaseRef &graph, PrimitiveVarMap *primitive_vars,
  145. bool multigraph = false);
  146. // Check var_node in two equivs is the same node
  147. bool IsSameNode(const EquivPtr &equiv1, const EquivPtr &equiv2, const VarPtr &var_node);
  148. // Get anf_node from equiv by var_node
  149. AnfNodePtr GetAnfNodeByVar(const EquivPtr &equiv, const VarPtr &var_node);
  150. // Compare tuple getitem's index, return bool[n1's index < n2's index]
  151. bool CompareTupleGetitem(const AnfNodePtr &n1, const AnfNodePtr &n2);
  152. // Get attr which is bool from cnode
  153. bool GetBoolAttr(const AnfNodePtr &node, const std::string &attr_name);
  154. // Check node's data type is in supported data type set
  155. bool CheckSupportDataType(const AnfNodePtr &node, const std::set<TypeId> &supported_data_type_set);
  156. } // namespace opt
  157. } // namespace mindspore
  158. #endif // MINDSPORE_CCSRC_PRE_ACTIVATE_COMMON_HELPER_H_