mindstudio-probe 1.2.1__py3-none-any.whl → 1.3.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.
Files changed (177) hide show
  1. {mindstudio_probe-1.2.1.dist-info → mindstudio_probe-1.3.0.dist-info}/METADATA +3 -3
  2. {mindstudio_probe-1.2.1.dist-info → mindstudio_probe-1.3.0.dist-info}/RECORD +168 -150
  3. msprobe/README.md +27 -22
  4. msprobe/core/common/const.py +129 -60
  5. msprobe/core/common/decorator.py +50 -0
  6. msprobe/core/common/exceptions.py +3 -1
  7. msprobe/core/common/file_utils.py +25 -2
  8. msprobe/core/common/inplace_ops.yaml +1 -0
  9. msprobe/core/common/utils.py +43 -33
  10. msprobe/core/compare/acc_compare.py +43 -74
  11. msprobe/core/compare/check.py +2 -6
  12. msprobe/core/compare/highlight.py +2 -0
  13. msprobe/core/compare/layer_mapping/data_scope_parser.py +1 -1
  14. msprobe/core/compare/layer_mapping/layer_mapping.py +2 -1
  15. msprobe/core/compare/merge_result/merge_result.py +16 -9
  16. msprobe/core/compare/merge_result/utils.py +81 -0
  17. msprobe/core/compare/multiprocessing_compute.py +19 -12
  18. msprobe/core/compare/npy_compare.py +30 -12
  19. msprobe/core/compare/utils.py +30 -10
  20. msprobe/core/data_dump/api_registry.py +176 -0
  21. msprobe/core/data_dump/data_collector.py +58 -13
  22. msprobe/core/data_dump/data_processor/base.py +94 -10
  23. msprobe/core/data_dump/data_processor/factory.py +3 -0
  24. msprobe/core/data_dump/data_processor/mindspore_processor.py +33 -33
  25. msprobe/core/data_dump/data_processor/pytorch_processor.py +99 -18
  26. msprobe/core/data_dump/json_writer.py +61 -40
  27. msprobe/core/grad_probe/constant.py +1 -0
  28. msprobe/core/grad_probe/grad_compare.py +1 -1
  29. msprobe/core/overflow_check/abnormal_scene.py +2 -0
  30. msprobe/docs/01.installation.md +27 -1
  31. msprobe/docs/02.config_introduction.md +27 -23
  32. msprobe/docs/03.config_examples.md +24 -0
  33. msprobe/docs/05.data_dump_PyTorch.md +103 -16
  34. msprobe/docs/06.data_dump_MindSpore.md +76 -32
  35. msprobe/docs/07.accuracy_checker_PyTorch.md +11 -1
  36. msprobe/docs/08.accuracy_checker_online_PyTorch.md +3 -1
  37. msprobe/docs/09.accuracy_checker_MindSpore.md +5 -3
  38. msprobe/docs/10.accuracy_compare_PyTorch.md +59 -33
  39. msprobe/docs/11.accuracy_compare_MindSpore.md +40 -16
  40. msprobe/docs/12.overflow_check_PyTorch.md +3 -1
  41. msprobe/docs/13.overflow_check_MindSpore.md +4 -2
  42. msprobe/docs/14.data_parse_PyTorch.md +1 -7
  43. msprobe/docs/18.online_dispatch.md +1 -1
  44. msprobe/docs/19.monitor.md +332 -273
  45. msprobe/docs/21.visualization_PyTorch.md +42 -13
  46. msprobe/docs/22.visualization_MindSpore.md +43 -13
  47. msprobe/docs/23.generate_operator_PyTorch.md +9 -9
  48. msprobe/docs/27.dump_json_instruction.md +301 -27
  49. msprobe/docs/28.debugger_save_instruction.md +94 -0
  50. msprobe/docs/28.kernel_dump_MindSpore.md +69 -0
  51. msprobe/docs/29.data_dump_MSAdapter.md +229 -0
  52. msprobe/docs/30.overflow_check_MSAdapter.md +31 -0
  53. msprobe/docs/FAQ.md +3 -11
  54. msprobe/docs/img/compare_result.png +0 -0
  55. msprobe/docs/img/merge_result.png +0 -0
  56. msprobe/docs/img/monitor/step_count_per_record.png +0 -0
  57. msprobe/docs/img/visualization/vis_browser_1.png +0 -0
  58. msprobe/docs/img/visualization/vis_match_info.png +0 -0
  59. msprobe/docs/img/visualization/vis_precision_info.png +0 -0
  60. msprobe/docs/img/visualization/vis_search_info.png +0 -0
  61. msprobe/docs/img/visualization/vis_show_info.png +0 -0
  62. msprobe/docs/img/visualization/vis_showcase.png +0 -0
  63. msprobe/docs/img/visualization/vis_unmatch_info.png +0 -0
  64. msprobe/mindspore/__init__.py +4 -2
  65. msprobe/mindspore/api_accuracy_checker/api_accuracy_checker.py +32 -7
  66. msprobe/mindspore/api_accuracy_checker/api_runner.py +70 -22
  67. msprobe/mindspore/api_accuracy_checker/base_compare_algorithm.py +2 -1
  68. msprobe/mindspore/api_accuracy_checker/bench_functions/flash_attention_score.py +602 -0
  69. msprobe/mindspore/api_accuracy_checker/bench_functions/fusion_operator.py +41 -0
  70. msprobe/mindspore/api_accuracy_checker/compute_element.py +47 -1
  71. msprobe/mindspore/api_accuracy_checker/data_manager.py +2 -1
  72. msprobe/mindspore/api_accuracy_checker/multi_api_accuracy_checker.py +2 -1
  73. msprobe/mindspore/api_accuracy_checker/torch_mindtorch_importer.py +130 -0
  74. msprobe/mindspore/api_accuracy_checker/type_mapping.py +24 -1
  75. msprobe/mindspore/api_accuracy_checker/utils.py +6 -1
  76. msprobe/mindspore/common/const.py +61 -0
  77. msprobe/mindspore/common/utils.py +48 -18
  78. msprobe/mindspore/compare/ms_compare.py +27 -19
  79. msprobe/mindspore/compare/ms_graph_compare.py +6 -5
  80. msprobe/mindspore/debugger/debugger_config.py +31 -6
  81. msprobe/mindspore/debugger/precision_debugger.py +45 -14
  82. msprobe/mindspore/dump/dump_tool_factory.py +5 -3
  83. msprobe/mindspore/dump/hook_cell/api_register.py +142 -0
  84. msprobe/mindspore/dump/hook_cell/hook_cell.py +9 -10
  85. msprobe/mindspore/dump/hook_cell/support_wrap_ops.yaml +24 -26
  86. msprobe/mindspore/dump/jit_dump.py +21 -15
  87. msprobe/mindspore/dym_loader/hook_dynamic_loader.cc +22 -56
  88. msprobe/mindspore/dym_loader/hook_dynamic_loader.h +0 -1
  89. msprobe/mindspore/free_benchmark/api_pynative_self_check.py +10 -6
  90. msprobe/mindspore/free_benchmark/perturbation/perturbation_factory.py +4 -2
  91. msprobe/mindspore/free_benchmark/self_check_tool_factory.py +6 -3
  92. msprobe/mindspore/grad_probe/global_context.py +2 -0
  93. msprobe/mindspore/grad_probe/grad_analyzer.py +2 -1
  94. msprobe/mindspore/grad_probe/hook.py +2 -4
  95. msprobe/mindspore/monitor/anomaly_detect.py +404 -0
  96. msprobe/mindspore/monitor/distributed/__init__.py +0 -0
  97. msprobe/mindspore/monitor/distributed/distributed_ops.yaml +15 -0
  98. msprobe/mindspore/monitor/distributed/stack_blacklist.yaml +5 -0
  99. msprobe/mindspore/monitor/distributed/wrap_distributed.py +300 -0
  100. msprobe/mindspore/monitor/features.py +63 -0
  101. msprobe/mindspore/monitor/module_hook.py +873 -0
  102. msprobe/mindspore/monitor/module_spec_verifier.py +94 -0
  103. msprobe/mindspore/monitor/utils.py +309 -0
  104. msprobe/mindspore/ms_config.py +8 -2
  105. msprobe/mindspore/overflow_check/overflow_check_tool_factory.py +5 -3
  106. msprobe/mindspore/service.py +114 -34
  107. msprobe/pytorch/__init__.py +0 -1
  108. msprobe/pytorch/api_accuracy_checker/compare/api_precision_compare.py +3 -6
  109. msprobe/pytorch/api_accuracy_checker/generate_op_script/op_generator.py +12 -7
  110. msprobe/pytorch/api_accuracy_checker/generate_op_script/operator_replication.template +2 -2
  111. msprobe/pytorch/api_accuracy_checker/run_ut/multi_run_ut.py +4 -5
  112. msprobe/pytorch/api_accuracy_checker/run_ut/run_overflow_check.py +5 -5
  113. msprobe/pytorch/api_accuracy_checker/run_ut/run_ut.py +25 -6
  114. msprobe/pytorch/api_accuracy_checker/run_ut/run_ut_utils.py +28 -19
  115. msprobe/pytorch/api_accuracy_checker/tensor_transport_layer/attl.py +3 -1
  116. msprobe/pytorch/bench_functions/apply_adam.py +215 -0
  117. msprobe/pytorch/bench_functions/group_norm_silu.py +27 -0
  118. msprobe/pytorch/{parse.py → bench_functions/mish.py} +6 -4
  119. msprobe/pytorch/bench_functions/moe_gating_top_k_softmax.py +50 -0
  120. msprobe/pytorch/bench_functions/sort_v2.py +21 -0
  121. msprobe/pytorch/common/utils.py +97 -4
  122. msprobe/pytorch/debugger/debugger_config.py +19 -9
  123. msprobe/pytorch/debugger/precision_debugger.py +24 -1
  124. msprobe/pytorch/dump/module_dump/module_dump.py +4 -3
  125. msprobe/pytorch/dump/module_dump/module_processer.py +21 -35
  126. msprobe/pytorch/free_benchmark/common/utils.py +1 -1
  127. msprobe/pytorch/free_benchmark/compare/single_benchmark.py +1 -1
  128. msprobe/pytorch/free_benchmark/perturbed_layers/npu/add_noise.py +3 -3
  129. msprobe/pytorch/free_benchmark/perturbed_layers/npu/bit_noise.py +3 -3
  130. msprobe/pytorch/free_benchmark/perturbed_layers/npu/change_value.py +1 -1
  131. msprobe/pytorch/free_benchmark/perturbed_layers/npu/improve_precision.py +1 -1
  132. msprobe/pytorch/free_benchmark/result_handlers/check_handler.py +1 -1
  133. msprobe/pytorch/function_factory.py +8 -2
  134. msprobe/pytorch/grad_probe/grad_monitor.py +2 -2
  135. msprobe/pytorch/hook_module/api_register.py +131 -0
  136. msprobe/pytorch/hook_module/hook_module.py +19 -14
  137. msprobe/pytorch/hook_module/register_optimizer_hook.py +2 -1
  138. msprobe/pytorch/hook_module/support_wrap_ops.yaml +173 -75
  139. msprobe/pytorch/monitor/anomaly_detect.py +14 -29
  140. msprobe/pytorch/monitor/csv2tb.py +18 -14
  141. msprobe/pytorch/monitor/distributed/wrap_distributed.py +8 -2
  142. msprobe/pytorch/monitor/module_hook.py +238 -193
  143. msprobe/pytorch/monitor/module_metric.py +9 -6
  144. msprobe/pytorch/monitor/optimizer_collect.py +100 -67
  145. msprobe/pytorch/monitor/unittest/test_monitor.py +1 -1
  146. msprobe/pytorch/monitor/utils.py +76 -44
  147. msprobe/pytorch/online_dispatch/compare.py +0 -2
  148. msprobe/pytorch/online_dispatch/dispatch.py +9 -0
  149. msprobe/pytorch/online_dispatch/dump_compare.py +3 -0
  150. msprobe/pytorch/online_dispatch/utils.py +3 -0
  151. msprobe/pytorch/parse_tool/lib/interactive_cli.py +1 -6
  152. msprobe/pytorch/parse_tool/lib/utils.py +2 -1
  153. msprobe/pytorch/pt_config.py +30 -29
  154. msprobe/pytorch/service.py +114 -32
  155. msprobe/visualization/builder/graph_builder.py +75 -10
  156. msprobe/visualization/builder/msprobe_adapter.py +7 -6
  157. msprobe/visualization/compare/graph_comparator.py +42 -38
  158. msprobe/visualization/compare/mode_adapter.py +0 -19
  159. msprobe/visualization/graph/base_node.py +11 -3
  160. msprobe/visualization/graph/distributed_analyzer.py +71 -3
  161. msprobe/visualization/graph/graph.py +0 -11
  162. msprobe/visualization/graph/node_op.py +4 -3
  163. msprobe/visualization/graph_service.py +4 -5
  164. msprobe/visualization/utils.py +12 -35
  165. msprobe/mindspore/dump/hook_cell/api_registry.py +0 -205
  166. msprobe/mindspore/dump/hook_cell/wrap_api.py +0 -212
  167. msprobe/pytorch/hook_module/api_registry.py +0 -166
  168. msprobe/pytorch/hook_module/wrap_distributed.py +0 -75
  169. msprobe/pytorch/hook_module/wrap_functional.py +0 -66
  170. msprobe/pytorch/hook_module/wrap_npu_custom.py +0 -85
  171. msprobe/pytorch/hook_module/wrap_tensor.py +0 -69
  172. msprobe/pytorch/hook_module/wrap_torch.py +0 -84
  173. msprobe/pytorch/hook_module/wrap_vf.py +0 -60
  174. {mindstudio_probe-1.2.1.dist-info → mindstudio_probe-1.3.0.dist-info}/LICENSE +0 -0
  175. {mindstudio_probe-1.2.1.dist-info → mindstudio_probe-1.3.0.dist-info}/WHEEL +0 -0
  176. {mindstudio_probe-1.2.1.dist-info → mindstudio_probe-1.3.0.dist-info}/entry_points.txt +0 -0
  177. {mindstudio_probe-1.2.1.dist-info → mindstudio_probe-1.3.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,94 @@
1
+ # 单点保存工具 README
2
+
3
+ ## 简介
4
+ L0, L1, mix dump存在盲区,网络中的非api/module的输入输出不会被批量dump下来。单点保存提供类似np.save和print的功能和使用体验,可以保存指定的变量。同时针对大模型场景进行了增强,具备以下特性:
5
+ - 可保存变量的反向梯度结果。
6
+ - 能直接保存嵌套结构数据(如 list、dict),无需手动遍历。
7
+ - 自动分 rank 保存。
8
+ - 多次调用时会自动计数。
9
+ - 可配置保存统计值或者张量。
10
+
11
+ ## 支持场景
12
+ 仅支持 PyTorch 与 MindSpore 的动态图场景。
13
+
14
+ ## 使能方式
15
+
16
+ ### 配置文件说明
17
+
18
+ 通用配置:
19
+
20
+ | 参数 | 解释 | 是否必选 |
21
+ | -------- |-------------------------------------------| -------- |
22
+ | task | dump 的任务类型,str 类型。 单点保存场景仅支持传入"statistics", "tensor"。 | 是 |
23
+ | level | dump 级别,str 类型,根据不同级别采集不同数据。单点保存场景传入"debug"。 | 是 |
24
+ | dump_path | 设置 dump 数据目录路径,str 类型。细节详见[通用配置说明](./02.config_introduction.md#11-通用配置) | 是 |
25
+ | rank | 指定对某张卡上的数据进行采集,list[Union[int, str]] 类型。细节详见[通用配置说明](./02.config_introduction.md#11-通用配置) | 否 |
26
+
27
+ "statistics" 任务子配置项:
28
+ | 参数 | 解释 | 是否必选 |
29
+ | -------- |-------------------------------------------| -------- |
30
+ | summary_mode | 控制 dump 文件输出的模式,str 类型。支持传入"statistics", "md5"。 细节详见[statistics任务子配置项说明](./02.config_introduction.md#12-task-配置为-statistics) | 否 |
31
+
32
+ "tensor" 任务无子配置项。
33
+
34
+ ### 接口调用说明
35
+
36
+ 调用PrecisionDebugger.save,传入需要保存的变量,指定变量名称以及是否需要保存反向数据。接口入参说明详见[pytorch单点保存接口](./05.data_dump_PyTorch.md#19-save),[mindspore单点保存接口](./06.data_dump_MindSpore.md#615-save)
37
+
38
+ ### 实例(以pytorch场景为例)
39
+
40
+ 配置文件
41
+ ```json
42
+ {
43
+ "task": "statistics",
44
+ "dump_path": "./dump_path",
45
+ "rank": [],
46
+ "level": "debug",
47
+ "statistics": {
48
+ "summary_mode": "statistics"
49
+ }
50
+ }
51
+ ```
52
+
53
+ 初始化
54
+ ```python
55
+ # 训练启动py脚本
56
+ from mindspore.pytorch import PrecisionDebugger
57
+ debugger = PrecisionDebugger("./config.json")
58
+ for data, label in data_loader:
59
+ # 执行模型训练
60
+ train(data, label)
61
+
62
+ ```
63
+
64
+ 初始化(无配置文件)
65
+ ```python
66
+ # 训练启动py脚本
67
+ from mindspore.pytorch import PrecisionDebugger
68
+ debugger = PrecisionDebugger(dump_path="dump_path", level="debug")
69
+ for data, label in data_loader:
70
+ # 执行模型训练
71
+ train(data, label)
72
+
73
+ ```
74
+
75
+ 调用保存接口
76
+ ```python
77
+ # 训练过程中被调用py文件
78
+ from mindspore.pytorch import PrecisionDebugger
79
+ dict_variable = {"key1": "value1", "key2": [1, 2]}
80
+ PrecisionDebugger.save(dict_variable, "dict_variable", save_backward=False)
81
+
82
+ ```
83
+
84
+ ## 输出结果
85
+ * **"task" 配置为 "statistics" 场景** :在 dump 目录下会生成包含变量统计值信息的 `debug.json` 文件。
86
+ * **"task" 配置为 "tensor" 场景** :除了在 dump 目录下生成包含变量统计值信息的 `debug.json` 文件外,还会在 dump 子目录 `dump_tensor_data` 中保存张量二进制文件,文件名称格式为 `{variable_name}{grad_flag}.{count}.tensor.{indexes}.{file_suffix}`。
87
+
88
+ - variable_name: 传入save接口的变量名称。
89
+ - grad_flag: 反向数据标识,反向数据为"_grad",正向数据为""。
90
+ - count: 调用计数,多次以相同变量名称调用时的计数。
91
+ - indexes: 索引,在保存嵌套结构数据时的索引。例如:嵌套结构为`{"key1": "value1", "key2": ["value2", "value3"]}`,"value2"的索引为"key2.0"
92
+ - file_suffix:文件后缀,pytorch场景为"pt",mindspore场景为"npy"
93
+
94
+
@@ -0,0 +1,69 @@
1
+ # MindSpore 动态图场景的 kernel dump 说明
2
+
3
+ 当使用 msprobe 数据采集功能时,level 配置为 "L2" 表示采集 kernel 层级的算子数据,仅支持昇腾 NPU 平台。
4
+
5
+ 本文主要介绍 kernel dump 的配置示例和采集结果介绍, msprobe 数据采集功能的详细使用参考 《[MindSpore 场景的精度数据采集](./06.data_dump_MindSpore.md)》。
6
+
7
+ ## 1 kernel dump 配置示例
8
+
9
+ 使用 kernel dump 时,list 必须要填一个 API 名称,kernel dump 目前每个 step 只支持采集一个 API 的数据。
10
+ API 名称填写参考 L1 dump 结果文件 dump.json 中的API名称,命名格式为:`{api_type}.{api_name}.{API调用次数}.{forward/backward}`。
11
+
12
+ ```json
13
+ {
14
+ "task": "tensor",
15
+ "dump_path": "/home/data_dump",
16
+ "level": "L2",
17
+ "rank": [],
18
+ "step": [],
19
+ "tensor": {
20
+ "scope": [],
21
+ "list": ["Functional.linear.0.backward"]
22
+ }
23
+ }
24
+ ```
25
+
26
+ ## 2 结果文件介绍
27
+
28
+ ### 2.1 采集结果说明
29
+
30
+ 如果 API kernel 级数据采集成功,会打印以下信息:
31
+
32
+ ```bash
33
+ The kernel data of {api_name} is dumped successfully.
34
+ ```
35
+
36
+ 注意:如果打印该信息后,没有数据生成,参考**常见问题3.1**进行排查。
37
+
38
+ 如果 kernel dump 遇到不支持的 API, 会打印以下信息:
39
+
40
+ ```bash
41
+ The kernel dump does not support the {api_name} API.
42
+ ```
43
+
44
+ 其中 {api_name} 是对应溢出的 API 名称。
45
+
46
+ ### 2.2 输出文件说明
47
+ kernel dump 采集成功后,会在指定的 dump_path 目录下生成如下文件:
48
+
49
+ ```
50
+ ├── /home/data_dump/
51
+ │ ├── step0
52
+ │ │ ├── 20241201103000 # 日期时间格式,表示2024-12-01 10:30:00
53
+ │ │ │ ├── 0 # 表示 device id
54
+ │ │ │ │ ├──{op_type}.{op_name}.{task_id}.{stream_id}.{timestamp} # kernel 层算子数据
55
+ │ │ │ ...
56
+ │ │ ├── kernel_config_{device_id}.json # kernel dump 在接口调用过程中生成的中间文件,一般情况下无需关注
57
+ │ │ ...
58
+ │ ├── step1
59
+ │ ...
60
+ ```
61
+ 成功采集到数据后,可以使用 msprobe 工具提供的《[PyTorch 场景的数据解析](./14.data_parse_PyTorch.md)》功能分析数据。
62
+
63
+ ## 3 常见问题
64
+
65
+ #### 3.1 采集结果文件为空,有可能是什么原因?
66
+
67
+ 1. 首先需要确认工具使用方式、配置文件内容、list 填写的 API 名称格式是否都正确无误。
68
+
69
+ 2. 其次需要确认 API 是否运行在昇腾 NPU 上,如果是运行在其他设备上则不会存在 kernel 级数据。
@@ -0,0 +1,229 @@
1
+ # MSAdapter 场景的精度数据采集
2
+
3
+ MSAdapter 是一款 MindSpore 生态适配工具,可以将 PyTorch 训练脚本高效迁移至 MindSpore 框架执行,以实现在不改变原有 PyTorch 用户开发习惯的情况下,使得 PyTorch 代码能在昇腾上获得高效性能。
4
+
5
+ msprobe 工具主要通过在训练脚本内添加 dump 接口、启动训练的方式采集精度数据。
6
+
7
+ 本工具提供固定的 API 支持列表,若需要删除或增加 dump 的 API,可以在 msprobe/pytorch/hook_module/support_wrap_ops.yaml 文件内手动修改,如下示例:
8
+
9
+ ```yaml
10
+ functional: # functional为算子类别,找到对应的类别,在该类别下按照下列格式删除或添加API
11
+ - conv1d
12
+ - conv2d
13
+ - conv3d
14
+ ```
15
+
16
+ 删除 API 的场景:部分模型代码逻辑会存在 API 原生类型校验,工具执行dump操作时,对封装后的模型 API 可能与模型的原生 API 类型不一致,此时可能引发校验失败,详见《[FAQ](FAQ.md)》中“异常情况”的第10和11条。
17
+
18
+ ## 1. 工具安装
19
+
20
+ 请参见[《msprobe 工具安装指南》](./01.installation.md)。
21
+
22
+ ## 2 接口介绍
23
+
24
+ ### 2.1 msprobe.mindspore.PrecisionDebugger
25
+
26
+ **功能说明**:通过加载 dump 配置文件的方式来确定 dump 操作的详细配置。
27
+
28
+ **原型**:
29
+
30
+ ```Python
31
+ PrecisionDebugger(config_path=None, task=None, dump_path=None, level=None, step=None)
32
+ ```
33
+
34
+ **参数说明**:
35
+
36
+ 1. config_path:指定 dump 配置文件路径,string 类型。参数示例:"./config.json"。未配置该路径时,默认使用 [config.json](../config.json) 文件的默认配置,配置选项含义可见 [config.json 介绍](./02.config_introduction.md)。
37
+
38
+ 2. 其他参数与 [config.json](../config.json) 文件中的同名配置字段含义相同,具体可见 [config.json 介绍](./02.config_introduction.md)。当参数值非None时,优先级高于 [config.json](../config.json) 文件中的同名配置。
39
+
40
+ #### 2.1.1 start
41
+
42
+ **功能说明**:启动精度数据采集。需要与 [**stop**](#212-stop) 接口一起添加在训练迭代的 for 循环内。
43
+
44
+ **原型**:
45
+
46
+ ```Python
47
+ start(model=None)
48
+ ```
49
+
50
+ **参数说明**:
51
+
52
+ 1. model:指定需要采集 Module 级数据的模型,支持传入 torch.nn.Module、list[torch.nn.Module]或Tuple[torch.nn.Module] 类型,默认未配置。level 配置为 "L0" 或 "mix" 时,必须在该接口中配置该参数。API级别("L1" level)dump 时,传入 model 可以采集 model 内包含 primitive op 对象在内的所有 API 数据,若不传入 model 参数,则只采集非 primitive op 的 API 数据。
53
+
54
+ #### 2.1.2 stop
55
+
56
+ **功能说明**:停止精度数据采集。在 **start** 接口调用之后的任意位置添加。若 **stop** 接口添加在反向计算代码之后,则会采集 **start** 和该接口之间的前反向数据。
57
+ 若 **stop** 接口添加在反向计算代码之前,则需要将 [**step**](#213-step) 接口添加到反向计算代码之后,才能采集 **start** 和该接口之间的前反向数据。
58
+
59
+ **注意**:**stop** 接口必须调用,否则可能导致精度数据落盘不全。
60
+
61
+ **原型**:
62
+
63
+ ```Python
64
+ stop()
65
+ ```
66
+
67
+ #### 2.1.3 step
68
+
69
+ **功能说明**:进行训练 step 数的自增,完成当前 step 所有数据的落盘并更新 dump 参数。在一个 step 训练结束的位置添加,且必须在 **stop** 接口之后的位置调用。该接口需要配合 **start** 和 **stop** 函数使用,尽量添加在反向计算代码之后,否则可能会导致反向数据丢失。
70
+
71
+ **原型**:
72
+
73
+ ```Python
74
+ step()
75
+ ```
76
+
77
+ #### 2.1.4 forward_backward_dump_end
78
+
79
+ **功能说明**:停止精度数据采集。与 **stop** 接口功能相同,该函数在将来会被移除,建议使用 **stop** 接口。
80
+
81
+ **原型**:
82
+
83
+ ```Python
84
+ forward_backward_dump_end()
85
+ ```
86
+
87
+ #### 2.1.5 save
88
+
89
+ **功能说明**:单点保存网络执行过程中正反向数值,并以统计值/张量文件落盘。
90
+
91
+ **原型**:
92
+ ```python
93
+ save(variable, name, save_backward=True)
94
+ ```
95
+
96
+ **参数说明**:
97
+ | 参数名称 | 参数含义 | 支持数据类型 | 是否必选|
98
+ | ---------- | ------------------| ------------------- | ------------------- |
99
+ | variable | 需要保存的变量 |dict, list, tuple, torch.tensor, int, float, str | 是 |
100
+ | name | 指定的名称 | str | 是 |
101
+ | save_backward | 是否保存反向数据 | boolean | 否 |
102
+
103
+ ### 2.2 msprobe.mindspore.seed_all
104
+
105
+ **功能说明**:用于固定网络中的随机性和开启确定性计算。
106
+
107
+ **原型**:
108
+ ```python
109
+ seed_all(seed=1234, mode=False, rm_dropout=True)
110
+ ```
111
+
112
+ **参数说明**:
113
+
114
+ 1. seed: 随机性种子,默认值:1234,非必选。参数示例: seed=1000。该参数用于 random、numpy.random, mindspore.common.Initializer、mindspore.nn.probability.distribution的随机数生成以及 Python 中 str、bytes、datetime 对象的 hash 算法。
115
+
116
+ 2. mode:确定性计算使能,可配置 True 或 False,默认值:False,非必选。参数示例:mode=True。该参数设置为 True 后,将会开启算子确定性运行模式与归约类通信算子(AllReduce、ReduceScatter、Reduce)的确定性计算。注意:确定性计算会导致 API 执行性能降低,建议在发现模型多次执行结果不同的情况下开启。
117
+
118
+ 3. rm_dropout:控制 dropout 失效的开关。可配置 True 或 False,默认值:True,非必选。参数示例:rm_dropout=True。该参数设置为 True 后,将会使 mindspore.ops.Dropout,mindspore.ops.Dropout2D,mindspore.ops.Dropout3D,mindspore.mint.nn.Dropout和mindspore.mint.nn.functional.dropout 失效,以避免因随机 dropout 造成的网络随机性。建议在采集数据前调用。
119
+
120
+ **注意**:通过 rm_dropout 控制 dropout 失效或生效需要在初始化 Dropout 实例前调用才能生效。
121
+
122
+ ## 3 示例代码
123
+
124
+ 以下为添加了 msprobe 工具 dump 接口的示例训练脚本。
125
+
126
+ ```python
127
+ import mindspore as ms
128
+ import torch
129
+ import torch.nn as nn
130
+ import torch.nn.functional as F
131
+
132
+ # 导入工具的数据采集接口
133
+ from msprobe.mindspore import PrecisionDebugger
134
+
135
+ # 在模型训练开始前实例化PrecisionDebugger
136
+ debugger = PrecisionDebugger(config_path='./config.json')
137
+
138
+
139
+ # 定义网络
140
+ class Net(nn.Module):
141
+ def __init__(self) -> None:
142
+ super().__init__()
143
+ self.linear1 = nn.Linear(in_features=8, out_features=4)
144
+ self.linear2 = nn.Linear(in_features=4, out_features=2)
145
+
146
+ def forward(self, x):
147
+ x1 = self.linear1(x)
148
+ x2 = self.linear2(x1)
149
+ logits = F.relu(x2)
150
+ return logits
151
+
152
+
153
+ net = Net()
154
+
155
+
156
+ def train_step(inputs):
157
+ return net(inputs)
158
+
159
+
160
+ if __name__ == "__main__":
161
+ data = (torch.randn(10, 8), torch.randn(10, 8), torch.randn(10, 8))
162
+ grad_fn = ms.value_and_grad(train_step, grad_position=0)
163
+
164
+ for inputs in data:
165
+ # 开启数据 dump
166
+ debugger.start(model=net)
167
+
168
+ out, grad = grad_fn(inputs)
169
+
170
+ # 停止数据 dump
171
+ debugger.stop()
172
+ # 更新 step 信息
173
+ debugger.step()
174
+ ```
175
+
176
+ ## 4 dump 结果文件介绍
177
+
178
+ 训练结束后,工具将 dump 的数据保存在 dump_path 参数指定的目录下。目录结构示例如下:
179
+
180
+ ```lua
181
+ ├── dump_path
182
+ │ ├── step0
183
+ │ | ├── rank0
184
+ │ | │ ├── dump_tensor_data
185
+ | | | | ├── Tensor.permute.1.forward.npy
186
+ | | | | ├── Functional.linear.5.backward.output.npy # 命名格式为{api_type}.{api_name}.{API调用次数}.{forward/backward}.{input/output}.{参数序号}, 其中,“参数序号”表示该API的第n个输入或输出,例如1,则为第一个参数,若该参数为list格式,则根据list继续排序,例如1.1,表示该API的第1个参数的第1个元素。
187
+ | | | | ...
188
+ | | | | ├── Module.conv1.Conv2d.forward.0.input.0.npy # 命名格式为{Module}.{module_name}.{class_name}.{forward/backward}.{调用次数}.{input/output}.{参数序号}, 其中,“参数序号”表示该Module的第n个参数,例如1,则为第一个参数,若该参数为list格式,则根据list继续排序,例如1.1,表示该Module的第1个参数的第1个元素。
189
+ | | | | ├── Module.conv1.Conv2D.forward.0.parameters.bias.npy # 模块参数数据:命名格式为{Module}.{module_name}.{class_name}.forward.{调用次数}.parameters.{parameter_name}。
190
+ | | | | └── Module.conv1.Conv2D.parameters_grad.weight.npy # 模块参数梯度数据:命名格式为{Module}.{module_name}.{class_name}.parameters_grad.{parameter_name}。因为同一模块的参数使用同一梯度进行更新,所以参数梯度文件名不包含调用次数。
191
+ | | | | # 当dump时传入的model参数为List[torch.nn.Module]或Tuple[torch.nn.Module]时,模块级数据的命名中包含该模块在列表中的索引index,命名格式为{Module}.{index}.*,*表示以上三种模块级数据的命名格式,例如:Module.0.conv1.Conv2d.forward.0.input.0.npy。
192
+ │ | | ├── dump.json
193
+ │ | | ├── stack.json
194
+ │ | | └── construct.json
195
+ │ | ├── rank1
196
+ | | | ├── dump_tensor_data
197
+ | | | | └── ...
198
+ │ | | ├── dump.json
199
+ │ | | ├── stack.json
200
+ | | | └── construct.json
201
+ │ | ├── ...
202
+ │ | |
203
+ | | └── rank7
204
+ │ ├── step1
205
+ │ | ├── ...
206
+ │ ├── step2
207
+ ```
208
+ * `rank`:设备 ID,每张卡的数据保存在对应的 `rank{ID}` 目录下。非分布式场景下没有 rank ID,目录名称为 rank。
209
+ * `dump_tensor_data`:保存采集到的张量数据。
210
+ * `dump.json`: 保存 API 或 Module 前反向数据的统计量信息。包含 dump 数据的 API 名称或 Module 名称,各数据的 dtype、 shape、max、min、mean、L2norm(L2范数,平方根)统计信息以及当配置 summary_mode="md5" 时的 CRC-32 数据。具体介绍可参考[dump.json文件说明](./27.dump_json_instruction.md#3-MSAdapter场景下的dump.json文件)。
211
+ * `stack.json`:API/Module 的调用栈信息。
212
+ * `construct.json`:分层分级结构,level 为 L1 时,construct.json 内容为空。
213
+
214
+
215
+ 当 task 为 tensor 时,dump 过程中,npy 文件在对应算子或者模块被执行后就会落盘,而 json 文件则需要在正常执行 PrecisionDebugger.stop() 后才会写入完整数据。因此如果程序异常终止,终止前被执行算子的相关 npy 文件得以保存,但 json 文件中的数据可能丢失。
216
+
217
+ 其中 rank 为设备上各卡的 ID,每张卡上 dump 的数据会生成对应 dump 目录。非分布式场景下没有 rank ID,目录名称为 rank。
218
+
219
+ npy 文件名的前缀含义如下:
220
+
221
+ | 前缀 | 含义 |
222
+ | ----------- | ---------------------------- |
223
+ | Tensor | torch.Tensor API数据 |
224
+ | Torch | torch API数据 |
225
+ | Functional | torch.nn.functional API数据 |
226
+ | NPU | NPU 亲和API数据 |
227
+ | Distributed | torch.distributed API数据 |
228
+ | Jit | 被 "jit" 装饰的模块或函数数据 |
229
+ | Module | torch.nn.Module 类(模块)数据 |
@@ -0,0 +1,31 @@
1
+ # MSAdapter 场景的溢出检测
2
+
3
+ msprobe 工具提供 MSAdapter 场景下的溢出检测功能。其检测对象为 **API** 级别(除 Primitive 和 Jit 类 API)或**模块**级别,分别对应 config.json 配置中的 **"L1"** 、**"L0"** level。
4
+
5
+ 需要注意,本工具仅支持在 INF/NAN 模式<sup>a</sup>下进行溢出检测。INF/NAN 模式的使能方式如下:
6
+
7
+ ```Shell
8
+ # 使能 CANN 侧 INF/NAN 模式
9
+ export INF_NAN_MODE_ENABLE=1
10
+ # 使能 MindSpore 框架侧 INF/NAN 模式
11
+ export MS_ASCEND_CHECK_OVERFLOW_MODE="INFNAN_MODE"
12
+ ```
13
+
14
+ **a**:在处理浮点数计算溢出问题时,NPU 当前支持两种溢出模式:INF/NAN 模式与饱和模式。INF/NAN 模式遵循 IEEE 754 标准,根据定义输出 INF/NAN 的计算结果。与之对应的饱和模式在计算出现溢出时,饱和为浮点数极值(+-MAX)。对于 CANN 侧配置,Atlas 训练系列产品,默认为饱和模式,且不建议使用 INF/NAN 模式;Atlas A2训练系列产品,默认为 INF/NAN 模式,且不建议使用饱和模式。对于 MindSpore 框架侧配置,仅支持对 Atlas A2 训练系列产品进行设置,默认为 INF/NAN 模式。CANN 侧 与 MindSpore 框架侧配置须一致。
15
+
16
+ 溢出检测任务的配置示例见["**MindSpore 动态图场景 task 配置为 overflow_check**"](./03.config_examples.md#33-task配置为overflow_check)小节。
17
+
18
+
19
+ ## 1 接口介绍
20
+
21
+ 溢出检测功能提供的接口与数据采集任务一致,详见 MSAdapter 场景的精度数据采集中的["**2 接口介绍**"](./29.data_dump_MSAdapter.md#2-接口介绍)小节。
22
+
23
+ 需要注意,目前暂不支持 "L1" level 下 primitive op 的溢出检测。
24
+
25
+ ## 2 示例代码
26
+
27
+ 溢出检测功能使用方式与数据采集任务一致,详见 MSAdapter 场景的精度数据采集中的["**3 示例代码**"](./29.data_dump_MSAdapter.md#3-示例代码)小节。
28
+
29
+ ## 3 溢出检测结果文件介绍
30
+
31
+ 溢出检测结果文件目录结构与含义与数据采集任务一致,但仅保存溢出 API 或 模块 的真实数据或统计信息。详见 MSAdapter 场景的精度数据采集中的["**4 dump 结果文件介绍**"](./29.data_dump_MSAdapter.md#4-dump-结果文件介绍)小节。
msprobe/docs/FAQ.md CHANGED
@@ -58,11 +58,7 @@
58
58
 
59
59
  答:对于 fp16 的数据,CPU 会上升一个精度 fp32 去计算,这是和算子那边对齐的精度结论,CPU 用更高精度去计算会更接近真实值。
60
60
 
61
- 6. 添加预检工具后截取操作报错:`IndexError: too many indices for tensor of dimension x` 或 `TypeError: len() of a 0-d tensor`。
62
-
63
- 答:注释工具目录 `mstt/debug/accuracy_tools/msprobe/pytorch/hook_module/support_wrap_ops.yaml` 文件中 Tensor: 下的 `- __getitem__`,工具会跳过采集该 API。如果是需要 dump 关键位置 API 也可以考虑根据报错堆栈信息注释引发报错的类型检查。
64
-
65
- 7. Tensor 魔法函数具体对应什么操作?
61
+ 6. Tensor 魔法函数具体对应什么操作?
66
62
 
67
63
  答:
68
64
 
@@ -202,15 +198,11 @@ def npu_forward_fused_softmax(self, input_, mask):
202
198
 
203
199
  答:正常现象,dataloader 通过 raise 结束程序,堆栈信息可忽略。
204
200
 
205
- 10. 添加 msprobe 工具后截取操作报错:`IndexError: too many indices for tensor of dimension x` 或 `TypeError: len() of a 0-d tensor`。
206
-
207
- 答:注释工具目录 `mstt/debug/accuracy_tools/msprobe/pytorch/hook_module/support_wrap_ops.yaml` 文件中 `Tensor: ` 下的 `- __getitem__`,工具会跳过采集该 API。如果是需要采集关键位置 API 也可以考虑根据报错堆栈信息注释引发报错的类型检查。
208
-
209
- 11. 使用 msprobe 工具数据采集功能后,模型出现报错,报错信息为:`activation_func must be F.gelu` 或 `ValueError(Only support fusion of gelu and swiglu)`。
201
+ 10. 使用 msprobe 工具数据采集功能后,模型出现报错,报错信息为:`activation_func must be F.gelu` 或 `ValueError(Only support fusion of gelu and swiglu)`。
210
202
 
211
203
  答:这一类报错常见于 Megatron/MindSpeed/ModelLink 等加速库或模型仓中,原因是工具本身会封装 torch 的 API(API类型和地址会发生改变),而有些 API 在工具使能前类型和地址就已经确定,此时工具无法对这类 API 再进行封装,而加速库中会对某些 API 进行类型检查,即会把工具无法封装的原始的 API和工具封装之后的 API 进行判断,所以会报错。
212
204
  规避方式有3种:①将PrecisionDebugger的实例化放在文件的开始位置,即导包后的位置,确保所有API都被封装;②注释 `mstt/debug/accuracy_tools/msprobe/pytorch/hook_module/support_wrap_ops.yaml` 文件中的 `-gelu` 或者 `-silu`,工具会跳过采集该 API。③ 可以考虑根据报错堆栈信息注释引发报错的类型检查。
213
205
 
214
- 12. 添加 msprobe 工具后触发与 AsStrided 算子相关、或者编译相关的报错,如:`Failed to compile Op [AsStrided]`。
206
+ 11. 添加 msprobe 工具后触发与 AsStrided 算子相关、或者编译相关的报错,如:`Failed to compile Op [AsStrided]`。
215
207
 
216
208
  答:注释工具目录 `mstt/debug/accuracy_tools/msprobe/pytorch/hook_module/support_wrap_ops.yaml` 文件中 `Tensor: `下的 `-t` 和 `- transpose`。
Binary file
Binary file
@@ -17,11 +17,13 @@ import os
17
17
 
18
18
  try:
19
19
  from msprobe.lib import _msprobe_c
20
- os.environ["MS_HOOK_ENABLE"] = "on"
21
20
  os.environ["HOOK_TOOL_PATH"] = _msprobe_c.__file__
22
21
  except ImportError:
23
22
  from .common.log import logger
24
23
  logger.info("Module _msprobe_c has not been installed. L2-Dump may not work normally.")
25
24
 
26
25
  from msprobe.mindspore.debugger.precision_debugger import PrecisionDebugger
27
- from msprobe.mindspore.common.utils import seed_all
26
+ from msprobe.mindspore.common.utils import seed_all, MsprobeStep, MsprobeInitStep
27
+ from msprobe.mindspore.monitor.module_hook import TrainerMon
28
+
29
+ os.environ["MS_HOOK_ENABLE"] = "on"
@@ -16,7 +16,7 @@
16
16
  import os
17
17
  from tqdm import tqdm
18
18
 
19
- from msprobe.core.common.const import Const, CompareConst, MsCompareConst
19
+ from msprobe.core.common.const import Const, CompareConst
20
20
  from msprobe.core.common.file_utils import FileOpen, create_directory, write_csv, load_json, load_yaml
21
21
  from msprobe.core.common.utils import add_time_as_suffix
22
22
  from msprobe.mindspore.api_accuracy_checker.api_info import ApiInfo
@@ -25,7 +25,9 @@ from msprobe.mindspore.api_accuracy_checker.base_compare_algorithm import compar
25
25
  from msprobe.mindspore.api_accuracy_checker.data_manager import DataManager
26
26
  from msprobe.mindspore.api_accuracy_checker.utils import (check_and_get_from_json_dict, global_context,
27
27
  trim_output_compute_element_list)
28
+ from msprobe.mindspore.common.const import MsCompareConst
28
29
  from msprobe.mindspore.common.log import logger
30
+ from msprobe.mindspore.api_accuracy_checker import torch_mindtorch_importer
29
31
 
30
32
  cur_path = os.path.dirname(os.path.realpath(__file__))
31
33
  yaml_path = os.path.join(cur_path, MsCompareConst.SUPPORTED_API_LIST_FILE)
@@ -82,9 +84,11 @@ class ApiAccuracyChecker:
82
84
  # get output
83
85
  if global_context.get_is_constructed():
84
86
  # constructed situation, need use constructed input to run mindspore api getting tested_output
85
- tested_outputs = api_runner(api_input_aggregation, api_name_str, forward_or_backward, Const.MS_FRAMEWORK)
87
+ tested_outputs = api_runner(api_input_aggregation, api_name_str,
88
+ forward_or_backward, global_context.get_framework())
86
89
  else:
87
90
  tested_outputs = api_info.get_compute_element_list(forward_or_backward, Const.OUTPUT)
91
+
88
92
  bench_outputs = api_runner(api_input_aggregation, api_name_str, forward_or_backward, Const.PT_FRAMEWORK)
89
93
  tested_outputs = trim_output_compute_element_list(tested_outputs, forward_or_backward)
90
94
  bench_outputs = trim_output_compute_element_list(bench_outputs, forward_or_backward)
@@ -153,13 +157,23 @@ class ApiAccuracyChecker:
153
157
  real_api_str = Const.SEP.join(api_name_str_list[1:-2])
154
158
  api_list = load_yaml(yaml_path)
155
159
  supported_tensor_api_list = api_list.get(MsCompareConst.SUPPORTED_TENSOR_LIST_KEY)
156
- if api_type_str in (MsCompareConst.MINT, MsCompareConst.MINT_FUNCTIONAL):
160
+ supported_fusion_api_list = MsCompareConst.SUPPORTED_FUSION_LIST
161
+ if api_type_str in (MsCompareConst.MINT, MsCompareConst.MINT_FUNCTIONAL) \
162
+ and global_context.get_framework() == Const.MS_FRAMEWORK:
163
+ return True
164
+ if api_type_str in MsCompareConst.MT_VALID_API_TYPES \
165
+ and global_context.get_framework() == Const.MT_FRAMEWORK:
157
166
  return True
158
- if api_type_str == MsCompareConst.TENSOR_API and real_api_str in supported_tensor_api_list:
167
+ if api_type_str == MsCompareConst.TENSOR_API and real_api_str in supported_tensor_api_list \
168
+ and global_context.get_framework() == Const.MS_FRAMEWORK:
169
+ return True
170
+ if api_type_str == MsCompareConst.FUNCTIONAL_API and real_api_str in supported_fusion_api_list \
171
+ and global_context.get_framework() == Const.MS_FRAMEWORK:
159
172
  return True
160
173
  return False
161
174
 
162
175
  def parse(self, api_info_path):
176
+
163
177
  api_info_dict = load_json(api_info_path)
164
178
 
165
179
  # init global context
@@ -167,14 +181,25 @@ class ApiAccuracyChecker:
167
181
  "task field in api_info.json", accepted_type=str,
168
182
  accepted_value=(MsCompareConst.STATISTICS_TASK,
169
183
  MsCompareConst.TENSOR_TASK))
184
+ try:
185
+ framework = check_and_get_from_json_dict(api_info_dict, MsCompareConst.FRAMEWORK,
186
+ "framework field in api_info.json", accepted_type=str,
187
+ accepted_value=(Const.MS_FRAMEWORK,
188
+ Const.MT_FRAMEWORK))
189
+ except Exception as e:
190
+ framework = Const.MS_FRAMEWORK
191
+ logger.warning(f"JSON parsing error in framework field: {e}")
192
+
193
+ if framework == Const.MT_FRAMEWORK and not torch_mindtorch_importer.is_valid_pt_mt_env:
194
+ raise Exception(f"Please check if you have a valid PyTorch and MindTorch environment")
195
+
170
196
  is_constructed = task == MsCompareConst.STATISTICS_TASK
171
197
  if not is_constructed:
172
198
  dump_data_dir = check_and_get_from_json_dict(api_info_dict, MsCompareConst.DUMP_DATA_DIR_FIELD,
173
- "dump_data_dir field in api_info.json",
174
- accepted_type=str)
199
+ "dump_data_dir field in api_info.json", accepted_type=str)
175
200
  else:
176
201
  dump_data_dir = ""
177
- global_context.init(is_constructed, dump_data_dir)
202
+ global_context.init(is_constructed, dump_data_dir, framework)
178
203
 
179
204
  api_info_data = check_and_get_from_json_dict(api_info_dict, MsCompareConst.DATA_FIELD,
180
205
  "data field in api_info.json", accepted_type=dict)