mindstudio-probe 8.1.2__py3-none-any.whl → 8.2.1__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 (181) hide show
  1. {mindstudio_probe-8.1.2.dist-info → mindstudio_probe-8.2.1.dist-info}/METADATA +2 -2
  2. {mindstudio_probe-8.1.2.dist-info → mindstudio_probe-8.2.1.dist-info}/RECORD +172 -147
  3. msprobe/README.md +6 -6
  4. msprobe/core/common/const.py +98 -41
  5. msprobe/core/common/db_manager.py +256 -0
  6. msprobe/core/common/file_utils.py +28 -5
  7. msprobe/core/common/log.py +7 -0
  8. msprobe/core/common/megatron_utils.py +59 -0
  9. msprobe/core/common/parallel_state.py +193 -0
  10. msprobe/core/common/utils.py +20 -13
  11. msprobe/core/common_config.py +5 -0
  12. msprobe/core/compare/acc_compare.py +140 -93
  13. msprobe/core/compare/check.py +13 -0
  14. msprobe/core/compare/compare_cli.py +64 -6
  15. msprobe/core/compare/config.py +10 -8
  16. msprobe/core/compare/diff_analyze/diff_analyze_threshold.yaml +14 -0
  17. msprobe/core/compare/diff_analyze/first_diff_analyze.py +135 -0
  18. msprobe/core/compare/diff_analyze/ignore_op_list.yaml +3 -0
  19. msprobe/core/compare/find_first/__init__.py +0 -0
  20. msprobe/core/compare/find_first/analyzer.py +282 -0
  21. msprobe/core/compare/find_first/data_processor.py +35 -0
  22. msprobe/core/compare/find_first/graph.py +188 -0
  23. msprobe/core/compare/find_first/utils.py +189 -0
  24. msprobe/core/compare/highlight.py +74 -101
  25. msprobe/core/compare/layer_mapping/layer_mapping.py +14 -9
  26. msprobe/core/compare/merge_result/merge_result.py +2 -2
  27. msprobe/core/compare/multiprocessing_compute.py +45 -28
  28. msprobe/core/compare/npy_compare.py +7 -10
  29. msprobe/core/compare/utils.py +338 -130
  30. msprobe/core/config_check/checkers/dataset_checker.py +2 -1
  31. msprobe/core/config_check/checkers/env_args_checker.py +5 -5
  32. msprobe/core/config_check/checkers/hyperparameter_checker.py +30 -10
  33. msprobe/core/config_check/checkers/pip_checker.py +4 -3
  34. msprobe/core/config_check/checkers/random_checker.py +3 -3
  35. msprobe/core/config_check/checkers/weights_checker.py +2 -1
  36. msprobe/core/config_check/ckpt_compare/megatron_loader.py +2 -0
  37. msprobe/core/config_check/resource/hyperparameter.yaml +11 -1
  38. msprobe/core/config_check/utils/hyperparameter_parser.py +7 -3
  39. msprobe/core/config_check/utils/utils.py +10 -0
  40. msprobe/core/data_dump/api_registry.py +49 -30
  41. msprobe/core/data_dump/data_collector.py +71 -29
  42. msprobe/core/data_dump/data_processor/base.py +2 -0
  43. msprobe/core/data_dump/data_processor/mindspore_processor.py +47 -53
  44. msprobe/core/data_dump/data_processor/pytorch_processor.py +227 -93
  45. msprobe/core/data_dump/json_writer.py +81 -7
  46. msprobe/core/data_dump/scope.py +4 -6
  47. msprobe/core/hook_manager.py +129 -70
  48. msprobe/core/monitor/csv2db.py +361 -0
  49. msprobe/core/monitor/db_utils.py +278 -0
  50. msprobe/core/monitor/utils.py +35 -1
  51. msprobe/core/service.py +31 -39
  52. msprobe/core/single_save/single_comparator.py +16 -3
  53. msprobe/docs/01.installation.md +51 -19
  54. msprobe/docs/02.config_introduction.md +16 -20
  55. msprobe/docs/03.config_examples.md +26 -0
  56. msprobe/docs/04.kernel_dump_PyTorch.md +1 -1
  57. msprobe/docs/05.data_dump_PyTorch.md +6 -2
  58. msprobe/docs/06.data_dump_MindSpore.md +44 -7
  59. msprobe/docs/07.accuracy_checker_PyTorch.md +1 -1
  60. msprobe/docs/10.accuracy_compare_PyTorch.md +124 -44
  61. msprobe/docs/11.accuracy_compare_MindSpore.md +75 -7
  62. msprobe/docs/14.data_parse_PyTorch.md +1 -1
  63. msprobe/docs/19.monitor.md +94 -7
  64. msprobe/docs/21.visualization_PyTorch.md +71 -101
  65. msprobe/docs/22.visualization_MindSpore.md +69 -119
  66. msprobe/docs/23.generate_operator_PyTorch.md +1 -1
  67. msprobe/docs/25.tool_function_introduction.md +0 -1
  68. msprobe/docs/26.data_dump_PyTorch_baseline.md +7 -7
  69. msprobe/docs/28.debugger_save_instruction.md +184 -81
  70. msprobe/docs/29.data_dump_MSAdapter.md +6 -0
  71. msprobe/docs/31.config_check.md +4 -2
  72. msprobe/docs/36.calculation_result_change.md +75 -0
  73. msprobe/docs/FAQ.md +22 -1
  74. msprobe/docs/data_dump_MindSpore/dynamic_graph_quick_start_example.md +6 -2
  75. msprobe/docs/img/compare_result.png +0 -0
  76. msprobe/docs/img/visualization/vis_browser_1.png +0 -0
  77. msprobe/docs/img/visualization/vis_match_info.png +0 -0
  78. msprobe/docs/img/visualization/vis_precision_info.png +0 -0
  79. msprobe/docs/img/visualization/vis_search_info.png +0 -0
  80. msprobe/docs/img/visualization/vis_show_info.png +0 -0
  81. msprobe/docs/img/visualization/vis_showcase.png +0 -0
  82. msprobe/docs/img/visualization/vis_unmatch_info.png +0 -0
  83. msprobe/docs/visualization/mindspeed_llamafactoary_img/1.png +0 -0
  84. msprobe/docs/visualization/mindspeed_llamafactoary_img/2.png +0 -0
  85. msprobe/docs/visualization/mindspeed_llamafactoary_img/3.png +0 -0
  86. msprobe/docs/visualization/mindspeed_llamafactoary_img/4.png +0 -0
  87. msprobe/docs/visualization/mindspeed_llamafactoary_img/5.png +0 -0
  88. msprobe/docs/visualization/mindspeed_llamafactoary_img/6.png +0 -0
  89. msprobe/docs/visualization/mindspeed_llamafactoary_img/7.png +0 -0
  90. msprobe/docs/visualization/mindspeed_llamafactoary_img/llamafactory-qwen25vl.txt +59 -0
  91. msprobe/docs/visualization/mindspeed_llamafactoary_img/llamafactory1.png +0 -0
  92. msprobe/docs/visualization/mindspeed_llamafactoary_img/llamafactory2.png +0 -0
  93. msprobe/docs/visualization/mindspeed_llamafactoary_img/mindspeed-mm-qwen25vl.txt +80 -0
  94. msprobe/docs/visualization/mindspeed_llamafactoary_img/mindspeed1.png +0 -0
  95. msprobe/docs/visualization/mindspeed_llamafactoary_img/mindspeed2.png +0 -0
  96. msprobe/docs/visualization/mindspeed_llamafactory_mapping.md +330 -0
  97. msprobe/mindspore/__init__.py +1 -1
  98. msprobe/mindspore/api_accuracy_checker/api_accuracy_checker.py +1 -1
  99. msprobe/mindspore/api_accuracy_checker/api_runner.py +9 -6
  100. msprobe/mindspore/api_accuracy_checker/compute_element.py +18 -12
  101. msprobe/mindspore/cell_processor.py +64 -25
  102. msprobe/mindspore/common/utils.py +51 -7
  103. msprobe/mindspore/compare/common_dir_compare.py +45 -37
  104. msprobe/mindspore/compare/ms_compare.py +10 -2
  105. msprobe/mindspore/compare/ms_graph_compare.py +47 -52
  106. msprobe/mindspore/debugger/debugger_config.py +18 -7
  107. msprobe/mindspore/debugger/precision_debugger.py +16 -12
  108. msprobe/mindspore/dump/cell_dump_process.py +130 -68
  109. msprobe/mindspore/dump/cell_dump_with_insert_gradient.py +10 -2
  110. msprobe/mindspore/dump/graph_mode_cell_dump.py +35 -9
  111. msprobe/mindspore/dump/graph_tensor_dump.py +11 -0
  112. msprobe/mindspore/dump/hook_cell/api_register.py +19 -20
  113. msprobe/mindspore/dump/hook_cell/hook_cell.py +12 -34
  114. msprobe/mindspore/dump/hook_cell/ms_hook_manager.py +142 -21
  115. msprobe/mindspore/dump/kernel_kbyk_dump.py +24 -0
  116. msprobe/mindspore/exception_dump/__init__.py +0 -0
  117. msprobe/mindspore/exception_dump/exception_dump_tool_factory.py +51 -0
  118. msprobe/mindspore/exception_dump/kernel_graph_exception_dump.py +57 -0
  119. msprobe/mindspore/free_benchmark/api_pynative_self_check.py +5 -4
  120. msprobe/mindspore/mindspore_service.py +2 -2
  121. msprobe/mindspore/mindtorch/mindtorch_adaptor.py +12 -7
  122. msprobe/mindspore/monitor/features.py +82 -0
  123. msprobe/mindspore/monitor/module_hook.py +168 -10
  124. msprobe/mindspore/monitor/utils.py +27 -1
  125. msprobe/mindspore/ms_config.py +12 -4
  126. msprobe/mindspore/overflow_check/overflow_check_tool_factory.py +1 -1
  127. msprobe/mindspore/task_handler_factory.py +3 -1
  128. msprobe/nan_analyze/graph.py +1 -1
  129. msprobe/pytorch/api_accuracy_checker/common/config.py +3 -36
  130. msprobe/pytorch/api_accuracy_checker/compare/api_precision_compare.py +0 -24
  131. msprobe/pytorch/api_accuracy_checker/compare/compare.py +2 -12
  132. msprobe/pytorch/api_accuracy_checker/config.yaml +1 -6
  133. msprobe/pytorch/api_accuracy_checker/run_ut/multi_run_ut.py +2 -2
  134. msprobe/pytorch/api_accuracy_checker/run_ut/run_ut.py +12 -132
  135. msprobe/pytorch/common/utils.py +1 -21
  136. msprobe/pytorch/compare/pt_compare.py +10 -2
  137. msprobe/pytorch/{hook_module/jit_script_wrapper.py → compare/pt_diff_analyze.py} +3 -15
  138. msprobe/pytorch/compare/utils.py +2 -1
  139. msprobe/pytorch/debugger/debugger_config.py +18 -23
  140. msprobe/pytorch/dump/module_dump/hook_wrapper.py +10 -7
  141. msprobe/pytorch/dump/module_dump/module_processer.py +41 -19
  142. msprobe/pytorch/free_benchmark/main.py +7 -4
  143. msprobe/pytorch/hook_module/api_register.py +62 -24
  144. msprobe/pytorch/hook_module/hook_module.py +9 -29
  145. msprobe/pytorch/hook_module/pt_hook_manager.py +84 -15
  146. msprobe/pytorch/hook_module/script_wrapper.py +140 -0
  147. msprobe/pytorch/hook_module/support_wrap_ops.yaml +6 -0
  148. msprobe/pytorch/monitor/csv2tb.py +1 -1
  149. msprobe/pytorch/monitor/features.py +94 -0
  150. msprobe/pytorch/monitor/module_hook.py +221 -81
  151. msprobe/pytorch/monitor/module_metric.py +27 -1
  152. msprobe/pytorch/monitor/optimizer_collect.py +109 -4
  153. msprobe/pytorch/online_dispatch/dispatch.py +42 -24
  154. msprobe/pytorch/online_dispatch/dump_compare.py +1 -1
  155. msprobe/pytorch/parse_tool/lib/visualization.py +0 -1
  156. msprobe/pytorch/pt_config.py +2 -51
  157. msprobe/pytorch/pytorch_service.py +7 -14
  158. msprobe/visualization/builder/graph_builder.py +192 -63
  159. msprobe/visualization/builder/graph_merger.py +986 -0
  160. msprobe/visualization/builder/msprobe_adapter.py +17 -15
  161. msprobe/visualization/compare/graph_comparator.py +26 -16
  162. msprobe/visualization/db_utils.py +252 -0
  163. msprobe/visualization/graph/base_node.py +2 -22
  164. msprobe/visualization/graph/distributed_analyzer.py +12 -12
  165. msprobe/visualization/graph/graph.py +44 -16
  166. msprobe/visualization/graph_service.py +143 -59
  167. msprobe/visualization/utils.py +103 -4
  168. msprobe/docs/08.accuracy_checker_online_PyTorch.md +0 -295
  169. msprobe/pytorch/api_accuracy_checker/tensor_transport_layer/attl.py +0 -205
  170. msprobe/pytorch/api_accuracy_checker/tensor_transport_layer/client.py +0 -378
  171. msprobe/pytorch/api_accuracy_checker/tensor_transport_layer/device_dispatch.py +0 -239
  172. msprobe/pytorch/api_accuracy_checker/tensor_transport_layer/dump_dispatch.py +0 -115
  173. msprobe/pytorch/api_accuracy_checker/tensor_transport_layer/server.py +0 -250
  174. msprobe/pytorch/api_accuracy_checker/tensor_transport_layer/torch_ops_config.yaml +0 -63
  175. msprobe/pytorch/api_accuracy_checker/tensor_transport_layer/utils.py +0 -198
  176. msprobe/pytorch/attl_manager.py +0 -65
  177. {mindstudio_probe-8.1.2.dist-info → mindstudio_probe-8.2.1.dist-info}/LICENSE +0 -0
  178. {mindstudio_probe-8.1.2.dist-info → mindstudio_probe-8.2.1.dist-info}/WHEEL +0 -0
  179. {mindstudio_probe-8.1.2.dist-info → mindstudio_probe-8.2.1.dist-info}/entry_points.txt +0 -0
  180. {mindstudio_probe-8.1.2.dist-info → mindstudio_probe-8.2.1.dist-info}/top_level.txt +0 -0
  181. /msprobe/{pytorch/api_accuracy_checker/tensor_transport_layer → core/compare/diff_analyze}/__init__.py +0 -0
