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,183 @@
1
+ import unittest
2
+ from msprobe.core.data_dump.json_writer import DataWriter
3
+
4
+ import os
5
+ import csv
6
+ from msprobe.core.common.file_check import FileOpen
7
+ from msprobe.core.common import utils
8
+ from pathlib import Path
9
+ import json
10
+
11
+ class TestDataWriter(unittest.TestCase):
12
+ def test_write_data_to_csv(self):
13
+ cur_path = os.path.dirname(os.path.realpath(__file__))
14
+ file_path = os.path.join(cur_path, "test.csv")
15
+
16
+ if os.path.exists(file_path):
17
+ utils.remove_path(file_path)
18
+
19
+ data = {"A":"1", "B":"2", "C":"3"}
20
+ result = data.values()
21
+ header = data.keys()
22
+ DataWriter.write_data_to_csv(result, header, file_path)
23
+ with FileOpen(file_path, "r") as f:
24
+ reader = csv.DictReader(f)
25
+ column_first = [row for row in reader][0]
26
+ self.assertEqual(data, column_first)
27
+
28
+
29
+
30
+
31
+ data = {"A":"4", "B":"5", "C":"6"}
32
+ result = data.values()
33
+ header = data.keys()
34
+ DataWriter.write_data_to_csv(result, header, file_path)
35
+ with FileOpen(file_path, "r") as f:
36
+ reader = csv.DictReader(f)
37
+ column_last = [row for row in reader][-1]
38
+ self.assertEqual(data, column_last)
39
+
40
+ utils.remove_path(file_path)
41
+
42
+ def test_initialize_json_file(self):
43
+ cur_path = os.path.dirname(os.path.realpath(__file__))
44
+ dump_tensor_data_dir = os.path.join(cur_path, "dump_tensor_data.json")
45
+ dump_file_path = os.path.join(cur_path, "dump_file.json")
46
+ stack_file_path = os.path.join(cur_path, "stack_file.json")
47
+ construct_file_path = os.path.join(cur_path, "construct_file.json")
48
+ if not os.path.exists(stack_file_path):
49
+ Path(stack_file_path).touch()
50
+ if not os.path.exists(construct_file_path):
51
+ Path(construct_file_path).touch()
52
+
53
+ test = DataWriter()
54
+ test.stack_file_path = stack_file_path
55
+ test.dump_file_path = dump_file_path
56
+ test.dump_tensor_data_dir = dump_tensor_data_dir
57
+ test.construct_file_path = construct_file_path
58
+
59
+ test.initialize_json_file()
60
+
61
+ with open(dump_file_path) as f:
62
+ load_data = json.load(f)
63
+ result = {"dump_data_dir": dump_tensor_data_dir, "data": {}}
64
+ self.assertEqual(result, load_data)
65
+ is_exist_1 = os.path.exists(test.stack_file_path)
66
+ self.assertTrue(is_exist_1)
67
+ os.access(test.stack_file_path, os.R_OK)
68
+ os.access(test.stack_file_path, os.W_OK)
69
+ is_exist_2 = os.path.exists(test.construct_file_path)
70
+ self.assertTrue(is_exist_2)
71
+ os.access(test.construct_file_path, os.R_OK)
72
+ os.access(test.construct_file_path, os.W_OK)
73
+
74
+ os.remove(construct_file_path)
75
+ os.remove(stack_file_path)
76
+ os.remove(dump_file_path)
77
+
78
+ def test_update_dump_paths(self):
79
+ test = DataWriter()
80
+ self.assertTrue(test.dump_file_path == None)
81
+
82
+ cur_path = os.path.dirname(os.path.realpath(__file__))
83
+ test_path = os.path.join(cur_path, "test1.json")
84
+
85
+ test.update_dump_paths(test_path, test_path, test_path, test_path, test_path)
86
+ self.assertTrue(test.dump_file_path == test_path)
87
+ self.assertTrue(test.stack_file_path == test_path)
88
+ self.assertTrue(test.construct_file_path == test_path)
89
+ self.assertTrue(test.dump_tensor_data_dir == test_path)
90
+ self.assertTrue(test.free_benchmark_file_path == test_path)
91
+
92
+ def test_update_data(self):
93
+ data = {"A":"1", "B":"2", "C":{"D":"2"}}
94
+ test = DataWriter()
95
+ test.cache_data["data"]["test_1"] = True
96
+ test.cache_data["data"]["test_2"] = False
97
+
98
+ test.update_data(data)
99
+ self.assertEqual(test.cache_data["data"]["A"], "1")
100
+
101
+ new_data = {"C":{"F":3}}
102
+ test.update_data(new_data)
103
+ self.assertEqual(test.cache_data["data"]["C"]["F"], 3)
104
+
105
+
106
+ def test_flush_data_when_buffer_is_full_and_test_write_data_json(self):
107
+ data = {"A":"1", "B":"2", "data":{}}
108
+ test = DataWriter()
109
+ test.buffer_size = 1
110
+ test.cache_data["data"] = {"A":"1", "B":"2", "C":"3"}
111
+
112
+ self.assertTrue(len(test.cache_data["data"]) >= test.buffer_size)
113
+ cur_path = os.path.dirname(os.path.realpath(__file__))
114
+ dump_tensor_data_dir = os.path.join(cur_path, "dump_tensor_data.json")
115
+ dump_file_path = os.path.join(cur_path, "dump_file.json")
116
+ stack_file_path = os.path.join(cur_path, "stack_file.json")
117
+ construct_file_path = os.path.join(cur_path, "construct_file.json")
118
+
119
+ test.dump_file_path = dump_file_path
120
+ test.dump_tensor_data_dir = dump_tensor_data_dir
121
+
122
+ with open(dump_file_path, "w") as f:
123
+ dump_data = json.dumps(data)
124
+ f.write(dump_data)
125
+
126
+ test.flush_data_when_buffer_is_full()
127
+
128
+ with open(dump_file_path, "r") as f:
129
+ new_data = json.load(f)
130
+
131
+ data.update({"data": {"A":"1", "B":"2", "C":"3"}})
132
+ self.assertEqual(new_data, data)
133
+
134
+ self.assertTrue(test.cache_data["data"] == {})
135
+ os.remove(dump_file_path)
136
+
137
+
138
+ def test_update_stack(self):
139
+ data = {"A":"1", "B":"2", "data":{}}
140
+ test = DataWriter()
141
+ test.update_stack(data)
142
+ self.assertEqual(test.cache_stack, data)
143
+
144
+ def test_update_construct(self):
145
+ data = {"A":"1", "B":"2", "data":{}}
146
+ test = DataWriter()
147
+ test.update_construct(data)
148
+ self.assertEqual(test.cache_construct, data)
149
+
150
+ def test_write_stack_info_json(self):
151
+ test = DataWriter()
152
+ data = {"A":"1", "B":"2", "data":{}}
153
+ test.cache_stack = data
154
+
155
+ cur_path = os.path.dirname(os.path.realpath(__file__))
156
+ file_path = os.path.join(cur_path, "dump.json")
157
+
158
+ test.write_stack_info_json(file_path)
159
+
160
+ with open(file_path, "r") as f:
161
+ load_result = json.load(f)
162
+ try:
163
+ self.assertEqual(load_result, data)
164
+ finally:
165
+ os.remove(file_path)
166
+
167
+
168
+ def test_write_construct_info_json(self):
169
+ test = DataWriter()
170
+ data = {"A":"1", "B":"2", "data":{}}
171
+ test.cache_construct = data
172
+
173
+ cur_path = os.path.dirname(os.path.realpath(__file__))
174
+ file_path = os.path.join(cur_path, "dump.json")
175
+
176
+ test.write_construct_info_json(file_path)
177
+
178
+ with open(file_path, "r") as f:
179
+ load_result = json.load(f)
180
+ try:
181
+ self.assertEqual(load_result, data)
182
+ finally:
183
+ os.remove(file_path)
@@ -0,0 +1,151 @@
1
+ import unittest
2
+ from unittest.mock import MagicMock
3
+
4
+ from msprobe.core.common.exceptions import ScopeException
5
+ from msprobe.core.data_dump.scope import (
6
+ build_scope,
7
+ build_range_scope_according_to_scope_name,
8
+ BaseScope,
9
+ ListScope,
10
+ RangeScope,
11
+ APIRangeScope,
12
+ ModuleRangeScope
13
+ )
14
+
15
+
16
+ class TestBuildScope(unittest.TestCase):
17
+ def test_build_scope(self):
18
+ scope_class = MagicMock()
19
+ result1 = build_scope(scope_class, None, None)
20
+ self.assertEqual(result1, None)
21
+
22
+ api_list = ['api1', 'api2']
23
+ result2 = build_scope(scope_class, None, api_list)
24
+ self.assertEqual(result2, scope_class.return_value)
25
+
26
+ def test_build_range_scope_according_to_scope_name(self):
27
+ result = build_range_scope_according_to_scope_name([], [])
28
+ self.assertIsInstance(result, APIRangeScope)
29
+
30
+
31
+ class TestBaseScope(unittest.TestCase):
32
+ def test_rectify_args(self):
33
+ scope = []
34
+ api_list = "invalid_api_list"
35
+ with self.assertRaises(ScopeException) as context:
36
+ BaseScope.rectify_args(scope, api_list)
37
+ self.assertEqual(context.exception.code, ScopeException.InvalidApiStr)
38
+
39
+ api_list = [1, 2, 3]
40
+ with self.assertRaises(ScopeException) as context:
41
+ BaseScope.rectify_args(scope, api_list)
42
+ self.assertEqual(context.exception.code, ScopeException.InvalidApiStr)
43
+
44
+ scope = "module1"
45
+ api_list = []
46
+
47
+ expected_scope = ["module1"]
48
+ expected_api_list = []
49
+ result_scope, result_api_list = BaseScope.rectify_args(scope, api_list)
50
+ self.assertEqual(result_scope, expected_scope)
51
+ self.assertEqual(result_api_list, expected_api_list)
52
+
53
+ scope = 123
54
+ api_list = []
55
+ with self.assertRaises(ScopeException) as context:
56
+ BaseScope.rectify_args(scope, api_list)
57
+ self.assertEqual(context.exception.code, ScopeException.InvalidScope)
58
+
59
+ scope = ["module1", 2, "module3"]
60
+ api_list = []
61
+ with self.assertRaises(ScopeException) as context:
62
+ BaseScope.rectify_args(scope, api_list)
63
+ self.assertEqual(context.exception.code, ScopeException.InvalidScope)
64
+
65
+
66
+ class TestListScope(unittest.TestCase):
67
+ def test_rectify_args(self):
68
+ scope = ["module1"]
69
+ api_list = ["api1"]
70
+ with self.assertRaises(ScopeException) as context:
71
+ ListScope.rectify_args(scope, api_list)
72
+ self.assertEqual(context.exception.code, ScopeException.ArgConflict)
73
+
74
+ def test_check(self):
75
+ list_scope = ListScope([], [])
76
+ module_name = "module1"
77
+ result = list_scope.check(module_name)
78
+ self.assertTrue(result)
79
+
80
+ list_scope = ListScope(["module1"], [])
81
+ module_name = "module1"
82
+ result = list_scope.check(module_name)
83
+ self.assertTrue(result)
84
+
85
+ list_scope = ListScope(["module1"], [])
86
+ module_name = "module2"
87
+ result = list_scope.check(module_name)
88
+ self.assertFalse(result)
89
+
90
+
91
+ class TestRangeScope(unittest.TestCase):
92
+ def test_rectify_args(self):
93
+ scope = ["module1", "module2", "module3"]
94
+ with self.assertRaises(ScopeException) as context:
95
+ RangeScope.rectify_args(scope, [])
96
+ self.assertEqual(context.exception.code, ScopeException.InvalidScope)
97
+
98
+ scope = ["module1"]
99
+ expected_scope = ["module1", "module1"]
100
+ result_scope, result_api_list = RangeScope.rectify_args(scope, [])
101
+ self.assertEqual(result_scope, expected_scope)
102
+
103
+
104
+ class TestAPIRangeScope(unittest.TestCase):
105
+ def test_check_scope_is_valid(self):
106
+ api_range_scope = APIRangeScope([], [])
107
+ result = api_range_scope.check_scope_is_valid()
108
+ self.assertTrue(result)
109
+
110
+ def test_check(self):
111
+ api_range_scope = APIRangeScope([], [])
112
+ api_name = "api1"
113
+ result = api_range_scope.check(api_name)
114
+ self.assertTrue(result)
115
+
116
+
117
+ class TestModuleRangeScope(unittest.TestCase):
118
+ def test_check_scope_is_valid(self):
119
+ module_range_scope = ModuleRangeScope([], [])
120
+ result = module_range_scope.check_scope_is_valid()
121
+ self.assertTrue(result)
122
+
123
+ def test_begin_module(self):
124
+ module_range_scope = ModuleRangeScope(["module1", "module2"], [])
125
+ module_name = "module1"
126
+ module_range_scope.begin_module(module_name)
127
+ self.assertTrue(module_range_scope.in_scope)
128
+
129
+ module_range_scope = ModuleRangeScope(["module1", "module2"], [])
130
+ module_name = "module3"
131
+ module_range_scope.begin_module(module_name)
132
+ self.assertFalse(module_range_scope.in_scope)
133
+
134
+ def test_end_module(self):
135
+ module_range_scope = ModuleRangeScope(["module1", "module2"], [])
136
+ module_name = "module2"
137
+ module_range_scope.in_scope = True
138
+ module_range_scope.end_module(module_name)
139
+ self.assertFalse(module_range_scope.in_scope)
140
+
141
+ module_range_scope = ModuleRangeScope(["module1", "module2"], [])
142
+ module_name = "module3"
143
+ module_range_scope.in_scope = True
144
+ module_range_scope.end_module(module_name)
145
+ self.assertTrue(module_range_scope.in_scope)
146
+
147
+ def test_check(self):
148
+ module_range_scope = ModuleRangeScope([], [])
149
+ module_name = "module1"
150
+ result = module_range_scope.check(module_name)
151
+ self.assertTrue(result)
@@ -0,0 +1,152 @@
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
+ from unittest import TestCase
18
+ from unittest.mock import patch
19
+
20
+ from msprobe.core.common.log import logger
21
+ from msprobe.core.common.const import Const
22
+ from msprobe.core.common.exceptions import MsaccException
23
+ from msprobe.core.common_config import CommonConfig, BaseConfig
24
+
25
+
26
+ class TestCommonConfig(TestCase):
27
+ @patch.object(logger, "error_log_with_exp")
28
+ def test_common_config(self, mock_error_log_with_exp):
29
+ json_config = dict()
30
+
31
+ common_config = CommonConfig(json_config)
32
+ self.assertIsNone(common_config.task)
33
+ self.assertIsNone(common_config.dump_path)
34
+ self.assertIsNone(common_config.rank)
35
+ self.assertIsNone(common_config.step)
36
+ self.assertIsNone(common_config.level)
37
+ self.assertIsNone(common_config.seed)
38
+ self.assertIsNone(common_config.acl_config)
39
+ self.assertFalse(common_config.is_deterministic)
40
+ self.assertFalse(common_config.enable_dataloader)
41
+
42
+ json_config.update({"task": "md5"})
43
+ CommonConfig(json_config)
44
+ self.assertEqual(mock_error_log_with_exp.call_args[0][0],
45
+ "task is invalid, it should be one of {}".format(Const.TASK_LIST))
46
+ self.assertEqual(str(mock_error_log_with_exp.call_args[0][1]),
47
+ MsaccException.err_strs.get(MsaccException.INVALID_PARAM_ERROR))
48
+
49
+ json_config.update({"task": Const.TENSOR})
50
+ json_config.update({"rank": 0})
51
+ CommonConfig(json_config)
52
+ self.assertEqual(mock_error_log_with_exp.call_args[0][0],
53
+ "rank is invalid, it should be a list")
54
+ self.assertEqual(str(mock_error_log_with_exp.call_args[0][1]),
55
+ MsaccException.err_strs.get(MsaccException.INVALID_PARAM_ERROR))
56
+
57
+ json_config.update({"task": Const.TENSOR})
58
+ json_config.update({"rank": [0]})
59
+ json_config.update({"step": 0})
60
+ CommonConfig(json_config)
61
+ self.assertEqual(mock_error_log_with_exp.call_args[0][0],
62
+ "step is invalid, it should be a list")
63
+ self.assertEqual(str(mock_error_log_with_exp.call_args[0][1]),
64
+ MsaccException.err_strs.get(MsaccException.INVALID_PARAM_ERROR))
65
+
66
+ json_config.update({"task": Const.TENSOR})
67
+ json_config.update({"rank": [0]})
68
+ json_config.update({"step": [0]})
69
+ json_config.update({"level": "L3"})
70
+ CommonConfig(json_config)
71
+ self.assertEqual(mock_error_log_with_exp.call_args[0][0],
72
+ "level is invalid, it should be one of {}".format(Const.LEVEL_LIST))
73
+ self.assertEqual(str(mock_error_log_with_exp.call_args[0][1]),
74
+ MsaccException.err_strs.get(MsaccException.INVALID_PARAM_ERROR))
75
+
76
+ json_config.update({"task": Const.TENSOR})
77
+ json_config.update({"rank": [0]})
78
+ json_config.update({"step": [0]})
79
+ json_config.update({"level": "L0"})
80
+ json_config.update({"seed": "1234"})
81
+ CommonConfig(json_config)
82
+ self.assertEqual(mock_error_log_with_exp.call_args[0][0],
83
+ "seed is invalid, it should be an integer")
84
+ self.assertEqual(str(mock_error_log_with_exp.call_args[0][1]),
85
+ MsaccException.err_strs.get(MsaccException.INVALID_PARAM_ERROR))
86
+
87
+ json_config.update({"task": Const.TENSOR})
88
+ json_config.update({"rank": [0]})
89
+ json_config.update({"step": [0]})
90
+ json_config.update({"level": "L0"})
91
+ json_config.update({"seed": 1234})
92
+ json_config.update({"is_deterministic": "ENABLE"})
93
+ CommonConfig(json_config)
94
+ self.assertEqual(mock_error_log_with_exp.call_args[0][0],
95
+ "is_deterministic is invalid, it should be a boolean")
96
+ self.assertEqual(str(mock_error_log_with_exp.call_args[0][1]),
97
+ MsaccException.err_strs.get(MsaccException.INVALID_PARAM_ERROR))
98
+
99
+ json_config.update({"task": Const.TENSOR})
100
+ json_config.update({"rank": [0]})
101
+ json_config.update({"step": [0]})
102
+ json_config.update({"level": "L0"})
103
+ json_config.update({"seed": 1234})
104
+ json_config.update({"is_deterministic": True})
105
+ json_config.update({"enable_dataloader": "ENABLE"})
106
+ CommonConfig(json_config)
107
+ self.assertEqual(mock_error_log_with_exp.call_args[0][0],
108
+ "enable_dataloader is invalid, it should be a boolean")
109
+ self.assertEqual(str(mock_error_log_with_exp.call_args[0][1]),
110
+ MsaccException.err_strs.get(MsaccException.INVALID_PARAM_ERROR))
111
+
112
+ @patch.object(logger, "error_log_with_exp")
113
+ def test_base_config(self, mock_error_log_with_exp):
114
+ json_config = dict()
115
+
116
+ base_config = BaseConfig(json_config)
117
+ base_config.check_config()
118
+ self.assertIsNone(base_config.scope)
119
+ self.assertIsNone(base_config.list)
120
+ self.assertIsNone(base_config.data_mode)
121
+ self.assertIsNone(base_config.backward_input)
122
+ self.assertIsNone(base_config.file_format)
123
+ self.assertIsNone(base_config.summary_mode)
124
+ self.assertIsNone(base_config.overflow_num)
125
+ self.assertIsNone(base_config.check_mode)
126
+
127
+ json_config.update({"scope": "Tensor_Add"})
128
+ base_config = BaseConfig(json_config)
129
+ base_config.check_config()
130
+ self.assertEqual(mock_error_log_with_exp.call_args[0][0],
131
+ "scope is invalid, it should be a list")
132
+ self.assertEqual(str(mock_error_log_with_exp.call_args[0][1]),
133
+ MsaccException.err_strs.get(MsaccException.INVALID_PARAM_ERROR))
134
+
135
+ json_config.update({"scope": ["Tensor_Add"]})
136
+ json_config.update({"list": "Tensor_Add"})
137
+ base_config = BaseConfig(json_config)
138
+ base_config.check_config()
139
+ self.assertEqual(mock_error_log_with_exp.call_args[0][0],
140
+ "list is invalid, it should be a list")
141
+ self.assertEqual(str(mock_error_log_with_exp.call_args[0][1]),
142
+ MsaccException.err_strs.get(MsaccException.INVALID_PARAM_ERROR))
143
+
144
+ json_config.update({"scope": ["Tensor_Add"]})
145
+ json_config.update({"list": ["Tensor_Add"]})
146
+ json_config.update({"data_mode": "all"})
147
+ base_config = BaseConfig(json_config)
148
+ base_config.check_config()
149
+ self.assertEqual(mock_error_log_with_exp.call_args[0][0],
150
+ "data_mode is invalid, it should be a list")
151
+ self.assertEqual(str(mock_error_log_with_exp.call_args[0][1]),
152
+ MsaccException.err_strs.get(MsaccException.INVALID_PARAM_ERROR))