mindstudio-probe 1.2.2__py3-none-any.whl → 8.1.0__py3-none-any.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.
- {mindstudio_probe-1.2.2.dist-info → mindstudio_probe-8.1.0.dist-info}/METADATA +4 -3
- {mindstudio_probe-1.2.2.dist-info → mindstudio_probe-8.1.0.dist-info}/RECORD +243 -191
- msprobe/README.md +57 -21
- msprobe/core/__init__.py +17 -0
- msprobe/core/common/const.py +224 -82
- msprobe/core/common/decorator.py +50 -0
- msprobe/core/common/exceptions.py +5 -3
- msprobe/core/common/file_utils.py +274 -40
- msprobe/core/common/framework_adapter.py +169 -0
- msprobe/core/common/global_lock.py +86 -0
- msprobe/core/common/runtime.py +25 -0
- msprobe/core/common/utils.py +148 -72
- msprobe/core/common_config.py +7 -0
- msprobe/core/compare/acc_compare.py +640 -462
- msprobe/core/compare/check.py +36 -107
- msprobe/core/compare/compare_cli.py +4 -0
- msprobe/core/compare/config.py +72 -0
- msprobe/core/compare/highlight.py +217 -215
- msprobe/core/compare/layer_mapping/layer_mapping.py +4 -1
- msprobe/core/compare/merge_result/merge_result.py +12 -6
- msprobe/core/compare/multiprocessing_compute.py +227 -107
- msprobe/core/compare/npy_compare.py +32 -16
- msprobe/core/compare/utils.py +218 -244
- msprobe/{mindspore/runtime.py → core/config_check/__init__.py} +2 -4
- msprobe/{pytorch/dump/kernel_dump/kernel_config.py → core/config_check/checkers/__init__.py} +8 -16
- msprobe/core/config_check/checkers/base_checker.py +60 -0
- msprobe/core/config_check/checkers/dataset_checker.py +138 -0
- msprobe/core/config_check/checkers/env_args_checker.py +96 -0
- msprobe/core/config_check/checkers/hyperparameter_checker.py +170 -0
- msprobe/core/config_check/checkers/pip_checker.py +90 -0
- msprobe/core/config_check/checkers/random_checker.py +367 -0
- msprobe/core/config_check/checkers/weights_checker.py +147 -0
- msprobe/core/config_check/ckpt_compare/ckpt_comparator.py +74 -0
- msprobe/core/config_check/ckpt_compare/megatron_loader.py +302 -0
- msprobe/core/config_check/ckpt_compare/metrics.py +83 -0
- msprobe/core/config_check/ckpt_compare/name_mapping.yaml +12 -0
- msprobe/core/config_check/config_check_cli.py +51 -0
- msprobe/core/config_check/config_checker.py +100 -0
- msprobe/{pytorch/parse.py → core/config_check/resource/dependency.yaml} +7 -4
- msprobe/core/config_check/resource/env.yaml +57 -0
- msprobe/core/config_check/resource/hyperparameter.yaml +21 -0
- msprobe/core/config_check/utils/hyperparameter_parser.py +115 -0
- msprobe/core/config_check/utils/utils.py +107 -0
- msprobe/core/data_dump/api_registry.py +239 -0
- msprobe/core/data_dump/data_collector.py +36 -9
- msprobe/core/data_dump/data_processor/base.py +74 -53
- msprobe/core/data_dump/data_processor/mindspore_processor.py +119 -78
- msprobe/core/data_dump/data_processor/pytorch_processor.py +134 -96
- msprobe/core/data_dump/json_writer.py +146 -57
- msprobe/core/debugger/precision_debugger.py +143 -0
- msprobe/core/grad_probe/constant.py +2 -1
- msprobe/core/grad_probe/grad_compare.py +2 -2
- msprobe/core/grad_probe/utils.py +1 -1
- msprobe/core/hook_manager.py +242 -0
- msprobe/core/monitor/anomaly_processor.py +384 -0
- msprobe/core/overflow_check/abnormal_scene.py +2 -0
- msprobe/core/service.py +356 -0
- msprobe/core/single_save/__init__.py +0 -0
- msprobe/core/single_save/single_comparator.py +243 -0
- msprobe/core/single_save/single_saver.py +157 -0
- msprobe/docs/01.installation.md +6 -5
- msprobe/docs/02.config_introduction.md +89 -30
- msprobe/docs/03.config_examples.md +1 -0
- msprobe/docs/04.kernel_dump_PyTorch.md +1 -1
- msprobe/docs/05.data_dump_PyTorch.md +184 -50
- msprobe/docs/06.data_dump_MindSpore.md +193 -28
- msprobe/docs/07.accuracy_checker_PyTorch.md +13 -3
- msprobe/docs/08.accuracy_checker_online_PyTorch.md +72 -10
- msprobe/docs/09.accuracy_checker_MindSpore.md +19 -7
- msprobe/docs/10.accuracy_compare_PyTorch.md +266 -102
- msprobe/docs/11.accuracy_compare_MindSpore.md +117 -43
- msprobe/docs/12.overflow_check_PyTorch.md +5 -3
- msprobe/docs/13.overflow_check_MindSpore.md +6 -4
- msprobe/docs/14.data_parse_PyTorch.md +4 -10
- msprobe/docs/17.grad_probe.md +2 -1
- msprobe/docs/18.online_dispatch.md +3 -3
- msprobe/docs/19.monitor.md +211 -103
- msprobe/docs/21.visualization_PyTorch.md +100 -28
- msprobe/docs/22.visualization_MindSpore.md +103 -31
- msprobe/docs/23.generate_operator_PyTorch.md +9 -9
- msprobe/docs/25.tool_function_introduction.md +23 -22
- msprobe/docs/26.data_dump_PyTorch_baseline.md +14 -3
- msprobe/docs/27.dump_json_instruction.md +278 -8
- msprobe/docs/28.debugger_save_instruction.md +111 -20
- msprobe/docs/28.kernel_dump_MindSpore.md +1 -1
- msprobe/docs/29.data_dump_MSAdapter.md +229 -0
- msprobe/docs/30.overflow_check_MSAdapter.md +31 -0
- msprobe/docs/31.config_check.md +95 -0
- msprobe/docs/32.ckpt_compare.md +69 -0
- msprobe/docs/33.generate_operator_MindSpore.md +190 -0
- msprobe/docs/34.RL_collect.md +92 -0
- msprobe/docs/35.nan_analyze.md +72 -0
- msprobe/docs/FAQ.md +3 -11
- msprobe/docs/data_dump_MindSpore/data_dump_MindSpore_baseline.md +12 -1
- msprobe/docs/data_dump_MindSpore/dynamic_graph_quick_start_example.md +3 -1
- msprobe/docs/img/compare_result.png +0 -0
- msprobe/docs/img/merge_result.png +0 -0
- msprobe/docs/img/save_compare_result_sample.png +0 -0
- msprobe/docs/img/visualization/proxy.png +0 -0
- msprobe/docs/img/visualization/vis_browser_1.png +0 -0
- msprobe/docs/img/visualization/vis_match_info.png +0 -0
- msprobe/docs/img/visualization/vis_precision_info.png +0 -0
- msprobe/docs/img/visualization/vis_search_info.png +0 -0
- msprobe/docs/img/visualization/vis_show_info.png +0 -0
- msprobe/docs/img/visualization/vis_showcase.png +0 -0
- msprobe/docs/img/visualization/vis_unmatch_info.png +0 -0
- msprobe/mindspore/__init__.py +3 -3
- msprobe/mindspore/api_accuracy_checker/api_accuracy_checker.py +151 -55
- msprobe/mindspore/api_accuracy_checker/api_runner.py +25 -11
- msprobe/mindspore/api_accuracy_checker/base_compare_algorithm.py +2 -1
- msprobe/mindspore/api_accuracy_checker/bench_functions/flash_attention_score.py +580 -0
- msprobe/mindspore/api_accuracy_checker/bench_functions/fusion_operator.py +41 -0
- msprobe/mindspore/api_accuracy_checker/cmd_parser.py +4 -0
- msprobe/mindspore/api_accuracy_checker/data_manager.py +4 -3
- msprobe/mindspore/api_accuracy_checker/generate_op_script/config_op.json +9 -0
- msprobe/mindspore/api_accuracy_checker/generate_op_script/op_generator.py +451 -0
- msprobe/mindspore/api_accuracy_checker/generate_op_script/operator_replication.template +2081 -0
- msprobe/mindspore/api_accuracy_checker/multi_api_accuracy_checker.py +11 -1
- msprobe/mindspore/api_accuracy_checker/torch_mindtorch_importer.py +2 -1
- msprobe/mindspore/cell_processor.py +204 -33
- msprobe/mindspore/code_mapping/graph_parser.py +4 -21
- msprobe/mindspore/common/const.py +73 -2
- msprobe/mindspore/common/utils.py +157 -29
- msprobe/mindspore/compare/common_dir_compare.py +382 -0
- msprobe/mindspore/compare/distributed_compare.py +2 -26
- msprobe/mindspore/compare/ms_compare.py +18 -398
- msprobe/mindspore/compare/ms_graph_compare.py +20 -10
- msprobe/mindspore/compare/utils.py +37 -0
- msprobe/mindspore/debugger/debugger_config.py +59 -7
- msprobe/mindspore/debugger/precision_debugger.py +83 -90
- msprobe/mindspore/dump/cell_dump_process.py +902 -0
- msprobe/mindspore/dump/cell_dump_with_insert_gradient.py +889 -0
- msprobe/mindspore/dump/dump_tool_factory.py +18 -8
- msprobe/mindspore/dump/graph_mode_cell_dump.py +139 -0
- msprobe/mindspore/dump/graph_tensor_dump.py +123 -0
- msprobe/mindspore/dump/hook_cell/api_register.py +176 -0
- msprobe/mindspore/dump/hook_cell/hook_cell.py +22 -12
- msprobe/mindspore/dump/hook_cell/ms_hook_manager.py +88 -0
- msprobe/mindspore/dump/hook_cell/primitive_hooks.py +8 -2
- msprobe/mindspore/dump/hook_cell/support_wrap_ops.yaml +42 -26
- msprobe/mindspore/dump/jit_dump.py +35 -27
- msprobe/mindspore/dump/kernel_kbyk_dump.py +6 -3
- msprobe/mindspore/dym_loader/hook_dynamic_loader.cpp +110 -0
- msprobe/mindspore/dym_loader/hook_dynamic_loader.h +15 -16
- msprobe/mindspore/free_benchmark/api_pynative_self_check.py +22 -12
- msprobe/mindspore/free_benchmark/common/utils.py +1 -1
- msprobe/mindspore/free_benchmark/perturbation/perturbation_factory.py +4 -2
- msprobe/mindspore/free_benchmark/self_check_tool_factory.py +6 -3
- msprobe/mindspore/grad_probe/global_context.py +9 -2
- msprobe/mindspore/grad_probe/grad_analyzer.py +2 -1
- msprobe/mindspore/grad_probe/grad_stat_csv.py +3 -2
- msprobe/mindspore/grad_probe/hook.py +2 -4
- msprobe/mindspore/mindspore_service.py +111 -0
- msprobe/mindspore/monitor/common_func.py +52 -0
- msprobe/mindspore/monitor/data_writers.py +237 -0
- msprobe/mindspore/monitor/distributed/wrap_distributed.py +1 -1
- msprobe/mindspore/monitor/features.py +13 -1
- msprobe/mindspore/monitor/module_hook.py +568 -444
- msprobe/mindspore/monitor/optimizer_collect.py +331 -0
- msprobe/mindspore/monitor/utils.py +71 -9
- msprobe/mindspore/ms_config.py +16 -15
- msprobe/mindspore/overflow_check/overflow_check_tool_factory.py +5 -3
- msprobe/mindspore/task_handler_factory.py +5 -2
- msprobe/msprobe.py +19 -0
- msprobe/nan_analyze/__init__.py +14 -0
- msprobe/nan_analyze/analyzer.py +255 -0
- msprobe/nan_analyze/graph.py +189 -0
- msprobe/nan_analyze/utils.py +211 -0
- msprobe/pytorch/api_accuracy_checker/common/config.py +2 -2
- msprobe/pytorch/api_accuracy_checker/compare/api_precision_compare.py +3 -6
- msprobe/pytorch/api_accuracy_checker/compare/compare.py +36 -34
- msprobe/pytorch/api_accuracy_checker/generate_op_script/op_generator.py +15 -13
- msprobe/pytorch/api_accuracy_checker/generate_op_script/operator_replication.template +206 -4
- msprobe/pytorch/api_accuracy_checker/run_ut/multi_run_ut.py +9 -9
- msprobe/pytorch/api_accuracy_checker/run_ut/run_overflow_check.py +6 -5
- msprobe/pytorch/api_accuracy_checker/run_ut/run_ut.py +31 -9
- msprobe/pytorch/api_accuracy_checker/run_ut/run_ut_utils.py +28 -20
- msprobe/pytorch/api_accuracy_checker/tensor_transport_layer/attl.py +3 -1
- msprobe/pytorch/api_accuracy_checker/tensor_transport_layer/client.py +29 -13
- msprobe/pytorch/api_accuracy_checker/tensor_transport_layer/device_dispatch.py +12 -2
- msprobe/pytorch/api_accuracy_checker/tensor_transport_layer/server.py +45 -31
- msprobe/pytorch/api_accuracy_checker/tensor_transport_layer/utils.py +154 -0
- msprobe/pytorch/attl_manager.py +65 -0
- msprobe/pytorch/bench_functions/moe_gating_top_k_softmax.py +6 -0
- msprobe/pytorch/bench_functions/npu_fusion_attention.py +27 -0
- msprobe/pytorch/common/utils.py +53 -19
- msprobe/pytorch/compare/distributed_compare.py +4 -36
- msprobe/pytorch/compare/pt_compare.py +13 -84
- msprobe/pytorch/compare/utils.py +47 -0
- msprobe/pytorch/debugger/debugger_config.py +34 -17
- msprobe/pytorch/debugger/precision_debugger.py +50 -96
- msprobe/pytorch/dump/module_dump/hook_wrapper.py +93 -0
- msprobe/pytorch/dump/module_dump/module_dump.py +15 -61
- msprobe/pytorch/dump/module_dump/module_processer.py +150 -114
- msprobe/pytorch/free_benchmark/common/utils.py +1 -1
- msprobe/pytorch/free_benchmark/compare/single_benchmark.py +1 -1
- msprobe/pytorch/free_benchmark/perturbed_layers/npu/add_noise.py +3 -3
- msprobe/pytorch/free_benchmark/perturbed_layers/npu/bit_noise.py +3 -3
- msprobe/pytorch/free_benchmark/perturbed_layers/npu/change_value.py +1 -1
- msprobe/pytorch/free_benchmark/perturbed_layers/npu/improve_precision.py +1 -1
- msprobe/pytorch/free_benchmark/result_handlers/check_handler.py +1 -1
- msprobe/pytorch/function_factory.py +1 -1
- msprobe/pytorch/grad_probe/grad_monitor.py +2 -2
- msprobe/pytorch/grad_probe/grad_stat_csv.py +3 -2
- msprobe/pytorch/hook_module/api_register.py +155 -0
- msprobe/pytorch/hook_module/hook_module.py +18 -22
- msprobe/pytorch/hook_module/jit_script_wrapper.py +33 -0
- msprobe/pytorch/hook_module/pt_hook_manager.py +68 -0
- msprobe/pytorch/hook_module/register_optimizer_hook.py +2 -1
- msprobe/pytorch/hook_module/support_wrap_ops.yaml +193 -75
- msprobe/pytorch/hook_module/utils.py +28 -2
- msprobe/pytorch/monitor/csv2tb.py +14 -4
- msprobe/pytorch/monitor/data_writers.py +259 -0
- msprobe/pytorch/monitor/distributed/wrap_distributed.py +8 -2
- msprobe/pytorch/monitor/module_hook.py +336 -241
- msprobe/pytorch/monitor/module_metric.py +17 -0
- msprobe/pytorch/monitor/optimizer_collect.py +244 -224
- msprobe/pytorch/monitor/utils.py +84 -4
- msprobe/pytorch/online_dispatch/compare.py +0 -2
- msprobe/pytorch/online_dispatch/dispatch.py +13 -2
- msprobe/pytorch/online_dispatch/dump_compare.py +8 -2
- msprobe/pytorch/online_dispatch/utils.py +3 -0
- msprobe/pytorch/parse_tool/lib/interactive_cli.py +1 -6
- msprobe/pytorch/parse_tool/lib/utils.py +5 -4
- msprobe/pytorch/pt_config.py +16 -11
- msprobe/pytorch/pytorch_service.py +70 -0
- msprobe/visualization/builder/graph_builder.py +69 -10
- msprobe/visualization/builder/msprobe_adapter.py +24 -12
- msprobe/visualization/compare/graph_comparator.py +63 -51
- msprobe/visualization/compare/mode_adapter.py +22 -20
- msprobe/visualization/graph/base_node.py +11 -4
- msprobe/visualization/graph/distributed_analyzer.py +1 -10
- msprobe/visualization/graph/graph.py +2 -13
- msprobe/visualization/graph/node_op.py +1 -2
- msprobe/visualization/graph_service.py +251 -104
- msprobe/visualization/utils.py +26 -44
- msprobe/mindspore/dump/hook_cell/api_registry.py +0 -207
- msprobe/mindspore/dump/hook_cell/wrap_api.py +0 -212
- msprobe/mindspore/dym_loader/hook_dynamic_loader.cc +0 -140
- msprobe/mindspore/monitor/anomaly_detect.py +0 -404
- msprobe/mindspore/monitor/module_spec_verifier.py +0 -94
- msprobe/mindspore/service.py +0 -543
- msprobe/pytorch/hook_module/api_registry.py +0 -166
- msprobe/pytorch/hook_module/wrap_distributed.py +0 -79
- msprobe/pytorch/hook_module/wrap_functional.py +0 -66
- msprobe/pytorch/hook_module/wrap_npu_custom.py +0 -85
- msprobe/pytorch/hook_module/wrap_tensor.py +0 -69
- msprobe/pytorch/hook_module/wrap_torch.py +0 -84
- msprobe/pytorch/hook_module/wrap_vf.py +0 -60
- msprobe/pytorch/monitor/anomaly_analyse.py +0 -201
- msprobe/pytorch/monitor/anomaly_detect.py +0 -410
- msprobe/pytorch/monitor/module_spec_verifier.py +0 -95
- msprobe/pytorch/monitor/unittest/test_monitor.py +0 -160
- msprobe/pytorch/service.py +0 -470
- {mindstudio_probe-1.2.2.dist-info → mindstudio_probe-8.1.0.dist-info}/LICENSE +0 -0
- {mindstudio_probe-1.2.2.dist-info → mindstudio_probe-8.1.0.dist-info}/WHEEL +0 -0
- {mindstudio_probe-1.2.2.dist-info → mindstudio_probe-8.1.0.dist-info}/entry_points.txt +0 -0
- {mindstudio_probe-1.2.2.dist-info → mindstudio_probe-8.1.0.dist-info}/top_level.txt +0 -0
- /msprobe/{mindspore → core}/compare/ms_to_pt_api.yaml +0 -0
- /msprobe/{mindspore/dump → core}/kernel_dump/kernel_config.py +0 -0
- /msprobe/{pytorch/monitor/unittest → core/monitor}/__init__.py +0 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# Copyright (c) 2025-2025, Huawei Technologies Co., Ltd.
|
|
2
|
+
# All rights reserved.
|
|
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
|
+
import os
|
|
16
|
+
|
|
17
|
+
from msprobe.core.common.const import Const, FileCheckConst, MsgConst
|
|
18
|
+
from msprobe.core.common.exceptions import MsprobeException
|
|
19
|
+
from msprobe.core.common.file_utils import FileChecker, load_json
|
|
20
|
+
from msprobe.core.common.utils import get_real_step_or_rank, check_init_step
|
|
21
|
+
from msprobe.core.common_config import CommonConfig
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class BasePrecisionDebugger:
|
|
25
|
+
_instance = None
|
|
26
|
+
tasks_not_need_debugger = [Const.GRAD_PROBE]
|
|
27
|
+
|
|
28
|
+
def __new__(cls, *args, **kwargs):
|
|
29
|
+
if cls._instance is None:
|
|
30
|
+
cls._instance = super(BasePrecisionDebugger, cls).__new__(cls)
|
|
31
|
+
cls._instance.config = None
|
|
32
|
+
cls._instance.enable_dataloader = False
|
|
33
|
+
cls._instance.initialized = False
|
|
34
|
+
cls.service = None
|
|
35
|
+
cls.first_start = False
|
|
36
|
+
return cls._instance
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self,
|
|
40
|
+
config_path=None,
|
|
41
|
+
task=None,
|
|
42
|
+
dump_path=None,
|
|
43
|
+
level=None,
|
|
44
|
+
step=None
|
|
45
|
+
):
|
|
46
|
+
if self.initialized:
|
|
47
|
+
return
|
|
48
|
+
self.initialized = True
|
|
49
|
+
self.check_input_params(config_path, task, dump_path, level)
|
|
50
|
+
self.common_config, self.task_config = self.parse_config_path(config_path, task)
|
|
51
|
+
self.task = self.common_config.task
|
|
52
|
+
if step is not None:
|
|
53
|
+
self.common_config.step = get_real_step_or_rank(step, Const.STEP)
|
|
54
|
+
|
|
55
|
+
@staticmethod
|
|
56
|
+
def get_task_config(task, json_config):
|
|
57
|
+
raise NotImplementedError("Subclass must implment get_task_config")
|
|
58
|
+
|
|
59
|
+
@staticmethod
|
|
60
|
+
def check_input_params(config_path, task, dump_path, level):
|
|
61
|
+
if not config_path:
|
|
62
|
+
config_path = os.path.join(os.path.dirname(__file__), "../../config.json")
|
|
63
|
+
if config_path is not None:
|
|
64
|
+
if not isinstance(config_path, str):
|
|
65
|
+
raise MsprobeException(
|
|
66
|
+
MsprobeException.INVALID_PARAM_ERROR, f"config_path must be a string")
|
|
67
|
+
file_checker = FileChecker(
|
|
68
|
+
file_path=config_path, path_type=FileCheckConst.FILE, file_type=FileCheckConst.JSON_SUFFIX)
|
|
69
|
+
file_checker.common_check()
|
|
70
|
+
|
|
71
|
+
if task is not None and task not in Const.TASK_LIST:
|
|
72
|
+
raise MsprobeException(
|
|
73
|
+
MsprobeException.INVALID_PARAM_ERROR, f"task must be one of {Const.TASK_LIST}")
|
|
74
|
+
|
|
75
|
+
if dump_path is not None:
|
|
76
|
+
if not isinstance(dump_path, str):
|
|
77
|
+
raise MsprobeException(
|
|
78
|
+
MsprobeException.INVALID_PARAM_ERROR, f"dump_path must be a string")
|
|
79
|
+
|
|
80
|
+
if level is not None and level not in Const.LEVEL_LIST:
|
|
81
|
+
raise MsprobeException(
|
|
82
|
+
MsprobeException.INVALID_PARAM_ERROR, f"level must be one of {Const.LEVEL_LIST}")
|
|
83
|
+
|
|
84
|
+
@classmethod
|
|
85
|
+
def get_instance(cls):
|
|
86
|
+
instance = cls._instance
|
|
87
|
+
if not instance:
|
|
88
|
+
raise Exception(MsgConst.NOT_CREATED_INSTANCE)
|
|
89
|
+
if instance.task in BasePrecisionDebugger.tasks_not_need_debugger:
|
|
90
|
+
instance = None
|
|
91
|
+
return instance
|
|
92
|
+
|
|
93
|
+
@classmethod
|
|
94
|
+
def forward_backward_dump_end(cls):
|
|
95
|
+
instance = cls._instance
|
|
96
|
+
instance.stop()
|
|
97
|
+
|
|
98
|
+
@classmethod
|
|
99
|
+
def set_init_step(cls, step):
|
|
100
|
+
instance = cls._instance
|
|
101
|
+
if not instance:
|
|
102
|
+
raise Exception(MsgConst.NOT_CREATED_INSTANCE)
|
|
103
|
+
check_init_step(step)
|
|
104
|
+
instance.service.init_step = step
|
|
105
|
+
instance.service.loop = 0
|
|
106
|
+
|
|
107
|
+
@classmethod
|
|
108
|
+
def register_custom_api(cls, module, api, api_prefix=None):
|
|
109
|
+
if not api_prefix:
|
|
110
|
+
api_prefix = getattr(module, "__name__", "Custom")
|
|
111
|
+
if not isinstance(api_prefix, str):
|
|
112
|
+
raise MsprobeException(
|
|
113
|
+
MsprobeException.INVALID_PARAM_ERROR, "api_prefix must be string")
|
|
114
|
+
if not hasattr(module, api):
|
|
115
|
+
raise MsprobeException(
|
|
116
|
+
MsprobeException.INVALID_PARAM_ERROR, f"module {str(module)} does not have {api}")
|
|
117
|
+
instance = cls._instance
|
|
118
|
+
if not instance:
|
|
119
|
+
raise Exception(MsgConst.NOT_CREATED_INSTANCE)
|
|
120
|
+
instance.service.register_custom_api(module, api, api_prefix)
|
|
121
|
+
|
|
122
|
+
@classmethod
|
|
123
|
+
def restore_custom_api(cls, module, api):
|
|
124
|
+
if not hasattr(module, api):
|
|
125
|
+
raise MsprobeException(
|
|
126
|
+
MsprobeException.INVALID_PARAM_ERROR, f"module {str(module)} does not have {api}")
|
|
127
|
+
instance = cls._instance
|
|
128
|
+
if not instance:
|
|
129
|
+
raise Exception(MsgConst.NOT_CREATED_INSTANCE)
|
|
130
|
+
instance.service.restore_custom_api(module, api)
|
|
131
|
+
|
|
132
|
+
def parse_config_path(self, json_file_path, task):
|
|
133
|
+
if not json_file_path:
|
|
134
|
+
json_file_path = os.path.join(os.path.dirname(__file__), "../../config.json")
|
|
135
|
+
json_config = load_json(json_file_path)
|
|
136
|
+
common_config = CommonConfig(json_config)
|
|
137
|
+
if task:
|
|
138
|
+
task_config = self.get_task_config(task, json_config)
|
|
139
|
+
else:
|
|
140
|
+
if not common_config.task:
|
|
141
|
+
common_config.task = Const.STATISTICS
|
|
142
|
+
task_config = self.get_task_config(common_config.task, json_config)
|
|
143
|
+
return common_config, task_config
|
|
@@ -31,6 +31,7 @@ class GradConst:
|
|
|
31
31
|
STEP = "step"
|
|
32
32
|
BOUNDS = "bounds"
|
|
33
33
|
OUTPUT_PATH = "output_path"
|
|
34
|
+
TIME_STAMP = "time_stamp"
|
|
34
35
|
|
|
35
36
|
# level const
|
|
36
37
|
LEVEL = "level"
|
|
@@ -51,7 +52,7 @@ class GradConst:
|
|
|
51
52
|
BOUNDS_MINIMUM = -2**63
|
|
52
53
|
BOUNDS_MAXIMUM = 2**63 - 1
|
|
53
54
|
|
|
54
|
-
# file
|
|
55
|
+
# file safety
|
|
55
56
|
DATA_DIR_AUTHORITY = 0o750
|
|
56
57
|
DATA_FILE_AUTHORITY = 0o640
|
|
57
58
|
DIRECTORY_LENGTH = 4096
|
|
@@ -112,7 +112,7 @@ class GradComparator:
|
|
|
112
112
|
result.append([key] + value)
|
|
113
113
|
result_csv_path = os.path.join(output_dir, "similarities.csv")
|
|
114
114
|
if os.path.exists(result_csv_path):
|
|
115
|
-
logger.warning(f"{result_csv_path} will be
|
|
115
|
+
logger.warning(f"{result_csv_path} will be deleted")
|
|
116
116
|
remove_path(result_csv_path)
|
|
117
117
|
write_csv(result, result_csv_path)
|
|
118
118
|
|
|
@@ -121,7 +121,7 @@ class GradComparator:
|
|
|
121
121
|
similarities = {}
|
|
122
122
|
logger.info(f"{len(steps)} steps will be compared")
|
|
123
123
|
grad_weight_order = cls._get_grad_weight_order(path1, path2)
|
|
124
|
-
for step in tqdm(steps, desc="
|
|
124
|
+
for step in tqdm(steps, desc="calculate similarities (by step)"):
|
|
125
125
|
grad_files = cls._get_matched_grad_files(path1, path2, step)
|
|
126
126
|
same_count_summary = 0
|
|
127
127
|
total_count_summary = 0
|
msprobe/core/grad_probe/utils.py
CHANGED
|
@@ -82,7 +82,7 @@ class ListCache(list):
|
|
|
82
82
|
if len(self) == 0:
|
|
83
83
|
return
|
|
84
84
|
if not self._output_file:
|
|
85
|
-
logger.warning("dumpfile path is not
|
|
85
|
+
logger.warning("dumpfile path is not set.")
|
|
86
86
|
write_csv(self, self._output_file)
|
|
87
87
|
logger.info(f"write {len(self)} items to {self._output_file}.")
|
|
88
88
|
self.clear()
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# Copyright (c) 2025, Huawei Technologies Co., Ltd.
|
|
2
|
+
# All rights reserved.
|
|
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
|
+
from abc import ABC, abstractmethod
|
|
18
|
+
import os
|
|
19
|
+
|
|
20
|
+
from msprobe.core.common.runtime import Runtime
|
|
21
|
+
from msprobe.core.common.utils import Const
|
|
22
|
+
from msprobe.core.data_dump.data_processor.base import (ModuleBackwardInputsOutputs, ModuleForwardInputsOutputs)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class HookSet:
|
|
26
|
+
def __init__(self, forward_hook=None, forward_pre_hook=None, backward_hook=None, backward_pre_hook=None):
|
|
27
|
+
self.forward_hook = forward_hook
|
|
28
|
+
self.forward_pre_hook = forward_pre_hook
|
|
29
|
+
self.backward_hook = backward_hook
|
|
30
|
+
self.backward_pre_hook = backward_pre_hook
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class BaseHookManager(ABC):
|
|
34
|
+
inner_switch = False
|
|
35
|
+
hook_handle_dict = {}
|
|
36
|
+
params_grad_info = {}
|
|
37
|
+
|
|
38
|
+
def __init__(self, data_collector, config, attl_manager=None):
|
|
39
|
+
self.data_collector = data_collector
|
|
40
|
+
self.config = config
|
|
41
|
+
self.attl_manager = attl_manager
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def _pid(self):
|
|
45
|
+
return os.getpid()
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
@abstractmethod
|
|
49
|
+
def _is_recompute(self):
|
|
50
|
+
pass
|
|
51
|
+
|
|
52
|
+
@staticmethod
|
|
53
|
+
@abstractmethod
|
|
54
|
+
def _no_grad_context():
|
|
55
|
+
pass
|
|
56
|
+
|
|
57
|
+
@staticmethod
|
|
58
|
+
@abstractmethod
|
|
59
|
+
def _add_count(name):
|
|
60
|
+
pass
|
|
61
|
+
|
|
62
|
+
@staticmethod
|
|
63
|
+
@abstractmethod
|
|
64
|
+
def _process_kwargs_and_output(module, hook_type, kwargs_or_output, output_or_kwargs):
|
|
65
|
+
pass
|
|
66
|
+
|
|
67
|
+
@staticmethod
|
|
68
|
+
def _clear_input_kwargs(module):
|
|
69
|
+
if hasattr(module, 'msprobe_input_kwargs'):
|
|
70
|
+
del module.msprobe_input_kwargs
|
|
71
|
+
|
|
72
|
+
@abstractmethod
|
|
73
|
+
def build_hook(self):
|
|
74
|
+
pass
|
|
75
|
+
|
|
76
|
+
@abstractmethod
|
|
77
|
+
def _get_params_dict(self, module):
|
|
78
|
+
pass
|
|
79
|
+
|
|
80
|
+
@abstractmethod
|
|
81
|
+
def _need_exchange(self, module):
|
|
82
|
+
pass
|
|
83
|
+
|
|
84
|
+
def _register_param_hook(self, name, module, params_dict):
|
|
85
|
+
ori_name = name.rsplit(Const.SEP, 2)[0]
|
|
86
|
+
grad_name = ori_name + Const.SEP + Const.PARAMS_GRAD
|
|
87
|
+
# 首次执行前向hook时,添加params_grad_name属性,并注册参数hook
|
|
88
|
+
setattr(module, 'params_grad_name', grad_name)
|
|
89
|
+
# data_mode为forward时,不注册参数hook
|
|
90
|
+
if not (Const.FORWARD in self.config.data_mode and Const.BACKWARD not in self.config.data_mode):
|
|
91
|
+
for param_name, param in params_dict.items():
|
|
92
|
+
if param.requires_grad:
|
|
93
|
+
name = ori_name + Const.SEP + param_name
|
|
94
|
+
old_handle = BaseHookManager.hook_handle_dict.get(name)
|
|
95
|
+
if old_handle and hasattr(old_handle, "remove"):
|
|
96
|
+
old_handle.remove()
|
|
97
|
+
handle = param.register_hook(self._build_grad_hook(module, ori_name, param_name))
|
|
98
|
+
BaseHookManager.hook_handle_dict[name] = handle
|
|
99
|
+
|
|
100
|
+
def _init_params_grad_info(self, module, params_dict):
|
|
101
|
+
'''
|
|
102
|
+
初始化参数梯度信息, 在前向hook结束后, 将参数梯度信息写入cache_data中用于占位
|
|
103
|
+
'''
|
|
104
|
+
if not params_dict:
|
|
105
|
+
return
|
|
106
|
+
if not (Const.FORWARD in self.config.data_mode and Const.BACKWARD not in self.config.data_mode):
|
|
107
|
+
grad_name = module.params_grad_name if hasattr(module, 'params_grad_name') else None
|
|
108
|
+
# 判断是否已经在cache_data中进行了占位, 若没有则先写入cache_data中
|
|
109
|
+
if not BaseHookManager.params_grad_info.get(grad_name):
|
|
110
|
+
data_info = {grad_name: {key: [None] for key, value in params_dict.items() if value.requires_grad}}
|
|
111
|
+
# 当模块中的参数有requires_grad属性为True时,才会进行梯度计算,此时才需要占位
|
|
112
|
+
if data_info.get(grad_name):
|
|
113
|
+
# 将grad_name的data_info先写入cache_data中, 梯度计算后再更新
|
|
114
|
+
self.data_collector.handle_data(grad_name, data_info,
|
|
115
|
+
flush=self.data_collector.data_processor.is_terminated)
|
|
116
|
+
# 记录当前模块的参数梯度信息已占位
|
|
117
|
+
BaseHookManager.params_grad_info[grad_name] = True
|
|
118
|
+
|
|
119
|
+
def _should_execute_hook(self, hook_type, module, is_forward):
|
|
120
|
+
is_module_hook = hook_type == Const.MODULE
|
|
121
|
+
if is_module_hook and not Runtime.is_running:
|
|
122
|
+
return False
|
|
123
|
+
elif not is_module_hook and is_forward and not Runtime.is_running:
|
|
124
|
+
return False
|
|
125
|
+
elif not is_module_hook and not is_forward and not module.forward_data_collected:
|
|
126
|
+
return False
|
|
127
|
+
if BaseHookManager.inner_switch:
|
|
128
|
+
return False
|
|
129
|
+
if not self.data_collector or self.data_collector.data_processor.is_terminated:
|
|
130
|
+
return False
|
|
131
|
+
return True
|
|
132
|
+
|
|
133
|
+
def _build_grad_hook(self, module, ori_name, param_name):
|
|
134
|
+
def hook_fn(grad):
|
|
135
|
+
if not self._should_execute_hook(Const.MODULE, module, False):
|
|
136
|
+
return
|
|
137
|
+
BaseHookManager.inner_switch = True
|
|
138
|
+
self.data_collector.params_data_collect(ori_name, param_name, self._pid, grad)
|
|
139
|
+
BaseHookManager.inner_switch = False
|
|
140
|
+
return
|
|
141
|
+
return hook_fn
|
|
142
|
+
|
|
143
|
+
def _build_forward_pre_hook(self, hook_type, full_name, api_name):
|
|
144
|
+
def forward_pre_hook(module, args, kwargs=None):
|
|
145
|
+
if hook_type == Const.MODULE:
|
|
146
|
+
return
|
|
147
|
+
if not self._should_execute_hook(hook_type, module, True):
|
|
148
|
+
return
|
|
149
|
+
if kwargs is None:
|
|
150
|
+
kwargs = module.msprobe_input_kwargs if hasattr(module, 'msprobe_input_kwargs') else {}
|
|
151
|
+
with self._no_grad_context():
|
|
152
|
+
BaseHookManager.inner_switch = False
|
|
153
|
+
module.forward_data_collected = True
|
|
154
|
+
self._add_count(api_name)
|
|
155
|
+
module_input_output = ModuleForwardInputsOutputs(args=args, kwargs=kwargs, output=None)
|
|
156
|
+
self.data_collector.update_api_or_module_name(full_name)
|
|
157
|
+
if getattr(self.config, "online_run_ut", False):
|
|
158
|
+
BaseHookManager.inner_switch = False
|
|
159
|
+
return
|
|
160
|
+
self.data_collector.forward_input_data_collect(
|
|
161
|
+
full_name,
|
|
162
|
+
module,
|
|
163
|
+
self._pid,
|
|
164
|
+
module_input_output,
|
|
165
|
+
self._is_recompute
|
|
166
|
+
)
|
|
167
|
+
BaseHookManager.inner_switch = False
|
|
168
|
+
return forward_pre_hook
|
|
169
|
+
|
|
170
|
+
def _build_forward_hook(self, hook_type, full_name):
|
|
171
|
+
def forward_hook(module, args, kwargs_or_output, output_or_kwargs=None):
|
|
172
|
+
if not self._should_execute_hook(hook_type, module, True):
|
|
173
|
+
self._clear_input_kwargs(module)
|
|
174
|
+
return None
|
|
175
|
+
kwargs, output = self._process_kwargs_and_output(module, hook_type, kwargs_or_output, output_or_kwargs)
|
|
176
|
+
BaseHookManager.inner_switch = True
|
|
177
|
+
self.data_collector.update_api_or_module_name(full_name)
|
|
178
|
+
module_input_output = ModuleForwardInputsOutputs(args=args, kwargs=kwargs, output=output)
|
|
179
|
+
with self._no_grad_context():
|
|
180
|
+
if getattr(self.config, "online_run_ut", False):
|
|
181
|
+
if self.data_collector.scope and not self.data_collector.scope.check(full_name):
|
|
182
|
+
return None
|
|
183
|
+
if self.attl_manager:
|
|
184
|
+
self.attl_manager.attl_send(full_name, args, kwargs, output)
|
|
185
|
+
BaseHookManager.inner_switch = False
|
|
186
|
+
return None
|
|
187
|
+
if hook_type == Const.MODULE:
|
|
188
|
+
params_dict = self._get_params_dict(module)
|
|
189
|
+
setattr(module_input_output, Const.PARAMS, params_dict)
|
|
190
|
+
if params_dict:
|
|
191
|
+
self._register_param_hook(full_name, module, params_dict)
|
|
192
|
+
self.data_collector.update_api_or_module_name(full_name)
|
|
193
|
+
self.data_collector.forward_data_collect(
|
|
194
|
+
full_name,
|
|
195
|
+
module,
|
|
196
|
+
self._pid,
|
|
197
|
+
module_input_output,
|
|
198
|
+
self._is_recompute
|
|
199
|
+
)
|
|
200
|
+
self._init_params_grad_info(module, params_dict)
|
|
201
|
+
else:
|
|
202
|
+
self.data_collector.forward_output_data_collect(
|
|
203
|
+
full_name,
|
|
204
|
+
module,
|
|
205
|
+
self._pid,
|
|
206
|
+
module_input_output,
|
|
207
|
+
self._is_recompute
|
|
208
|
+
)
|
|
209
|
+
self._clear_input_kwargs(module)
|
|
210
|
+
|
|
211
|
+
if self.data_collector.if_return_forward_new_output():
|
|
212
|
+
forward_new_output = self.data_collector.get_forward_new_output()
|
|
213
|
+
BaseHookManager.inner_switch = False
|
|
214
|
+
return forward_new_output
|
|
215
|
+
|
|
216
|
+
BaseHookManager.inner_switch = False
|
|
217
|
+
return output
|
|
218
|
+
return forward_hook
|
|
219
|
+
|
|
220
|
+
def _build_backward_hook(self, hook_type, full_name):
|
|
221
|
+
def backward_hook(module, grad_input, grad_output):
|
|
222
|
+
if not self._should_execute_hook(hook_type, module, False):
|
|
223
|
+
return
|
|
224
|
+
BaseHookManager.inner_switch = True
|
|
225
|
+
self.data_collector.update_api_or_module_name(full_name)
|
|
226
|
+
if getattr(self.config, "online_run_ut", False):
|
|
227
|
+
BaseHookManager.inner_switch = False
|
|
228
|
+
return
|
|
229
|
+
need_exchange = self._need_exchange(module) if hook_type == Const.MODULE else True
|
|
230
|
+
if need_exchange:
|
|
231
|
+
module_input_output = ModuleBackwardInputsOutputs(grad_input=grad_output, grad_output=grad_input)
|
|
232
|
+
else:
|
|
233
|
+
module_input_output = ModuleBackwardInputsOutputs(grad_input=grad_input, grad_output=grad_output)
|
|
234
|
+
self.data_collector.backward_data_collect(
|
|
235
|
+
full_name,
|
|
236
|
+
module,
|
|
237
|
+
self._pid,
|
|
238
|
+
module_input_output,
|
|
239
|
+
self._is_recompute
|
|
240
|
+
)
|
|
241
|
+
BaseHookManager.inner_switch = False
|
|
242
|
+
return backward_hook
|