@@ -1,7 +1,9 @@
1
1
  # 单点保存工具
2
2
 
3
3
  ## 简介
4
+
4
5
  L0, L1, mix级别的dump能力存在盲区,网络中的非API或module的输入输出不会被批量dump下来。单点保存提供类似np.save和print的功能和使用体验,可以保存指定的变量。同时针对大模型场景进行了增强,具备以下特性:
6
+
5
7
  - 可保存变量的反向梯度结果。
6
8
  - 能直接保存嵌套结构数据(如 list、dict),无需手动遍历。
7
9
  - 自动分 Rank 保存。
@@ -10,29 +12,32 @@ L0, L1, mix级别的dump能力存在盲区,网络中的非API或module的输
10
12
  - 可配置保存统计值(MindSpore静态图暂不支持)或者张量。
11
13
  - 支持异步保存。
12
14
 
15
+ 单点保存工具的使用过程中可能会涉及到工具跨文件使用的场景,具体使能方式见[跨文件采集数据](./05.data_dump_PyTorch.md#24-跨文件采集数据)。
16
+
13
17
  ## 支持场景
14
18
 
15
19
  ## 动态图场景(Pytorch&MindSpore)
16
20
 
17
- ### 使能方式
21
+ ### 使能方式
18
22
 
19
23
  #### 配置文件说明
20
24
 
21
25
  通用配置 (细节详见[通用配置说明](./02.config_introduction.md#11-通用配置) ):
22
26
 
23
- | 参数 | 解释 | 是否必选 |
24
- | -------- |-------------------------------------------| -------- |
25
- | task | dump 的任务类型,str 类型。 单点保存场景仅支持传入"statistics", "tensor"。 | |
26
- | level | dump 级别,str 类型,根据不同级别采集不同数据。单点保存场景传入"debug"。 | 是 |
27
- | dump_path | 设置 dump 数据目录路径,str 类型。 | 是 |
28
- | rank | 指定对某张卡上的数据进行采集,list[Union[int, str]] 类型。 | 否 |
29
- | step | 指定采集某个 Step 的数据,list[Union[int, str]] 类型。 | 否 |
30
- | async_dump | 异步 dump 开关,bool 类型。 | 否 |
27
+ | 参数 | 解释 | 是否必选 |
28
+ | ---------- | ------------------------------------------------------------------------------------------------------- | -------- |
29
+ | task | dump 的任务类型,str 类型。 单点保存场景仅支持传入"statistics", "tensor"。 | |
30
+ | level | dump 级别,str 类型,根据不同级别采集不同数据。单点保存场景传入"debug"。 | 是 |
31
+ | dump_path | 设置 dump 数据目录路径,str 类型。 | 是 |
32
+ | rank | 指定对某张卡上的数据进行采集,list[Union[int, str]] 类型。 | 否 |
33
+ | step | 指定采集某个 Step 的数据,list[Union[int, str]] 类型。 | 否 |
34
+ | async_dump | 异步 dump 开关,bool 类型。该模式下,summary_mode 不支持 md5 值,也不支持复数类型 tensor 的统计量计算。 | 否 |
31
35
 
32
36
  "statistics" 任务子配置项:
33
- | 参数 | 解释 | 是否必选 |
34
- | -------- |-------------------------------------------| -------- |
35
- | summary_mode | 控制 dump 文件输出的模式,str 类型。支持传入"statistics", "md5"。 细节详见[statistics任务子配置项说明](./02.config_introduction.md#12-task-配置为-statistics) | 否 |
37
+
38
+ | 参数 | 解释 | 是否必选 |
39
+ | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
40
+ | summary_mode | 控制 dump 文件输出的模式,str 类型。支持传入"statistics", "md5"。 细节详见[statistics任务子配置项说明](./02.config_introduction.md#12-task-配置为-statistics) | 否 |
36
41
 
37
42
  "tensor" 任务无子配置项。
38
43
 
@@ -41,8 +46,10 @@ L0, L1, mix级别的dump能力存在盲区,网络中的非API或module的输
41
46
  调用PrecisionDebugger.save,传入需要保存的变量,指定变量名称以及是否需要保存反向数据。接口入参说明详见[PyTorch单点保存接口](./05.data_dump_PyTorch.md#19-save),[MindSpore单点保存接口](./06.data_dump_MindSpore.md#615-save)
42
47
 
43
48
  #### 实例
49
+
44
50
  (以PyTorch场景为例,MindSpore场景只需要从msprobe.mindspore模块导包即可)
45
51
  配置文件
52
+
46
53
  ```json
47
54
  {
48
55
  "task": "statistics",
@@ -58,6 +65,7 @@ L0, L1, mix级别的dump能力存在盲区,网络中的非API或module的输
58
65
  ```
59
66
 
60
67
  初始化
68
+
61
69
  ```python
62
70
  # 训练启动py脚本
63
71
  from msprobe.pytorch import PrecisionDebugger
@@ -69,6 +77,7 @@ for data, label in data_loader:
69
77
  ```
70
78
 
71
79
  初始化(无配置文件)
80
+
72
81
  ```python
73
82
  # 训练启动py脚本
74
83
  from msprobe.pytorch import PrecisionDebugger
@@ -79,12 +88,74 @@ for data, label in data_loader:
79
88
 
80
89
  ```
81
90
 
82
- 调用保存接口
91
+ 调用保存接口示例(以PyTorch代码为例,MindSpore使用方法相同)
92
+
93
+ ```python
94
+ import torch
95
+ import torch.nn as nn
96
+ import torch.nn.functional as F
97
+
98
+ from msprobe.pytorch import PrecisionDebugger, seed_all
99
+ # 在模型训练开始前实例化PrecisionDebugger
100
+ debugger = PrecisionDebugger(dump_path="dump_path", level="debug")
101
+
102
+ # 定义网络
103
+ class ModuleOP(nn.Module):
104
+ def __init__(self) -> None:
105
+ super().__init__()
106
+ self.linear_1 = nn.Linear(in_features=8, out_features=4)
107
+ self.linear_2 = nn.Linear(in_features=4, out_features=2)
108
+
109
+ def forward(self, x):
110
+ x1 = self.linear_1(x)
111
+ x2 = self.linear_2(x1)
112
+ debugger.save(x2, "x2", save_backward=True) # 调用save接口
113
+ r1 = F.relu(x2)
114
+ return r1
115
+
116
+ if __name__ == "__main__":
117
+ module = ModuleOP()
118
+
119
+ x = torch.randn(10, 8)
120
+ out = module(x)
121
+ loss = out.sum()
122
+ loss.backward()
123
+ ```
124
+
125
+ 分step保存数据(以PyTorch代码为例,MindSpore使用方法相同)
126
+
83
127
  ```python
84
- # 训练过程中被调用py文件
128
+ import torch
129
+ import torch.nn as nn
130
+ import torch.nn.functional as F
131
+
85
132
  from msprobe.pytorch import PrecisionDebugger
86
- dict_variable = {"key1": "value1", "key2": [1, 2]}
87
- PrecisionDebugger.save(dict_variable, "dict_variable", save_backward=False)
133
+ # 在模型训练开始前实例化PrecisionDebugger
134
+ debugger = PrecisionDebugger(dump_path="dump_path", level="debug")
135
+
136
+ # 定义网络
137
+ class ModuleOP(nn.Module):
138
+ def __init__(self) -> None:
139
+ super().__init__()
140
+ self.linear_1 = nn.Linear(in_features=8, out_features=4)
141
+ self.linear_2 = nn.Linear(in_features=4, out_features=2)
142
+
143
+ def forward(self, x):
144
+ x1 = self.linear_1(x)
145
+ x2 = self.linear_2(x1)
146
+ debugger.save(x2, "x2", save_backward=True) # 调用save接口
147
+ r1 = F.relu(x2)
148
+ return r1
149
+
150
+ if __name__ == "__main__":
151
+ module = ModuleOP()
152
+ train_iter = 10
153
+ for i in range(train_iter):
154
+ x = torch.randn(10, 8)
155
+ out = module(x)
156
+ loss = out.sum()
157
+ loss.backward()
158
+ debugger.step() # 调用debugger.step用于区分step保存
88
159
 
89
160
  ```
90
161
 
@@ -92,94 +163,126 @@ PrecisionDebugger.save(dict_variable, "dict_variable", save_backward=False)
92
163
 
93
164
  ### 使能方式
94
165
 
95
- #### 接口调用说明
96
- 工具提供两个对外接口`save`和`save_grad`,分别用于保存训练中的tensor以及tensor对应的反向数据
97
- | 接口名称 | 入参 | device | MindSpore版本 |备注 |
98
- | ------- | ------ | -------------- | --------------|--------------------------------------------------- |
99
- | save | save_dir name, data | Ascend | >= 2.6.0 | (主流场景)图模式下只支持Ascend,pynative下支持Ascend/GPU/CPU。 |
100
- | save_grad | save_dir, name, data | Ascend | >= 2.6.0 | (主流场景)图模式下只支持Ascend,pynative下支持Ascend/GPU/CPU。 |
101
-
102
-
103
- ----
104
- > 函数原型:
105
- `save(save_dir:str, name:str, data)`
106
- - save_dir:表示要保存的目录。
107
- - name :表示要保存的文件标志名称。
108
- - data :表示数据入参,可以是`mindspore.Tensor`或者是`List`,`Tuple`,`Dict`等嵌套结构。
109
-
110
- > 函数原型:
111
- `save_grad(save_dir:str, name:str, data)`
112
- - save_dir:表示要保存的目录。
113
- - name :表示要保存的文件标志名称。
114
- - data :表示数据入参,**只能**是`mindspore.Tensor`。
166
+ ### 接口说明
115
167
 
116
- #### 实例
168
+ 工具提供三个对外接口用于保存训练过程中的数据:
169
+
170
+ | 接口名称 | 功能描述 | 支持设备 | MindSpore版本 | 使用场景说明 |
171
+ | --------- | ------------------------ | -------------- | ------------- | ---------------------------------------------- |
172
+ | save | 保存正向传播的tensor数据 | Ascend/GPU/CPU | >= 2.6.0 | 图模式下仅支持Ascend,PyNative模式下支持全平台 |
173
+ | save_grad | 保存反向传播的梯度数据 | Ascend/GPU/CPU | >= 2.6.0 | 图模式下仅支持Ascend,PyNative模式下支持全平台 |
174
+ | step | 更新训练步数 | Ascend/GPU/CPU | >= 2.6.0 | 控制数据保存的step目录 |
117
175
 
118
- - save接口使用:
176
+ ### 详细接口定义
177
+
178
+ #### 1. save 接口
119
179
 
120
180
  ```python
121
- # save api usage
122
- # **first import**
123
- from msprobe.mindspore import save
181
+ save(save_dir: str, name: str, data: Union[Tensor, List, Tuple, Dict])
182
+ ```
183
+
184
+ **参数说明**:
185
+
186
+ - `save_dir`: 数据保存目录路径
187
+ - `name`: 数据标识名称(将作为文件名前缀)
188
+ - `data`: 支持多种数据类型:
189
+ - `mindspore.Tensor` 单个张量
190
+ - `List/Tuple/Dict` 嵌套结构(会自动展开保存)
124
191
 
192
+ **使用示例**:
193
+
194
+ ```python
195
+ from msprobe.mindspore import save
125
196
 
126
197
  class Net(nn.Cell):
127
- def construct(self, x, y, z):
128
- # **use save api**
129
- save("./test_dump", 'x', x)
130
- return x * y * z
131
-
132
- x = Tensor([1, 2], ms.float32)
133
- y = Tensor([-2, 3], ms.float32)
134
- z = Tensor([0, 3], ms.float32)
135
- net = Net()
136
- output = grad(net, grad_position=(1, 2))(x, y, z)
137
- time.sleep(1)
138
-
139
- # then will generate **./test_dump/step0/rank0/x_float32_0.npy**
198
+ def construct(self, x):
199
+ save("./dump_data", 'input', x) # 保存输入数据
200
+ return x * 2
201
+ ```
202
+
203
+ #### 2. save_grad 接口
204
+
205
+ ```python
206
+ save_grad(save_dir: str, name: str, data: Tensor) -> Tensor
140
207
  ```
141
208
 
142
- - save_grad接口使用:
209
+ **参数说明**:
210
+
211
+ - `save_dir`: 梯度保存目录路径
212
+ - `name`: 梯度标识名称(将作为文件名前缀)
213
+ - `data`: 必须是 `mindspore.Tensor`类型
214
+
215
+ **特别注意**:
216
+
217
+ - 必须接收返回值并传回原计算图
218
+ - 此操作不会影响计算精度
219
+
220
+ **使用示例**:
143
221
 
144
222
  ```python
145
- # save_grad usage
146
- # **first import**
147
223
  from msprobe.mindspore import save_grad
224
+
148
225
  class Net(nn.Cell):
149
- def construct(self, x, y, z):
150
- # **use save api** the return value of save_grad must be received by origin
151
- z = save_grad("./test_dump", 'z', z)
152
- return x * y * z
153
-
154
- x = Tensor([1, 2], ms.float32)
155
- y = Tensor([-2, 3], ms.float32)
156
- z = Tensor([0, 3], ms.float32)
157
- net = Net()
158
- output = grad(net, grad_position=(1, 2))(x, y, z)
159
- time.sleep(1)
160
-
161
- # then will generate **./test_dump/step0/rank0/z_grad_float32_0.npy**
226
+ def construct(self, x):
227
+ x = save_grad("./dump_data", 'grad', x) # 保存梯度数据
228
+ return x * 2
229
+ ```
230
+
231
+ #### 3. step 接口
232
+
233
+ ```python
234
+ step()
235
+ ```
236
+
237
+ **功能说明**:
238
+
239
+ - 递增训练步数计数器
240
+ - 控制数据保存到不同的step目录(如step0/, step1/等)
241
+ - 如果不调用,所有数据会保存到同一个step目录
242
+
243
+ **使用示例**:
244
+
245
+ ```python
246
+ from msprobe.mindspore import save, step
247
+
248
+ # 训练循环中
249
+ for epoch in range(epochs):
250
+ train_one_epoch()
251
+ step() # 每个epoch后更新step
162
252
  ```
163
- **注意**save_grad需要将返回值回传给原tensor,此操作不会有精度影响,只会传递原值。
164
253
 
165
254
 
166
255
  ## 输出结果
256
+
167
257
  ### 动态图场景(Pytorch&MindSpore)
168
- * **"task" 配置为 "statistics" 场景** :在 dump 目录下会生成包含变量统计值信息的 `debug.json` 文件。
258
+
259
+ * **"task" 配置为 "statistics" 场景** :在 dump 目录下会生成包含变量统计值信息的 `debug.json` 文件。
169
260
  `debug.json` 中统计值的key命名格式为 `{variable_name}{grad_flag}.{count}.debug`。
170
- * **"task" 配置为 "tensor" 场景** :除了在 dump 目录下生成包含变量统计值信息的 `debug.json` 文件外,还会在 dump 子目录 `dump_tensor_data` 中保存张量二进制文件,文件名称格式为 `{variable_name}{grad_flag}.{count}.debug.{indexes}.{file_suffix}`。
261
+ * **"task" 配置为 "tensor" 场景** :除了在 dump 目录下生成包含变量统计值信息的 `debug.json` 文件外,还会在 dump 子目录 `dump_tensor_data` 中保存张量二进制文件,文件名称格式为 `{variable_name}{grad_flag}.{count}.debug.{indexes}.{file_suffix}`。
171
262
 
172
- - variable_name: 传入save接口的变量名称。
173
- - grad_flag: 反向数据标识,反向数据为"_grad",正向数据为""。
174
- - count: 调用计数,多次以相同变量名称调用时的计数。
175
- - indexes: 索引,在保存嵌套结构数据时的索引。例如:嵌套结构为`{"key1": "value1", "key2": ["value2", "value3"]}`,"value2"的索引为"key2.0"。
176
- - file_suffix:文件后缀,PyTorch场景为"pt",MindSpore场景为"npy"。
263
+ - variable_name: 传入save接口的变量名称。
264
+ - grad_flag: 反向数据标识,反向数据为"_grad",正向数据为""。
265
+ - count: 调用计数,多次以相同变量名称调用时的计数。
266
+ - indexes: 索引,在保存嵌套结构数据时的索引。例如:嵌套结构为 `{"key1": "value1", "key2": ["value2", "value3"]}`,"value2"的索引为"key2.0"。
267
+ - file_suffix:文件后缀,PyTorch场景为"pt",MindSpore场景为"npy"。
177
268
 
178
269
  ### 静态图场景(MindSpore)
179
- 在指定目录`save_dir`下生成`{step}/{rank}`目录,目录下生成指定`{name}`的npy文件,如果是save_grad接口调用,则会生成`{name}_grad`的npy文件。
180
270
 
181
- 如`save("./test_dump", 'x', x)` -> `./test_dump/step0/rank0/x_float32_0.npy`。
271
+ 在指定目录 `save_dir`下生成 `{step}/{rank}`目录,目录下生成指定 `{name}`的npy文件,如果是save_grad接口调用,则会生成 `{name}_grad`的npy文件。
182
272
 
183
- 或如`z = save_grad("./test_dump", 'z', z)` -> `./test_dump/step0/rank0/z_grad_float32_0.npy`。
273
+ `save("./test_dump", 'x', x)` -> `./test_dump/step0/rank0/x_float32_0.npy`。
184
274
 
275
+ 或如 `z = save_grad("./test_dump", 'z', z)` -> `./test_dump/step0/rank0/z_grad_float32_0.npy`。
185
276
 
277
+ 结构如下:
278
+
279
+ ```
280
+ ./save_dir/
281
+ ├── step0/
282
+ │ ├── rank0/
283
+ │ │ ├── x_float32_0.npy # save保存的正向数据
284
+ │ │ └── z_grad_float32_0.npy # save_grad保存的梯度数据
285
+ ├── step1/
286
+ │ ├── rank0/
287
+ │ │ ├── ...
288
+ ```
@@ -4,6 +4,12 @@ MSAdapter 是一款 MindSpore 生态适配工具,可以将 PyTorch 训练脚
4
4
 
5
5
  msprobe 工具主要通过在训练脚本内添加 dump 接口、启动训练的方式采集精度数据。
6
6
 
7
+ **注意**:
8
+
9
+ - 为了正确识别 MSAdapter 场景,在导入 msprobe 工具前,需完成 torch 模块的导入。
10
+
11
+ - 因 MindSpore 框架自动微分机制的限制,dump 数据中可能会缺少原地操作模块/API 及其上一个模块/API 的反向数据。
12
+
7
13
  本工具提供固定的 API 支持列表,若需要删除或增加 dump 的 API,可以在 msprobe/pytorch/hook_module/support_wrap_ops.yaml 文件内手动修改,如下示例:
8
14
 
9
15
  ```yaml
@@ -35,7 +35,7 @@ msprobe -f pytorch/mindspore config_check -d **.sh **.yaml -o output_path
35
35
 
36
36
  -d或--dump 代表数据采集模式,可传入启动训练的 shell 脚本路径或 yaml 配置文件路径,可选,不传入代表不采集。
37
37
 
38
- -o或--output 代表输出路径,可选,默认为 config_check_pack.zip
38
+ -o或--output 代表输出路径,可选,默认为 config_check_pack.zip,必须以 `.zip` 后缀结尾。
39
39
 
40
40
  #### 动态数据采集
41
41
 
@@ -55,7 +55,7 @@ apply_patches:启动数据采集所需的各项patch,参数如下:
55
55
  在模型初始化好之后插入如下代码:
56
56
  ```
57
57
  from msprobe.core.config_check import ConfigChecker
58
- ConfigChecker(model, shell_path, output_zip_path, fmk)
58
+ ConfigChecker(model=model, shell_path="", output_zip_path="./config_check_pack.zip", fmk="pytorch")
59
59
  ```
60
60
 
61
61
  说明:
@@ -103,3 +103,5 @@ msprobe -f pytorch config_check -c bench_zip_path cmp_zip_path -o output_path
103
103
 
104
104
  1. 在使用 MindSpeed-LLM 进行数据采集时,需要注意动态数据采集中的 [apply_patches](#动态数据采集) 函数需要在 MindSpeed-LLM
105
105
  框架 pretrain_gpt.py 的 megatron_adaptor 函数导入之后执行。
106
+
107
+ 2. 静态数据采集功能只能获取到系统中的环境变量,shell 脚本中解析的超参不支持复杂运算的数据还原,有类似问题时建议使用[动态采集方式](#动态数据采集)。
@@ -0,0 +1,75 @@
1
+ # 模型计算结果改变原因分析
2
+
3
+ ## 介绍
4
+ 在模型训练场景下,使用seed_all接口同时固定随机性和打开计算,通信确定性计算,是能够保证模型跑两次得到的loss和gnorm结果完全一样。如果出现使能工具后loss或者gnorm出现偏差,可能是以下原因导致。
5
+
6
+ ## 工具引入同步导致计算结果变化
7
+ 工具采集统计量数据时,会涉及到将device上的tensor计算后的统计量信息通过item的时候传到cpu侧,再落盘到json文件中,item操作是一个同步的操作,可能会导致模型的计算结果出现变化。**一般的现象就是模型计算出现了NaN,但加了工具后问题不复现了。**
8
+
9
+ ASCEND_LAUNCH_BLOCKING 是一个环境变量,用于控制在 PyTorch 训练或在线推理场景中算子的执行模式。当设置为“1”时,算子将采用同步模式运行。因此如果出现加工具计算结果变化,可以设置ASCEND_LAUNCH_BLOCKING 为1,如果结果一样发生了变化,则说明是由于同步引起的结果改变。这个时候需要复现问题现象完成问题定位,推荐使用msprobe工具的异步dump功能,具体使用方式可查看[config配置](02.config_introduction.md)中的async_dump字段。
10
+
11
+ ## Hook机制导致计算结果变化
12
+
13
+ pytorch/mindspore的hook机制会导致某些特殊场景下梯度计算的累加序产生变化,从而影响模型反向计算的gnorm结果。具体代码示例如下:
14
+ ```python
15
+ import random, os
16
+ import numpy as np
17
+ import torch
18
+ from torch import nn
19
+
20
+
21
+ class Net(nn.Module):
22
+ def __init__(self) -> None:
23
+ super().__init__()
24
+ self.ln1 = nn.Linear(32, 32)
25
+ self.bn1 = nn.BatchNorm1d(32)
26
+ self.ln2 = nn.Linear(32, 32)
27
+
28
+ def forward(self, x):
29
+ x1 = self.ln1(x)
30
+
31
+ x2 = self.bn1(x)
32
+ x2 = self.ln2(x2)
33
+ return x1 + x2
34
+
35
+
36
+ class BigNet(nn.Module):
37
+ def __init__(self) -> None:
38
+ super().__init__()
39
+ self.net1 = Net()
40
+ self.net2 = Net()
41
+
42
+ def forward(self, x):
43
+ out1 = self.net1(x)
44
+ out2 = self.net2(out1)
45
+ return out1, out2
46
+
47
+
48
+ def my_backward_hook(module, grad_input, grad_output):
49
+ pass
50
+
51
+
52
+ if __name__ == "__main__":
53
+ os.environ["HCCL_DETERMINISTIC"] = 'true'
54
+
55
+ seed = 1234
56
+ os.environ["PYTHONHASHSEED"] = str(seed)
57
+ torch.manual_seed(seed)
58
+ random.seed(seed)
59
+ np.random.seed(seed)
60
+
61
+ model = BigNet()
62
+ model.net2.register_full_backward_hook(my_backward_hook)
63
+ inputs = torch.randn(3, 32)
64
+
65
+ out1, out2 = model(inputs)
66
+ loss = out1.sum() + out2.sum()
67
+ loss.backward()
68
+
69
+ for name, param in model.named_parameters():
70
+ print(f"{name}: {param.grad.mean()}")
71
+
72
+ ```
73
+ 执行一遍以上脚本,可以打印得到模型中各层的权重梯度,注释model.net2.register_full_backward_hook(my_backward_hook)后再执行一篇,可以看出bn层的权重梯度已经发生了变化。
74
+
75
+ **如果在msprobe L0,mix级别采集出现gnorm发生变化,可以尝试将采集级别改为L1,若L1级别gnorm不发生变化,则大概率是hook机制导致的梯度计算结果变化。**
msprobe/docs/FAQ.md CHANGED
@@ -36,8 +36,29 @@
36
36
  该信息说明 module 挂载了被 PyTorch 框架废弃的 register_backward_hook,这与工具使用的 register_full_backward_hook 接口会产生冲突,故工具会跳过该 module 的反向数据采集。
37
37
  - 如果您希望所有 module 数据都能采集下来,可以将模型中使用的 register_backward_hook 接口改为 PyTorch 框架推荐的 register_full_backward_pre_hook 或 register_full_backward_hook 接口。
38
38
 
39
+ 5. 在vllm场景下进行数据dump时,发现报错:`RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and npu:0!`
40
+ - 这是因为工具的debugger实例化早于LLM实例化导致的,解决方法就需要将debugger的实例化移至LLM实例化之后进行,可参考下方示例:
41
+ ```python
42
+ from vllm import LLM, SamplingParams
43
+ from msprobe.pytorch import PrecisionDebugger
44
+ prompts = [
45
+ "Hello, my name is",
46
+ "The president of the United States is",
47
+ "The capital of France is",
48
+ "The future of AI is",
49
+ ]
50
+
51
+ sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
52
+ llm = LLM(model="Qwen/Qwen2.5-0.5B-Instruct")
53
+
54
+ debugger = PrecisionDebugger("./config.json") # debugger实例化晚于LLM实例化
55
+
56
+ debugger.start()
57
+ outputs = llm.generate(prompts, sampling_params)
58
+ debugger.stop()
59
+ ```
39
60
 
40
- 5. 在使用 msprobe 进行 Pytorch 框架的数据采集功能时,请注意确认环境变量 NPU_ASD_ENABLE=0 ,即关闭特征值检测功能。 由于工具冲突, 在该功能开启的情况下可能导致某些 api 数据采集的缺失。
61
+ 6. 在使用 msprobe 进行 PyTorch 框架的数据采集功能时,请注意确认环境变量 NPU_ASD_ENABLE=0 ,即关闭特征值检测功能。 由于工具冲突, 在该功能开启的情况下可能导致某些 api 数据采集的缺失。
41
62
 
42
63
  # 2 精度预检(PyTorch)
43
64
 
@@ -35,7 +35,7 @@ import os
35
35
  import numpy as np
36
36
  import mindspore as ms
37
37
  from mindspore import nn, ops
38
- from mindspore import context
38
+ from mindspore import context, set_device, set_deterministic
39
39
  from mindspore import Tensor
40
40
  from msprobe.mindspore import PrecisionDebugger, seed_all
41
41
 
@@ -50,7 +50,11 @@ config_path = os.path.join(script_dir, 'config.json')
50
50
  debugger = PrecisionDebugger(config_path=config_path)
51
51
 
52
52
  # 设置 MindSpore 设备上下文
53
- context.set_context(mode=ms.PYNATIVE_MODE, device_target="Ascend", device_id=0)
53
+ context.set_context(mode=ms.PYNATIVE_MODE)
54
+
55
+ set_device("Ascend", 0)
56
+
57
+ set_deterministic(True)
54
58
  print("Context set successfully. Please wait for the training task.")
55
59
 
56
60
  # 定义卷积层
Binary file
@@ -0,0 +1,59 @@
1
+ DeepSpeedEngine(
2
+ (module): Qwen2_5_VLForConditionalGeneration(
3
+ (visual): Qwen2_5_VisionTransformerPretrainedModel(
4
+ (patch_embed): Qwen2_5_VisionPatchEmbed(
5
+ (proj): Conv3d(3, 1280, kernel_size=(2, 14, 14), stride=(2, 14, 14), bias=False)
6
+ )
7
+ (rotary_pos_emb): Qwen2_5_VisionRotaryEmbedding()
8
+ (blocks): ModuleList(
9
+ (0-15): 16 x Qwen2_5_VLVisionBlock(
10
+ (norm1): Qwen2RMSNorm((0,), eps=1e-06)
11
+ (norm2): Qwen2RMSNorm((0,), eps=1e-06)
12
+ (attn): Qwen2_5_VLVisionSdpaAttention(
13
+ (qkv): Linear(in_features=1280, out_features=3840, bias=True)
14
+ (proj): Linear(in_features=1280, out_features=1280, bias=True)
15
+ )
16
+ (mlp): Qwen2_5_VLMLP(
17
+ (gate_proj): Linear(in_features=1280, out_features=3420, bias=True)
18
+ (up_proj): Linear(in_features=1280, out_features=3420, bias=True)
19
+ (down_proj): Linear(in_features=3420, out_features=1280, bias=True)
20
+ (act_fn): SiLU()
21
+ )
22
+ )
23
+ )
24
+ (merger): Qwen2_5_VLPatchMerger(
25
+ (ln_q): Qwen2RMSNorm((0,), eps=1e-06)
26
+ (mlp): Sequential(
27
+ (0): Linear(in_features=5120, out_features=5120, bias=True)
28
+ (1): GELU(approximate='none')
29
+ (2): Linear(in_features=5120, out_features=2048, bias=True)
30
+ )
31
+ )
32
+ )
33
+ (model): Qwen2_5_VLModel(
34
+ (embed_tokens): Embedding(151936, 2048)
35
+ (layers): ModuleList(
36
+ (0-7): 8 x Qwen2_5_VLDecoderLayer(
37
+ (self_attn): Qwen2_5_VLSdpaAttention(
38
+ (q_proj): Linear(in_features=2048, out_features=2048, bias=True)
39
+ (k_proj): Linear(in_features=2048, out_features=256, bias=True)
40
+ (v_proj): Linear(in_features=2048, out_features=256, bias=True)
41
+ (o_proj): Linear(in_features=2048, out_features=2048, bias=False)
42
+ (rotary_emb): Qwen2_5_VLRotaryEmbedding()
43
+ )
44
+ (mlp): Qwen2MLP(
45
+ (gate_proj): Linear(in_features=2048, out_features=11008, bias=False)
46
+ (up_proj): Linear(in_features=2048, out_features=11008, bias=False)
47
+ (down_proj): Linear(in_features=11008, out_features=2048, bias=False)
48
+ (act_fn): SiLU()
49
+ )
50
+ (input_layernorm): Qwen2RMSNorm((0,), eps=1e-06)
51
+ (post_attention_layernorm): Qwen2RMSNorm((0,), eps=1e-06)
52
+ )
53
+ )
54
+ (norm): Qwen2RMSNorm((0,), eps=1e-06)
55
+ (rotary_emb): Qwen2_5_VLRotaryEmbedding()
56
+ )
57
+ (lm_head): Linear(in_features=2048, out_features=151936, bias=False)
58
+ )
59
+ )