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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 kHostAllGatherOpName = "HostAllGather";
  45. constexpr auto kBroadcastOpName = "Broadcast";
  46. constexpr auto kReduceScatterOpName = "ReduceScatter";
  47. constexpr auto kHostReduceScatterOpName = "HostReduceScatter";
  48. constexpr auto kMemCpyAsyncOpName = "memcpy_async";
  49. constexpr auto kTopKOpName = "TopK";
  50. constexpr auto kExtractImagePatchesOpName = "ExtractImagePatches";
  51. constexpr auto kBNTrainingReduceOpName = "BNTrainingReduce";
  52. constexpr auto kBNTrainingUpdateOpName = "BNTrainingUpdate";
  53. constexpr auto kBNTrainingUpdateV2OpName = "BNTrainingUpdateV2";
  54. constexpr auto kSimpleMeanGradOpName = "SimpleMeanGrad";
  55. constexpr auto kMeanGradOpName = "MeanGrad";
  56. constexpr auto kSliceOpName = "Slice";
  57. constexpr auto kSliceGradOpName = "SliceGrad";
  58. constexpr auto kTileOpName = "Tile";
  59. constexpr auto kScatterNdOpName = "ScatterNd";
  60. constexpr auto kStridedSliceAssignOpName = "StridedSliceAssign";
  61. constexpr auto kStridedSliceOpName = "StridedSlice";
  62. constexpr auto kStridedSliceGradOpName = "StridedSliceGrad";
  63. constexpr auto kUnsortedSegmentProdOpName = "UnsortedSegmentProd";
  64. constexpr auto kUnsortedSegmentMinOpName = "UnsortedSegmentMin";
  65. constexpr auto kFlattenGradOpName = "FlattenGrad";
  66. constexpr auto kExpandDimsOpName = "ExpandDims";
  67. constexpr auto kSplitOpName = "Split";
  68. constexpr auto kSparseApplyAdagradOpName = "SparseApplyAdagrad";
  69. constexpr auto kMomentumOpName = "Momentum";
  70. constexpr auto kApplyMomentumOpName = "ApplyMomentum";
  71. constexpr auto kApplyAdadeltaOpName = "ApplyAdadelta";
  72. constexpr auto kApplyAdagradOpName = "ApplyAdagrad";
  73. constexpr auto kApplyAdagradDAName = "ApplyAdagradDA";
  74. constexpr auto kApplyAdamOpName = "ApplyAdam";
  75. constexpr auto kApplyAdaMaxOpName = "ApplyAdaMax";
  76. constexpr auto kApplyAddSignOpName = "ApplyAddSign";
  77. constexpr auto kApplyCenteredRMSPOpName = "ApplyCenteredRMSP";
  78. constexpr auto kApplyFtrlOpName = "ApplyFtrl";
  79. constexpr auto kApplyFtrlV2OpName = "ApplyFtrlV2";
  80. constexpr auto kApplyGradientDescentOpName = "ApplyGradientDescent";
  81. constexpr auto kApplyPowerSignOpName = "ApplyPowerSign";
  82. constexpr auto kApplyProximalAdagradOpName = "ApplyProximalAdagrad ";
  83. constexpr auto kApplyProximalGradientDescentOpName = "ApplyProximalGradientDescent";
  84. constexpr auto kApplyRMSPropOpName = "ApplyRMSProp";
  85. constexpr auto kTransDataOpName = "TransData";
  86. constexpr auto kBNTrainingUpdateGradOpName = "BNTrainingUpdateGrad";
  87. constexpr auto kBNTrainingReduceGradOpName = "BNTrainingReduceGrad";
  88. constexpr auto kSquareSumV1OpName = "SquareSumV1";
  89. constexpr auto kSquareSumV2OpName = "SquareSumV2";
  90. constexpr auto kClipByNormNoDivSumOpName = "ClipByNormNoDivSum";
  91. constexpr auto kGreaterOpName = "Greater";
  92. constexpr auto kSqrtOpName = "Sqrt";
  93. constexpr auto kRsqrtOpName = "Rsqrt";
  94. constexpr auto kErfOpName = "Erf";
  95. constexpr auto kRealDivOpName = "RealDiv";
  96. constexpr auto kLambUpdateWithLROpName = "LambUpdateWithLR";
  97. constexpr auto kLambNextMVWithDecayOpName = "LambNextMVWithDecay";
  98. constexpr auto kLambNextMVWithDecayV1OpName = "LambNextMVWithDecayV1";
  99. constexpr auto kClipByValueOpName = "ClipByValue";
  100. constexpr auto kLambNextRightOpName = "LambNextRight";
  101. constexpr auto kConfusionSoftmaxGradOpName = "ConfusionSoftmaxGrad";
  102. constexpr auto kLambUpdateWithLrV2OpName = "LambUpdateWithLrV2";
  103. constexpr auto kLayerNormXBackpropOpName = "LayerNormXBackprop";
  104. constexpr auto kLayerNormBetaGammaBackpropOpName = "LayerNormBetaGammaBackprop";
  105. constexpr auto kLambNextMVOpName = "LambNextMV";
  106. constexpr auto kConfusionTransposeDOpName = "ConfusionTransposeD";
  107. constexpr auto kAdamApplyOneWithDecayOpName = "AdamApplyOneWithDecay";
  108. constexpr auto kBatchNormGradOpName = "BatchNormGrad";
  109. constexpr auto kBNInferOpName = "BNInfer";
  110. constexpr auto kAdamApplyOneOpName = "AdamApplyOne";
  111. constexpr auto kResizeNearestNeighborGradOpName = "ResizeNearestNeighborGrad";
  112. constexpr auto kFusedMulAddOpName = "FusedMulAdd";
  113. constexpr auto kFusedMulAddNOpName = "FusedMulAddN";
  114. constexpr auto kFusedMulApplyMomentumOpName = "FusedMulApplyMomentum";
  115. constexpr auto kBiasAddOpName = "BiasAdd";
  116. constexpr auto kConfusionMulGradOpName = "ConfusionMulGrad";
  117. constexpr auto kStreamSwitchOpName = "StreamSwitch";
  118. constexpr auto kStreamActiveOpName = "StreamActive";
  119. constexpr auto kAssignAddOpName = "AssignAdd";
  120. constexpr auto kSendOpName = "Send";
  121. constexpr auto kRecvOpName = "Recv";
  122. constexpr auto kReluV2OpName = "ReLUV2";
  123. constexpr auto kReluGradV2OpName = "ReluGradV2";
  124. constexpr auto kAddNOpName = "AddN";
  125. constexpr auto kResizeNearestNeighborV2OpName = "ResizeNearestNeighborV2";
  126. constexpr auto kResizeNearestNeighborV2GradOpName = "ResizeNearestNeighborV2Grad";
  127. constexpr auto kApplyRMSPropOpname = "ApplyRMSProp";
  128. constexpr auto kCumsumOpName = "Cumsum";
  129. constexpr auto kResizeBilinearV2OpName = "kResizeBilinearV2";
  130. constexpr auto kReduceProdOpName = "ReduceProd";
  131. constexpr auto kCumprodOpName = "Cumprod";
  132. constexpr auto kSpaceToBatchOpName = "SpaceToBatch";
  133. constexpr auto kBatchToSpaceOpName = "BatchToSpace";
  134. constexpr auto kPadOpName = "Pad";
  135. constexpr auto kConv2DBackpropInputOpName = "Conv2DBackpropInput";
  136. constexpr auto kFusionOpConv2DBackpropInputReluGradV2Name = "FusionOp_Conv2DBackpropInput_ReluGradV2";
  137. constexpr auto kFusionOpConv2DBackpropInputAddNReluGradV2Name = "FusionOp_Conv2DBackpropInput_AddN_ReluGradV2";
  138. constexpr auto kLabelSetOpName = "LabelSet";
  139. constexpr auto kLabelSwitchOpName = "LabelSwitch";
  140. constexpr auto kLabelGotoOpName = "LabelGoto";
  141. constexpr auto kBNInferGradOpName = "BNInferGrad";
  142. constexpr auto kCallOpName = "call";
  143. constexpr auto kPartialOpName = "partial";
  144. constexpr auto kSwitchOpName = "switch";
  145. constexpr auto kReturnOpName = "return";
  146. constexpr auto kLarsV2OpName = "LarsV2";
  147. constexpr auto kLarsV2UpdateOpName = "LarsV2Update";
  148. constexpr auto kSquareSumAllOpName = "SquareSumAll";
  149. constexpr auto kNMSWithMaskOpName = "NMSWithMask";
  150. // attr key name
  151. constexpr auto kAttrInputNames = "input_names";
  152. constexpr auto kAttrIsAICPUKernel = "is_AICPU_kernel";
  153. constexpr auto kIsBackendCast = "is_backed_cast";
  154. constexpr auto kAttrOutputNames = "output_names";
  155. constexpr auto kAttrVisited = "visited";
  156. constexpr auto kAttrShape = "shape";
  157. constexpr auto kAttrMomentum = "momentum";
  158. constexpr auto kAttrEps = "eps";
  159. constexpr auto kAttrEpsilon = "epsilon";
  160. constexpr auto kAttrFactor = "factor";
  161. constexpr auto kAttrIsRef = "isRef";
  162. constexpr auto kAttrDataShape = "data_shape";
  163. constexpr auto kAttrAxis = "axis";
  164. constexpr auto kAttrKeepDims = "keep_dims";
  165. constexpr auto kAttrShapeGamma = "shape_gamma";
  166. constexpr auto kAttrPerm = "perm";
  167. constexpr auto kAttrTransposeFirst = "transpose_first";
  168. constexpr auto kAttrAutomicAddMemSize = "automic_add_mem_size";
  169. constexpr auto kAttrAutomicOutputIndexs = "atomic_output_clean_indexs";
  170. constexpr auto kAttrAutomicWorkspaceSize = "atomic_workspace_clean_size";
  171. constexpr auto kAttrSwitchCondition = "switch_condition";
  172. constexpr auto kAttrDataType = "data_type";
  173. constexpr auto kAttrActiveTarget = "active_target";
  174. constexpr auto kAttrActiveStreamList = "active_stream_list";
  175. constexpr auto kAttrTrueBranchStream = "true_branch_stream";
  176. constexpr auto kAttrEventId = "event_id";
  177. constexpr auto kAttrDynInput = "dynamic";
  178. constexpr auto kAttrDynInputSizes = "dyn_input_sizes";
  179. constexpr auto kAttrSrcFormat = "src_format";
  180. constexpr auto kAttrOutputUsedNum = "output_used_num";
  181. constexpr auto kAttrHasBias = "has_bias";
  182. constexpr auto kAttrN = "n";
  183. constexpr auto kAttrLabelForInsertStreamActive = "label_for_insert_stream_active";
  184. constexpr auto kAttrFusion = "fusion";
  185. constexpr auto kAttrGroup = "group";
  186. constexpr auto kAttrOp = "op";
  187. constexpr auto kAttrIsTraining = "is_training";
  188. constexpr auto kAttrFusionId = "fusion_id";
  189. constexpr auto kAttrLabelIndex = "label_index";
  190. constexpr auto kAttrLabelSwitchList = "label_switch_list";
  191. constexpr auto kAttrNewAxisMask = "new_axis_mask";
  192. constexpr auto kAttrShrinkAxisMask = "shrink_axis_mask";
  193. // attr value
  194. constexpr auto kValueTargetSwitch = "target_switch";
  195. constexpr auto kValueTargetOther = "target_other";
  196. // some size
  197. const size_t kShape4dDims = 4;
  198. const size_t kShape5dDims = 5;
  199. const size_t kCubeSize = 16;
  200. const size_t kMemAlignSize = 512;
  201. const int kParameterDataTensorMask = 0;
  202. const int kParameterWeightTensorMask = 1;
  203. const int kValueNodeTensorMask = 2;
  204. // define special index in special node
  205. constexpr auto kAnfPrimitiveIndex = 0;
  206. constexpr auto kAnfPartialFuncGraphIndex = 1;
  207. constexpr auto kRealInputNodeIndexInTupleGetItem = 1;
  208. constexpr auto kInputNodeOutputIndexInTupleGetItem = 2;
  209. constexpr auto kTupleGetItemInputSize = 3;
  210. // index define of control depend
  211. constexpr auto kControlDependPriorIndex = 1;
  212. constexpr auto kControlDependBehindIndex = 2;
  213. // index define of depend
  214. constexpr auto kRealInputIndexInDepend = 1;
  215. constexpr auto kDependAttachNodeIndex = 2;
  216. // format
  217. constexpr auto kOpFormat_DEFAULT = "DefaultFormat";
  218. constexpr auto kOpFormat_NC1KHKWHWC0 = "NC1KHKWHWC0";
  219. constexpr auto kOpFormat_ND = "ND";
  220. constexpr auto kOpFormat_NCHW = "NCHW";
  221. constexpr auto kOpFormat_NHWC = "NHWC";
  222. constexpr auto kOpFormat_HWCN = "HWCN";
  223. constexpr auto kOpFormat_NC1HWC0 = "NC1HWC0";
  224. constexpr auto kOpFormat_FRAC_Z = "FracZ";
  225. constexpr auto kOpFormat_FRAC_NZ = "FRACTAL_NZ";
  226. constexpr auto kOpFormat_C1HWNCoC0 = "C1HWNCoC0";
  227. constexpr auto kOpFormat_NC1HWC0_C04 = "NC1HWC0_C04";
  228. constexpr auto kOpFormat_FRACTAL_Z_C04 = "FRACTAL_Z_C04";
  229. constexpr auto kOpFormat_NDHWC = "NDHWC";
  230. const std::set<std::string> kOpFormatList = {
  231. kOpFormat_DEFAULT, kOpFormat_NC1KHKWHWC0, kOpFormat_ND, kOpFormat_NCHW, kOpFormat_NHWC,
  232. kOpFormat_HWCN, kOpFormat_NC1HWC0, kOpFormat_FRAC_Z, kOpFormat_C1HWNCoC0, kOpFormat_FRAC_NZ,
  233. kOpFormat_NC1HWC0_C04, kOpFormat_FRACTAL_Z_C04, kOpFormat_NDHWC};
  234. const std::set<std::string> kDefaultCompatibleFormat = {kOpFormat_ND, kOpFormat_NCHW, kOpFormat_NHWC, kOpFormat_HWCN};
  235. const std::set<std::string> kOptOperatorSet = {
  236. kMomentumOpName, kApplyMomentumOpName, kApplyAdadeltaOpName,
  237. kApplyAdagradOpName, kApplyAdagradDAName, kApplyAdamOpName,
  238. kApplyAdaMaxOpName, kApplyAddSignOpName, kApplyCenteredRMSPOpName,
  239. kApplyFtrlOpName, kApplyFtrlV2OpName, kApplyGradientDescentOpName,
  240. kApplyPowerSignOpName, kApplyProximalAdagradOpName, kApplyProximalGradientDescentOpName,
  241. kApplyRMSPropOpName,
  242. };
  243. const std::set<std::string> kNeedTransFormatSet = {kOpFormat_FRAC_Z, kOpFormat_NC1KHKWHWC0, kOpFormat_NC1HWC0,
  244. kOpFormat_FRAC_NZ, kOpFormat_C1HWNCoC0, kOpFormat_NC1HWC0_C04,
  245. kOpFormat_FRACTAL_Z_C04};
  246. static inline void ChangeFileMode(const std::string &file_name, mode_t mode) {
  247. if (access(file_name.c_str(), F_OK) != 0) {
  248. MS_LOG(DEBUG) << "File `" << file_name << "` does not exist.";
  249. return;
  250. }
  251. if (chmod(file_name.c_str(), mode) != 0) {
  252. MS_LOG(WARNING) << "Change file `" << file_name << "` to mode " << std::oct << mode << " fail.";
  253. }
  254. }
  255. } // namespace mindspore
  256. #endif // MINDSPORE_MINDSPORE_CCSRC_UTILS_UTILS_H_