已合并
[test]profiler fix uttest #38680
hhz0创建于 6月16日
[test]profiler fix uttest #38680
已合并
hhz0创建于 6月16日
3 个文件变更+9-24
@@ -1,14 +1,12 @@
1import os1import os
2import shutil2import shutil
3import stat3import stat
4-from unittest.mock import patch, MagicMock
5 4 
6from torch_npu.profiler.analysis.prof_common_func._constant import Constant5from torch_npu.profiler.analysis.prof_common_func._constant import Constant
7from torch_npu.profiler.analysis.prof_common_func._file_manager import FileManager6from torch_npu.profiler.analysis.prof_common_func._file_manager import FileManager
8from torch_npu.profiler.analysis.prof_common_func._path_manager import ProfilerPathManager7from torch_npu.profiler.analysis.prof_common_func._path_manager import ProfilerPathManager
9 8 
10from torch_npu.testing.testcase import TestCase, run_tests9from torch_npu.testing.testcase import TestCase, run_tests
11-from torch_npu.utils._path_manager import PathManager
12 10 
13 11 
14class TestPathManager(TestCase):12class TestPathManager(TestCase):
@@ -120,7 +118,7 @@ class TestPathManager(TestCase):
120 os.makedirs(os.path.join(prof_path2, "PROF_1_2_3a"))118 os.makedirs(os.path.join(prof_path2, "PROF_1_2_3a"))
121 self.assertEqual([prof_path1], ProfilerPathManager.get_profiler_path_list(prof_path1))119 self.assertEqual([prof_path1], ProfilerPathManager.get_profiler_path_list(prof_path1))
122 self.assertEqual([prof_path2], ProfilerPathManager.get_profiler_path_list(prof_path2))120 self.assertEqual([prof_path2], ProfilerPathManager.get_profiler_path_list(prof_path2))
123- self.assertEqual(set((prof_path1, prof_path2)), set(ProfilerPathManager.get_profiler_path_list(self.tmp_dir)))121+ self.assertEqual({prof_path1, prof_path2}, set(ProfilerPathManager.get_profiler_path_list(self.tmp_dir)))
124 122 
125 def test_device_all_file_list_by_tag(self):123 def test_device_all_file_list_by_tag(self):
126 self.assertEqual([], ProfilerPathManager.get_output_all_file_list_by_type(self.tmp_dir, "mindstudio_profiler_output"))124 self.assertEqual([], ProfilerPathManager.get_output_all_file_list_by_type(self.tmp_dir, "mindstudio_profiler_output"))
@@ -215,20 +213,6 @@ class TestPathManager(TestCase):
215 ]213 ]
216 self.assertCountEqual(result_depth_2, expected_depth_2)214 self.assertCountEqual(result_depth_2, expected_depth_2)
217 215 
218- @patch('os.stat')
219- def test_path_is_other_writable(self, mock_stat):
220- mock_stat_result = MagicMock()
221- mock_stat_result.st_mode = 0o777
222- mock_stat.return_value = mock_stat_result
223- 
224- self.assertTrue(ProfilerPathManager.path_is_other_writable(self.tmp_dir))
225- mock_stat_result.st_mode = 0o755
226- self.assertFalse(ProfilerPathManager.path_is_other_writable(self.tmp_dir))
227- mock_stat_result.st_mode = 0o775
228- self.assertTrue(ProfilerPathManager.path_is_other_writable(self.tmp_dir))
229- mock_stat_result.st_mode = 0o700
230- self.assertFalse(ProfilerPathManager.path_is_other_writable(self.tmp_dir))
231- 
232 216 
233if __name__ == "__main__":217if __name__ == "__main__":
234 run_tests()218 run_tests()
@@ -4,7 +4,7 @@ import logging
4import sys4import sys
5import threading5import threading
6from collections.abc import Callable6from collections.abc import Callable
7-from typing import Any7+from typing import Any, Optional
8 8 
9from torch_npu.npu.mstx import mstx9from torch_npu.npu.mstx import mstx
10 10 
@@ -70,7 +70,7 @@ class FlopsHookManager:
70 _installed = False70 _installed = False
71 71 
72 @classmethod72 @classmethod
73- def install(cls, target_ops: dict[str, tuple[Any, str]] | None = None):73+ def install(cls, target_ops: Optional[dict[str, tuple[Any, str]]] = None):
74 if cls._installed:74 if cls._installed:
75 return75 return
76 if target_ops is None:76 if target_ops is None:
@@ -1,16 +1,17 @@
1import logging1import logging
2from collections.abc import Callable2from collections.abc import Callable
3+from typing import Optional
3 4 
4 5 
5logger = logging.getLogger(__name__)6logger = logging.getLogger(__name__)
6 7 
7-_default_npu_flop_registry: dict[str, tuple[Callable, str | None]] = {}8+_default_npu_flop_registry: dict[str, tuple[Callable, Optional[str]]] = {}
8-_npu_flop_registry: dict[str, tuple[Callable, str | None]] = {}9+_npu_flop_registry: dict[str, tuple[Callable, Optional[str]]] = {}
9 10 
10 11 
11def register_npu_flop(12def register_npu_flop(
12- target: str | None = None,13+ target: Optional[str] = None,
13- op_name: str | None = None,14+ op_name: Optional[str] = None,
14 *,15 *,
15 is_default: bool = False,16 is_default: bool = False,
16):17):
@@ -34,7 +35,7 @@ def register_npu_flop(
34 return decorator35 return decorator
35 36 
36 37 
37-def get_flop_func(op_name: str) -> Callable | None:38+def get_flop_func(op_name: str) -> Optional[Callable]:
38 entry = _npu_flop_registry.get(op_name) or _default_npu_flop_registry.get(op_name)39 entry = _npu_flop_registry.get(op_name) or _default_npu_flop_registry.get(op_name)
39 return entry[0] if entry else None40 return entry[0] if entry else None
40 41