mindspore 2.3.0__cp39-cp39-win_amd64.whl → 2.4.1__cp39-cp39-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of mindspore might be problematic. Click here for more details.
- mindspore/.commit_id +1 -1
- mindspore/__init__.py +3 -1
- mindspore/_c_dataengine.cp39-win_amd64.pyd +0 -0
- mindspore/_c_expression.cp39-win_amd64.pyd +0 -0
- mindspore/_c_mindrecord.cp39-win_amd64.pyd +0 -0
- mindspore/_checkparam.py +50 -9
- mindspore/_extends/parse/compile_config.py +41 -0
- mindspore/_extends/parse/parser.py +9 -7
- mindspore/_extends/parse/standard_method.py +52 -14
- mindspore/_extends/pijit/pijit_func_white_list.py +350 -24
- mindspore/amp.py +24 -10
- mindspore/avcodec-59.dll +0 -0
- mindspore/avdevice-59.dll +0 -0
- mindspore/avfilter-8.dll +0 -0
- mindspore/avformat-59.dll +0 -0
- mindspore/avutil-57.dll +0 -0
- mindspore/common/__init__.py +6 -4
- mindspore/common/_pijit_context.py +190 -0
- mindspore/common/_register_for_tensor.py +2 -1
- mindspore/common/_tensor_overload.py +139 -0
- mindspore/common/api.py +102 -87
- mindspore/common/dump.py +5 -6
- mindspore/common/generator.py +1 -7
- mindspore/common/hook_handle.py +14 -26
- mindspore/common/initializer.py +51 -15
- mindspore/common/mindir_util.py +2 -2
- mindspore/common/parameter.py +62 -15
- mindspore/common/recompute.py +39 -9
- mindspore/common/sparse_tensor.py +7 -3
- mindspore/common/tensor.py +183 -37
- mindspore/communication/__init__.py +1 -1
- mindspore/communication/_comm_helper.py +38 -3
- mindspore/communication/comm_func.py +315 -60
- mindspore/communication/management.py +14 -14
- mindspore/context.py +132 -22
- mindspore/dataset/__init__.py +1 -1
- mindspore/dataset/audio/__init__.py +1 -1
- mindspore/dataset/core/config.py +7 -0
- mindspore/dataset/core/validator_helpers.py +7 -0
- mindspore/dataset/engine/cache_client.py +1 -1
- mindspore/dataset/engine/datasets.py +72 -44
- mindspore/dataset/engine/datasets_audio.py +7 -7
- mindspore/dataset/engine/datasets_standard_format.py +53 -3
- mindspore/dataset/engine/datasets_text.py +20 -20
- mindspore/dataset/engine/datasets_user_defined.py +174 -104
- mindspore/dataset/engine/datasets_vision.py +33 -33
- mindspore/dataset/engine/iterators.py +29 -0
- mindspore/dataset/engine/obs/util.py +7 -0
- mindspore/dataset/engine/queue.py +114 -60
- mindspore/dataset/engine/serializer_deserializer.py +2 -2
- mindspore/dataset/engine/validators.py +34 -14
- mindspore/dataset/text/__init__.py +1 -4
- mindspore/dataset/transforms/__init__.py +0 -3
- mindspore/dataset/utils/line_reader.py +2 -0
- mindspore/dataset/vision/__init__.py +1 -4
- mindspore/dataset/vision/utils.py +1 -1
- mindspore/dataset/vision/validators.py +2 -1
- mindspore/dnnl.dll +0 -0
- mindspore/{nn/extend → experimental/es}/__init__.py +4 -11
- mindspore/experimental/es/embedding_service.py +883 -0
- mindspore/{nn/layer → experimental/es}/embedding_service_layer.py +218 -30
- mindspore/experimental/llm_boost/__init__.py +21 -0
- mindspore/{nn/extend/layer → experimental/llm_boost/atb}/__init__.py +4 -8
- mindspore/experimental/llm_boost/atb/boost_base.py +211 -0
- mindspore/experimental/llm_boost/atb/llama_boost.py +115 -0
- mindspore/experimental/llm_boost/atb/qwen_boost.py +101 -0
- mindspore/experimental/llm_boost/register.py +129 -0
- mindspore/experimental/llm_boost/utils.py +31 -0
- mindspore/experimental/optim/adamw.py +85 -0
- mindspore/experimental/optim/optimizer.py +3 -0
- mindspore/hal/__init__.py +3 -3
- mindspore/hal/contiguous_tensors_handle.py +175 -0
- mindspore/hal/stream.py +18 -0
- mindspore/include/api/model_group.h +13 -1
- mindspore/include/api/types.h +10 -10
- mindspore/include/dataset/config.h +2 -2
- mindspore/include/dataset/constants.h +2 -2
- mindspore/include/dataset/execute.h +2 -2
- mindspore/include/dataset/vision.h +4 -0
- mindspore/jpeg62.dll +0 -0
- mindspore/log.py +1 -1
- mindspore/mindrecord/filewriter.py +68 -51
- mindspore/mindspore_backend.dll +0 -0
- mindspore/mindspore_common.dll +0 -0
- mindspore/mindspore_core.dll +0 -0
- mindspore/mindspore_glog.dll +0 -0
- mindspore/mindspore_np_dtype.dll +0 -0
- mindspore/mindspore_ops.dll +0 -0
- mindspore/mint/__init__.py +983 -46
- mindspore/mint/distributed/__init__.py +31 -0
- mindspore/mint/distributed/distributed.py +254 -0
- mindspore/mint/nn/__init__.py +268 -23
- mindspore/mint/nn/functional.py +125 -19
- mindspore/mint/nn/layer/__init__.py +39 -0
- mindspore/mint/nn/layer/activation.py +133 -0
- mindspore/mint/nn/layer/normalization.py +477 -0
- mindspore/mint/nn/layer/pooling.py +110 -0
- mindspore/mint/optim/adamw.py +26 -13
- mindspore/mint/special/__init__.py +63 -0
- mindspore/multiprocessing/__init__.py +2 -1
- mindspore/nn/__init__.py +0 -1
- mindspore/nn/cell.py +276 -96
- mindspore/nn/layer/activation.py +211 -44
- mindspore/nn/layer/basic.py +137 -10
- mindspore/nn/layer/embedding.py +137 -2
- mindspore/nn/layer/normalization.py +101 -5
- mindspore/nn/layer/padding.py +34 -48
- mindspore/nn/layer/pooling.py +161 -7
- mindspore/nn/layer/transformer.py +3 -3
- mindspore/nn/loss/__init__.py +2 -2
- mindspore/nn/loss/loss.py +84 -6
- mindspore/nn/optim/__init__.py +2 -1
- mindspore/nn/optim/adadelta.py +1 -1
- mindspore/nn/optim/adam.py +1 -1
- mindspore/nn/optim/lamb.py +1 -1
- mindspore/nn/optim/tft_wrapper.py +124 -0
- mindspore/nn/wrap/cell_wrapper.py +12 -23
- mindspore/nn/wrap/grad_reducer.py +5 -5
- mindspore/nn/wrap/loss_scale.py +17 -3
- mindspore/numpy/__init__.py +1 -1
- mindspore/numpy/array_creations.py +65 -68
- mindspore/numpy/array_ops.py +64 -60
- mindspore/numpy/fft.py +610 -75
- mindspore/numpy/logic_ops.py +11 -10
- mindspore/numpy/math_ops.py +85 -84
- mindspore/numpy/utils_const.py +4 -4
- mindspore/opencv_core452.dll +0 -0
- mindspore/opencv_imgcodecs452.dll +0 -0
- mindspore/opencv_imgproc452.dll +0 -0
- mindspore/ops/__init__.py +6 -4
- mindspore/ops/_grad_experimental/grad_array_ops.py +0 -11
- mindspore/ops/_grad_experimental/grad_comm_ops.py +67 -4
- mindspore/ops/_grad_experimental/grad_math_ops.py +0 -22
- mindspore/ops/_vmap/vmap_array_ops.py +2 -4
- mindspore/ops/_vmap/vmap_math_ops.py +17 -1
- mindspore/ops/_vmap/vmap_nn_ops.py +43 -2
- mindspore/ops/auto_generate/cpp_create_prim_instance_helper.py +91 -7
- mindspore/ops/auto_generate/gen_arg_dtype_cast.py +2 -0
- mindspore/ops/auto_generate/gen_extend_func.py +767 -13
- mindspore/ops/auto_generate/gen_ops_def.py +2452 -364
- mindspore/ops/auto_generate/gen_ops_prim.py +5442 -1756
- mindspore/ops/auto_generate/pyboost_inner_prim.py +176 -56
- mindspore/ops/composite/base.py +85 -48
- mindspore/ops/composite/multitype_ops/_compile_utils.py +1 -0
- mindspore/ops/composite/multitype_ops/not_in_impl.py +2 -2
- mindspore/ops/function/__init__.py +22 -0
- mindspore/ops/function/array_func.py +492 -153
- mindspore/ops/function/debug_func.py +113 -1
- mindspore/ops/function/fft_func.py +15 -2
- mindspore/ops/function/grad/grad_func.py +3 -2
- mindspore/ops/function/math_func.py +564 -207
- mindspore/ops/function/nn_func.py +817 -383
- mindspore/ops/function/other_func.py +3 -2
- mindspore/ops/function/random_func.py +402 -12
- mindspore/ops/function/reshard_func.py +13 -11
- mindspore/ops/function/sparse_unary_func.py +1 -1
- mindspore/ops/function/vmap_func.py +3 -2
- mindspore/ops/functional.py +24 -14
- mindspore/ops/op_info_register.py +3 -3
- mindspore/ops/operations/__init__.py +7 -2
- mindspore/ops/operations/_grad_ops.py +2 -76
- mindspore/ops/operations/_infer_ops.py +1 -1
- mindspore/ops/operations/_inner_ops.py +71 -94
- mindspore/ops/operations/array_ops.py +14 -146
- mindspore/ops/operations/comm_ops.py +63 -53
- mindspore/ops/operations/custom_ops.py +83 -19
- mindspore/ops/operations/debug_ops.py +42 -10
- mindspore/ops/operations/manually_defined/_inner.py +12 -0
- mindspore/ops/operations/manually_defined/ops_def.py +273 -20
- mindspore/ops/operations/math_ops.py +12 -223
- mindspore/ops/operations/nn_ops.py +20 -114
- mindspore/ops/operations/other_ops.py +7 -4
- mindspore/ops/operations/random_ops.py +46 -1
- mindspore/ops/primitive.py +18 -6
- mindspore/ops_generate/arg_dtype_cast.py +2 -0
- mindspore/ops_generate/gen_aclnn_implement.py +11 -11
- mindspore/ops_generate/gen_constants.py +36 -0
- mindspore/ops_generate/gen_ops.py +67 -52
- mindspore/ops_generate/gen_ops_inner_prim.py +1 -1
- mindspore/ops_generate/gen_pyboost_func.py +131 -47
- mindspore/ops_generate/op_proto.py +10 -3
- mindspore/ops_generate/pyboost_utils.py +14 -1
- mindspore/ops_generate/template.py +43 -21
- mindspore/parallel/__init__.py +3 -1
- mindspore/parallel/_auto_parallel_context.py +31 -9
- mindspore/parallel/_cell_wrapper.py +85 -0
- mindspore/parallel/_parallel_serialization.py +47 -19
- mindspore/parallel/_tensor.py +127 -13
- mindspore/parallel/_utils.py +53 -22
- mindspore/parallel/algo_parameter_config.py +5 -5
- mindspore/parallel/checkpoint_transform.py +46 -39
- mindspore/parallel/cluster/process_entity/__init__.py +1 -1
- mindspore/parallel/cluster/process_entity/_api.py +31 -23
- mindspore/parallel/cluster/process_entity/_utils.py +2 -27
- mindspore/parallel/parameter_broadcast.py +3 -4
- mindspore/parallel/shard.py +162 -31
- mindspore/parallel/transform_safetensors.py +1146 -0
- mindspore/profiler/__init__.py +2 -1
- mindspore/profiler/common/constant.py +29 -0
- mindspore/profiler/common/registry.py +47 -0
- mindspore/profiler/common/util.py +28 -0
- mindspore/profiler/dynamic_profiler.py +694 -0
- mindspore/profiler/envprofiling.py +17 -19
- mindspore/profiler/parser/ascend_analysis/constant.py +18 -0
- mindspore/profiler/parser/ascend_analysis/file_manager.py +25 -4
- mindspore/profiler/parser/ascend_analysis/function_event.py +43 -19
- mindspore/profiler/parser/ascend_analysis/fwk_cann_parser.py +31 -26
- mindspore/profiler/parser/ascend_analysis/fwk_file_parser.py +56 -10
- mindspore/profiler/parser/ascend_analysis/msprof_timeline_parser.py +55 -8
- mindspore/profiler/parser/ascend_analysis/path_manager.py +313 -0
- mindspore/profiler/parser/ascend_analysis/profiler_info_parser.py +27 -20
- mindspore/profiler/parser/ascend_analysis/trace_event_manager.py +9 -2
- mindspore/profiler/parser/ascend_msprof_exporter.py +5 -4
- mindspore/profiler/parser/ascend_timeline_generator.py +27 -25
- mindspore/profiler/parser/base_timeline_generator.py +19 -25
- mindspore/profiler/parser/cpu_gpu_timeline_generator.py +25 -12
- mindspore/profiler/parser/framework_parser.py +1 -391
- mindspore/profiler/parser/gpu_analysis/__init__.py +14 -0
- mindspore/profiler/parser/gpu_analysis/function_event.py +44 -0
- mindspore/profiler/parser/gpu_analysis/fwk_file_parser.py +89 -0
- mindspore/profiler/parser/gpu_analysis/profiler_info_parser.py +72 -0
- mindspore/profiler/parser/memory_usage_parser.py +0 -154
- mindspore/profiler/parser/profiler_info.py +78 -6
- mindspore/profiler/profiler.py +153 -0
- mindspore/profiler/profiling.py +285 -413
- mindspore/rewrite/__init__.py +1 -2
- mindspore/rewrite/common/namespace.py +4 -4
- mindspore/rewrite/symbol_tree/symbol_tree.py +3 -3
- mindspore/run_check/_check_version.py +39 -104
- mindspore/safeguard/rewrite_obfuscation.py +591 -247
- mindspore/swresample-4.dll +0 -0
- mindspore/swscale-6.dll +0 -0
- mindspore/tinyxml2.dll +0 -0
- mindspore/train/__init__.py +4 -3
- mindspore/train/_utils.py +105 -19
- mindspore/train/amp.py +171 -53
- mindspore/train/callback/__init__.py +2 -2
- mindspore/train/callback/_callback.py +4 -4
- mindspore/train/callback/_checkpoint.py +97 -31
- mindspore/train/callback/_cluster_monitor.py +1 -1
- mindspore/train/callback/_flops_collector.py +1 -0
- mindspore/train/callback/_loss_monitor.py +3 -3
- mindspore/train/callback/_on_request_exit.py +145 -31
- mindspore/train/callback/_summary_collector.py +5 -5
- mindspore/train/callback/_tft_register.py +375 -0
- mindspore/train/dataset_helper.py +15 -3
- mindspore/train/metrics/metric.py +3 -3
- mindspore/train/metrics/roc.py +4 -4
- mindspore/train/mind_ir_pb2.py +44 -39
- mindspore/train/model.py +154 -58
- mindspore/train/serialization.py +342 -128
- mindspore/turbojpeg.dll +0 -0
- mindspore/utils/__init__.py +21 -0
- mindspore/utils/utils.py +60 -0
- mindspore/version.py +1 -1
- {mindspore-2.3.0.dist-info → mindspore-2.4.1.dist-info}/METADATA +13 -7
- {mindspore-2.3.0.dist-info → mindspore-2.4.1.dist-info}/RECORD +260 -254
- {mindspore-2.3.0.dist-info → mindspore-2.4.1.dist-info}/WHEEL +1 -1
- mindspore/include/c_api/ms/abstract.h +0 -67
- mindspore/include/c_api/ms/attribute.h +0 -197
- mindspore/include/c_api/ms/base/handle_types.h +0 -43
- mindspore/include/c_api/ms/base/macros.h +0 -32
- mindspore/include/c_api/ms/base/status.h +0 -33
- mindspore/include/c_api/ms/base/types.h +0 -283
- mindspore/include/c_api/ms/context.h +0 -102
- mindspore/include/c_api/ms/graph.h +0 -160
- mindspore/include/c_api/ms/node.h +0 -606
- mindspore/include/c_api/ms/tensor.h +0 -161
- mindspore/include/c_api/ms/value.h +0 -84
- mindspore/mindspore_shared_lib.dll +0 -0
- mindspore/nn/extend/basic.py +0 -140
- mindspore/nn/extend/embedding.py +0 -143
- mindspore/nn/extend/layer/normalization.py +0 -109
- mindspore/nn/extend/pooling.py +0 -117
- mindspore/nn/layer/embedding_service.py +0 -531
- mindspore/ops/_op_impl/aicpu/strided_slice_v2.py +0 -93
- mindspore/ops/_op_impl/aicpu/strided_slice_v2_grad.py +0 -66
- mindspore/ops/extend/__init__.py +0 -53
- mindspore/ops/extend/array_func.py +0 -218
- mindspore/ops/extend/math_func.py +0 -76
- mindspore/ops/extend/nn_func.py +0 -308
- mindspore/ops/silent_check.py +0 -162
- mindspore/profiler/parser/msadvisor_analyzer.py +0 -82
- mindspore/profiler/parser/msadvisor_parser.py +0 -240
- mindspore/train/callback/_mindio_ttp.py +0 -443
- {mindspore-2.3.0.dist-info → mindspore-2.4.1.dist-info}/entry_points.txt +0 -0
- {mindspore-2.3.0.dist-info → mindspore-2.4.1.dist-info}/top_level.txt +0 -0
mindspore/ops/extend/nn_func.py
DELETED
|
@@ -1,308 +0,0 @@
|
|
|
1
|
-
# Copyright 2023 Huawei Technologies Co., Ltd
|
|
2
|
-
#
|
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
# you may not use this file except in compliance with the License.
|
|
5
|
-
# You may obtain a copy of the License at
|
|
6
|
-
#
|
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
#
|
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
# See the License for the specific language governing permissions and
|
|
13
|
-
# limitations under the License.
|
|
14
|
-
# ============================================================================
|
|
15
|
-
|
|
16
|
-
"""
|
|
17
|
-
|
|
18
|
-
NN Operators with better performance
|
|
19
|
-
|
|
20
|
-
"""
|
|
21
|
-
from mindspore.ops._primitive_cache import _get_cache_prim
|
|
22
|
-
from mindspore.ops.auto_generate.gen_ops_prim import Convolution, ConstantPadND, MaxPoolWithIndices, MaxPoolWithMask
|
|
23
|
-
from mindspore.ops.auto_generate import leaky_relu_ext
|
|
24
|
-
from mindspore import _checkparam as validator
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def _check_stride_when_same_mode(stride):
|
|
28
|
-
""" stride must be 1 when pad mode is same """
|
|
29
|
-
if isinstance(stride, int):
|
|
30
|
-
if stride != 1:
|
|
31
|
-
raise ValueError(f"For conv2d, 'padding=same' is not supported for stride convolution, " \
|
|
32
|
-
f"but got {stride}")
|
|
33
|
-
elif isinstance(stride, tuple):
|
|
34
|
-
validator.check_int(len(stride), 2, validator.EQ, "stride", 'conv2d')
|
|
35
|
-
if not all(s == 1 for s in stride):
|
|
36
|
-
raise ValueError(f"For conv2d, 'padding=same' is not supported for stride convolution, " \
|
|
37
|
-
f"but got {stride}")
|
|
38
|
-
else:
|
|
39
|
-
raise TypeError(f"For conv2d, the parameter 'stride' must be a int/tuple, but got {type(stride)}")
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
def _get_pad_info(dilation, weight):
|
|
43
|
-
""" Get pad list by dilation and weight shape """
|
|
44
|
-
need_pad_nd = False
|
|
45
|
-
pad_l = ()
|
|
46
|
-
pad_r = ()
|
|
47
|
-
for i in range(2):
|
|
48
|
-
d = dilation[i]
|
|
49
|
-
weight_size = weight.shape[i + 2]
|
|
50
|
-
pad = d * (weight_size - 1)
|
|
51
|
-
pad_l += (int(pad / 2),)
|
|
52
|
-
pad_r += (int(pad - pad_l[i]),)
|
|
53
|
-
if pad_l[i] != pad_r[i]:
|
|
54
|
-
need_pad_nd = True
|
|
55
|
-
return need_pad_nd, pad_l, pad_r
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
def _get_pad_nd_info(pad_l, pad_r):
|
|
59
|
-
""" Get pad_nd list if input need to exec pad_nd """
|
|
60
|
-
pad_nd = ()
|
|
61
|
-
new_pad_l = ()
|
|
62
|
-
for i in range(2):
|
|
63
|
-
delta_pad = pad_r[i] - pad_l[i]
|
|
64
|
-
if delta_pad > 0:
|
|
65
|
-
pad_nd = (0, delta_pad,) + pad_nd
|
|
66
|
-
new_pad_l += (pad_l[i],)
|
|
67
|
-
else:
|
|
68
|
-
pad_nd = (delta_pad, 0,) + pad_nd
|
|
69
|
-
new_pad_l += (pad_r[i],)
|
|
70
|
-
return pad_nd, new_pad_l
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
def conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1):
|
|
74
|
-
r"""
|
|
75
|
-
Applies a 2D convolution over an input tensor. The input tenor is typically of
|
|
76
|
-
shape :math:`(N, C_{in}, H_{in}, W_{in})`, where :math:`N` is batch size, :math:`C` is
|
|
77
|
-
channel number, :math:`H` is feature height, :math:`W` is feature width.
|
|
78
|
-
|
|
79
|
-
The output is calculated based on formula:
|
|
80
|
-
|
|
81
|
-
.. math::
|
|
82
|
-
|
|
83
|
-
\text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) +
|
|
84
|
-
\sum_{k = 0}^{C_{in} - 1} \text{ccor}({\text{weight}(C_{\text{out}_j}, k), \text{X}(N_i, k)})
|
|
85
|
-
|
|
86
|
-
where :math:`bias` is the output channel bias, :math:`ccor` is
|
|
87
|
-
the `cross-correlation <https://en.wikipedia.org/wiki/Cross-correlation>`_,
|
|
88
|
-
, :math:`weight` is the convolution kernel value and :math:`X` represents the input feature map.
|
|
89
|
-
|
|
90
|
-
Here are the indices' meanings:
|
|
91
|
-
|
|
92
|
-
- :math:`i` corresponds to the batch number, the range is :math:`[0, N-1]`,
|
|
93
|
-
where :math:`N` is the batch size of the input.
|
|
94
|
-
|
|
95
|
-
- :math:`j` corresponds to the output channel, the range is :math:`[0, C_{out}-1]`,
|
|
96
|
-
where :math:`C_{out}` is the number of output channels, which is also equal to the number of kernels.
|
|
97
|
-
|
|
98
|
-
- :math:`k` corresponds to the input channel, the range is :math:`[0, C_{in}-1]`,
|
|
99
|
-
where :math:`C_{in}` is the number of
|
|
100
|
-
input channels, which is also equal to the number of channels in the convolutional kernels.
|
|
101
|
-
|
|
102
|
-
Therefore, in the above formula, :math:`{bias}(C_{out_j})` represents the bias of the :math:`j`-th
|
|
103
|
-
output channel, :math:`{weight}(C_{out_j}, k)` represents the slice of the :math:`j`-th convolutional
|
|
104
|
-
kernel in the :math:`k`-th channel, and :math:`{X}(N_i, k)` represents the slice of the :math:`k`-th input
|
|
105
|
-
channel in the :math:`i`-th batch of the input feature map.
|
|
106
|
-
|
|
107
|
-
The shape of the convolutional kernel is given by :math:`(\text{kernel_size[0]}, \text{kernel_size[1]})`,
|
|
108
|
-
where :math:`\text{kernel_size[0]}` and :math:`\text{kernel_size[1]}` are the height and width of the kernel,
|
|
109
|
-
respectively.
|
|
110
|
-
If we consider the input and output channels as well as the `group` parameter, the complete kernel shape
|
|
111
|
-
will be :math:`(C_{out}, C_{in} / \text{group}, \text{kernel_size[0]}, \text{kernel_size[1]})`,
|
|
112
|
-
where `group` is the number of groups dividing `x`'s input channel when applying group convolution.
|
|
113
|
-
|
|
114
|
-
For more details about convolution layer, please refer to `Gradient Based Learning Applied to Document Recognition
|
|
115
|
-
<http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf>`_ and
|
|
116
|
-
`ConvNets <http://cs231n.github.io/convolutional-networks/>`_.
|
|
117
|
-
|
|
118
|
-
Note:
|
|
119
|
-
On Ascend platform, only group convolution in depthwise convolution scenarios is supported.
|
|
120
|
-
That is, when `groups>1`, condition :math:`C_{in}` = :math:`C_{out}` = `groups` must be satisfied.
|
|
121
|
-
|
|
122
|
-
Args:
|
|
123
|
-
input (Tensor): Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`.
|
|
124
|
-
weight (Tensor): Tensor of shape
|
|
125
|
-
:math:`(N, C_{in} / \text{groups}, \text{kernel_size[0]}, \text{kernel_size[1]})`, then the size of kernel
|
|
126
|
-
is :math:`(\text{kernel_size[0]}, \text{kernel_size[1]})`.
|
|
127
|
-
bias (Tensor, optional): Bias Tensor with shape :math:`(C_{out})`.
|
|
128
|
-
When bias is ``None`` , zeros will be used. Default: ``None`` .
|
|
129
|
-
stride (Union(int, tuple[int]), optional): The distance of kernel moving, an int number that represents
|
|
130
|
-
the height and width of movement are both strides, or a tuple of two int numbers that
|
|
131
|
-
represent height and width of movement respectively. Default: ``1`` .
|
|
132
|
-
padding (Union(int, tuple[int], list[int], str), optional): Implicit paddings on both sides of the input `x`.
|
|
133
|
-
Can be a string, one integer or a tuple/list with 2 integers.
|
|
134
|
-
If `padding` is a string, the optional values are ``"same"`` , ``"valid"``.
|
|
135
|
-
|
|
136
|
-
- same: Adopts the way of completion. The height and width of the output will be equal to
|
|
137
|
-
the input `x` divided by stride. The padding will be evenly calculated in top and bottom,
|
|
138
|
-
left and right possiblily. Otherwise, the last extra padding will be calculated from the bottom
|
|
139
|
-
and the right side. If this mode is set, `padding` must be 0.
|
|
140
|
-
|
|
141
|
-
- valid: Adopts the way of discarding. The possible largest height and width of output will be returned
|
|
142
|
-
without padding. Extra pixels will be discarded. If this mode is set, `padding` must be 0.
|
|
143
|
-
|
|
144
|
-
If `padding` is one integer, the paddings of top, bottom, left and right are the same, equal to padding.
|
|
145
|
-
If `padding` is a tuple/list with 2 integers, the padding of top adn bottom is padding[0],
|
|
146
|
-
and the padding of left and right is padding[1]. Default: ``0`` .
|
|
147
|
-
dilation (Union(int, tuple[int]), optional): Gaps between kernel elements.The data type is int or a tuple of
|
|
148
|
-
2 integers. Specifies the dilation rate to use for dilated convolution. If set to be :math:`k > 1`,
|
|
149
|
-
there will be :math:`k - 1` pixels skipped for each sampling location. Its value must
|
|
150
|
-
be greater than or equal to 1 and bounded by the height and width of the input `x`. Default: ``1`` .
|
|
151
|
-
groups (int, optional): Splits `input` into groups. Default: ``1`` .
|
|
152
|
-
|
|
153
|
-
Returns:
|
|
154
|
-
Tensor, the value that applied 2D convolution. The shape is :math:`(N, C_{out}, H_{out}, W_{out})`.
|
|
155
|
-
To see how different pad modes affect the output shape, please refer to
|
|
156
|
-
:class:`mindspore.nn.Conv2d` for more details.
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
Raises:
|
|
160
|
-
TypeError: If `stride`, `padding` or `dilation` is neither an int nor a tuple.
|
|
161
|
-
TypeError: `groups` is not an int.
|
|
162
|
-
TypeError: If `bias` is not a Tensor.
|
|
163
|
-
ValueError: If the shape of `bias` is not :math:`(C_{out})` .
|
|
164
|
-
ValueError: If `stride` or `dilation` is less than 1.
|
|
165
|
-
ValueError: If `pad_mode` is not one of 'same', 'valid' or 'pad'.
|
|
166
|
-
ValueError: If `padding` is a tuple/list whose length is not equal to 2.
|
|
167
|
-
|
|
168
|
-
Supported Platforms:
|
|
169
|
-
``Ascend``
|
|
170
|
-
|
|
171
|
-
Examples:
|
|
172
|
-
>>> import mindspore
|
|
173
|
-
>>> import numpy as np
|
|
174
|
-
>>> from mindspore import Tensor, ops
|
|
175
|
-
>>> x = Tensor(np.ones([10, 32, 32, 32]), mindspore.float32)
|
|
176
|
-
>>> weight = Tensor(np.ones([32, 32, 3, 3]), mindspore.float32)
|
|
177
|
-
>>> output = ops.extend.conv2d(x, weight)
|
|
178
|
-
>>> print(output.shape)
|
|
179
|
-
(10, 32, 30, 30)
|
|
180
|
-
"""
|
|
181
|
-
|
|
182
|
-
def _convolution_same(input, weight, bias, dilation, groups):
|
|
183
|
-
""" convolution when mode is 'same' """
|
|
184
|
-
if isinstance(dilation, int):
|
|
185
|
-
dilation = (dilation,) * 2
|
|
186
|
-
validator.check_int(len(weight.shape), 4, validator.EQ, "weight.shape", 'conv2d')
|
|
187
|
-
validator.check_int(len(dilation), 2, validator.EQ, "dilation", 'conv2d')
|
|
188
|
-
|
|
189
|
-
# Calc padding info
|
|
190
|
-
need_pad_nd, pad_l, pad_r = _get_pad_info(dilation, weight)
|
|
191
|
-
if not need_pad_nd:
|
|
192
|
-
conv = _get_cache_prim(Convolution)(stride, pad_l, dilation, False, (0, 0), groups)
|
|
193
|
-
return conv(input, weight, bias)
|
|
194
|
-
|
|
195
|
-
# Calc pad nd info
|
|
196
|
-
pad_nd, pad_l = _get_pad_nd_info(pad_l, pad_r)
|
|
197
|
-
pad_nd_op = _get_cache_prim(ConstantPadND)()
|
|
198
|
-
padded_input = pad_nd_op(input, pad_nd, 0)
|
|
199
|
-
conv = _get_cache_prim(Convolution)(stride, pad_l, dilation, False, (0, 0), groups)
|
|
200
|
-
return conv(padded_input, weight, bias)
|
|
201
|
-
|
|
202
|
-
if isinstance(padding, int):
|
|
203
|
-
padding = (padding,) * 2
|
|
204
|
-
|
|
205
|
-
if isinstance(padding, (tuple, list)):
|
|
206
|
-
conv = _get_cache_prim(Convolution)(stride, padding, dilation, False, (0, 0), groups)
|
|
207
|
-
return conv(input, weight, bias)
|
|
208
|
-
if isinstance(padding, str):
|
|
209
|
-
if padding == 'valid':
|
|
210
|
-
conv = _get_cache_prim(Convolution)(stride, (0, 0), dilation, False, (0, 0), groups)
|
|
211
|
-
return conv(input, weight, bias)
|
|
212
|
-
if padding == 'same':
|
|
213
|
-
_check_stride_when_same_mode(stride)
|
|
214
|
-
return _convolution_same(input, weight, bias, dilation, groups)
|
|
215
|
-
raise ValueError(f"For conv2d, the parameter 'padding' must be 'same' or 'valid' when " \
|
|
216
|
-
f"the type of 'padding' is string.")
|
|
217
|
-
raise TypeError(f"For conv2d, the parameter 'padding' must be a tuple/list " \
|
|
218
|
-
f"or a string, but got {type(padding)}")
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
def max_pool2d(input, kernel_size, stride=None, padding=0, dilation=1, *, ceil_mode=False, return_indices=False):
|
|
222
|
-
r"""
|
|
223
|
-
Performs a 2D max pooling on the input Tensor.
|
|
224
|
-
|
|
225
|
-
Typically, the input is a Tensor with shape :math:`(N_{in}, C_{in}, H_{in}, W_{in})`, outputs
|
|
226
|
-
regional maximum in the :math:`(H_{in}, W_{in})`-dimension. Given `kernel_size`
|
|
227
|
-
:math:`ks = (h_{ker}, w_{ker})` and `stride` :math:`s = (s_0, s_1)`, the operation is as follows:
|
|
228
|
-
|
|
229
|
-
.. math::
|
|
230
|
-
\text{output}(N_i, C_j, h, w) =
|
|
231
|
-
\max_{m=0, \ldots, h_{ker}-1} \max_{n=0, \ldots, w_{ker}-1}
|
|
232
|
-
\text{input}(N_i, C_j, s_0 \times h + m, s_1 \times w + n)
|
|
233
|
-
|
|
234
|
-
.. warning::
|
|
235
|
-
Only support on Atlas A2 training series.
|
|
236
|
-
|
|
237
|
-
Args:
|
|
238
|
-
input (Tensor): Tensor of shape :math:`(N_{in}, C_{in}, H_{in}, W_{in})` with data type of float32
|
|
239
|
-
in Ascend.
|
|
240
|
-
kernel_size (Union[int, tuple[int]]): The size of kernel used to take the maximum value and arg
|
|
241
|
-
value, is an int number that represents height and width of the kernel, or a tuple of
|
|
242
|
-
two int numbers that represent height and width respectively.
|
|
243
|
-
stride (Union[int, tuple[int], None]): The distance of kernel moving, an int number that represents
|
|
244
|
-
the height and width of movement are both stride, or a tuple of two int numbers that
|
|
245
|
-
represent height and width of movement respectively.
|
|
246
|
-
Default: ``None`` , which indicates the moving step is `kernel_size` .
|
|
247
|
-
padding (Union[int, tuple[int]]): An int number that represents the height and width of movement are both
|
|
248
|
-
strides, or a tuple of two int numbers that represent height and width of movement respectively.
|
|
249
|
-
Default: ``0`` .
|
|
250
|
-
dilation (Union[int, tuple[int]]): Control the stride of elements in the kernel. Default: ``1`` .
|
|
251
|
-
ceil_mode (bool): Whether to use ceil instead of floor to calculate output shape. Default: ``False`` .
|
|
252
|
-
return_indices (bool): Whether to output the indices of max value. Default: ``False`` .
|
|
253
|
-
|
|
254
|
-
Returns:
|
|
255
|
-
If `return_indices` is ``False`` , return a Tensor `output`, else return a tuple (`output`, `argmax`).
|
|
256
|
-
|
|
257
|
-
- **output** (Tensor) - Maxpooling result, with shape :math:`(N_{out}, C_{out}, H_{out}, W_{out})`.
|
|
258
|
-
It has the same data type as `input`.
|
|
259
|
-
|
|
260
|
-
.. math::
|
|
261
|
-
H_{out} = \left\lfloor\frac{H_{in} + 2 * \text{padding[0]} - \text{dilation[0]}
|
|
262
|
-
\times (\text{kernel_size[0]} - 1) - 1}{\text{stride[0]}} + 1\right\rfloor
|
|
263
|
-
|
|
264
|
-
.. math::
|
|
265
|
-
W_{out} = \left\lfloor\frac{W_{in} + 2 * \text{padding[1]} - \text{dilation[1]}
|
|
266
|
-
\times (\text{kernel_size[1]} - 1) - 1}{\text{stride[1]}} + 1\right\rfloor
|
|
267
|
-
|
|
268
|
-
- **argmax** (Tensor) - Index corresponding to the maximum value. In Ascend, data type is int32.
|
|
269
|
-
It will be return only when `return_indices` is True.
|
|
270
|
-
|
|
271
|
-
Raises:
|
|
272
|
-
TypeError: If `input` is not a Tensor.
|
|
273
|
-
ValueError: If length of shape of `input` is not equal to 4.
|
|
274
|
-
TypeError: If `kernel_size` , `stride` , `padding` or `dilation` is not int or tuple.
|
|
275
|
-
ValueError: If `kernel_size`, `stride` or `dilation` is less than 1.
|
|
276
|
-
ValueError: If `dilation` is not all 1.
|
|
277
|
-
ValueError: If `padding` is less than 0.
|
|
278
|
-
ValueError: If `padding` is more than half of `kernel_size`.
|
|
279
|
-
TypeError: If `ceil_mode` is not bool.
|
|
280
|
-
|
|
281
|
-
Supported Platforms:
|
|
282
|
-
``Ascend``
|
|
283
|
-
|
|
284
|
-
Examples:
|
|
285
|
-
>>> import mindspore
|
|
286
|
-
>>> import numpy as np
|
|
287
|
-
>>> from mindspore import Tensor, ops
|
|
288
|
-
>>> input = Tensor(np.arange(20 * 16 * 50 * 32).reshape((20, 16, 50, 32)), mindspore.float32)
|
|
289
|
-
>>> output_tensor, argmax = ops.extend.max_pool2d(input, kernel_size=(3, 2), stride=(2, 1),
|
|
290
|
-
ceil_mode=False, return_indices=True)
|
|
291
|
-
>>> print(output_tensor.shape)
|
|
292
|
-
(20, 16, 24, 31)
|
|
293
|
-
>>> print(argmax.shape)
|
|
294
|
-
(20, 16, 24, 31)
|
|
295
|
-
"""
|
|
296
|
-
strides = stride if (stride is not None) else kernel_size
|
|
297
|
-
if return_indices:
|
|
298
|
-
max_pool_func_ = _get_cache_prim(MaxPoolWithIndices)(kernel_size, strides, padding, dilation, ceil_mode)
|
|
299
|
-
out, indices = max_pool_func_(input)
|
|
300
|
-
else:
|
|
301
|
-
max_pool_func_ = _get_cache_prim(MaxPoolWithMask)(kernel_size, strides, padding, dilation, ceil_mode)
|
|
302
|
-
out, indices = max_pool_func_(input)
|
|
303
|
-
if return_indices:
|
|
304
|
-
return out, indices
|
|
305
|
-
return out
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
__all__ = ['conv2d', 'max_pool2d', 'leaky_relu_ext']
|
mindspore/ops/silent_check.py
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
# Copyright 2024 Huawei Technologies Co., Ltd
|
|
2
|
-
#
|
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
# you may not use this file except in compliance with the License.
|
|
5
|
-
# You may obtain a copy of the License at
|
|
6
|
-
#
|
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
#
|
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
# See the License for the specific language governing permissions and
|
|
13
|
-
# limitations under the License.
|
|
14
|
-
# ============================================================================
|
|
15
|
-
"""Silent Check."""
|
|
16
|
-
import os
|
|
17
|
-
|
|
18
|
-
from mindspore.common.tensor import Tensor
|
|
19
|
-
from mindspore.common.parameter import Parameter
|
|
20
|
-
import mindspore.common.dtype as mstype
|
|
21
|
-
|
|
22
|
-
from . import operations
|
|
23
|
-
from .operations._inner_ops import _MirrorSilentCheck
|
|
24
|
-
from .operations import RmsNorm as OriginRmsNorm
|
|
25
|
-
from .operations import LayerNorm as OriginLayerNorm
|
|
26
|
-
from .primitive import Primitive
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
NPU_ASD_ENABLE = 'NPU_ASD_ENABLE'
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
class ASDBase:
|
|
33
|
-
"""
|
|
34
|
-
ASDBase is the base class of operator with feature value detection in python.
|
|
35
|
-
|
|
36
|
-
Args:
|
|
37
|
-
cls (Primitive): Original operator requiring feature value detection.
|
|
38
|
-
args (tuple): A variable parameter tuple to the original operator.
|
|
39
|
-
kwargs (dict): A variable parameter dictionary passed the original operator.
|
|
40
|
-
|
|
41
|
-
Supported Platforms:
|
|
42
|
-
``Ascend``
|
|
43
|
-
|
|
44
|
-
Examples:
|
|
45
|
-
>>> from mindspore.ops.silent_check import ASDBase
|
|
46
|
-
>>> from mindspore.ops import LayerNorm as OriginLayerNorm
|
|
47
|
-
>>> class LayerNormASD(ASDBase):
|
|
48
|
-
... def __init__(self, *args, **kwargs):
|
|
49
|
-
... super().__init__(OriginLayerNorm, *args, **kwargs)
|
|
50
|
-
... # init parameters for accuracy-sensitive detection by calling the base class method generate_params()
|
|
51
|
-
... self.pre_val, self.min_val, self.max_val, self.cnt = self.generate_params()
|
|
52
|
-
...
|
|
53
|
-
... def __call__(self, input_x, gamma, beta):
|
|
54
|
-
... if self.enable_check:
|
|
55
|
-
... # execute accuracy-sensitive detection by calling the check_op of base class
|
|
56
|
-
... input_x = self.check_op(
|
|
57
|
-
... input_x, self.pre_val, self.min_val, self.max_val, self.cnt, None)
|
|
58
|
-
... self.cnt += 1
|
|
59
|
-
... # return the result of original operator
|
|
60
|
-
... return self.op(input_x, gamma, beta)
|
|
61
|
-
"""
|
|
62
|
-
_index = 0
|
|
63
|
-
__ms_class__ = True
|
|
64
|
-
|
|
65
|
-
def __init__(self, cls, *args, **kwargs):
|
|
66
|
-
self.op = cls(*args, **kwargs)
|
|
67
|
-
self.check_op = _MirrorSilentCheck()
|
|
68
|
-
self._suffix = "ASD_" + cls.__name__
|
|
69
|
-
primitive_attr = dir(Primitive)
|
|
70
|
-
self._op_attr_dict = {
|
|
71
|
-
name for name in primitive_attr if not name.startswith("_")}
|
|
72
|
-
self.enable_check = os.environ.get(NPU_ASD_ENABLE) == "1"
|
|
73
|
-
|
|
74
|
-
def __getattr__(self, name):
|
|
75
|
-
def method_wrapper(*args, **kwargs):
|
|
76
|
-
out = getattr(self.op, name)(*args, **kwargs)
|
|
77
|
-
if out is self.op:
|
|
78
|
-
return self
|
|
79
|
-
return out
|
|
80
|
-
|
|
81
|
-
if name in self._op_attr_dict:
|
|
82
|
-
if callable(getattr(self.op, name)):
|
|
83
|
-
return method_wrapper
|
|
84
|
-
if hasattr(self.op, name):
|
|
85
|
-
return getattr(self.op, name)
|
|
86
|
-
return super().__getattr__(self, name)
|
|
87
|
-
|
|
88
|
-
def __repr__(self):
|
|
89
|
-
return self.op.__repr__()
|
|
90
|
-
|
|
91
|
-
def generate_params(self):
|
|
92
|
-
"""
|
|
93
|
-
Generate support params for feature value detection.
|
|
94
|
-
|
|
95
|
-
Returns:
|
|
96
|
-
tuple consisting of four elements.
|
|
97
|
-
The derived class initializes the parameters required for feature value detection by calling
|
|
98
|
-
this function.
|
|
99
|
-
|
|
100
|
-
Examples:
|
|
101
|
-
>>> from mindspore.ops.silent_check import ASDBase
|
|
102
|
-
>>> from mindspore.ops import LayerNorm as OriginLayerNorm
|
|
103
|
-
>>> class LayerNormASD(ASDBase):
|
|
104
|
-
... def __init__(self, *args, **kwargs):
|
|
105
|
-
... super().__init__(OriginLayerNorm, *args, **kwargs)
|
|
106
|
-
... # init parameters for feature value detection by calling the base class function
|
|
107
|
-
... self.pre_val, self.min_val, self.max_val, self.cnt = self.generate_params()
|
|
108
|
-
"""
|
|
109
|
-
pre_val = Parameter(Tensor(0, mstype.float32),
|
|
110
|
-
name=f"{self._suffix}_pre_val_{self._index}",
|
|
111
|
-
requires_grad=False)
|
|
112
|
-
min_val = Parameter(Tensor(0, mstype.float32),
|
|
113
|
-
name=f"{self._suffix}_min_val_{self._index}",
|
|
114
|
-
requires_grad=False)
|
|
115
|
-
max_val = Parameter(Tensor(0, mstype.float32),
|
|
116
|
-
name=f"{self._suffix}_max_val_{self._index}",
|
|
117
|
-
requires_grad=False)
|
|
118
|
-
cnt = Parameter(Tensor(0, mstype.int32),
|
|
119
|
-
name=f"{self._suffix}_cnt_{self._index}",
|
|
120
|
-
requires_grad=False)
|
|
121
|
-
ASDBase._index += 1
|
|
122
|
-
return pre_val, min_val, max_val, cnt
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
class RmsNormASD(ASDBase):
|
|
126
|
-
"""
|
|
127
|
-
RmsNorm with ASD.
|
|
128
|
-
"""
|
|
129
|
-
|
|
130
|
-
def __init__(self, *args, **kwargs):
|
|
131
|
-
super().__init__(OriginRmsNorm, *args, **kwargs)
|
|
132
|
-
self.pre_val, self.min_val, self.max_val, self.cnt = self.generate_params()
|
|
133
|
-
|
|
134
|
-
def __call__(self, input_x, gamma):
|
|
135
|
-
if self.enable_check:
|
|
136
|
-
input_x = self.check_op(
|
|
137
|
-
input_x, self.pre_val, self.min_val, self.max_val, self.cnt, None)
|
|
138
|
-
self.cnt += 1
|
|
139
|
-
return self.op(input_x, gamma)
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
class LayerNormASD(ASDBase):
|
|
143
|
-
"""
|
|
144
|
-
LayerNorm with ASD.
|
|
145
|
-
"""
|
|
146
|
-
|
|
147
|
-
def __init__(self, *args, **kwargs):
|
|
148
|
-
super().__init__(OriginLayerNorm, *args, **kwargs)
|
|
149
|
-
self.pre_val, self.min_val, self.max_val, self.cnt = self.generate_params()
|
|
150
|
-
|
|
151
|
-
def __call__(self, input_x, gamma, beta):
|
|
152
|
-
if self.enable_check:
|
|
153
|
-
input_x = self.check_op(
|
|
154
|
-
input_x, self.pre_val, self.min_val, self.max_val, self.cnt, None)
|
|
155
|
-
self.cnt += 1
|
|
156
|
-
return self.op(input_x, gamma, beta)
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
def _silent_check():
|
|
160
|
-
if os.environ.get(NPU_ASD_ENABLE) == "1":
|
|
161
|
-
operations.LayerNorm = LayerNormASD
|
|
162
|
-
operations.RmsNorm = RmsNormASD
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
# Copyright 2022 Huawei Technologies Co., Ltd
|
|
2
|
-
#
|
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
# you may not use this file except in compliance with the License.
|
|
5
|
-
# You may obtain a copy of the License at
|
|
6
|
-
#
|
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
#
|
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
# See the License for the specific language governing permissions and
|
|
13
|
-
# limitations under the License.
|
|
14
|
-
# ============================================================================
|
|
15
|
-
"""
|
|
16
|
-
The MSAdvisor analyzer.
|
|
17
|
-
"""
|
|
18
|
-
|
|
19
|
-
import os
|
|
20
|
-
import stat
|
|
21
|
-
import subprocess
|
|
22
|
-
|
|
23
|
-
from mindspore import log as logger
|
|
24
|
-
from mindspore.profiler.common.validator.validate_path import validate_and_normalize_path
|
|
25
|
-
from mindspore.profiler.parser.msadvisor_parser import MsadvisorParser
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
class Msadvisor:
|
|
29
|
-
"""
|
|
30
|
-
The interface to call MSAdvisor(CANN) by command line.
|
|
31
|
-
"""
|
|
32
|
-
def __init__(self, job_id, rank_id, output_path, pretty=False):
|
|
33
|
-
self._job_id, self._device_id = job_id.split("/")
|
|
34
|
-
self._rank_id = str(rank_id)
|
|
35
|
-
self._output_path = output_path
|
|
36
|
-
self._pretty = pretty
|
|
37
|
-
|
|
38
|
-
def call_msadvisor(self):
|
|
39
|
-
"""
|
|
40
|
-
Call MSAdvisor by command line.
|
|
41
|
-
"""
|
|
42
|
-
output_path = os.path.join(self._output_path, "msadvisor")
|
|
43
|
-
output_path = os.path.join(output_path, "device_" + self._rank_id)
|
|
44
|
-
output_path = validate_and_normalize_path(output_path)
|
|
45
|
-
logger.info("MSAdvisor is running. Log and result files are saved in %s", output_path)
|
|
46
|
-
|
|
47
|
-
subprocess.run(["msadvisor", "-d", output_path, "-c", "all"],
|
|
48
|
-
capture_output=True, check=True)
|
|
49
|
-
logger.info("MSAdvisor process finished.")
|
|
50
|
-
|
|
51
|
-
# Modified recommendation file permission, only Owner can read and write.
|
|
52
|
-
recommendation_path = os.path.join(output_path, "recommendation")
|
|
53
|
-
recommendation_path = validate_and_normalize_path(recommendation_path)
|
|
54
|
-
os.chmod(recommendation_path, stat.S_IRWXU)
|
|
55
|
-
file_list = os.listdir(recommendation_path)
|
|
56
|
-
file_list.sort(key=lambda fn: os.path.getmtime(os.path.join(recommendation_path, fn)))
|
|
57
|
-
recommendation_path = os.path.join(recommendation_path, file_list[-1])
|
|
58
|
-
recommendation_path = validate_and_normalize_path(recommendation_path)
|
|
59
|
-
os.chmod(recommendation_path, stat.S_IRUSR | stat.S_IWUSR)
|
|
60
|
-
|
|
61
|
-
# Modified log file permission, only Owner can read and write.
|
|
62
|
-
log_path = os.path.join(output_path, "log")
|
|
63
|
-
log_path = validate_and_normalize_path(log_path)
|
|
64
|
-
os.chmod(log_path, stat.S_IRWXU)
|
|
65
|
-
file_list = os.listdir(log_path)
|
|
66
|
-
file_list.sort(key=lambda fn: os.path.getmtime(os.path.join(log_path, fn)))
|
|
67
|
-
log_path = os.path.join(log_path, file_list[-1])
|
|
68
|
-
log_path = validate_and_normalize_path(log_path)
|
|
69
|
-
os.chmod(log_path, stat.S_IRWXU)
|
|
70
|
-
file_list = os.listdir(log_path)
|
|
71
|
-
log_path = os.path.join(log_path, file_list[0])
|
|
72
|
-
log_path = validate_and_normalize_path(log_path)
|
|
73
|
-
os.chmod(log_path, stat.S_IRUSR | stat.S_IWUSR)
|
|
74
|
-
|
|
75
|
-
def analyse(self):
|
|
76
|
-
"""
|
|
77
|
-
Execute the MSAdvisor parser, generate timeline file and call MSAdvisor by command line.
|
|
78
|
-
"""
|
|
79
|
-
reformater = MsadvisorParser(self._job_id, self._device_id,
|
|
80
|
-
self._rank_id, self._output_path, pretty=self._pretty)
|
|
81
|
-
reformater.parse()
|
|
82
|
-
self.call_msadvisor()
|