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.

utils.h 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_MINDSPORE_CCSRC_UTILS_UTILS_H_
  17. #define MINDSPORE_MINDSPORE_CCSRC_UTILS_UTILS_H_
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <sys/stat.h>
  21. #include <string>
  22. #include <vector>
  23. #include <set>
  24. #include "utils/log_adapter.h"
  25. namespace mindspore {
  26. // op name. Op which not exists in operator/ops.h, so define it's name here
  27. constexpr auto kFour2FiveOpName = "Four2Five";
  28. constexpr auto kFive2FourOpName = "Five2Four";
  29. constexpr auto kConvBN1OpName = "ConvBN1";
  30. constexpr auto kBN2AddReluOpName = "BN2AddRelu";
  31. constexpr auto kBN2ReLUOpName = "BN2Relu";
  32. constexpr auto kBN2OpName = "BN2";
  33. constexpr auto kFusedBN1OpName = "FusedBN1";
  34. constexpr auto kFusedBN2OpName = "FusedBN2";
  35. constexpr auto kFusedBN3OpName = "FusedBN3";
  36. constexpr auto kBNGrad1OpName = "BNGrad1";
  37. constexpr auto kBNGrad2OpName = "BNGrad2";
  38. constexpr auto kBNGrad3OpName = "BNGrad3";
  39. constexpr auto kClearZeroOpName = "ClearZero";
  40. constexpr auto kAtomicAddrCleanOpName = "AtomicAddrClean";
  41. constexpr auto kGetNextOpName = "GetNext";
  42. constexpr auto kAllReduceOpName = "AllReduce";
  43. constexpr auto kAllGatherOpName = "AllGather";
  44. constexpr auto kBroadcastOpName = "Broadcast";
  45. constexpr auto kReduceScatterOpName = "ReduceScatter";
  46. constexpr auto kMemCpyAsyncOpName = "memcpy_async";
  47. constexpr auto kTopKOpName = "TopK";
  48. constexpr auto kExtractImagePatchesOpName = "ExtractImagePatches";
  49. constexpr auto kBNTrainingReduceOpName = "BNTrainingReduce";
  50. constexpr auto kBNTrainingUpdateOpName = "BNTrainingUpdate";
  51. constexpr auto kSimpleMeanGradOpName = "SimpleMeanGrad";
  52. constexpr auto kMeanGradOpName = "MeanGrad";
  53. constexpr auto kSliceOpName = "Slice";
  54. constexpr auto kSliceGradOpName = "SliceGrad";
  55. constexpr auto kTileOpName = "Tile";
  56. constexpr auto kScatterNdOpName = "ScatterNd";
  57. constexpr auto kStridedSliceAssignOpName = "StridedSliceAssign";
  58. constexpr auto kStridedSliceOpName = "StridedSlice";
  59. constexpr auto kStridedSliceGradOpName = "StridedSliceGrad";
  60. constexpr auto kUnsortedSegmentProdOpName = "UnsortedSegmentProd";
  61. constexpr auto kUnsortedSegmentMinOpName = "UnsortedSegmentMin";
  62. constexpr auto kFlattenGradOpName = "FlattenGrad";
  63. constexpr auto kExpandDimsOpName = "ExpandDims";
  64. constexpr auto kSplitOpName = "Split";
  65. constexpr auto kSparseApplyAdagradOpName = "SparseApplyAdagrad";
  66. constexpr auto kMomentumOpName = "Momentum";
  67. constexpr auto kApplyMomentumOpName = "ApplyMomentum";
  68. constexpr auto kApplyAdadeltaOpName = "ApplyAdadelta";
  69. constexpr auto kApplyAdagradOpName = "ApplyAdagrad";
  70. constexpr auto kApplyAdagradDAName = "ApplyAdagradDA";
  71. constexpr auto kApplyAdamOpName = "ApplyAdam";
  72. constexpr auto kApplyAdaMaxOpName = "ApplyAdaMax";
  73. constexpr auto kApplyAddSignOpName = "ApplyAddSign";
  74. constexpr auto kApplyCenteredRMSPOpName = "ApplyCenteredRMSP";
  75. constexpr auto kApplyFtrlOpName = "ApplyFtrl";
  76. constexpr auto kApplyFtrlV2OpName = "ApplyFtrlV2";
  77. constexpr auto kApplyGradientDescentOpName = "ApplyGradientDescent";
  78. constexpr auto kApplyPowerSignOpName = "ApplyPowerSign";
  79. constexpr auto kApplyProximalAdagradOpName = "ApplyProximalAdagrad ";
  80. constexpr auto kApplyProximalGradientDescentOpName = "ApplyProximalGradientDescent";
  81. constexpr auto kApplyRMSPropOpName = "ApplyRMSProp";
  82. constexpr auto kTransDataOpName = "TransData";
  83. constexpr auto kBNTrainingUpdateGradOpName = "BNTrainingUpdateGrad";
  84. constexpr auto kBNTrainingReduceGradOpName = "BNTrainingReduceGrad";
  85. constexpr auto kSquareSumV1OpName = "SquareSumV1";
  86. constexpr auto kSquareSumV2OpName = "SquareSumV2";
  87. constexpr auto kClipByNormNoDivSumOpName = "ClipByNormNoDivSum";
  88. constexpr auto kGreaterOpName = "Greater";
  89. constexpr auto kSqrtOpName = "Sqrt";
  90. constexpr auto kRsqrtOpName = "Rsqrt";
  91. constexpr auto kErfOpName = "Erf";
  92. constexpr auto kRealDivOpName = "RealDiv";
  93. constexpr auto kLambUpdateWithLROpName = "LambUpdateWithLR";
  94. constexpr auto kLambNextMVWithDecayOpName = "LambNextMVWithDecay";
  95. constexpr auto kLambNextMVWithDecayV1OpName = "LambNextMVWithDecayV1";
  96. constexpr auto kClipByValueOpName = "ClipByValue";
  97. constexpr auto kLambNextRightOpName = "LambNextRight";
  98. constexpr auto kConfusionSoftmaxGradOpName = "ConfusionSoftmaxGrad";
  99. constexpr auto kLambUpdateWithLrV2OpName = "LambUpdateWithLrV2";
  100. constexpr auto kLayerNormXBackpropOpName = "LayerNormXBackprop";
  101. constexpr auto kLayerNormBetaGammaBackpropOpName = "LayerNormBetaGammaBackprop";
  102. constexpr auto kLambNextMVOpName = "LambNextMV";
  103. constexpr auto kConfusionTransposeDOpName = "ConfusionTransposeD";
  104. constexpr auto kAdamApplyOneWithDecayOpName = "AdamApplyOneWithDecay";
  105. constexpr auto kBatchNormOpName = "BatchNorm";
  106. constexpr auto kAdamApplyOneOpName = "AdamApplyOne";
  107. constexpr auto kDropoutGenMask = "DropoutGenMask";
  108. constexpr auto kResizeNearestNeighborGrad = "ResizeNearestNeighborGrad";
  109. constexpr auto kFusedMulAddOpName = "FusedMulAdd";
  110. constexpr auto kFusedMulAddNOpName = "FusedMulAddN";
  111. constexpr auto kFusedMulApplyMomentumOpName = "FusedMulApplyMomentum";
  112. constexpr auto kBiasAddOpName = "BiasAdd";
  113. constexpr auto kConfusionMulGradOpName = "ConfusionMulGrad";
  114. constexpr auto kStreamSwitchOpName = "StreamSwitch";
  115. constexpr auto kStreamActiveOpName = "StreamActive";
  116. constexpr auto kAssignAddOpName = "AssignAdd";
  117. constexpr auto kSendOpName = "Send";
  118. constexpr auto kRecvOpName = "Recv";
  119. constexpr auto kReluV2OpName = "ReLUV2";
  120. constexpr auto kReluGradV2OpName = "ReluGradV2";
  121. constexpr auto kFusionOpConv2DBackpropInputReluGradV2Name = "FusionOp_Conv2DBackpropInput_ReluGradV2";
  122. constexpr auto kFusionOpConv2DBackpropInputAddNReluGradV2Name = "FusionOp_Conv2DBackpropInput_AddN_ReluGradV2";
  123. // attr key name
  124. constexpr auto kAttrInputNames = "input_names";
  125. constexpr auto kAttrOutputNames = "output_names";
  126. constexpr auto kAttrVisited = "visited";
  127. constexpr auto kAttrShape = "shape";
  128. constexpr auto kAttrMomentum = "momentum";
  129. constexpr auto kAttrEps = "eps";
  130. constexpr auto kAttrEpsilon = "epsilon";
  131. constexpr auto kAttrFactor = "factor";
  132. constexpr auto kAttrIsRef = "isRef";
  133. constexpr auto kAttrDataShape = "data_shape";
  134. constexpr auto kAttrAxis = "axis";
  135. constexpr auto kAttrKeepDims = "keep_dims";
  136. constexpr auto kAttrShapeGamma = "shape_gamma";
  137. constexpr auto kAttrPerm = "perm";
  138. constexpr auto kAttrTransposeFirst = "transpose_first";
  139. constexpr auto kAttrAutomicAddMemSize = "automic_add_mem_size";
  140. constexpr auto kAttrAutomicOutputIndexs = "atomic_output_clean_indexs";
  141. constexpr auto kAttrAutomicWorkspaceSize = "atomic_workspace_clean_size";
  142. constexpr auto kAttrSwitchCondition = "switch_condition";
  143. constexpr auto kAttrDataType = "data_type";
  144. constexpr auto kAttrActiveTarget = "active_target";
  145. constexpr auto kAttrActiveStreamList = "active_stream_list";
  146. constexpr auto kAttrTrueBranchStream = "true_branch_stream";
  147. constexpr auto kAttrEventId = "event_id";
  148. constexpr auto kAttrDynInput = "dynamic";
  149. constexpr auto kAttrDynInputSizes = "dyn_input_sizes";
  150. constexpr auto kAttrSrcFormat = "src_format";
  151. constexpr auto kAttrOutputUsedNum = "output_used_num";
  152. constexpr auto kAttrHasBias = "has_bias";
  153. constexpr auto kAttrN = "n";
  154. constexpr auto kAttrLabelForInsertStreamActive = "label_for_insert_stream_active";
  155. constexpr auto kAttrFusion = "fusion";
  156. constexpr auto kAttrGroup = "group";
  157. constexpr auto kAttrOp = "op";
  158. // attr value
  159. constexpr auto kValueTargetSwitch = "target_switch";
  160. constexpr auto kValueTargetOther = "target_other";
  161. // some size
  162. const size_t kShape4dDims = 4;
  163. const size_t kShape5dDims = 5;
  164. const size_t kCubeSize = 16;
  165. const size_t kMemAlignSize = 512;
  166. // define special index in special node
  167. constexpr auto kAnfPrimitiveIndex = 0;
  168. constexpr auto kRealInputNodeIndexInTupleGetItem = 1;
  169. constexpr auto kInputNodeOutputIndexInTupleGetItem = 2;
  170. constexpr auto kTupleGetItemInputSize = 3;
  171. // index define of control depend
  172. constexpr auto kControlDependPriorIndex = 1;
  173. constexpr auto kControlDependBehindIndex = 2;
  174. // index define of depend
  175. constexpr auto kRealInputIndexInDepend = 1;
  176. constexpr auto kDependAttachNodeIndex = 2;
  177. // format
  178. constexpr auto kOpFormat_DEFAULT = "DefaultFormat";
  179. constexpr auto kOpFormat_NC1KHKWHWC0 = "NC1KHKWHWC0";
  180. constexpr auto kOpFormat_ND = "ND";
  181. constexpr auto kOpFormat_NCHW = "NCHW";
  182. constexpr auto kOpFormat_NHWC = "NHWC";
  183. constexpr auto kOpFormat_HWCN = "HWCN";
  184. constexpr auto kOpFormat_NC1HWC0 = "NC1HWC0";
  185. constexpr auto kOpFormat_FRAC_Z = "FracZ";
  186. constexpr auto kOpFormat_FRAC_NZ = "FRACTAL_NZ";
  187. constexpr auto kOpFormat_C1HWNCoC0 = "C1HWNCoC0";
  188. constexpr auto kOpFormat_NC1HWC0_C04 = "NC1HWC0_C04";
  189. const std::set<std::string> k1DSupportFormat = {kOpFormat_DEFAULT, kOpFormat_NCHW, kOpFormat_NHWC,
  190. kOpFormat_FRAC_Z, kOpFormat_NC1KHKWHWC0, kOpFormat_NC1HWC0,
  191. kOpFormat_C1HWNCoC0};
  192. const std::set<std::string> k2DSupportFormat = {kOpFormat_DEFAULT, kOpFormat_NCHW, kOpFormat_NHWC, kOpFormat_FRAC_Z,
  193. kOpFormat_NC1KHKWHWC0};
  194. const std::set<std::string> k3DSupportFormat = {kOpFormat_DEFAULT, kOpFormat_NC1KHKWHWC0};
  195. const std::set<std::string> k4DSupportFormat = k1DSupportFormat;
  196. const std::vector<std::set<std::string>> kShapeSupportFormatMap = {k1DSupportFormat, k2DSupportFormat, k3DSupportFormat,
  197. k4DSupportFormat};
  198. const std::set<std::string> kDefaultCompatibleFormat = {kOpFormat_ND, kOpFormat_NCHW, kOpFormat_NHWC, kOpFormat_HWCN};
  199. const std::set<std::string> kOptOperatorSet = {
  200. kMomentumOpName, kApplyMomentumOpName, kApplyAdadeltaOpName,
  201. kApplyAdagradOpName, kApplyAdagradDAName, kApplyAdamOpName,
  202. kApplyAdaMaxOpName, kApplyAddSignOpName, kApplyCenteredRMSPOpName,
  203. kApplyFtrlOpName, kApplyFtrlV2OpName, kApplyGradientDescentOpName,
  204. kApplyPowerSignOpName, kApplyProximalAdagradOpName, kApplyProximalGradientDescentOpName,
  205. kApplyRMSPropOpName,
  206. };
  207. const std::set<std::string> kNeedTransFormatSet = {kOpFormat_FRAC_Z, kOpFormat_NC1KHKWHWC0, kOpFormat_NC1HWC0,
  208. kOpFormat_FRAC_NZ, kOpFormat_C1HWNCoC0};
  209. static inline void ChangeFileMode(const std::string &file_name, mode_t mode) {
  210. if (access(file_name.c_str(), F_OK) != 0) {
  211. MS_LOG(DEBUG) << "File `" << file_name << "` does not exist.";
  212. return;
  213. }
  214. if (chmod(file_name.c_str(), mode) != 0) {
  215. MS_LOG(WARNING) << "Change file `" << file_name << "` to mode " << std::oct << mode << " fail.";
  216. }
  217. }
  218. } // namespace mindspore
  219. #endif // MINDSPORE_MINDSPORE_CCSRC_UTILS_UTILS_H_