mindstudio-probe 1.0.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 (228) hide show
  1. mindstudio_probe-1.0.1.dist-info/LICENSE +201 -0
  2. mindstudio_probe-1.0.1.dist-info/METADATA +30 -0
  3. mindstudio_probe-1.0.1.dist-info/RECORD +228 -0
  4. mindstudio_probe-1.0.1.dist-info/WHEEL +5 -0
  5. mindstudio_probe-1.0.1.dist-info/entry_points.txt +2 -0
  6. mindstudio_probe-1.0.1.dist-info/top_level.txt +1 -0
  7. msprobe/README.md +182 -0
  8. msprobe/__init__.py +0 -0
  9. msprobe/config/README.md +397 -0
  10. msprobe/config/config.json +28 -0
  11. msprobe/config/img/free_benchmark.png +0 -0
  12. msprobe/core/common/const.py +241 -0
  13. msprobe/core/common/exceptions.py +88 -0
  14. msprobe/core/common/file_check.py +265 -0
  15. msprobe/core/common/log.py +55 -0
  16. msprobe/core/common/utils.py +516 -0
  17. msprobe/core/common_config.py +58 -0
  18. msprobe/core/data_dump/data_collector.py +140 -0
  19. msprobe/core/data_dump/data_processor/base.py +245 -0
  20. msprobe/core/data_dump/data_processor/factory.py +61 -0
  21. msprobe/core/data_dump/data_processor/pytorch_processor.py +346 -0
  22. msprobe/core/data_dump/json_writer.py +116 -0
  23. msprobe/core/data_dump/scope.py +178 -0
  24. msprobe/mindspore/__init__.py +1 -0
  25. msprobe/mindspore/debugger/__init__.py +0 -0
  26. msprobe/mindspore/debugger/debugger_config.py +51 -0
  27. msprobe/mindspore/debugger/precision_debugger.py +32 -0
  28. msprobe/mindspore/doc/dump.md +65 -0
  29. msprobe/mindspore/dump/__init__.py +0 -0
  30. msprobe/mindspore/dump/api_kbk_dump.py +55 -0
  31. msprobe/mindspore/dump/dump_tool_factory.py +38 -0
  32. msprobe/mindspore/dump/kernel_graph_dump.py +60 -0
  33. msprobe/mindspore/ms_config.py +78 -0
  34. msprobe/mindspore/overflow_check/__init__.py +0 -0
  35. msprobe/mindspore/overflow_check/kernel_graph_overflow_check.py +45 -0
  36. msprobe/mindspore/overflow_check/overflow_check_tool_factory.py +32 -0
  37. msprobe/mindspore/task_handler_factory.py +21 -0
  38. msprobe/msprobe.py +67 -0
  39. msprobe/pytorch/__init__.py +4 -0
  40. msprobe/pytorch/advisor/advisor.py +124 -0
  41. msprobe/pytorch/advisor/advisor_const.py +59 -0
  42. msprobe/pytorch/advisor/advisor_result.py +58 -0
  43. msprobe/pytorch/api_accuracy_checker/.keep +0 -0
  44. msprobe/pytorch/api_accuracy_checker/__init__.py +0 -0
  45. msprobe/pytorch/api_accuracy_checker/common/.keep +0 -0
  46. msprobe/pytorch/api_accuracy_checker/common/__init__.py +0 -0
  47. msprobe/pytorch/api_accuracy_checker/common/config.py +50 -0
  48. msprobe/pytorch/api_accuracy_checker/common/utils.py +224 -0
  49. msprobe/pytorch/api_accuracy_checker/compare/__init__.py +0 -0
  50. msprobe/pytorch/api_accuracy_checker/compare/algorithm.py +216 -0
  51. msprobe/pytorch/api_accuracy_checker/compare/api_precision_compare.py +545 -0
  52. msprobe/pytorch/api_accuracy_checker/compare/api_precision_standard.yaml +133 -0
  53. msprobe/pytorch/api_accuracy_checker/compare/api_precision_threshold.yaml +390 -0
  54. msprobe/pytorch/api_accuracy_checker/compare/compare.py +345 -0
  55. msprobe/pytorch/api_accuracy_checker/compare/compare_column.py +74 -0
  56. msprobe/pytorch/api_accuracy_checker/compare/compare_utils.py +249 -0
  57. msprobe/pytorch/api_accuracy_checker/config.yaml +4 -0
  58. msprobe/pytorch/api_accuracy_checker/run_ut/.keep +0 -0
  59. msprobe/pytorch/api_accuracy_checker/run_ut/__init__.py +0 -0
  60. msprobe/pytorch/api_accuracy_checker/run_ut/data_generate.py +328 -0
  61. msprobe/pytorch/api_accuracy_checker/run_ut/multi_run_ut.py +203 -0
  62. msprobe/pytorch/api_accuracy_checker/run_ut/run_overflow_check.py +127 -0
  63. msprobe/pytorch/api_accuracy_checker/run_ut/run_ut.py +493 -0
  64. msprobe/pytorch/api_accuracy_checker/run_ut/run_ut_utils.py +7 -0
  65. msprobe/pytorch/api_accuracy_checker/run_ut/torch_ut_setting.json +5 -0
  66. msprobe/pytorch/common/__init__.py +2 -0
  67. msprobe/pytorch/common/compare_script.template +14 -0
  68. msprobe/pytorch/common/log.py +32 -0
  69. msprobe/pytorch/common/parse_json.py +37 -0
  70. msprobe/pytorch/common/utils.py +224 -0
  71. msprobe/pytorch/compare/acc_compare.py +1024 -0
  72. msprobe/pytorch/compare/distributed_compare.py +111 -0
  73. msprobe/pytorch/compare/highlight.py +100 -0
  74. msprobe/pytorch/compare/mapping.yaml +607 -0
  75. msprobe/pytorch/compare/match.py +36 -0
  76. msprobe/pytorch/compare/npy_compare.py +244 -0
  77. msprobe/pytorch/debugger/__init__.py +0 -0
  78. msprobe/pytorch/debugger/debugger_config.py +86 -0
  79. msprobe/pytorch/debugger/precision_debugger.py +95 -0
  80. msprobe/pytorch/doc/FAQ.md +193 -0
  81. msprobe/pytorch/doc/api_accuracy_checker.md +269 -0
  82. msprobe/pytorch/doc/atat/321/207/342/226/223/342/225/233/321/205/342/225/221/320/266/321/205/342/225/226/320/265/321/205/320/225/342/225/226/321/206/320/245/342/226/221/321/206/320/235/320/276dump/321/206/320/260/320/227/321/205/320/227/320/226/321/206/320/220/320/267/321/210/320/223/342/225/234/321/205/320/257/342/225/221/321/207/342/225/221/342/224/220/321/206/320/232/320/265/321/205/320/241/320/232.md +182 -0
  83. msprobe/pytorch/doc/dump.md +207 -0
  84. msprobe/pytorch/doc/img/BLOOM-7B_1.png +0 -0
  85. msprobe/pytorch/doc/img/BLOOM-7B_2.png +0 -0
  86. msprobe/pytorch/doc/img/BLOOM-7B_3.png +0 -0
  87. msprobe/pytorch/doc/img/BLOOM-7B_4.png +0 -0
  88. msprobe/pytorch/doc/img/GPT-3_1.png +0 -0
  89. msprobe/pytorch/doc/img/GPT-3_2.png +0 -0
  90. msprobe/pytorch/doc/img/GPT-3_3.png +0 -0
  91. msprobe/pytorch/doc/img/GPT-3_4.png +0 -0
  92. msprobe/pytorch/doc/img/GPT-3_5.png +0 -0
  93. msprobe/pytorch/doc/img/GPT-3_6.png +0 -0
  94. msprobe/pytorch/doc/img/GPT-3_7.png +0 -0
  95. msprobe/pytorch/doc/img/GPT-3_8.png +0 -0
  96. msprobe/pytorch/doc/img/YOLOV5S_1.png +0 -0
  97. msprobe/pytorch/doc/img/YOLOV5S_2.png +0 -0
  98. msprobe/pytorch/doc/img/accuracy_checking_details.png +0 -0
  99. msprobe/pytorch/doc/img/accuracy_checking_result.png +0 -0
  100. msprobe/pytorch/doc/img/api_precision_compare_details.png +0 -0
  101. msprobe/pytorch/doc/img/api_precision_compare_result.png +0 -0
  102. msprobe/pytorch/doc/img/auto_analyze_log.png +0 -0
  103. msprobe/pytorch/doc/img/compare_result_pkl.png +0 -0
  104. msprobe/pytorch/doc/img/compare_result_pkl_md5.png.png +0 -0
  105. msprobe/pytorch/doc/img/cpu_info.png +0 -0
  106. msprobe/pytorch/doc/img/module_compare.png +0 -0
  107. msprobe/pytorch/doc/parse_tool.md +286 -0
  108. msprobe/pytorch/doc/ptdbg_ascend_compare.md +176 -0
  109. msprobe/pytorch/doc/ptdbg_ascend_overview.md +68 -0
  110. msprobe/pytorch/doc/ptdbg_ascend_quickstart.md +381 -0
  111. msprobe/pytorch/doc/run_overflow_check.md +25 -0
  112. msprobe/pytorch/doc//321/205/320/254/320/270/321/207/342/225/221/342/224/220/321/207/342/226/223/342/225/233/321/205/342/225/221/320/266/321/206/320/277/320/244/321/205/320/277/342/225/243.md +90 -0
  113. msprobe/pytorch/free_benchmark/__init__.py +8 -0
  114. msprobe/pytorch/free_benchmark/common/__init__.py +0 -0
  115. msprobe/pytorch/free_benchmark/common/constant.py +67 -0
  116. msprobe/pytorch/free_benchmark/common/counter.py +72 -0
  117. msprobe/pytorch/free_benchmark/common/enums.py +37 -0
  118. msprobe/pytorch/free_benchmark/common/params.py +129 -0
  119. msprobe/pytorch/free_benchmark/common/utils.py +98 -0
  120. msprobe/pytorch/free_benchmark/compare/grad_saver.py +183 -0
  121. msprobe/pytorch/free_benchmark/compare/single_benchmark.py +104 -0
  122. msprobe/pytorch/free_benchmark/main.py +102 -0
  123. msprobe/pytorch/free_benchmark/perturbed_layers/__init__.py +0 -0
  124. msprobe/pytorch/free_benchmark/perturbed_layers/base_layer.py +13 -0
  125. msprobe/pytorch/free_benchmark/perturbed_layers/layer_factory.py +41 -0
  126. msprobe/pytorch/free_benchmark/perturbed_layers/npu/__init__.py +0 -0
  127. msprobe/pytorch/free_benchmark/perturbed_layers/npu/add_noise.py +90 -0
  128. msprobe/pytorch/free_benchmark/perturbed_layers/npu/bit_noise.py +104 -0
  129. msprobe/pytorch/free_benchmark/perturbed_layers/npu/change_value.py +63 -0
  130. msprobe/pytorch/free_benchmark/perturbed_layers/npu/improve_precision.py +68 -0
  131. msprobe/pytorch/free_benchmark/perturbed_layers/npu/no_change.py +28 -0
  132. msprobe/pytorch/free_benchmark/perturbed_layers/npu/npu_base_layser.py +45 -0
  133. msprobe/pytorch/free_benchmark/perturbed_layers/run_cpu.py +19 -0
  134. msprobe/pytorch/free_benchmark/result_handlers/__init__.py +0 -0
  135. msprobe/pytorch/free_benchmark/result_handlers/base_handler.py +203 -0
  136. msprobe/pytorch/free_benchmark/result_handlers/check_handler.py +39 -0
  137. msprobe/pytorch/free_benchmark/result_handlers/fix_handler.py +24 -0
  138. msprobe/pytorch/free_benchmark/result_handlers/handler_factory.py +31 -0
  139. msprobe/pytorch/free_benchmark/result_handlers/preheat_handler.py +170 -0
  140. msprobe/pytorch/functional/__init__.py +0 -0
  141. msprobe/pytorch/functional/data_processor.py +0 -0
  142. msprobe/pytorch/functional/dump_module.py +39 -0
  143. msprobe/pytorch/hook_module/__init__.py +1 -0
  144. msprobe/pytorch/hook_module/api_registry.py +161 -0
  145. msprobe/pytorch/hook_module/hook_module.py +109 -0
  146. msprobe/pytorch/hook_module/support_wrap_ops.yaml +1876 -0
  147. msprobe/pytorch/hook_module/utils.py +29 -0
  148. msprobe/pytorch/hook_module/wrap_aten.py +100 -0
  149. msprobe/pytorch/hook_module/wrap_distributed.py +75 -0
  150. msprobe/pytorch/hook_module/wrap_functional.py +108 -0
  151. msprobe/pytorch/hook_module/wrap_npu_custom.py +73 -0
  152. msprobe/pytorch/hook_module/wrap_tensor.py +72 -0
  153. msprobe/pytorch/hook_module/wrap_torch.py +88 -0
  154. msprobe/pytorch/hook_module/wrap_vf.py +64 -0
  155. msprobe/pytorch/module_processer.py +98 -0
  156. msprobe/pytorch/online_dispatch/__init__.py +20 -0
  157. msprobe/pytorch/online_dispatch/compare.py +236 -0
  158. msprobe/pytorch/online_dispatch/dispatch.py +274 -0
  159. msprobe/pytorch/online_dispatch/dump_compare.py +186 -0
  160. msprobe/pytorch/online_dispatch/single_compare.py +391 -0
  161. msprobe/pytorch/online_dispatch/torch_ops_config.yaml +50 -0
  162. msprobe/pytorch/online_dispatch/utils.py +187 -0
  163. msprobe/pytorch/parse.py +4 -0
  164. msprobe/pytorch/parse_tool/__init__.py +0 -0
  165. msprobe/pytorch/parse_tool/cli.py +32 -0
  166. msprobe/pytorch/parse_tool/lib/__init__.py +0 -0
  167. msprobe/pytorch/parse_tool/lib/compare.py +259 -0
  168. msprobe/pytorch/parse_tool/lib/config.py +51 -0
  169. msprobe/pytorch/parse_tool/lib/file_desc.py +31 -0
  170. msprobe/pytorch/parse_tool/lib/interactive_cli.py +102 -0
  171. msprobe/pytorch/parse_tool/lib/parse_exception.py +54 -0
  172. msprobe/pytorch/parse_tool/lib/parse_tool.py +158 -0
  173. msprobe/pytorch/parse_tool/lib/utils.py +367 -0
  174. msprobe/pytorch/parse_tool/lib/visualization.py +90 -0
  175. msprobe/pytorch/pt_config.py +93 -0
  176. msprobe/pytorch/service.py +167 -0
  177. msprobe/test/core_ut/common/test_utils.py +345 -0
  178. msprobe/test/core_ut/data_dump/test_data_collector.py +47 -0
  179. msprobe/test/core_ut/data_dump/test_json_writer.py +183 -0
  180. msprobe/test/core_ut/data_dump/test_scope.py +151 -0
  181. msprobe/test/core_ut/test_common_config.py +152 -0
  182. msprobe/test/core_ut/test_file_check.py +218 -0
  183. msprobe/test/core_ut/test_log.py +109 -0
  184. msprobe/test/mindspore_ut/test_api_kbk_dump.py +51 -0
  185. msprobe/test/mindspore_ut/test_debugger_config.py +42 -0
  186. msprobe/test/mindspore_ut/test_dump_tool_factory.py +51 -0
  187. msprobe/test/mindspore_ut/test_kernel_graph_dump.py +66 -0
  188. msprobe/test/mindspore_ut/test_kernel_graph_overflow_check.py +63 -0
  189. msprobe/test/mindspore_ut/test_ms_config.py +69 -0
  190. msprobe/test/mindspore_ut/test_overflow_check_tool_factory.py +51 -0
  191. msprobe/test/mindspore_ut/test_precision_debugger.py +56 -0
  192. msprobe/test/mindspore_ut/test_task_handler_factory.py +58 -0
  193. msprobe/test/pytorch_ut/advisor/test_advisor.py +83 -0
  194. msprobe/test/pytorch_ut/api_accuracy_checker/common/test_common_utils.py +108 -0
  195. msprobe/test/pytorch_ut/api_accuracy_checker/common/test_config.py +39 -0
  196. msprobe/test/pytorch_ut/api_accuracy_checker/compare/test_algorithm.py +112 -0
  197. msprobe/test/pytorch_ut/api_accuracy_checker/compare/test_api_precision_compare.py +77 -0
  198. msprobe/test/pytorch_ut/api_accuracy_checker/compare/test_compare.py +125 -0
  199. msprobe/test/pytorch_ut/api_accuracy_checker/compare/test_compare_column.py +10 -0
  200. msprobe/test/pytorch_ut/api_accuracy_checker/compare/test_compare_utils.py +43 -0
  201. msprobe/test/pytorch_ut/api_accuracy_checker/run_ut/dump.json +179 -0
  202. msprobe/test/pytorch_ut/api_accuracy_checker/run_ut/forward.json +63 -0
  203. msprobe/test/pytorch_ut/api_accuracy_checker/run_ut/test_data_generate.py +99 -0
  204. msprobe/test/pytorch_ut/api_accuracy_checker/run_ut/test_multi_run_ut.py +115 -0
  205. msprobe/test/pytorch_ut/api_accuracy_checker/run_ut/test_run_ut.py +72 -0
  206. msprobe/test/pytorch_ut/compare/test_acc_compare.py +17 -0
  207. msprobe/test/pytorch_ut/free_benchmark/perturbed_layers/test_perturbed_layser.py +105 -0
  208. msprobe/test/pytorch_ut/free_benchmark/result_handlers/test_result_handler.py +121 -0
  209. msprobe/test/pytorch_ut/free_benchmark/test_main.py +101 -0
  210. msprobe/test/pytorch_ut/functional/test_dump_module.py +15 -0
  211. msprobe/test/pytorch_ut/hook_module/test_api_registry.py +130 -0
  212. msprobe/test/pytorch_ut/hook_module/test_hook_module.py +42 -0
  213. msprobe/test/pytorch_ut/hook_module/test_wrap_aten.py +65 -0
  214. msprobe/test/pytorch_ut/hook_module/test_wrap_distributed.py +35 -0
  215. msprobe/test/pytorch_ut/hook_module/test_wrap_functional.py +20 -0
  216. msprobe/test/pytorch_ut/hook_module/test_wrap_tensor.py +35 -0
  217. msprobe/test/pytorch_ut/hook_module/test_wrap_torch.py +43 -0
  218. msprobe/test/pytorch_ut/hook_module/test_wrap_vf.py +11 -0
  219. msprobe/test/pytorch_ut/test_pt_config.py +69 -0
  220. msprobe/test/pytorch_ut/test_service.py +59 -0
  221. msprobe/test/resources/advisor.txt +3 -0
  222. msprobe/test/resources/compare_result_20230703104808.csv +9 -0
  223. msprobe/test/resources/compare_result_without_accuracy.csv +9 -0
  224. msprobe/test/resources/config.yaml +3 -0
  225. msprobe/test/resources/npu_test.pkl +8 -0
  226. msprobe/test/run_test.sh +30 -0
  227. msprobe/test/run_ut.py +58 -0
  228. msprobe/test/test_module_processer.py +64 -0
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ # Copyright (C) 2024-2024. Huawei Technologies Co., Ltd. All rights reserved.
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ """
17
+
18
+ import os
19
+ import yaml
20
+
21
+ from msprobe.core.common.file_check import FileOpen
22
+
23
+ cur_path = os.path.dirname(os.path.realpath(__file__))
24
+ yaml_path = os.path.join(cur_path, "support_wrap_ops.yaml")
25
+ with FileOpen(yaml_path, 'r') as f:
26
+ Ops = yaml.safe_load(f)
27
+ WrapFunctionalOps = Ops.get('functional')
28
+ WrapTensorOps = Ops.get('tensor')
29
+ WrapTorchOps = Ops.get('torch')
@@ -0,0 +1,100 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ # Copyright (C) 2023-2023. Huawei Technologies Co., Ltd. All rights reserved.
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ """
17
+
18
+ import os
19
+ import torch
20
+
21
+ import yaml
22
+
23
+ from msprobe.pytorch.hook_module.hook_module import HOOKModule
24
+ from msprobe.pytorch.common.utils import torch_device_guard
25
+ from msprobe.core.common.const import Const
26
+ from msprobe.core.common.file_check import FileOpen
27
+
28
+
29
+ cur_path = os.path.dirname(os.path.realpath(__file__))
30
+ yaml_path = os.path.join(cur_path, "support_wrap_ops.yaml")
31
+ with FileOpen(yaml_path, 'r') as f:
32
+ WrapAtenOps = yaml.safe_load(f).get('aten')
33
+
34
+
35
+ aten_func = {}
36
+ for f in dir(torch.ops.aten):
37
+ aten_func[f] = getattr(torch.ops.aten, f)
38
+
39
+
40
+ def get_aten_ops():
41
+ global WrapAtenOps
42
+ _all_aten_ops = dir(torch.ops.aten)
43
+ return set(WrapAtenOps) & set(_all_aten_ops)
44
+
45
+
46
+ class HOOKAtenOP(object):
47
+ pass
48
+
49
+
50
+ class AtenOPTemplate(HOOKModule):
51
+ def __init__(self, op, hook):
52
+ if isinstance(op, torch._ops.OpOverloadPacket):
53
+ op_name_ = op._qualified_op_name.split("::")[-1]
54
+ else:
55
+ op_name_ = op.name().split("::")[-1]
56
+ overload_name = op._overloadname
57
+ if not '.' + overload_name in op_name_:
58
+ op_name_ = op_name_ + '.' + overload_name
59
+ self.op = op
60
+ self.prefix_op_name_ = "Aten" + Const.SEP + str(op_name_) + Const.SEP
61
+ super().__init__(hook)
62
+
63
+ @torch_device_guard
64
+ def forward(self, *args, **kwargs):
65
+ return self.op(*args, **kwargs)
66
+
67
+
68
+ class AtenOPPacketTemplate():
69
+ def __init__(self, opPacket, hook):
70
+ self.opPacket = opPacket
71
+ self.hook = hook
72
+
73
+ def __getattr__(self, key):
74
+ try:
75
+ attr = getattr(self.opPacket, key)
76
+ except AttributeError as e:
77
+ raise AttributeError(f"AtenOPPacketTemplate or OpOverloadPacket does not have attribute '{key}'.") from e
78
+ if isinstance(attr, torch._ops.OpOverload):
79
+ return AtenOPTemplate(attr, self.hook)
80
+ else:
81
+ return attr
82
+
83
+ @torch_device_guard
84
+ def __call__(self, *args, **kwargs):
85
+ return AtenOPTemplate(self.opPacket, self.hook)(*args, **kwargs)
86
+
87
+ def overloads(self):
88
+ return self.opPacket.overloads()
89
+
90
+
91
+ def wrap_aten_op(op, hook):
92
+ return AtenOPPacketTemplate(op, hook)
93
+
94
+
95
+ def wrap_aten_ops_and_bind(hook):
96
+ _aten_ops = get_aten_ops()
97
+ for op_name in _aten_ops:
98
+ if not isinstance(aten_func.get(op_name), torch._ops.OpOverloadPacket):
99
+ continue
100
+ setattr(HOOKAtenOP, "wrap_" + str(op_name), wrap_aten_op(aten_func.get(op_name), hook))
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ # Copyright (C) 2022-2023. Huawei Technologies Co., Ltd. All rights reserved.
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ """
17
+
18
+ import os
19
+ from functools import wraps
20
+ import torch.distributed as dist
21
+ import yaml
22
+
23
+ from msprobe.pytorch.hook_module.hook_module import HOOKModule
24
+ from msprobe.pytorch.common.utils import torch_device_guard
25
+ from msprobe.core.common.const import Const
26
+ from msprobe.core.common.file_check import FileOpen
27
+
28
+
29
+ cur_path = os.path.dirname(os.path.realpath(__file__))
30
+ yaml_path = os.path.join(cur_path, "support_wrap_ops.yaml")
31
+ with FileOpen(yaml_path, 'r') as f:
32
+ WrapDistributedOps = yaml.safe_load(f).get('distributed')
33
+
34
+
35
+ distributed_func = {}
36
+ for f in dir(dist):
37
+ distributed_func[f] = getattr(dist, f)
38
+
39
+
40
+ def get_distributed_ops():
41
+ global WrapDistributedOps
42
+ _all_distributed_ops = dir(dist)
43
+ return set(WrapDistributedOps) & set(_all_distributed_ops)
44
+
45
+
46
+ class HOOKDistributedOP(object):
47
+ pass
48
+
49
+
50
+ class DistributedOPTemplate(HOOKModule):
51
+ def __init__(self, op_name, build_hook):
52
+ self.op_name_ = op_name
53
+ self.prefix_op_name_ = "Distributed" + Const.SEP + str(op_name) + Const.SEP
54
+ super().__init__(build_hook)
55
+ if not self.stop_hook and self.op_name_ in Const.INPLACE_LIST:
56
+ self.op_is_inplace = True
57
+
58
+ @torch_device_guard
59
+ def forward(self, *args, **kwargs):
60
+ return distributed_func.get(self.op_name_)(*args, **kwargs)
61
+
62
+
63
+ def wrap_distributed_op(op_name, hook):
64
+ @wraps(DistributedOPTemplate)
65
+ def distributed_op_template(*args, **kwargs):
66
+ return DistributedOPTemplate(op_name, hook)(*args, **kwargs)
67
+
68
+ distributed_op_template.__name__ = op_name
69
+ return distributed_op_template
70
+
71
+
72
+ def wrap_distributed_ops_and_bind(hook):
73
+ _distributed_ops = get_distributed_ops()
74
+ for op_name in _distributed_ops:
75
+ setattr(HOOKDistributedOP, "wrap_" + str(op_name), wrap_distributed_op(op_name, hook))
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ # Copyright (C) 2019-2020. Huawei Technologies Co., Ltd. All rights reserved.
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ """
17
+
18
+ import os
19
+
20
+ import torch
21
+ import yaml
22
+
23
+ from msprobe.pytorch.hook_module.hook_module import HOOKModule
24
+ from msprobe.pytorch.common.utils import torch_device_guard
25
+ from msprobe.core.common.const import Const
26
+ from msprobe.pytorch.common.log import logger
27
+ from msprobe.core.common.file_check import FileOpen
28
+
29
+
30
+ def remove_dropout():
31
+ if torch.__version__ > "1.8":
32
+ logger.info_on_rank_0("For precision comparison, the probability p in the dropout method is set to 0.")
33
+ import torch.nn.functional as F
34
+ from torch import _VF
35
+ from torch.overrides import has_torch_function_unary, handle_torch_function
36
+
37
+ def function_dropout(input: torch.Tensor, p: float = 0.5, training: bool = True,
38
+ inplace: bool = False) -> torch.Tensor:
39
+ if has_torch_function_unary(input):
40
+ return handle_torch_function(function_dropout, (input,), input, p=0., training=training, inplace=inplace)
41
+ if p < 0.0 or p > 1.0:
42
+ raise ValueError("dropout probability has to be between 0 and 1, " "but got {}".format(p))
43
+ return _VF.dropout_(input, 0., training) if inplace else _VF.dropout(input, 0., training)
44
+
45
+
46
+ def function_dropout2d(input: torch.Tensor, p: float = 0.5, training: bool = True,
47
+ inplace: bool = False) -> torch.Tensor:
48
+ if has_torch_function_unary(input):
49
+ return handle_torch_function(function_dropout2d, (input,), input, p=0., training=training, inplace=inplace)
50
+ if p < 0.0 or p > 1.0:
51
+ raise ValueError("dropout probability has to be between 0 and 1, " "but got {}".format(p))
52
+ return _VF.feature_dropout_(input, 0., training) if inplace else _VF.feature_dropout(input, 0., training)
53
+
54
+
55
+ def function_dropout3d(input: torch.Tensor, p: float = 0.5, training: bool = True,
56
+ inplace: bool = False) -> torch.Tensor:
57
+ if has_torch_function_unary(input):
58
+ return handle_torch_function(function_dropout3d, (input,), input, p=0., training=training, inplace=inplace)
59
+ if p < 0.0 or p > 1.0:
60
+ raise ValueError("dropout probability has to be between 0 and 1, " "but got {}".format(p))
61
+ return _VF.feature_dropout_(input, 0., training) if inplace else _VF.feature_dropout(input, 0., training)
62
+
63
+ F.dropout = function_dropout
64
+ F.dropout2d = function_dropout2d
65
+ F.dropout3d = function_dropout3d
66
+
67
+ cur_path = os.path.dirname(os.path.realpath(__file__))
68
+ yaml_path = os.path.join(cur_path, "support_wrap_ops.yaml")
69
+ with FileOpen(yaml_path, 'r') as f:
70
+ WrapFunctionalOps = yaml.safe_load(f).get('functional')
71
+
72
+
73
+ def get_functional_ops():
74
+ global WrapFunctionalOps
75
+ _all_functional_ops = dir(torch.nn.functional)
76
+ return set(WrapFunctionalOps) & set(_all_functional_ops)
77
+
78
+
79
+ TorchFunctions = {func: getattr(torch.nn.functional, func) for func in get_functional_ops()}
80
+
81
+
82
+ class HOOKFunctionalOP(object):
83
+ pass
84
+
85
+
86
+ class FunctionalOPTemplate(HOOKModule):
87
+ def __init__(self, op_name, hook, need_hook=True):
88
+ self.op_name_ = op_name
89
+ self.prefix_op_name_ = "Functional" + Const.SEP + str(op_name) + Const.SEP
90
+ if need_hook:
91
+ super().__init__(hook)
92
+
93
+ @torch_device_guard
94
+ def forward(self, *args, **kwargs):
95
+ return TorchFunctions[str(self.op_name_)](*args, **kwargs)
96
+
97
+
98
+ def wrap_functional_op(op_name, hook):
99
+ def functional_op_template(*args, **kwargs):
100
+ return FunctionalOPTemplate(op_name, hook)(*args, **kwargs)
101
+
102
+ return functional_op_template
103
+
104
+
105
+ def wrap_functional_ops_and_bind(hook):
106
+ _functional_ops = get_functional_ops()
107
+ for op_name in _functional_ops:
108
+ setattr(HOOKFunctionalOP, "wrap_" + op_name, wrap_functional_op(op_name, hook))
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ # Copyright (C) 2019-2020. Huawei Technologies Co., Ltd. All rights reserved.
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ """
17
+
18
+ import os
19
+ import torch
20
+ import torch_npu
21
+ import yaml
22
+
23
+ from msprobe.pytorch.hook_module.hook_module import HOOKModule
24
+ from msprobe.pytorch.common.utils import torch_device_guard, torch_without_guard_version
25
+ from msprobe.core.common.const import Const
26
+ from msprobe.core.common.file_check import FileOpen
27
+
28
+ cur_path = os.path.dirname(os.path.realpath(__file__))
29
+ yaml_path = os.path.join(cur_path, "support_wrap_ops.yaml")
30
+ with FileOpen(yaml_path, 'r') as f:
31
+ WrapNpuOps = yaml.safe_load(f).get('torch_npu')
32
+
33
+
34
+ def get_npu_ops():
35
+ global WrapNpuOps
36
+ if torch_without_guard_version:
37
+ _npu_ops = dir(torch.ops.npu)
38
+ else:
39
+ _npu_ops = dir(torch_npu._C._VariableFunctionsClass)
40
+ return set(WrapNpuOps) & set(_npu_ops)
41
+
42
+
43
+ class HOOKNpuOP(object):
44
+ pass
45
+
46
+
47
+ class NpuOPTemplate(HOOKModule):
48
+
49
+ def __init__(self, op_name, hook):
50
+ self.op_name_ = op_name
51
+ self.prefix_op_name_ = "NPU" + Const.SEP + str(op_name) + Const.SEP
52
+ super().__init__(hook)
53
+
54
+ @torch_device_guard
55
+ def forward(self, *args, **kwargs):
56
+ if torch_without_guard_version:
57
+ return getattr(torch.ops.npu, str(self.op_name_))(*args, **kwargs)
58
+ else:
59
+ return getattr(torch_npu._C._VariableFunctionsClass, str(self.op_name_))(*args, **kwargs)
60
+
61
+
62
+ def wrap_npu_op(op_name, hook):
63
+
64
+ def npu_op_template(*args, **kwargs):
65
+ return NpuOPTemplate(op_name, hook)(*args, **kwargs)
66
+
67
+ return npu_op_template
68
+
69
+
70
+ def wrap_npu_ops_and_bind(hook):
71
+ _npu_ops = get_npu_ops()
72
+ for op_name in _npu_ops:
73
+ setattr(HOOKNpuOP, "wrap_" + str(op_name), wrap_npu_op(op_name, hook))
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ # Copyright (C) 2019-2020. Huawei Technologies Co., Ltd. All rights reserved.
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ """
17
+
18
+ import os
19
+
20
+ import torch
21
+ import yaml
22
+
23
+ from msprobe.pytorch.hook_module.hook_module import HOOKModule
24
+ from msprobe.pytorch.common.utils import torch_device_guard, parameter_adapter
25
+ from msprobe.core.common.const import Const
26
+ from msprobe.core.common.file_check import FileOpen
27
+
28
+ cur_path = os.path.dirname(os.path.realpath(__file__))
29
+ yaml_path = os.path.join(cur_path, "support_wrap_ops.yaml")
30
+ with FileOpen(yaml_path, 'r') as f:
31
+ WrapTensorOps = yaml.safe_load(f).get('tensor')
32
+
33
+
34
+ def get_tensor_ops():
35
+ global WrapTensorOps
36
+ _tensor_ops = dir(torch.Tensor)
37
+ return set(WrapTensorOps) & set(_tensor_ops)
38
+
39
+
40
+ TensorOps = {op: getattr(torch.Tensor, op) for op in get_tensor_ops()}
41
+
42
+
43
+ class HOOKTensor(object):
44
+ pass
45
+
46
+
47
+ class TensorOPTemplate(HOOKModule):
48
+
49
+ def __init__(self, op_name, hook, need_hook=True):
50
+ self.op_name_ = op_name
51
+ self.prefix_op_name_ = "Tensor" + Const.SEP + str(op_name) + Const.SEP
52
+ if need_hook:
53
+ super().__init__(hook)
54
+
55
+ @torch_device_guard
56
+ @parameter_adapter
57
+ def forward(self, *args, **kwargs):
58
+ return TensorOps[str(self.op_name_)](*args, **kwargs)
59
+
60
+
61
+ def wrap_tensor_op(op_name, hook):
62
+
63
+ def tensor_op_template(*args, **kwargs):
64
+ return TensorOPTemplate(op_name, hook)(*args, **kwargs)
65
+
66
+ return tensor_op_template
67
+
68
+
69
+ def wrap_tensor_ops_and_bind(hook):
70
+ _tensor_ops = get_tensor_ops()
71
+ for op_name in _tensor_ops:
72
+ setattr(HOOKTensor, "wrap_" + str(op_name), wrap_tensor_op(op_name, hook))
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ # Copyright (C) 2019-2020. Huawei Technologies Co., Ltd. All rights reserved.
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ """
17
+
18
+ import os
19
+
20
+ import torch
21
+ import yaml
22
+
23
+ from msprobe.pytorch.hook_module.hook_module import HOOKModule
24
+ from msprobe.pytorch.common.utils import torch_device_guard
25
+ from msprobe.core.common.const import Const
26
+ from msprobe.core.common.file_check import FileOpen
27
+
28
+ cur_path = os.path.dirname(os.path.realpath(__file__))
29
+ yaml_path = os.path.join(cur_path, "support_wrap_ops.yaml")
30
+ with FileOpen(yaml_path, 'r') as f:
31
+ WrapTorchOps = yaml.safe_load(f).get('torch')
32
+
33
+
34
+ def get_torch_ops():
35
+ global WrapTorchOps
36
+ _torch_ops = []
37
+ for operation in WrapTorchOps:
38
+ if '.' in operation:
39
+ operation_sub_module_name, operation_sub_op = operation.rsplit('.', 1)
40
+ operation_sub_module = getattr(torch, operation_sub_module_name)
41
+ if operation_sub_op in dir(operation_sub_module):
42
+ _torch_ops.append(operation)
43
+ else:
44
+ if hasattr(torch, operation):
45
+ _torch_ops.append(operation)
46
+ return set(_torch_ops)
47
+
48
+
49
+ TorchOps = {}
50
+ for op in get_torch_ops():
51
+ if '.' in op:
52
+ sub_module_name, sub_op = op.rsplit('.', 1)
53
+ sub_module = getattr(torch, sub_module_name)
54
+ TorchOps[op] = getattr(sub_module, sub_op)
55
+ else:
56
+ TorchOps[op] = getattr(torch, op)
57
+
58
+
59
+
60
+ class HOOKTorchOP(object):
61
+ pass
62
+
63
+
64
+ class TorchOPTemplate(HOOKModule):
65
+
66
+ def __init__(self, op_name, hook, need_hook=True):
67
+ self.op_name_ = op_name
68
+ self.prefix_op_name_ = "Torch" + Const.SEP + str(op_name) + Const.SEP
69
+ if need_hook:
70
+ super().__init__(hook)
71
+
72
+ @torch_device_guard
73
+ def forward(self, *args, **kwargs):
74
+ return TorchOps[str(self.op_name_)](*args, **kwargs)
75
+
76
+
77
+ def wrap_torch_op(op_name, hook):
78
+
79
+ def torch_op_template(*args, **kwargs):
80
+ return TorchOPTemplate(op_name, hook)(*args, **kwargs)
81
+
82
+ return torch_op_template
83
+
84
+
85
+ def wrap_torch_ops_and_bind(hook):
86
+ _torch_ops = get_torch_ops()
87
+ for op_name in _torch_ops:
88
+ setattr(HOOKTorchOP, "wrap_" + op_name, wrap_torch_op(op_name, hook))
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ # Copyright (C) 2019-2020. Huawei Technologies Co., Ltd. All rights reserved.
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ """
17
+
18
+ import os
19
+
20
+ import torch
21
+ import yaml
22
+
23
+ from msprobe.pytorch.hook_module.hook_module import HOOKModule
24
+ from msprobe.core.common.file_check import FileOpen
25
+ from msprobe.pytorch.common.utils import torch_device_guard
26
+ from msprobe.core.common.const import Const
27
+
28
+ cur_path = os.path.dirname(os.path.realpath(__file__))
29
+ yaml_path = os.path.join(cur_path, "support_wrap_ops.yaml")
30
+ with FileOpen(yaml_path, 'r') as f:
31
+ WrapVfOps = yaml.safe_load(f).get('_VF')
32
+
33
+
34
+ def get_vf_ops():
35
+ global WrapVfOps
36
+ return WrapVfOps
37
+
38
+
39
+ class HOOKVfOP(object):
40
+ pass
41
+
42
+
43
+ class VfOPTemplate(HOOKModule):
44
+ def __init__(self, op_name, hook):
45
+ self.op_name_ = op_name
46
+ self.prefix_op_name_ = "VF" + Const.SEP + str(op_name) + Const.SEP
47
+ super().__init__(hook)
48
+
49
+ @torch_device_guard
50
+ def forward(self, *args, **kwargs):
51
+ return getattr(torch._C._VariableFunctionsClass, str(self.op_name_))(*args, **kwargs)
52
+
53
+
54
+ def wrap_vf_op(op_name, hook):
55
+ def vf_op_template(*args, **kwargs):
56
+ return VfOPTemplate(op_name, hook)(*args, **kwargs)
57
+
58
+ return vf_op_template
59
+
60
+
61
+ def wrap_vf_ops_and_bind(hook):
62
+ _vf_ops = get_vf_ops()
63
+ for op_name in _vf_ops:
64
+ setattr(HOOKVfOP, "wrap_" + op_name, wrap_vf_op(op_name, hook))