1 /**
2 * Copyright 2019-2023 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_INCLUDE_COMMON_UTILS_UTILS_H_
18 #define MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_UTILS_H_
19
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #ifndef _MSC_VER
23 #include <sys/time.h>
24 #endif
25 #include <algorithm>
26 #include <map>
27 #include <memory>
28 #include <set>
29 #include <string>
30 #include <sstream>
31 #include <vector>
32 #include <tuple>
33
34 #include "include/common/visible.h"
35 #include "include/common/utils/stream_util.h"
36 #include "ir/dtype/type.h"
37 #include "utils/log_adapter.h"
38
39 #ifndef MS_UNLIKELY
40 #ifdef _MSC_VER
41 #define MS_UNLIKELY(x) (x)
42 #else
43 #define MS_UNLIKELY(x) __builtin_expect(!!(x), 0)
44 #endif
45 #endif
46
47 #ifndef MS_LIKELY
48 #ifdef _MSC_VER
49 #define MS_LIKELY(x) (x)
50 #else
51 #define MS_LIKELY(x) __builtin_expect(!!(x), 1)
52 #endif
53 #endif
54
55 namespace mindspore {
56 // attr key name
57 constexpr auto kAttrSegment = "segment";
58 constexpr auto kAttrAlignCorners = "align_corners";
59 constexpr auto kAttrHalfPixelCenters = "half_pixel_centers";
60 constexpr auto kAttrInputNames = "input_names";
61 constexpr auto kAttrAttrNames = "attr_names";
62 constexpr auto kAttrAnyTypeCast = "any_type";
63 constexpr auto kAttrBins = "bins";
64 constexpr auto kAttrMin = "min";
65 constexpr auto kAttrMax = "max";
66 constexpr auto kAttrCopyData = "need_copy";
67 constexpr auto kAttrInputDefaultFormat = "input_default_format";
68 constexpr auto kAttrOutputDefaultFormat = "output_default_format";
69 constexpr auto kAttrConvertAttrNode = "convert_attr_node";
70 constexpr auto kAttrNeedCast = "need_cast";
71 constexpr auto kAttrIsAiCpuKernel = "is_AICPU_kernel";
72 constexpr auto kIsBackendCast = "is_backend_cast";
73 constexpr auto kAttrOutputNames = "output_names";
74 constexpr auto kAttrAsync = "async";
75 constexpr auto kAttrOffload = "offload";
76 constexpr auto kAttrOutIdx = "out_idx";
77 constexpr auto kAttrVisited = "visited";
78 constexpr auto kAttrReshapePaddingAxis = "reshape_padding_axis";
79 constexpr auto kAttrBeginNormAxis = "begin_norm_axis";
80 constexpr auto kAttrBeginParamsAxis = "begin_params_axis";
81 constexpr auto kAttrShape = "shape";
82 constexpr auto kAttrMomentum = "momentum";
83 constexpr auto kAttrEps = "eps";
84 constexpr auto kAttrEpsilon = "epsilon";
85 constexpr auto kAttrFactor = "factor";
86 constexpr auto kAttrIsRef = "isRef";
87 constexpr auto kAttrDataShape = "data_shape";
88 constexpr auto kAttrFormat = "format";
89 constexpr auto kAttrOriginFormat = "origin_format";
90 constexpr auto kAttrReshapeType = "reshape_type";
91 constexpr auto kAttrAxis = "axis";
92 constexpr auto kAttrAxes = "axes";
93 constexpr auto kAttrAlpha = "alpha";
94 constexpr auto kAttrAclSpecialFormat = "acl_special_format";
95 constexpr auto kAttrAclSpecialInputFormat = "acl_special_input_format";
96 constexpr auto kAttrAclInconsistentInputDtype = "acl_inconsistent_input_dtype";
97 constexpr auto kAttrBatchDims = "batch_dims";
98 constexpr auto kAttrKeepDims = "keep_dims";
99 constexpr auto kTransposeA = "transpose_a";
100 constexpr auto kTransposeB = "transpose_b";
101 constexpr auto kAttrSkipMode = "skip_mode";
102 constexpr auto kAttrShapeGamma = "shape_gamma";
103 constexpr auto kAttrPerm = "perm";
104 constexpr auto kAttrTransposeFirst = "transpose_first";
105 constexpr auto kAttrTbeFusionType = "tbe_fusion_type";
106 constexpr auto kAttrAtomicAddMemSize = "automic_add_mem_size";
107 constexpr auto kAttrAtomicOutputIndexs = "atomic_output_clean_indexs";
108 constexpr auto kAttrNeedAtomic = "need_atomic";
109 constexpr auto kAttrAtomicWorkspaceIndexs = "atomic_workspace_clean_indexs";
110 constexpr auto kAttrSwitchCondition = "switch_condition";
111 constexpr auto kAttrDataType = "data_type";
112 constexpr auto kAttrDType = "dtype";
113 constexpr auto kAttrActiveTarget = "active_target";
114 constexpr auto kAttrActiveStreamId = "active_stream_id";
115 constexpr auto kAttrActiveStreamList = "active_stream_list";
116 constexpr auto kAttrTrueBranchStream = "true_branch_stream";
117 constexpr auto kAttrStreamSwitchKind = "stream_switch_kind";
118 constexpr auto kAttrEventId = "event_id";
119 constexpr auto kAttrLabelId = "label_id";
120 constexpr auto kAttrLogicId = "logic_id";
121 constexpr auto kAttrNodeInfo = "node_info";
122 constexpr auto kAttrNodeName = "node_name";
123 constexpr auto kAttrDynInput = "dynamic";
124 constexpr auto kAttrDynInputSizes = "dyn_input_sizes";
125 constexpr auto kAttrChannelName = "channel_name";
126 constexpr auto kAttrTupleInputStructural = "tuple_input_structural";
127 constexpr auto kAttrListStartIndex = "list_start_index";
128 constexpr auto kAttrPyExecuteNeedUpdateShape = "pyexecute_need_update_shape";
129 constexpr auto kAttrPyExecuteOutput = "pyexecute_output";
130 constexpr auto kAttrSrcFormat = "src_format";
131 constexpr auto kAttrDstFormat = "dst_format";
132 constexpr auto kAttrFixPrecision = "fix_precision";
133 constexpr auto kAttrOutputPrecision = "output_precision";
134 constexpr auto kAttrOutputUsedNum = "output_used_num";
135 constexpr auto kAttrHasBias = "has_bias";
136 constexpr auto kAttrN = "n";
137 constexpr auto kAttrLabelForInsertStreamActive = "label_for_insert_stream_active";
138 constexpr auto kAttrFpBpEnd = "fpbp_end";
139 constexpr auto kAttrFusion = "fusion";
140 constexpr auto kAttrCommInputDepend = "comm_input_depend";
141 constexpr auto kAttrRecomputeCommDepend = "recompute_comm_depend";
142 constexpr auto kAttrNotDelayFusion = "not_delay_fusion";
143 constexpr auto kAttrGroup = "group";
144 constexpr auto kAttrRankList = "rank_list";
145 constexpr auto kAttrGroups = "groups";
146 constexpr auto kAttrGroupBack = "group_back";
147 constexpr auto kAttrFracZGroup = "fracz_group";
148 constexpr auto kAttrFracZGroupIdx = "fracz_group_idx";
149 constexpr auto kAttrOp = "op";
150 constexpr auto kAttrDestRank = "dest_rank";
151 constexpr auto kAttrSrcRank = "src_rank";
152 constexpr auto kAttrSrTag = "sr_tag";
153 constexpr auto kAttrRootRank = "root_rank";
154 constexpr auto kAttrComm = "comm";
155 constexpr auto kAttrIsTraining = "is_training";
156 constexpr auto kAttrFusionId = "fusion_id";
157 constexpr auto kAttrDuplicated = "duplicated";
158 constexpr auto kAttrGradOutputIndex = "grad_output_index";
159 constexpr auto kAttrLabelIndex = "label_index";
160 constexpr auto kAttrLabelSwitchList = "label_switch_list";
161 constexpr auto kAttrBeginMask = "begin_mask";
162 constexpr auto kAttrEndMask = "end_mask";
163 constexpr auto kAttrEllipsisMask = "ellipsis_mask";
164 constexpr auto kAttrNewAxisMask = "new_axis_mask";
165 constexpr auto kAttrShrinkAxisMask = "shrink_axis_mask";
166 constexpr auto kAttrDatadumpOriginalNames = "_datadump_original_names";
167 constexpr auto kAttrDatadumpIsMultiop = "_datadump_is_multiop";
168 constexpr auto kAttrNeedRecordEvent = "need_record_event";
169 constexpr auto kAttrStreamId = "stream_id";
170 constexpr auto kAttrRecomputeId = "recompute_id";
171 constexpr auto kAttrRecordEvent = "record_event";
172 constexpr auto kAttrAccumulatedAttention = "AccumulatedAttention";
173 constexpr auto kAttrWaitEvent = "wait_event";
174 constexpr auto kAttrRecordEventStream = "record_event_stream";
175 constexpr auto kAttrWaitEventStream = "wait_event_stream";
176 constexpr auto kAttrRecordWaitEventStreamPairId = "record_wait_event_stream_pair_id";
177 constexpr auto kAttrInputMultiStreamSafe = "input_multi_thread_safe";
178 constexpr auto kAttrStream = "stream";
179 constexpr auto kAttrIndex = "index";
180 constexpr auto kAttrSplit = "split";
181 constexpr auto kAttrSplitDim = "split_dim";
182 constexpr auto kAttrNumSplit = "num_split";
183 constexpr auto kAttrNumGroups = "num_groups";
184 constexpr auto kAttrActivateSilu = "activate_silu";
185 constexpr auto kAttrReduction = "reduction";
186 constexpr auto kAttrOutputNum = "output_num";
187 constexpr auto kAttrOutputSize = "output_size";
188 constexpr auto kAttrScales = "scales";
189 constexpr auto kAttrSizeSplits = "size_splits";
190 constexpr auto kAttrOutputDefault = "output_default";
191 constexpr auto kAttrPrimitiveTarget = "primitive_target";
192 constexpr auto kAttrUseLocking = "use_locking";
193 constexpr auto kAttrReduceScatterFlag = "reduce_scatter_flag";
194 constexpr auto kAttrOffset = "offset";
195 constexpr auto kAttrCacheEnable = "cache_enable";
196 constexpr auto kAttrPsKey = "ps_key";
197 constexpr auto kAttrOptimizerType = "optim_type";
198 constexpr auto kAttrChildGraph = "child_graph";
199 constexpr auto kAttrInputNums = "inputNums";
200 constexpr auto kAttrT = "T";
201 constexpr auto kAttrNum = "num";
202 constexpr auto kAttrRecvType = "recv_type";
203 constexpr auto kAttrConcatDim = "concat_dim";
204 constexpr auto kAttrSplitCount = "split_count";
205 constexpr auto kAttrSendRankIds = "send_rank_ids";
206 constexpr auto kAttrRecvRankIds = "recv_rank_ids";
207 constexpr auto kAttrSendLens = "send_lens";
208 constexpr auto kAttrRecvLens = "recv_lens";
209 constexpr auto kAttrRankSize = "rank_size";
210 constexpr auto kAttrPadDimSize = "pad_dim_size";
211 constexpr auto kAttrPaddings = "paddings";
212 constexpr auto kAttrNumSegments = "num_segments";
213 constexpr auto kAttrStackOpName = "stack_op_name";
214 constexpr auto kAttrBegin = "begin";
215 constexpr auto kAttrEnd = "end";
216 constexpr auto kAttrSize = "size";
217 constexpr auto kAttrSizes = "sizes";
218 constexpr auto kAttrKsizes = "ksizes";
219 constexpr auto kAttrIsKernelDynamicImpl = "is_kernel_dynamic_impl";
220 constexpr auto kAttrIsKernelDynamicShape = "is_kernel_dynamic_shape";
221 constexpr auto kAttrIsPyboostTupleInput = "is_pyboost_tuple_input";
222 constexpr auto kAttrIsDynamicRank = "is_dynamic_rank";
223 constexpr auto kAttrInputIsDynamicRank = "input_is_dynamic_rank";
224 constexpr auto kAttrOutputIsDynamicRank = "output_is_dynamic_rank";
225 constexpr auto kAttrInputIsDynamicShape = "input_is_dynamic_shape";
226 constexpr auto kAttrOutputIsDynamicShape = "output_is_dynamic_shape";
227 constexpr auto kAttrPynativeNextOpName = "next_op";
228 constexpr auto kAttrPynativeNextIndex = "next_index";
229 constexpr auto kAttrMutableOpName = "mutable";
230 constexpr auto kAttrMutableKernel = "mutable_kernel";
231 constexpr auto kAttrAclHighPrecision = "acl_high_precision";
232 constexpr auto kAttrCompileInfo = "compile_info";
233 constexpr auto kAttrFusionType = "fusion_type";
234 constexpr auto kAttrStride = "stride";
235 constexpr auto kAttrStrides = "strides";
236 constexpr auto kAttrShapex = "shapex";
237 constexpr auto kAttrKernelSize = "kernel_size";
238 constexpr auto kAttrDilation = "dilation";
239 constexpr auto kAttrDatFormat = "data_format";
240 constexpr auto kAttrPadMode = "pad_mode";
241 constexpr auto kAttPaddingMode = "padding_mode";
242 constexpr auto kAttrPad = "pad";
243 constexpr auto kAttrPadding = "padding";
244 constexpr auto kAttrMode = "mode";
245 constexpr auto kAttrWindow = "window";
246 constexpr auto kAttrCeilMode = "ceil_mode";
247 constexpr auto kAttrGlobalPooling = "global_pooling";
248 constexpr auto kAttrNonTask = "non_task";
249 constexpr auto kAttrIsGrad = "is_grad";
250 constexpr auto kAttrRecompute = "recompute";
251 constexpr auto kAttrCheckpoint = "checkpoint";
252 constexpr auto kAttrSliceActivation = "slice_activation";
253 constexpr auto kAttrNeedCseAfterRecompute = "need_cse_after_recompute";
254 constexpr auto kAttrParallelDimInfo = "parallel_dim_info";
255 constexpr auto kAttrParallelFusionType = "parallel_fusion_type";
256 constexpr auto kAttrParallelTypeInfo = "parallel_type_info";
257 constexpr auto kAttrCompositeType = "composite_type";
258 constexpr auto kAttrStitch = "stitch";
259 constexpr auto kAttrTopoSortRhsFirst = "topo_sort_rhs_first";
260 constexpr auto kAttrIgnoreSideEffect = "ignore_side_effect";
261 constexpr auto kAttrSwitchLayer = "switch_layer";
262 constexpr auto kAttrReturn = "return";
263 constexpr auto kAttrRecursiveStart = "recursive_start";
264 constexpr auto kAttrRecursiveEnd = "recursive_end";
265 constexpr auto kAttrRecursive = "recursive";
266 constexpr auto kAttrMultiCallEnd = "multicall_end";
267 constexpr auto kAttrProfilingIterEnd = "PROFILING_ITER_END";
268 constexpr auto kAttrHiddenSize = "hidden_size";
269 constexpr auto kAttrInputSize = "input_size";
270 constexpr auto kAttrDstType = "dst_type";
271 constexpr auto kAttrDump = "dump";
272 constexpr auto kAttrUselessInput = "useless_input";
273 constexpr auto kAttrSkipNopOpAddr = "skip_nop_op_addr";
274 constexpr auto kAttrSkipNopOpExecution = "skip_nop_op_execution";
275 constexpr auto kAttrFixedInputFormat = "fixed_input_format";
276 constexpr auto kAttrFixedOutputFormat = "fixed_output_format";
277 constexpr auto kAttrFixedInputDeviceShape = "fixed_input_device_shape";
278 constexpr auto kAttrFixedOutputDeviceShape = "fixed_output_device_shape";
279 constexpr auto kAttrFuncType = "func_type";
280 constexpr auto kAttrFuncName = "func_name";
281 constexpr auto kAttrFunctor = "functor";
282 constexpr auto kAttrCustAicpu = "cust_aicpu";
283 constexpr auto kAttrIsInternalOutputNopNode = "is_internal_output_nop_node";
284 constexpr auto kAttrIsUBFusionOp = "is_ub_fusion_op";
285 constexpr auto kAttrNopOp = "nop_op";
286 constexpr auto kAttrPlaceHolderIndex = "placeholder_index";
287 constexpr auto kAttrMicro = "micro";
288 constexpr auto kAttrJsonFileName = "json_file_name";
289 constexpr auto kAttrNeedDropInput = "need_drop_input";
290 constexpr auto kAttrNeedConvertToValueNode = "need_convert_to_value_node";
291 constexpr auto kAttrSendSrcNodeName = "send_src_node_name";
292 constexpr auto kAttrSendDstNodeName = "send_dst_node_name";
293 constexpr auto kAttrSendDstRanks = "send_dst_ranks";
294 constexpr auto kAttrSendDstRoles = "send_dst_roles";
295 constexpr auto kAttrRecvSrcNodeName = "recv_src_node_name";
296 constexpr auto kAttrRecvDstNodeName = "recv_dst_node_name";
297 constexpr auto kAttrRecvSrcRanks = "recv_src_ranks";
298 constexpr auto kAttrRecvSrcRoles = "recv_src_roles";
299 constexpr auto kAttrInterProcessEdgeNames = "inter_process_edge_names";
300 constexpr auto kAttrInterProcessEdgeLabel = "inter_process_edge_label";
301 constexpr auto kAttrIsMuxRpcKernel = "is_mux_rpc_kernel";
302 constexpr auto kAttrGroupRankIds = "group_rank_ids";
303 constexpr auto kAttrReuseCommunication = "reuse_communication_node";
304 constexpr auto kAttrPrecisionFlag = "precision_flag";
305 constexpr auto kAttrDfmGroup = "deformable_groups";
306 constexpr auto kAttrModulated = "modulated";
307 constexpr auto kAttrDilations = "dilations";
308 constexpr auto kAttrDataFormat = "data_format";
309 constexpr auto kAttrPads = "pads";
310 constexpr auto kAttrKsize = "ksize";
311 constexpr auto kAttrOnlyUseFirstOutput = "only_use_first_output";
312 constexpr auto kAttrOnlyUseSecondOutput = "only_use_second_output";
313 constexpr auto kAttrOpAdaptationProcessed = "op_adaptation_processed";
314 constexpr auto kAttrAbstractAdaptationProcessed = "abstract_adaptation_processed";
315 constexpr auto kAttrMeOpName = "me_op_name";
316 constexpr auto kAttrIRChange = "ir_change";
317 constexpr auto kParamterIsSequence = "param_is_sequence";
318 constexpr auto kAttrZeroInfinity = "zero_infinity";
319 constexpr auto kAttrBlank = "blank";
320 constexpr auto kAttrUpdateSlots = "update_slots";
321 constexpr auto kAttrLr = "lr";
322 constexpr auto kAttrWithBiasAdd = "with_bias_add";
323 constexpr auto kAttrWithRelu = "with_relu";
324 constexpr auto kAttrNeedGradFlagOfInputs = "need_grad_flag_of_inputs";
325 constexpr auto kAttrIsCNodeNeedGrad = "is_cnode_need_grad";
326 constexpr auto kAttrJitLevel = "jit_level";
327 constexpr auto kAttrJitLevelO0 = "O0";
328 constexpr auto kAttrJitLevelO1 = "O1";
329 constexpr auto kAttrJitLevelO2 = "O2";
330 constexpr auto kAttrCellJitConfigDict = "_jit_config_dict";
331 constexpr auto kAttrBinaryOutput = "binary_output";
332 constexpr auto kAttrMinLength = "minlength";
333 constexpr auto kAttrMaxLength = "maxlength";
334 constexpr auto kAttrIouThreshold = "iou_threshold";
335 constexpr auto kAttrEnableEmbeddingStorage = "enable_embedding_storage";
336 constexpr auto kAttrParameterKey = "parameter_key";
337 constexpr auto kAttrJitCallNode = "jit_call_node";
338 constexpr auto kAttrInsertDefaultValue = "insert_default_value";
339 constexpr auto kAttrIsSparse = "IsSparse";
340 constexpr auto kAttrKernelBackoffWithFailureInfo = "kernel_backoff_with_failure_info";
341 constexpr auto kAttrKernelBackoffWithFailureType = "kernel_backoff_with_failure_type";
342 constexpr auto kAttrKernelGraph = "kernel_graph";
343 constexpr auto kAttrPreKernelGraph = "pre_kernel_graph";
344 constexpr auto kAttrKernelGraphBoundary = "kernel_graph_boundary";
345 constexpr auto kAttrKernelPacketNode = "kernel_packet_node";
346 constexpr auto kAttrNeedInline = "need_inline";
347 constexpr auto kAttrOriFusionName = "ori_fusion_name";
348 constexpr auto kAttrDynamicLenName = "is_dynamic_len";
349 constexpr auto kAttrAnyOutputName = "is_any_output";
350 constexpr auto kAttrForFormatChange = "for_format_change";
351 constexpr auto kAttrReplaceRealKernelInBackend = "replace_real_kernel_in_backend";
352 constexpr auto kAttrRefNodeMonadOutputIdx = "ref_node_monad_output_idx";
353 constexpr auto kAttrRandomOpSnapShot = "random_op_snapshot";
354 constexpr auto kAttrTbeOpAtomicDtypes = "tbe_op_atomic_dtypes";
355 constexpr auto kAttrTbeOpAtomicInt64Values = "tbe_op_atomic_int64_values";
356 constexpr auto kAttrTbeOpAtomicFloatValues = "tbe_op_atomic_float_values";
357 constexpr auto kAttrDtypes = "dtypes";
358 constexpr auto kAttrValuesInt = "values_int";
359 constexpr auto kAttrValuesFloat = "values_float";
360 constexpr auto kAttrRecomputeSubGraph = "recompute_sub_graph";
361 constexpr auto kAttrExpandDimsMask = "expand_dims_mask";
362 constexpr auto kAttrTupleIndexTypes = "tuple_index_types";
363 constexpr auto kAttrTupleIndexAxis = "tuple_index_axis";
364 constexpr auto kAttrInitByNone = "init_by_none";
365 constexpr auto kAttrExpandDimsCnt = "expand_dims_cnt";
366 constexpr auto kAttrEmptyIndicesOut = "empty_indices_out";
367 constexpr auto kAttrHasTrue = "has_true";
368 constexpr auto kAttrHasSequence = "has_sequence";
369 constexpr auto kAttrOriginIndexType = "origin_index_type";
370 constexpr auto kIntIndex = "int_index";
371 constexpr auto kTensorIndexSequenceIndex = "tensor_index_sequence_index";
372 constexpr auto kNoneIndex = "none_index";
373 constexpr auto kBoolSequenceIndex = "bool_sequence_index";
374 constexpr auto kSliceIndex = "slice_index";
375 constexpr auto kEllipsisIndex = "ellipsis_index";
376 constexpr auto kSetitemByTupleWithTensor = "setitem_by_tuple_with_tensor";
377 constexpr auto kSetitemByTuple = "setitem_by_tuple";
378 constexpr auto kPreSetitemByTuple = "pre_setitem_by_tuple";
379 constexpr auto kAttrTupleIndexInfoType = "tuple_index_info_type";
380 constexpr auto kAttrSimpleSliceInfo = "simple_slice_info";
381 constexpr auto kAttrNotCut = "not_cut";
382 constexpr auto kAttrNotSupportOpForDevice = "not_support_op_for_device";
383 constexpr auto kAttrGraphSplitGroup = "graph_split_group";
384 constexpr const char kAttrNeedAllGather[] = "parallel_optimizer_allgather";
385 constexpr const char kAttrNodeCloseFollowing[] = "node_close_following";
386 constexpr const char kAttrNodeWithoutOutput[] = "node_without_output";
387 constexpr char kAttrInputLayout[] = "input_layout";
388 constexpr char kAttrKeepProb[] = "keep_prob";
389 constexpr char kAttrHeadNum[] = "head_num";
390 constexpr auto kAttrFuncGraph = "func_graph";
391 constexpr char kAttrScaleValue[] = "scale_value";
392 constexpr char kAttrPreTokens[] = "pre_tokens";
393 constexpr char kAttrNextTokens[] = "next_tokens";
394 constexpr char kAttrSparseMode[] = "sparse_mode";
395 constexpr char kAttrEnableLoadBalance[] = "enable_load_balance";
396 constexpr char kAttrIsTransA[] = "is_trans_a";
397 constexpr char kAttrIsTransB[] = "is_trans_b";
398 constexpr char kAttrReduceOp[] = "reduce_op";
399 constexpr char kAttrTransposeX1[] = "transpose_x1";
400 constexpr char kAttrTransposeX2[] = "transpose_x2";
401 constexpr char kAttrCommTurn[] = "comm_turn";
402 constexpr char kAttrGatherIndex[] = "gather_index";
403 constexpr char kAttrBranchOutputNum[] = "branch_output_num";
404 constexpr char kAttrBranchGraphName[] = "branch_graph_name";
405 constexpr char kInlineSubGraphName[] = "inline_sub_graph_name";
406 constexpr char kAttrBpropAutoMonadLevel[] = "bprop_auto_monad_level";
407 constexpr char kAttrSideEffectBpropAppPropagate[] = "side_effect_bprop_app_propagate";
408 constexpr char kAttrSideEffectBpropApp[] = "side_effect_bprop_app";
409 constexpr auto kAttrOriginOutputShape = "origin_output_shape";
410 constexpr auto kAttrOriginInputShapes = "origin_input_shapes";
411 constexpr char kAttrNotRemove[] = "not_remove";
412 constexpr const char kAttrValueDepend[] = "value_depend";
413 constexpr const char kAttrOnlyDependShape[] = "only_depend_shape";
414
415 // FuncGraph Flags
416 constexpr auto kFlagIsPynativeBpropGraph = "is_pynative_bprop_graph";
417 constexpr auto kFlagPyNativeRunInGraph = "pynative_run_in_graph";
418 constexpr auto kFlagNeedRenormalize = "need_renormalize";
419 constexpr auto kFlagEnableZeroCopyInGraph = "enable_zero_copy_in_graph";
420 constexpr auto kFlagPyNativeBpropGraphWithBpropCut = "pynative_bprop_graph_with_bprop_cut";
421 constexpr auto kFlagPyNativeBpropGraphIsDynamic = "pynative_bprop_graph_is_dynamic";
422 constexpr auto kFlagEnableRunGraphBySingleOp = "enable_run_graph_by_single_op";
423 constexpr auto kFlagIsPyNativeBpropKernelGraph = "is_pynative_bprop_kernel_graph";
424 constexpr auto kFlagPyNativeWithJitCallGraph = "pynative_with_jit_call_graph";
425 constexpr auto kFlagJitCallGraph = "jit_call_graph";
426 constexpr auto kFlagJitGraph = "jit_graph";
427 constexpr auto kFlagSwitchInline = "switch_inline_graph";
428 constexpr auto kFlagIsControlFlow = "is_control_flow";
429
430 // custom operator func type
431 constexpr auto kCustomTypeAOT = "aot";
432 constexpr auto kCustomTypeJULIA = "julia";
433 constexpr auto kCustomTypePyfunc = "pyfunc";
434 constexpr auto kCustomTypeTbe = "tbe";
435 constexpr auto kCustomTypeAICPU = "aicpu";
436 constexpr auto kCustomTypeHybrid = "hybrid";
437
438 // primal attr key name
439 constexpr auto kPrimalAttrForwardNodeName = "forward_node_name";
440 constexpr auto kPrimalAttrMicro = "micro";
441 constexpr auto kPrimalAttrChunk = "chunk";
442 constexpr auto kPrimalAttrPipelineParam = "pipeline_param";
443 constexpr auto kPrimalAttrBackwardMicroEnd = "backward_micro_end";
444 constexpr auto kPrimalAttrForwardEnd = "forward_end";
445 constexpr auto kPrimalAttrSegmentMax = "segment_max";
446 constexpr auto kPrimalAttrUniqueId = "unique_id";
447 constexpr auto kPrimalAttrForwardUniqueId = "forward_unique_id";
448 constexpr auto kPrimalAttrForwardCommNodeUniqueId = "forward_comm_node_unique_id";
449 constexpr auto kPrimalAttrMirrorUserId = "mirror_user_id";
450
451 // attr value
452 constexpr auto kValueTargetSwitch = "target_switch";
453 constexpr auto kValueTargetOther = "target_other";
454 constexpr auto kValueTrue = "true";
455 constexpr auto kTensorValueIsType = "tensor_value_is_type";
456 constexpr auto kTensorValueIsEmpty = "tensor_value_is_empty";
457 constexpr auto kTensorUserDataIsSensTensor = "is_sens_tensor";
458 constexpr auto kFakeTensorPos = "fake_tensor_pos";
459 constexpr auto kFakeTensorListPos = "fake_tensor_list_pos";
460 constexpr auto kChannelNameNpuLog = "_npu_log";
461
462 // env key
463 constexpr auto kCompilerCacheEnable = "MS_COMPILER_CACHE_ENABLE";
464 constexpr auto kCompilerCachePath = "MS_COMPILER_CACHE_PATH";
465 constexpr auto kSimulationLevel = "MS_SIMULATION_LEVEL";
466 constexpr auto kSimulationLevelCompileGraph = "0";
467 constexpr auto kSimulationLevelCompileKernel = "1";
468
469 // comm
470 constexpr auto kHCCLWorldGroup = "hccl_world_group";
471 constexpr auto kNCCLWorldGroup = "nccl_world_group";
472 constexpr auto kEnvRankSize = "RANK_SIZE";
473 constexpr auto kEnvRankId = "RANK_ID";
474 constexpr auto kEnvLocalRankSize = "LOCAL_RANK_SIZE";
475 constexpr auto kEnvLocalRankId = "LOCAL_RANK_ID";
476
477 // some size
478 const size_t kShape4dDims = 4;
479 const size_t kShape3dDims = 3;
480 const size_t kShape2dDims = 2;
481 const size_t kShape5dDims = 5;
482 const size_t kShape6dDims = 6;
483 const size_t kShape1dDims = 1;
484 const size_t kCubeSize = 16;
485 const size_t kCubeSize_C04 = 4;
486 const size_t kNiSize = 16;
487 const size_t kMemAlignSize = 512;
488 const size_t kBNChannelMultipleFactor = 4;
489 constexpr auto kNCHWShapeSize = 4;
490 const size_t kMaxTensorIndexDimNums = 8;
491
492 // define special index in special node
493 constexpr auto kAnfPrimitiveIndex = 0;
494 constexpr auto kFirstDataInputIndex = 1;
495 constexpr auto kRealInputNodeIndexInTupleGetItem = 1;
496 constexpr auto kInputNodeOutputIndexInTupleGetItem = 2;
497 constexpr auto kSparseGetAttrInputSize = 2;
498 constexpr auto kTupleGetItemInputSize = 3;
499
500 // index define of kTupleSetItem
501 constexpr auto kTupleSetItemTupleIndex = 1;
502 constexpr auto kTupleSetItemIndexIndex = 2;
503 constexpr auto kTupleSetItemValueIndex = 3;
504 constexpr auto kTupleSetItemInputSize = 4;
505 // index define of partial
506 constexpr auto kPartialMinInputSize = 2;
507 constexpr auto kPartialGraphIndex = 1;
508
509 // index define of switch
510 constexpr auto kSwitchInputSize = 4;
511 constexpr auto kSwitchCondIndex = 1;
512 constexpr auto kSwitchTrueBranchIndex = 2;
513 constexpr auto kSwitchFalseBranchIndex = 3;
514 constexpr auto kSwitchBranchesNum = 2;
515
516 // index define of GridSampler & GridSamplerGrad
517 constexpr int kGridSamplerInputNum = 5;
518 constexpr int kGridSamplerOutputNum = 1;
519 constexpr int kGridSamplerGradInputNum = 6;
520 constexpr int kGridSamplerGradOutputNum = 2;
521
522 // index define of switch_layer
523 constexpr auto kSwitchLayerInputSize = 3;
524 constexpr auto kSwitchLayerSelectIndex = 1;
525 constexpr auto kSwitchLayerBranchesIndex = 2;
526
527 // index define of depend
528 constexpr auto kRealInputIndexInDepend = 1;
529 constexpr auto kDependAttachNodeIndex = 2;
530 constexpr auto kDependInputSize = 3;
531 // index define of UpdateState
532 constexpr auto kUpdateStateStateInput = 1;
533 constexpr auto kUpdateStateRealInput = 2;
534 // index define of Load
535 constexpr auto kLoadRealInput = 1;
536 constexpr auto kLoadStateInput = 2;
537 constexpr auto kGenerateEodMaskOpName = "GenerateEodMask";
538 // time transfer unit
539 constexpr int kBasicTimeTransferUnit = 1000;
540 constexpr int kMaxVectorSize = 10000;
541 // index of input or output
542 constexpr size_t kIndex0 = 0;
543 constexpr size_t kIndex1 = 1;
544 constexpr size_t kIndex2 = 2;
545 constexpr size_t kIndex3 = 3;
546 constexpr size_t kIndex4 = 4;
547 constexpr size_t kIndex5 = 5;
548 constexpr size_t kIndex6 = 6;
549 constexpr size_t kIndex7 = 7;
550 constexpr size_t kIndex8 = 8;
551 constexpr size_t kIndex9 = 9;
552 constexpr size_t kIndex10 = 10;
553 constexpr size_t kIndex11 = 11;
554 constexpr size_t kIndex12 = 12;
555 constexpr size_t kIndex13 = 13;
556 constexpr size_t kIndex14 = 14;
557 constexpr size_t kIndex15 = 15;
558 constexpr size_t kIndex16 = 16;
559 constexpr size_t kIndex17 = 17;
560 constexpr size_t kIndex18 = 18;
561 constexpr size_t kIndex19 = 19;
562 constexpr size_t kIndex20 = 20;
563 constexpr size_t kIndex21 = 21;
564 constexpr size_t kIndex22 = 22;
565 constexpr size_t kIndex23 = 23;
566 constexpr size_t kIndex24 = 24;
567 constexpr size_t kIndex25 = 25;
568 constexpr size_t kIndex26 = 26;
569 constexpr size_t kIndex27 = 27;
570 constexpr size_t kIndex28 = 28;
571 // dim of shape
572 constexpr size_t kDim0 = 0;
573 constexpr size_t kDim1 = 1;
574 constexpr size_t kDim2 = 2;
575 constexpr size_t kDim3 = 3;
576 constexpr size_t kDim4 = 4;
577 constexpr size_t kDim5 = 5;
578 constexpr size_t kDim6 = 6;
579 // format
580 constexpr auto kOpFormat_DEFAULT = "DefaultFormat";
581 constexpr auto kOpFormat_ChannelFirst = "ChannelFirst";
582 constexpr auto kOpFormat_ChannelLast = "ChannelLast";
583 constexpr auto kOpFormat_NC1KHKWHWC0 = "NC1KHKWHWC0";
584 constexpr auto kOpFormat_ND = "ND";
585 constexpr auto kOpFormat_NCHW = "NCHW";
586 constexpr auto kOpFormat_NHWC = "NHWC";
587 constexpr auto kOpFormat_HWCN = "HWCN";
588 constexpr auto kOpFormat_CHWN = "CHWN";
589 constexpr auto kOpFormat_NC1HWC0 = "NC1HWC0";
590 constexpr auto kOpFormat_FRAC_Z = "FRACTAL_Z";
591 constexpr auto kOpFormat_FRACTAL_Z = "FRACTAL_Z";
592 constexpr auto kOpFormat_FRAC_NZ = "FRACTAL_NZ";
593 constexpr auto kOpFormat_C1HWNCoC0 = "C1HWNCoC0";
594 constexpr auto kOpFormat_NC1HWC0_C04 = "NC1HWC0_C04";
595 constexpr auto kOpFormat_FRACTAL_Z_C04 = "FRACTAL_Z_C04";
596 constexpr auto kOpFormat_NDHWC = "NDHWC";
597 constexpr auto kOpFormat_NCDHW = "NCDHW";
598 constexpr auto kOpFormat_DHWNC = "DHWNC";
599 constexpr auto kOpFormat_DHWCN = "DHWCN";
600 constexpr auto kOpFormat_NDC1HWC0 = "NDC1HWC0";
601 constexpr auto kOpFormat_FRACTAL_Z_3D = "FRACTAL_Z_3D";
602 constexpr auto kOpFormat_FRACTAL_ZN_LSTM = "FRACTAL_ZN_LSTM";
603 constexpr auto kOpFormat_FRACTAL_ZN_RNN = "FRACTAL_ZN_RNN";
604 constexpr auto kOpFormat_ND_RNN_BIAS = "ND_RNN_BIAS";
605 constexpr auto kOpFormat_NCL = "NCL";
606 constexpr auto kSliceStart = "start";
607 constexpr auto kSliceStop = "stop";
608 constexpr auto kSliceStep = "step";
609
610 // graph parse
611 constexpr auto kClassTensorObject = "class_tensor_object";
612
613 // graph type
614 constexpr auto kFuncGraphTypeName = "FuncGraph";
615 constexpr auto kKernelGraphTypeName = "KernelGraph";
616
617 // graph group
618 constexpr auto kDefaultGroup = "DefaultGroup";
619 constexpr auto kKernelGroup = "KernelGroup";
620 constexpr auto kGraphGroup = "GraphGroup";
621
622 // compile cache
623 constexpr auto kUniqueCacheName = "UniqueCacheName";
624 constexpr auto kDistributedSplit = "distribtued_split";
625 constexpr auto kValidate = "validate";
626 constexpr auto kGraphId = "graph_id";
627 constexpr auto kBackendFrontAnf = "backend_front_anf";
628 constexpr auto kInternalParameterToFrontNode = "internal_parameter_to_front_node";
629 constexpr auto kOutInRef = "out_in_ref";
630 constexpr auto kIsFeatureMap = "is_feature_map";
631 constexpr auto kGraphValueNodes = "graph_value_nodes";
632 constexpr auto kExecutionOrder = "execution_order";
633 constexpr auto kChildGraphOrder = "child_graph_order";
634 constexpr auto kRunMode = "run_mode";
635 constexpr auto kIsLoopCountSink = "is_loop_count_sink";
636 constexpr auto kIsDynamicShape = "is_dynamic_shape";
637 constexpr auto kInputs = "inputs";
638 constexpr auto kParameters = "parameters";
639 constexpr auto kForwardOutput = "forward_output";
640 constexpr auto kChildGraphResult = "child_graph_result";
641 constexpr auto kDeviceTarget = "device_target";
642 constexpr auto kRootGraphId = "root_graph_id";
643 constexpr auto kExecutable = "executable";
644 constexpr auto kValidInputs = "valid_inputs";
645 constexpr auto kNeedInline = "need_inline";
646 constexpr auto kStartLabel = "start_label";
647 constexpr auto kEndGoto = "end_goto";
648 constexpr auto kPreGraphs = "pre_graphs";
649 constexpr auto kPostGraphs = "post_graphs";
650 constexpr auto kHasRecursiveCall = "has_recursive_call";
651 constexpr auto kHasSubgraphMultiCall = "has_subgraph_multicall";
652 constexpr auto kIsNeedGil = "is_need_gil";
653 constexpr auto kIsFromSingleOp = "is_from_single_op";
654 constexpr auto kCommSubGraphIds = "comm_sub_graph_ids";
655 constexpr auto kNodesKernelInfo = "nodes_kernel_info";
656 constexpr auto kAllInputFormat = "all_input_format";
657 constexpr auto kAllOutputFormat = "all_output_format";
658 constexpr auto kAllInputDeviceType = "all_input_device_type";
659 constexpr auto kAllOutputDeviceType = "all_output_device_type";
660 constexpr auto kAllInputReshapeType = "all_input_reshape_type";
661 constexpr auto kAllOutputReshapeType = "all_output_reshape_type";
662 constexpr auto kOutputDataDesc = "output_data_desc";
663 constexpr auto kCoreType = "core_type";
664 constexpr auto kRuntimeCacheValid = "runtime_cache_valid";
665 constexpr auto kRuntimeCacheDeviceTarget = "runtime_cache_device_target";
666 constexpr auto kRuntimeCacheOutputTensorNum = "runtime_cache_output_tensor_num";
667 constexpr auto kRuntimeCacheIsRealKernel = "runtime_cache_is_real_kernel";
668 constexpr auto kRuntimeCachePrevOutputs = "runtime_cache_prev_outputs";
669 constexpr auto kCorrespondFrontendGraph = "correspond_frontend_graph";
670 constexpr auto kReturnNode = "_return_node";
671 constexpr auto kReturnPrimNode = "_return_prim_node";
672 constexpr auto kOriginDataFormat = "origin_data_format";
673 constexpr auto kKernelType = "kernel_type";
674 constexpr auto kOpType = "op_type";
675 constexpr auto kFusionType = "fusion_type";
676 constexpr auto kOpPattern = "op_pattern";
677 constexpr auto kProcessor = "processor";
678 constexpr auto kKernelBuildInfoValid = "kernel_build_info_valid";
679 constexpr auto kInputKernelObjectTypes = "input_kernel_object_types";
680 constexpr auto kOutputKernelObjectTypes = "output_kernel_object_types";
681 constexpr auto kOutputElementsKernelObjectTypes = "output_elements_kernel_object_types";
682 constexpr auto kInputSizeList = "input_size_list";
683 constexpr auto kOutputSizeList = "output_size_list";
684 constexpr auto kJsonName = "json_name";
685 constexpr auto kHasSelectKernelBuildInfo = "has_select_kernel_build_info";
686 constexpr auto kBackendParamToFrontendParamIndex = "backend_param_to_frontend_param_index_";
687 constexpr auto kLabelNum = "label_num";
688 constexpr auto kParameterUniqueNameToName = "param_unique_name_to_name";
689 constexpr auto kRefInOutMap = "ref_in_out_map";
690 constexpr auto kRetryIntervalMilliSeconds = 500;
691 constexpr auto kSummaryNodes = "summary_nodes";
692 constexpr auto kSummaryNodeExist = "summary_node_exist";
693 constexpr auto kGeCache = "ge_cache";
694 constexpr auto kGeGraphKey = "ge.graph_key";
695 constexpr auto kGeGraphCompilerCacheDir = "ge.graph_compiler_cache_dir";
696 constexpr auto kIsRefGraph = "is_ref_graph";
697 constexpr auto kFromRefGraph = "from_ref_graph";
698
699 // recompute and parallel
700 constexpr auto kRecomputeInsert = "recompute_insert";
701 constexpr auto kAddedRecomputeDependAttr = "added_recompute_depend";
702 constexpr auto kCondidateOverlapBlockId = "condidate_overlap_block_id";
703 constexpr auto kNcclWorldGroup = "nccl_world_group";
704 constexpr auto kHcclWorldGroup = "hccl_world_group";
705 constexpr auto kSyncBnGroup = "sync_bn_group";
706 constexpr auto kRankID = "RANK_ID";
707
708 // User data key.
709
710 // pyexecute.
711 constexpr auto kSyncUserDataHandler = "sync_user_data_handler";
712
713 constexpr auto kRealElementsSize = "real_elements_size";
714
715 // For expander and pynative grad graph
716 enum class InputType {
717 // Scala or Constant tensor, no need to grad
718 kConstant = 0,
719 // Weight parameter tensor
720 kParameter,
721 // Net input tensor
722 kInput,
723 // Other op output tensor
724 kOpOutput,
725 // Default
726 kUnkown,
727 };
728
729 // Return vec<filename, line number, function name>
730 COMMON_EXPORT std::vector<std::tuple<std::string, int, std::string>> GetPythonStack_();
731 COMMON_EXPORT std::string GetPythonStackStr_();
732
733 COMMON_EXPORT bool IsOneOfCustomAkgType(const std::string &name);
734 COMMON_EXPORT bool IsOneOfOperator(const std::string &name);
735 COMMON_EXPORT bool IsOneOfNotSupportedTransFormat(const std::string &format);
736 COMMON_EXPORT bool IsOneOfPosteriorOperator(const std::string &name);
737 COMMON_EXPORT bool IsOneOfCacheBlackList(const std::string &name);
738 COMMON_EXPORT bool IsOneOf3DFormat(const std::string &format);
739 COMMON_EXPORT bool IsOneOfNoPaddingFormat(const std::string &format);
740 COMMON_EXPORT bool IsOneOfDynamicShapeConstInputToAttrGPU(const std::string &name);
741 COMMON_EXPORT bool IsOneOfComputeDepend(const std::string &name);
742 COMMON_EXPORT bool IsOneOfHWSpecialFormat(const std::string &format);
743 COMMON_EXPORT bool IsOneOfFormat(const std::string &format);
744 COMMON_EXPORT bool IsOneOfDefaultFormat(const std::string &format);
745 COMMON_EXPORT bool IsOneOfServerFormatC04(const std::string &format);
746 COMMON_EXPORT bool IsOneOfDynRankNeedPadShape(const std::string &format);
747 COMMON_EXPORT bool IsOneOfUnsignedType(const TypeId &type_id);
748
749 COMMON_EXPORT size_t GetSystemMemorySize(const std::string &key);
750 COMMON_EXPORT size_t GetSystemFreeDiskSize(const std::string &path);
751
752 COMMON_EXPORT bool IsEnableRefMode();
753 COMMON_EXPORT bool IsMemoryPoolRecycle();
754
755 // copy once flag, and reset copy flag when step end
756 COMMON_EXPORT bool SkipOrResetCopyAction(bool need_reset = false);
757 // only sync once flag
758 COMMON_EXPORT bool SkipOrResetSyncAction(bool need_reset = false);
759 // Return vec<filename, line number, function name>
760 COMMON_EXPORT std::vector<std::tuple<std::string, int, std::string>> GetPythonStack();
761 COMMON_EXPORT std::string GetPythonStackStr();
762
763 // The map between kernel's output and input ref relationship.
764 // Key is the output index while the value is input index which will be used as the reference of output.
765 using OutputInputRefMap = std::map<size_t, size_t>;
766
767 using HashTableExportData = std::vector<std::shared_ptr<std::vector<char>>>;
768
GetCurrentUSec()769 static inline double GetCurrentUSec() {
770 auto time_now = std::chrono::system_clock::now();
771 auto tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(time_now.time_since_epoch()).count();
772 return static_cast<double>(tv_usec);
773 }
774
775 #define PROF_START(stage) double start_usec_##stage = mindspore::GetCurrentUSec()
776 #define PROF_END(stage) \
777 do { \
778 double end_usec_##stage = mindspore::GetCurrentUSec(); \
779 std::ostringstream oss; \
780 oss << "[PROF]" << #stage << " costs " << (end_usec_##stage - start_usec_##stage) / kBasicTimeTransferUnit \
781 << " msec."; \
782 if (common::IsEnableRuntimeConfig(common::kRuntimeCompileStat)) { \
783 std::cout << oss.str() << std::endl; \
784 } \
785 MS_LOG(INFO) << oss.str(); \
786 } while (0)
787
788 #define PROF_MULTI_DEFINE(stage) \
789 do { \
790 static uint64_t total_##stage = 0; \
791 static uint64_t count_##stage = 0; \
792 } while (0)
793
794 #define PROF_LOCAL_DEFINE(stage) \
795 do { \
796 uint64_t total_##stage = 0; \
797 uint64_t count_##stage = 0; \
798 } while (0)
799
800 #define PROF_MULTI_START(stage) uint64_t start_usec_##stage = mindspore::GetCurrentUSec()
801
802 #define PROF_MULTI_END(stage) \
803 do { \
804 ++count_##stage; \
805 uint64_t end_usec_##stage = mindspore::GetCurrentUSec(); \
806 total_##stage += (end_usec_##stage - start_usec_##stage); \
807 } while (0)
808
809 #define PROF_MULTI_PRINT(stage) \
810 do { \
811 MS_LOG(INFO) << #stage << " called " << count_##stage << " times, costs " << total_##stage << " usec."; \
812 } while (0)
813
814 #define SET_FLAG(value, flag) ((value) = ((value) | (flag)))
815 #define TEST_FLAG(value, flag) (((value) & (flag)) == (flag))
816 #define CLEAR_FLAG(value, flag) ((value) = ((value) & (~(flag))))
817
818 #define _STRING_COMPILE_OPT(x) #x
819 #define STRING_COMPILE_OPT(x) _STRING_COMPILE_OPT(x)
820 } // namespace mindspore
821 #endif // MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_UTILS_H_
822