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

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