1 /**
2 * Copyright 2019-2021 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
17 #ifndef MINDSPORE_CCSRC_UTILS_UTILS_H_
18 #define MINDSPORE_CCSRC_UTILS_UTILS_H_
19
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <sys/stat.h>
23 #include <sys/time.h>
24 #include <string>
25 #include <vector>
26 #include <set>
27
28 #include "utils/log_adapter.h"
29 #include "ir/dtype/type.h"
30
31 namespace mindspore {
32 // op name. Op which not exists in operator/ops.h, so define it's name here
33 constexpr auto kConcatOpName = "Concat";
34 constexpr auto kUniqueOpName = "Unique";
35 constexpr auto kMaskedSelectOpName = "MaskedSelect";
36 constexpr auto kComputeAccidentalHitsOpName = "ComputeAccidentalHits";
37 constexpr auto kCTCGreedyDecoderOpName = "CTCGreedyDecoder";
38 constexpr auto kDynamicStitchOpName = "DynamicStitch";
39 constexpr auto kFour2FiveOpName = "Four2Five";
40 constexpr auto kFive2FourOpName = "Five2Four";
41 constexpr auto kConv3DOpName = "Conv3D";
42 constexpr auto kConv3DBackpropFilterOpName = "Conv3DBackpropFilter";
43 constexpr auto kConv3DBackpropInputOpName = "Conv3DBackpropInput";
44 constexpr auto kConv2DOpName = "Conv2D";
45 constexpr auto kConvBN1OpName = "ConvBN1";
46 constexpr auto kBN2AddReluOpName = "BN2AddRelu";
47 constexpr auto kBN2ReLUOpName = "BN2Relu";
48 constexpr auto kBN2OpName = "BN2";
49 constexpr auto kFusedBN1OpName = "FusedBN1";
50 constexpr auto kFusedBN2OpName = "FusedBN2";
51 constexpr auto kFusedBN3OpName = "FusedBN3";
52 constexpr auto kBNGrad1OpName = "BNGrad1";
53 constexpr auto kBNGrad2OpName = "BNGrad2";
54 constexpr auto kBNGrad3OpName = "BNGrad3";
55 constexpr auto kBatchNorm = "BatchNorm";
56 constexpr auto kInstanceNorm = "InstanceNorm";
57 constexpr auto kBatchNormWithActivation = "BatchNormWithActivation";
58 constexpr auto kBatchNormWithAddAndActivation = "BatchNormWithAddAndActivation";
59 constexpr auto kBatchNormGradWithActivation = "BatchNormGradWithActivation";
60 constexpr auto kBatchNormGradWithAddAndActivation = "BatchNormGradWithAddAndActivation";
61 constexpr auto kClearZeroOpName = "ClearZero";
62 constexpr auto kAtomicAddrCleanOpName = "AtomicAddrClean";
63 constexpr auto kGetNextOpName = "GetNext";
64 constexpr auto kInitDatasetQueueOpName = "InitDataSetQueue";
65 constexpr auto kEndOfSequence = "EndOfSequence";
66 constexpr auto kAllToAllVOpName = "AllToAllv";
67 constexpr auto kAllReduceOpName = "AllReduce";
68 constexpr auto kAllGatherOpName = "AllGather";
69 constexpr auto kHostAllGatherOpName = "HostAllGather";
70 constexpr auto kBroadcastOpName = "Broadcast";
71 constexpr auto kReceiveOpName = "Receive";
72 constexpr auto kHcomSendOpName = "Send";
73 constexpr auto kReduceScatterOpName = "ReduceScatter";
74 constexpr auto kHostReduceScatterOpName = "HostReduceScatter";
75 constexpr auto kMemCpyAsyncOpName = "memcpy_async";
76 constexpr auto kTopKOpName = "TopK";
77 constexpr auto kLinSpaceOpName = "LinSpace";
78 constexpr auto kExtractImagePatchesOpName = "ExtractImagePatches";
79 constexpr auto kBNTrainingReduceOpName = "BNTrainingReduce";
80 constexpr auto kBNTrainingUpdateOpName = "BNTrainingUpdate";
81 constexpr auto kBNTrainingUpdateV2OpName = "BNTrainingUpdateV2";
82 constexpr auto kBNTrainingUpdateV3OpName = "BNTrainingUpdateV3";
83 constexpr auto kSimpleMeanGradOpName = "SimpleMeanGrad";
84 constexpr auto kMeanGradOpName = "MeanGrad";
85 constexpr auto kSliceOpName = "Slice";
86 constexpr auto kSliceGradOpName = "SliceGrad";
87 constexpr auto kTileOpName = "Tile";
88 constexpr auto kScatterNdOpName = "ScatterNd";
89 constexpr auto kStridedSliceAssignOpName = "StridedSliceAssign";
90 constexpr auto kStridedSliceOpName = "StridedSlice";
91 constexpr auto kStridedSliceGradOpName = "StridedSliceGrad";
92 constexpr auto kSparseGatherV2OpName = "SparseGatherV2";
93 constexpr auto kUnsortedSegmentProdOpName = "UnsortedSegmentProd";
94 constexpr auto kUnsortedSegmentMinOpName = "UnsortedSegmentMin";
95 constexpr auto kFlattenGradOpName = "FlattenGrad";
96 constexpr auto kExpandDimsOpName = "ExpandDims";
97 constexpr auto kReshapeOpName = "Reshape";
98 constexpr auto kTransposeOpName = "Transpose";
99 constexpr auto kTransposeNODOpName = "TransposeNOD";
100 constexpr auto kSplitOpName = "Split";
101 constexpr auto kSplitVOpName = "SplitV";
102 constexpr auto kSparseApplyAdagradOpName = "SparseApplyAdagrad";
103 constexpr auto kMomentumOpName = "Momentum";
104 constexpr auto kApplyMomentumOpName = "ApplyMomentum";
105 constexpr auto kCombineMomentumOpName = "CombineMomentum";
106 constexpr auto kCombineMomentumWeightOpName = "CombineMomentumWeight";
107 constexpr auto kApplyAdadeltaOpName = "ApplyAdadelta";
108 constexpr auto kApplyAdagradOpName = "ApplyAdagrad";
109 constexpr auto kApplyAdagradDAName = "ApplyAdagradDA";
110 constexpr auto kApplyAdamOpName = "Adam";
111 constexpr auto kApplyAdaMaxOpName = "ApplyAdaMax";
112 constexpr auto kApplyAddSignOpName = "ApplyAddSign";
113 constexpr auto kApplyCenteredRMSPOpName = "ApplyCenteredRMSP";
114 constexpr auto kApplyCenteredRMSPropOpName = "ApplyCenteredRMSProp";
115 constexpr auto kApplyFtrlOpName = "ApplyFtrl";
116 constexpr auto kApplyFtrlV2OpName = "ApplyFtrlV2";
117 constexpr auto kApplyGradientDescentOpName = "ApplyGradientDescent";
118 constexpr auto kApplyPowerSignOpName = "ApplyPowerSign";
119 constexpr auto kApplyProximalAdagradOpName = "ApplyProximalAdagrad ";
120 constexpr auto kApplyProximalGradientDescentOpName = "ApplyProximalGradientDescent";
121 constexpr auto kApplyRMSPropOpName = "ApplyRMSProp";
122 constexpr auto kTransDataOpName = "TransData";
123 constexpr auto kTransDataRNNOpName = "TransDataRNN";
124 constexpr auto kStackInitOpName = "StackInit";
125 constexpr auto kStackPushOpName = "StackPush";
126 constexpr auto kStackPopOpName = "StackPop";
127 constexpr auto kStackOpName = "Stack";
128 constexpr auto kStackDestroyOpName = "StackDestroy";
129 constexpr auto kBNTrainingUpdateGradOpName = "BNTrainingUpdateGrad";
130 constexpr auto kBNTrainingReduceGradOpName = "BNTrainingReduceGrad";
131 constexpr auto kSquareSumV1OpName = "SquareSumV1";
132 constexpr auto kSquareSumV2OpName = "SquareSumV2";
133 constexpr auto kClipByNormNoDivSumOpName = "ClipByNormNoDivSum";
134 constexpr auto kPReluOpName = "PReLU";
135 constexpr auto kGreaterOpName = "Greater";
136 constexpr auto kSqrtOpName = "Sqrt";
137 constexpr auto kRsqrtOpName = "Rsqrt";
138 constexpr auto kErfOpName = "Erf";
139 constexpr auto kRealDivOpName = "RealDiv";
140 constexpr auto kLambUpdateWithLROpName = "LambUpdateWithLR";
141 constexpr auto kLambNextMVWithDecayOpName = "LambNextMVWithDecay";
142 constexpr auto kLambNextMVWithDecayV1OpName = "LambNextMVWithDecayV1";
143 constexpr auto kClipByValueOpName = "ClipByValue";
144 constexpr auto kLambNextRightOpName = "LambNextRight";
145 constexpr auto kConfusionSoftmaxGradOpName = "ConfusionSoftmaxGrad";
146 constexpr auto kLambUpdateWithLrV2OpName = "LambUpdateWithLrV2";
147 constexpr auto kLayerNormXBackpropOpName = "LayerNormXBackprop";
148 constexpr auto kLayerNormXBackpropV2OpName = "LayerNormXBackpropV2";
149 constexpr auto kLayerNormBetaGammaBackpropOpName = "LayerNormBetaGammaBackprop";
150 constexpr auto kLayerNormBetaGammaBackpropV2OpName = "LayerNormBetaGammaBackpropV2";
151 constexpr auto kLambNextMVOpName = "LambNextMV";
152 constexpr auto kConfusionTransposeDOpName = "ConfusionTransposeD";
153 constexpr auto kAdamApplyOneWithDecayOpName = "AdamApplyOneWithDecay";
154 constexpr auto kAdamApplyOneWithDecayAssignOpName = "AdamApplyOneWithDecayAssign";
155 constexpr auto kBatchNormGradOpName = "BatchNormGrad";
156 constexpr auto kBNInferOpName = "BNInfer";
157 constexpr auto kAdamApplyOneOpName = "AdamApplyOne";
158 constexpr auto kAdamApplyOneAssignOpName = "AdamApplyOneAssign";
159 constexpr auto kResizeNearestNeighborGradOpName = "ResizeNearestNeighborGrad";
160 constexpr auto kFusedMulAddOpName = "FusedMulAdd";
161 constexpr auto kFusedMulAddNOpName = "FusedMulAddN";
162 constexpr auto kFusedMulApplyMomentumOpName = "FusedMulApplyMomentum";
163 constexpr auto kBiasAddOpName = "BiasAdd";
164 constexpr auto kConfusionMulGradOpName = "ConfusionMulGrad";
165 constexpr auto kStreamSwitchOpName = "StreamSwitch";
166 constexpr auto kStreamActiveOpName = "StreamActive";
167 constexpr auto kAssignAddOpName = "AssignAdd";
168 constexpr auto kSendOpName = "StreamSend";
169 constexpr auto kRecvOpName = "StreamRecv";
170 constexpr auto kReluV2OpName = "ReLUV2";
171 constexpr auto kReluGradV2OpName = "ReluGradV2";
172 constexpr auto kAddNOpName = "AddN";
173 constexpr auto kResizeNearestNeighborV2OpName = "ResizeNearestNeighborV2";
174 constexpr auto kResizeNearestNeighborV2GradOpName = "ResizeNearestNeighborV2Grad";
175 constexpr auto kApplyRMSPropOpname = "ApplyRMSProp";
176 constexpr auto kCumsumOpName = "Cumsum";
177 constexpr auto kInplaceAddOpName = "InplaceAdd";
178 constexpr auto kInplaceSubOpName = "InplaceSub";
179 constexpr auto kResizeBilinearV2OpName = "kResizeBilinearV2";
180 constexpr auto kReduceProdOpName = "ReduceProd";
181 constexpr auto kCumprodOpName = "Cumprod";
182 constexpr auto kSpaceToBatchOpName = "SpaceToBatch";
183 constexpr auto kBatchToSpaceOpName = "BatchToSpace";
184 constexpr auto kSpaceToDepthOpName = "SpaceToDepth";
185 constexpr auto kPadOpName = "Pad";
186 constexpr auto kConv2DTransposeOpName = "Conv2DTranspose";
187 constexpr auto kConv2DBackpropInputOpName = "Conv2DBackpropInput";
188 constexpr auto kConv2DBackpropFilterOpName = "Conv2DBackpropFilter";
189 constexpr auto kDepthwiseConv2dNativeOpName = "DepthwiseConv2dNative";
190 constexpr auto kDepthwiseConv2dNativeBackpropInputOpName = "DepthwiseConv2dNativeBackpropInput";
191 constexpr auto kDepthwiseConv2dNativeBackpropFilterOpName = "DepthwiseConv2dNativeBackpropFilter";
192 constexpr auto kFusionOpConv2DBackpropInputReluGradV2Name = "FusionOp_Conv2DBackpropInput_ReluGradV2";
193 constexpr auto kFusionOpConv2DBackpropInputAddNReluGradV2Name = "FusionOp_Conv2DBackpropInput_AddN_ReluGradV2";
194 constexpr auto kLabelSetOpName = "LabelSet";
195 constexpr auto kLabelSwitchOpName = "LabelSwitch";
196 constexpr auto kLabelGotoOpName = "LabelGoto";
197 constexpr auto kBNInferGradOpName = "BNInferGrad";
198 constexpr auto kCallOpName = "call";
199 constexpr auto kPartialOpName = "partial";
200 constexpr auto kSwitchOpName = "Switch";
201 constexpr auto kReturnOpName = "Return";
202 constexpr auto kBpropCutOpName = "bprop_cut";
203 constexpr auto kLarsV2OpName = "LarsV2";
204 constexpr auto kLarsV2UpdateOpName = "LarsV2Update";
205 constexpr auto kSquareSumAllOpName = "SquareSumAll";
206 constexpr auto kNMSWithMaskOpName = "NMSWithMask";
207 constexpr auto kSoftmaxGradExtOpName = "SoftmaxGradExt";
208 constexpr auto kStridedReadOpName = "StridedRead";
209 constexpr auto kStridedWriteOpName = "StridedWrite";
210 constexpr auto kFusedAdamWeightDecayName = "FusedAdamWeightDecay";
211 constexpr auto kAdamWeightDecayName = "AdamWeightDecay";
212 constexpr auto kFusedCastAdamWeightDecayName = "FusedCastAdamWeightDecay";
213 constexpr auto kFusedAdamName = "FusedAdam";
214 constexpr auto kFusedSparseAdamName = "FusedSparseAdam";
215 constexpr auto kFusedMatMulBiasAddName = "FusedMatMulBiasAdd";
216 constexpr auto kDeadNodeName = "DeadNode";
217 constexpr auto kPolyNodeName = "PolyNode";
218 constexpr auto kApplyAdagradV2OpName = "ApplyAdagradV2";
219 constexpr auto kSparseApplyAdagradV2OpName = "SparseApplyAdagradV2";
220 constexpr auto kSparseApplyFtrlOpName = "SparseApplyFtrl";
221 constexpr auto kSparseApplyFtrlV2OpName = "SparseApplyFtrlV2";
222 constexpr auto kApplyKerasMomentumOpName = "ApplyKerasMomentum";
223 constexpr auto kSparseApplyProximalAdagradOpName = "SparseApplyProximalAdagrad";
224 constexpr auto kSparseApplyRMSPropOpName = "SparseApplyRMSProp";
225 constexpr auto kSparseApplyAdadeltaOpName = "SparseApplyAdadelta";
226 constexpr auto kApplyAdamWithAmsgradOpName = "ApplyAdamWithAmsgrad";
227 constexpr auto kTensorMoveOpName = "TensorMove";
228 constexpr auto kTensorCopySlicesOpName = "TensorCopySlices";
229 constexpr auto kTensorScatterUpdateOpName = "TensorScatterUpdate";
230 constexpr auto kScatterNdUpdateOpName = "ScatterNdUpdate";
231 constexpr auto kPushOpName = "Push";
232 constexpr auto kPullOpName = "Pull";
233 constexpr auto kPushWeightOpName = "PushWeight";
234 constexpr auto kPullWeightOpName = "PullWeight";
235 constexpr auto kFusedPushWeightOpName = "FusedPushWeight";
236 constexpr auto kFusedPullWeightOpName = "FusedPullWeight";
237 constexpr auto kUpdateCacheOpName = "UpdateCache";
238 constexpr auto kCacheSwapTableOpName = "CacheSwapTable";
239 constexpr auto kEmbeddingLookupOpName = "EmbeddingLookup";
240 constexpr auto kEmbeddingLookupProxyOpName = "EmbeddingLookupProxy";
241 constexpr auto kGatherV2OpName = "Gather";
242 constexpr auto kPaddingOpName = "Padding";
243 constexpr auto kAvgPoolOpName = "AvgPool";
244 constexpr auto kAvgPoolGradOpName = "AvgPoolGrad";
245 constexpr auto kAvgPoolGradVmOpName = "AvgPoolGradVm";
246 constexpr auto kMaxPoolOpName = "MaxPool";
247 constexpr auto kmaxPoolGradOpName = "MaxPoolGrad";
248 constexpr auto kMaxPoolWithArgmaxOpName = "MaxPoolWithArgmax";
249 constexpr auto kMaxPoolGradWithArgmaxOpName = "MaxPoolGradWithArgmax";
250 constexpr auto kTensorAddOpName = "Add";
251 constexpr auto kMaxPool3DGradGradOpName = "MaxPool3DGradGrad";
252 constexpr auto kCastOpName = "Cast";
253 constexpr auto kGreaterEqualOpName = "GreaterEqual";
254 constexpr auto kAbsOpName = "Abs";
255 constexpr auto kExpOpName = "Exp";
256 constexpr auto kNegOpName = "Neg";
257 constexpr auto kMinimumOpName = "Minimum";
258 constexpr auto kMaximumOpName = "Maximum";
259 constexpr auto kMulOpName = "Mul";
260 constexpr auto kSubOpName = "Sub";
261 constexpr auto kLogOpName = "Log";
262 constexpr auto kPowOpName = "Pow";
263 constexpr auto kReciprocalOpName = "Reciprocal";
264 constexpr auto kEqualOpName = "Equal";
265 constexpr auto kLessOpName = "Less";
266 constexpr auto kLessEqualOpName = "LessEqual";
267 constexpr auto kSquareOpName = "Square";
268 constexpr auto kSelectOpName = "Select";
269 constexpr auto kReduceSumOpName = "ReduceSum";
270 constexpr auto kReduceMinOpName = "ReduceMin";
271 constexpr auto kReduceMaxOpName = "ReduceMax";
272 constexpr auto kReduceMeanOpName = "ReduceMean";
273 constexpr auto kReduceAnyOpName = "ReduceAny";
274 constexpr auto kReduceAllOpName = "ReduceAll";
275 constexpr auto kFusedWeightScaleApplyMomentum = "FusedWeightScaleApplyMomentum";
276 constexpr auto kFusedWeightApplyMomentum = "FusedWeightApplyMomentum";
277 constexpr auto kFusedScaleApplyMomentum = "FusedScaleApplyMomentum";
278 constexpr auto kBasicLSTMCellWeightGradOpName = "BasicLSTMCellWeightGrad";
279 constexpr auto kBasicLSTMCellInputGradOpName = "BasicLSTMCellInputGrad";
280 constexpr auto kBasicLSTMCellOpName = "BasicLSTMCell";
281 constexpr auto kDynamicRNNOpName = "DynamicRNN";
282 constexpr auto kLSTMOpName = "LSTM";
283 constexpr auto kLSTMGradOpName = "LSTMGrad";
284 constexpr auto kLSTMInputGradOpName = "LSTMInputGrad";
285 constexpr auto kDynamicGRUV2OpName = "DynamicGRUV2";
286 constexpr auto kGRUV2HiddenGradOpName = "GRUV2HiddenGrad";
287 constexpr auto kGRUV2HiddenGradCellOpName = "GRUV2HiddenGradCell";
288 constexpr auto kFusedSparseFtrlName = "FusedSparseFtrl";
289 constexpr auto kFusedSparseProximalAdagradName = "FusedSparseProximalAdagrad";
290 constexpr auto kFusedSparseLazyAdamName = "FusedSparseLazyAdam";
291 constexpr auto kSparseApplyFtrlName = "SparseApplyFtrl";
292 constexpr auto kSparseApplyFtrlV2Name = "SparseApplyFtrlV2";
293 constexpr auto kSGDName = "SGD";
294 constexpr auto kLARSUpdateName = "LARSUpdate";
295 constexpr auto kBasicLSTMCellCStateGradOpName = "BasicLSTMCellCStateGrad";
296 constexpr auto kBasicLSTMCellCStateGradV2OpName = "BasicLSTMCellCStateGradV2";
297 constexpr auto kMatMulOpName = "MatMul";
298 constexpr auto kMatMulV2OpName = "MatMulV2";
299 constexpr auto kBatchMatMulOpName = "BatchMatMul";
300 constexpr auto kBroadcastToOpName = "BroadcastTo";
301 constexpr auto kFusedAddReluV2Name = "FusedAddReluV2";
302 constexpr auto kFusedAddReluGradV2Name = "FusedAddReluGradV2";
303 constexpr auto kDropoutOpName = "Dropout";
304 constexpr auto kDropoutGradOpName = "DropoutGrad";
305 constexpr auto kDropoutGenMaskOpName = "DropoutGenMask";
306 constexpr auto kDropoutDoMaskOpName = "DropoutDoMask";
307 constexpr auto kSubAndFilterOpName = "SubAndFilter";
308 constexpr auto kPadAndShiftOpName = "PadAndShift";
309 constexpr auto kSparseSoftmaxCrossEntropyWithLogitsOpName = "SparseSoftmaxCrossEntropyWithLogits";
310 constexpr auto kOneHotOpName = "OneHot";
311 constexpr auto kSoftmaxCrossEntropyWithLogitsOpName = "SoftmaxCrossEntropyWithLogits";
312 constexpr auto kUniformCandidateSamplerOpName = "UniformCandidateSampler";
313 constexpr auto kLogSoftmaxGradOpName = "LogSoftmaxGrad";
314 constexpr auto kLayerNormGradOpName = "LayerNormGrad";
315 constexpr auto kMinimumGradOpName = "MinimumGrad";
316 constexpr auto kMaximumGradOpName = "MaximumGrad";
317 constexpr auto kFusedDbnDwOpName = "FusedDbnDw";
318 constexpr auto kNPUAllocFloatStatusOpName = "NPUAllocFloatStatus";
319 constexpr auto kNPUGetFloatStatusOpName = "NPUGetFloatStatus";
320 constexpr auto kNPUClearFloatStatusOpName = "NPUClearFloatStatus";
321 constexpr auto kAssignOpName = "Assign";
322
323 // Communication world group
324 constexpr auto kNcclWorldGroup = "nccl_world_group";
325 constexpr auto kHcclWorldGroup = "hccl_world_group";
326 constexpr auto kSyncBnGroup = "sync_bn_group";
327
328 // Hcom Op Type
329 constexpr auto kHcomOpTypeAllReduce = "HcomAllReduce";
330 constexpr auto kHcomOpTypeAllGather = "HcomAllGather";
331 constexpr auto kHcomOpTypeBroadcast = "HcomBroadcast";
332 constexpr auto kHcomOpTypeSend = "HcomSend";
333 constexpr auto kHcomOpTypeReceive = "HcomReceive";
334 constexpr auto kHcomOpTypeReduceScatter = "HcomReduceScatter";
335
336 // attr key name
337 constexpr auto kAttrInputNames = "input_names";
338 constexpr auto kAttrIsAICPUKernel = "is_AICPU_kernel";
339 constexpr auto kIsBackendCast = "is_backed_cast";
340 constexpr auto kAttrOutputNames = "output_names";
341 constexpr auto kAttrAsync = "async";
342 constexpr auto kAttrVisited = "visited";
343 constexpr auto kAttrShape = "shape";
344 constexpr auto kAttrMomentum = "momentum";
345 constexpr auto kAttrEps = "eps";
346 constexpr auto kAttrEpsilon = "epsilon";
347 constexpr auto kAttrFactor = "factor";
348 constexpr auto kAttrIsRef = "isRef";
349 constexpr auto kAttrDataShape = "data_shape";
350 constexpr auto kAttrFormat = "format";
351 constexpr auto kAttrReshapeType = "reshape_type";
352 constexpr auto kAttrAxis = "axis";
353 constexpr auto kAttrAxes = "axes";
354 constexpr auto kAttrKeepDims = "keep_dims";
355 constexpr auto kAttrShapeGamma = "shape_gamma";
356 constexpr auto kAttrPerm = "perm";
357 constexpr auto kAttrTransposeFirst = "transpose_first";
358 constexpr auto kAttrAtomicAddMemSize = "automic_add_mem_size";
359 constexpr auto kAttrAtomicOutputIndexs = "atomic_output_clean_indexs";
360 constexpr auto kAttrAtomicWorkspaceIndexs = "atomic_workspace_clean_indexs";
361 constexpr auto kAttrSwitchCondition = "switch_condition";
362 constexpr auto kAttrDataType = "data_type";
363 constexpr auto kAttrDType = "dtype";
364 constexpr auto kAttrActiveTarget = "active_target";
365 constexpr auto kAttrActiveStreamId = "active_stream_id";
366 constexpr auto kAttrActiveStreamList = "active_stream_list";
367 constexpr auto kAttrTrueBranchStream = "true_branch_stream";
368 constexpr auto kAttrStreamSwitchKind = "stream_switch_kind";
369 constexpr auto kAttrEventId = "event_id";
370 constexpr auto kAttrLabelId = "label_id";
371 constexpr auto kAttrLogicId = "logic_id";
372 constexpr auto kAttrNodeInfo = "node_info";
373 constexpr auto kAttrNodeName = "node_name";
374 constexpr auto kAttrDynInput = "dynamic";
375 constexpr auto kAttrDynInputSizes = "dyn_input_sizes";
376 constexpr auto kAttrSrcFormat = "src_format";
377 constexpr auto kAttrDstFormat = "dst_format";
378 constexpr auto kAttrMultiples = "multiples";
379 constexpr auto kAttrFixPrecision = "fix_precision";
380 constexpr auto kAttrOutputPrecision = "output_precision";
381 constexpr auto kAttrOutputUsedNum = "output_used_num";
382 constexpr auto kAttrHasBias = "has_bias";
383 constexpr auto kAttrN = "n";
384 constexpr auto kAttrLabelForInsertStreamActive = "label_for_insert_stream_active";
385 constexpr auto kAttrFpBpEnd = "fpbp_end";
386 constexpr auto kAttrFusion = "fusion";
387 constexpr auto kAttrGroup = "group";
388 constexpr auto kAttrGroups = "groups";
389 constexpr auto kAttrGroupBack = "group_back";
390 constexpr auto kAttrFracZGroup = "fracz_group";
391 constexpr auto kAttrFracZGroupIdx = "fracz_group_idx";
392 constexpr auto kAttrOp = "op";
393 constexpr auto kAttrDestRank = "dest_rank";
394 constexpr auto kAttrSrcRank = "src_rank";
395 constexpr auto kAttrSrTag = "sr_tag";
396 constexpr auto kAttrRootRank = "root_rank";
397 constexpr auto kAttrIsTraining = "is_training";
398 constexpr auto kAttrFusionId = "fusion_id";
399 constexpr auto kAttrDuplicated = "duplicated";
400 constexpr auto kAttrBucketId = "bucket_id";
401 constexpr auto kAttrGradOutputIndex = "grad_output_index";
402 constexpr auto kAttrLabelIndex = "label_index";
403 constexpr auto kAttrLabelSwitchList = "label_switch_list";
404 constexpr auto kAttrNewAxisMask = "new_axis_mask";
405 constexpr auto kAttrShrinkAxisMask = "shrink_axis_mask";
406 constexpr auto kAttrDatadumpOriginalNames = "_datadump_original_names";
407 constexpr auto kAttrDatadumpIsMultiop = "_datadump_is_multiop";
408 constexpr auto kAttrNeedRecordEvent = "need_record_event";
409 constexpr auto kAttrStreamId = "stream_id";
410 constexpr auto kAttrRecordEvent = "record_event";
411 constexpr auto kAttrWaitEvent = "wait_event";
412 constexpr auto kAttrRecordEventStream = "record_event_stream";
413 constexpr auto kAttrWaitEventStream = "wait_event_stream";
414 constexpr auto kAttrIndex = "index";
415 constexpr auto kAttrSplitDim = "split_dim";
416 constexpr auto kAttrNumSplit = "num_split";
417 constexpr auto kAttrReduction = "reduction";
418 constexpr auto kAttrOutputNum = "output_num";
419 constexpr auto kAttrSizeSplits = "size_splits";
420 constexpr auto kAttrOutputDefault = "output_default";
421 constexpr auto kAttrPrimitiveTarget = "primitive_target";
422 constexpr auto kAttrUseLocking = "use_locking";
423 constexpr auto kAttrReduceScatterFlag = "reduce_scatter_flag";
424 constexpr auto kAttrOffset = "offset";
425 constexpr auto kAttrCacheEnable = "cache_enable";
426 constexpr auto kAttrPsKey = "ps_key";
427 constexpr auto kAttrOptimizerType = "optim_type";
428 constexpr auto kAttrChildGraph = "child_graph";
429 constexpr auto kAttrInputNums = "inputNums";
430 constexpr auto kAttrT = "T";
431 constexpr auto kAttrNum = "num";
432 constexpr auto kAttrRecvType = "recv_type";
433 constexpr auto kAttrConcatDim = "concat_dim";
434 constexpr auto kAttrSplitCount = "split_count";
435 constexpr auto kAttrSendRankIds = "send_rank_ids";
436 constexpr auto kAttrRecvRankIds = "recv_rank_ids";
437 constexpr auto kAttrRankSize = "rank_size";
438 constexpr auto kAttrPadDimSize = "pad_dim_size";
439 constexpr auto kAttrPaddings = "paddings";
440 constexpr auto kAttrNumSegments = "num_segments";
441 constexpr auto kAttrStackOpName = "stack_op_name";
442 constexpr auto kAttrBegin = "begin";
443 constexpr auto kAttrEnd = "end";
444 constexpr auto kAttrSize = "size";
445 constexpr auto kAttrIsDynamicShape = "is_dynamic_shape";
446 constexpr auto kAttrInputIsDynamicShape = "input_is_dynamic_shape";
447 constexpr auto kAttrOutputIsDynamicShape = "output_is_dynamic_shape";
448 constexpr auto kAttrPynativeNextOpName = "next_op";
449 constexpr auto kAttrPynativeNextIndex = "next_index";
450 constexpr auto kAttrCompileInfo = "compile_info";
451 constexpr auto kAttrFusionType = "fusion_type";
452 constexpr auto kAttrStride = "stride";
453 constexpr auto kAttrStrides = "strides";
454 constexpr auto kAttrKernelSize = "kernel_size";
455 constexpr auto kAttrDilation = "dilation";
456 constexpr auto kAttrPadMode = "pad_mode";
457 constexpr auto kAttrPad = "pad";
458 constexpr auto kAttrPadding = "padding";
459 constexpr auto kAttrNonTask = "non_task";
460 constexpr auto kAttrIsGrad = "is_grad";
461 constexpr auto kAttrRecompute = "recompute";
462 constexpr auto kAttrNeedCseAfterRecompute = "need_cse_after_recompute";
463 constexpr auto kAttrParallelDimInfo = "parallel_dim_info";
464 constexpr auto kAttrParallelFusionType = "parallel_fusion_type";
465 constexpr auto kAttrParallelTypeInfo = "parallel_type_info";
466 constexpr auto kAttrCompositeType = "composite_type";
467 constexpr auto kAttrStitch = "stitch";
468 constexpr auto kAttrTopoSortRhsFirst = "topo_sort_rhs_first";
469 constexpr auto kAttrIgnoreSideEffect = "ignore_side_effect";
470 constexpr auto kAttrSwitchLayer = "switch_layer";
471 constexpr auto kAttrReturn = "return";
472 constexpr auto kAttrRecursiveStart = "recursive_start";
473 constexpr auto kAttrRecursiveEnd = "recursive_end";
474 constexpr auto kAttrRecursive = "recursive";
475 constexpr auto kAttrMultiCallEnd = "multicall_end";
476 constexpr auto kAttrProfilingIterEnd = "PROFILING_ITER_END";
477 constexpr auto kAttrHiddenSize = "hidden_size";
478 constexpr auto kAttrInputSize = "input_size";
479
480 // primal attr key name
481 constexpr auto kPrimalAttrForwardNodeName = "forward_node_name";
482
483 // attr value
484 constexpr auto kValueTargetSwitch = "target_switch";
485 constexpr auto kValueTargetOther = "target_other";
486
487 // env key
488 constexpr auto kGraphOpRun = "GRAPH_OP_RUN";
489 constexpr auto kEnableMemScheduler = "ENABLE_MEM_SCHEDULER";
490
491 // some size
492 const size_t kShape4dDims = 4;
493 const size_t kShape3dDims = 3;
494 const size_t kShape2dDims = 2;
495 const size_t kShape5dDims = 5;
496 const size_t kShape1dDims = 1;
497 const size_t kCubeSize = 16;
498 const size_t kCubeSize_C04 = 4;
499 const size_t kNiSize = 16;
500 const size_t kMemAlignSize = 512;
501 const size_t kBNChannelMultipleFactor = 4;
502 const int kParameterDataTensorMask = 0;
503 const int kParameterWeightTensorMask = 1;
504 const int kValueNodeTensorMask = 2;
505 constexpr auto kNCHWShapeSize = 4;
506
507 // define special index in special node
508 constexpr auto kAnfPrimitiveIndex = 0;
509 constexpr auto kFirstDataInputIndex = 1;
510 constexpr auto kRealInputNodeIndexInTupleGetItem = 1;
511 constexpr auto kInputNodeOutputIndexInTupleGetItem = 2;
512 constexpr auto kTupleGetItemInputSize = 3;
513 constexpr auto kSwitchInputSize = 4;
514 constexpr auto kFirstBranchInSwitch = 2;
515 constexpr auto kCallKernelGraphIndex = 1;
516 constexpr auto kSwitchTrueKernelGraphIndex = 2;
517 constexpr auto kSwitchFalseKernelGraphIndex = 3;
518 constexpr auto kMakeTupleInSwitchLayerIndex = 2;
519 constexpr auto kSwitchLayerInputSize = 3;
520 // index define of control depend
521 constexpr auto kControlDependPriorIndex = 1;
522 constexpr auto kControlDependBehindIndex = 2;
523 constexpr auto kControlDependInputSize = 3;
524 constexpr auto kControlDependMode = "depend_mode";
525 // index define of depend
526 constexpr auto kRealInputIndexInDepend = 1;
527 constexpr auto kDependAttachNodeIndex = 2;
528 constexpr auto kDependInputSize = 3;
529 // index define of UpdateState
530 constexpr auto kUpdateStateStateInput = 1;
531 constexpr auto kUpdateStateRealInput = 2;
532 // index define of Load
533 constexpr auto kLoadRealInput = 1;
534 constexpr auto kLoadStateInput = 2;
535 // time transfer unit
536 constexpr int kBasicTimeTransferUnit = 1000;
537 constexpr int kMaxVectorSize = 10000;
538 // index of input or output
539 enum Index : size_t {
540 kIndex0 = 0,
541 kIndex1,
542 kIndex2,
543 kIndex3,
544 kIndex4,
545 kIndex5,
546 kIndex6,
547 kIndex7,
548 kIndex8,
549 kIndex9,
550 kIndex10,
551 kIndex11,
552 kIndex12,
553 kIndex13,
554 kIndex14,
555 kIndex15,
556 kIndex16,
557 };
558 // dim of shape
559 enum Dim : size_t {
560 kDim0 = 0,
561 kDim1,
562 kDim2,
563 kDim3,
564 kDim4,
565 kDim5,
566 };
567
568 // format
569 constexpr auto kOpFormat_DEFAULT = "DefaultFormat";
570 constexpr auto kOpFormat_ChannelFirst = "ChannelFirst";
571 constexpr auto kOpFormat_ChannelLast = "ChannelLast";
572 constexpr auto kOpFormat_NC1KHKWHWC0 = "NC1KHKWHWC0";
573 constexpr auto kOpFormat_ND = "ND";
574 constexpr auto kOpFormat_NCHW = "NCHW";
575 constexpr auto kOpFormat_NHWC = "NHWC";
576 constexpr auto kOpFormat_HWCN = "HWCN";
577 constexpr auto kOpFormat_NC1HWC0 = "NC1HWC0";
578 constexpr auto kOpFormat_FRAC_Z = "FracZ";
579 constexpr auto kOpFormat_FRACTAL_Z = "FRACTAL_Z";
580 constexpr auto kOpFormat_FRAC_NZ = "FRACTAL_NZ";
581 constexpr auto kOpFormat_C1HWNCoC0 = "C1HWNCoC0";
582 constexpr auto kOpFormat_NC1HWC0_C04 = "NC1HWC0_C04";
583 constexpr auto kOpFormat_FRACTAL_Z_C04 = "FRACTAL_Z_C04";
584 constexpr auto kOpFormat_NDHWC = "NDHWC";
585 constexpr auto kOpFormat_NCDHW = "NCDHW";
586 constexpr auto kOpFormat_DHWNC = "DHWNC";
587 constexpr auto kOpFormat_DHWCN = "DHWCN";
588 constexpr auto kOpFormat_NDC1HWC0 = "NDC1HWC0";
589 constexpr auto kOpFormat_FRACTAL_Z_3D = "FRACTAL_Z_3D";
590 constexpr auto kOpFormat_FRACTAL_ZN_LSTM = "FRACTAL_ZN_LSTM";
591 constexpr auto kOpFormat_FRACTAL_ZN_RNN = "FRACTAL_ZN_RNN";
592 constexpr auto kOpFormat_ND_RNN_BIAS = "ND_RNN_BIAS";
593
594 const std::set<std::string> kOpFormatList = {kOpFormat_DEFAULT,
595 kOpFormat_NC1KHKWHWC0,
596 kOpFormat_ND,
597 kOpFormat_NCHW,
598 kOpFormat_NHWC,
599 kOpFormat_HWCN,
600 kOpFormat_NC1HWC0,
601 kOpFormat_FRAC_Z,
602 kOpFormat_C1HWNCoC0,
603 kOpFormat_FRAC_NZ,
604 kOpFormat_NC1HWC0_C04,
605 kOpFormat_FRACTAL_Z_C04,
606 kOpFormat_NDHWC,
607 kOpFormat_FRACTAL_ZN_LSTM,
608 kOpFormat_FRACTAL_ZN_RNN,
609 kOpFormat_ND_RNN_BIAS,
610 kOpFormat_NDC1HWC0,
611 kOpFormat_NCDHW,
612 kOpFormat_FRACTAL_Z_3D,
613 kOpFormat_DHWNC,
614 kOpFormat_DHWCN};
615
616 const std::set<std::string> kDefaultCompatibleFormat = {kOpFormat_ND, kOpFormat_NCHW, kOpFormat_NHWC, kOpFormat_HWCN,
617 kOpFormat_NCDHW};
618
619 const std::set<std::string> kOptOperatorSet = {kMomentumOpName,
620 kApplyMomentumOpName,
621 kApplyAdadeltaOpName,
622 kApplyAdagradOpName,
623 kApplyAdagradDAName,
624 kApplyAdamOpName,
625 kApplyAdaMaxOpName,
626 kApplyAddSignOpName,
627 kApplyCenteredRMSPOpName,
628 kApplyFtrlOpName,
629 kApplyFtrlV2OpName,
630 kApplyGradientDescentOpName,
631 kApplyPowerSignOpName,
632 kApplyProximalAdagradOpName,
633 kApplyProximalGradientDescentOpName,
634 kApplyRMSPropOpName,
635 kAdamApplyOneWithDecayOpName,
636 kAdamApplyOneWithDecayAssignOpName,
637 kFusedAdamWeightDecayName,
638 kAdamWeightDecayName,
639 kFusedCastAdamWeightDecayName,
640 kFusedAdamName,
641 kFusedSparseAdamName,
642 kFusedMulApplyMomentumOpName,
643 kFusedWeightScaleApplyMomentum,
644 kFusedScaleApplyMomentum,
645 kApplyCenteredRMSPropOpName,
646 kFusedSparseFtrlName,
647 kFusedSparseProximalAdagradName,
648 kFusedSparseLazyAdamName,
649 kSparseApplyFtrlName,
650 kSparseApplyFtrlV2Name,
651 kSGDName,
652 kLARSUpdateName,
653 kCombineMomentumWeightOpName,
654 kCombineMomentumOpName,
655 kSparseApplyProximalAdagradOpName};
656
657 const std::set<std::string> kPosteriorOperatorSet = {kPullOpName};
658
659 const std::set<std::string> kOpCacheBlackList = {kUniformCandidateSamplerOpName, kInitDatasetQueueOpName,
660 kGetNextOpName};
661
662 const std::set<std::string> kOpNeedSetFlushZeroModeList = {kLSTMOpName, kLSTMGradOpName};
663
664 const std::set<std::string> kOpNotSupportMultiThreadExecList = {kAvgPoolOpName, kAvgPoolGradOpName, kMaxPoolOpName,
665 kBatchNorm, kBatchNormGradOpName};
666
667 const std::set<std::string> kHWSpecialFormatSet = {
668 kOpFormat_FRACTAL_Z_3D, kOpFormat_NC1KHKWHWC0, kOpFormat_NC1HWC0, kOpFormat_FRAC_NZ,
669 kOpFormat_C1HWNCoC0, kOpFormat_NC1HWC0_C04, kOpFormat_FRACTAL_Z_C04, kOpFormat_FRACTAL_ZN_LSTM,
670 kOpFormat_FRACTAL_ZN_RNN, kOpFormat_NDC1HWC0, kOpFormat_FRAC_Z};
671
672 const std::set<TypeId> kFloatDataTypeSet = {kNumberTypeFloat16, kNumberTypeFloat32};
673
674 const std::set<std::string> kComputeDepend = {kUniqueOpName, kComputeAccidentalHitsOpName, kSubAndFilterOpName,
675 kPadAndShiftOpName, kCTCGreedyDecoderOpName, kDropoutGenMaskOpName,
676 kMaskedSelectOpName, kDynamicStitchOpName, kGetNextOpName};
677
678 const std::set<std::string> k3DFormatSet = {kOpFormat_NCDHW, kOpFormat_NDC1HWC0, kOpFormat_FRACTAL_Z_3D,
679 kOpFormat_NDHWC, kOpFormat_DHWCN, kOpFormat_DHWNC};
680
681 const std::set<std::string> kNoPaddingFormatSet = {kOpFormat_ChannelLast, kOpFormat_FRAC_NZ, kOpFormat_FRACTAL_ZN_RNN,
682 kOpFormat_ND_RNN_BIAS};
683
684 const std::set<std::string> DynamicShapeConstInputToAttr = {
685 kCastOpName, kExpandDimsOpName, kReshapeOpName, kEmbeddingLookupOpName, kReduceMinOpName,
686 kReduceMeanOpName, kReduceMaxOpName, kReduceAllOpName, kReduceAnyOpName, kConcatOpName};
687
688 const std::set<std::string> DynamicShapeConstInputToAttrGPU = {
689 kCastOpName, kExpandDimsOpName, kReshapeOpName, kEmbeddingLookupOpName, kTransposeOpName, kReduceSumOpName,
690 kReduceMinOpName, kReduceMeanOpName, kReduceMaxOpName, kReduceAllOpName, kReduceAnyOpName, kConcatOpName};
691
ChangeFileMode(const std::string & file_name,mode_t mode)692 static inline void ChangeFileMode(const std::string &file_name, mode_t mode) {
693 if (access(file_name.c_str(), F_OK) == -1) {
694 return;
695 }
696 try {
697 if (chmod(common::SafeCStr(file_name), mode) != 0) {
698 MS_LOG(WARNING) << "Change file `" << file_name << "` to mode " << std::oct << mode << " fail.";
699 }
700 } catch (std::exception &e) {
701 MS_LOG(WARNING) << "File `" << file_name << "` change mode failed! May be not exist.";
702 }
703 }
704
GetCurrentUSec()705 static inline uint64_t GetCurrentUSec() {
706 struct timeval tv;
707 int ret = gettimeofday(&tv, nullptr);
708 if (ret != 0) {
709 MS_LOG(EXCEPTION) << "Fail gettimeofday, ret = " << ret;
710 }
711 return static_cast<uint64_t>(tv.tv_usec + tv.tv_sec * 1000000);
712 }
713
714 #define PROF_START(stage) uint64_t start_usec_##stage = mindspore::GetCurrentUSec()
715 #define PROF_END(stage) \
716 do { \
717 uint64_t end_usec_##stage = mindspore::GetCurrentUSec(); \
718 MS_LOG(INFO) << #stage << " costs " << (end_usec_##stage - start_usec_##stage) << " usec."; \
719 } while (0)
720
721 #define PROF_MULTI_DEFINE(stage) \
722 static uint64_t total_##stage = 0; \
723 static uint64_t count_##stage = 0;
724
725 #define PROF_LOCAL_DEFINE(stage) \
726 uint64_t total_##stage = 0; \
727 uint64_t count_##stage = 0;
728
729 #define PROF_MULTI_START(stage) uint64_t start_usec_##stage = mindspore::GetCurrentUSec()
730
731 #define PROF_MULTI_END(stage) \
732 do { \
733 ++count_##stage; \
734 uint64_t end_usec_##stage = mindspore::GetCurrentUSec(); \
735 total_##stage += (end_usec_##stage - start_usec_##stage); \
736 } while (0)
737
738 #define PROF_MULTI_PRINT(stage) \
739 do { \
740 MS_LOG(INFO) << #stage << " called " << count_##stage << " times, costs " << total_##stage << " usec."; \
741 } while (0)
742 } // namespace mindspore
743 #endif // MINDSPORE_CCSRC_UTILS_UTILS_H_
744