已合并
[feat]profiler remove safe interrupt #38142
hhz0创建于 6月10日
[feat]profiler remove safe interrupt #38142
已合并
共 5 个文件变更+19-42
| @@ -1,5 +1,6 @@ | |||
| 1 | import os | 1 | import os |
| 2 | import re | 2 | import re |
| 3 | +import warnings | ||
| 3 | 4 | ||
| 4 | from torch_npu.utils._error_code import ErrCode, prof_error | 5 | from torch_npu.utils._error_code import ErrCode, prof_error |
| 5 | 6 | ||
| @@ -182,10 +183,8 @@ class ProfilerPathManager: | |||
| 182 | ) | 183 | ) |
| 183 | path = os.path.expanduser(path) | 184 | path = os.path.expanduser(path) |
| 184 | if os.path.islink(path): | 185 | if os.path.islink(path): |
| 185 | - msg = f"Invalid input path is a soft chain: {path}" + prof_error( | 186 | + msg = f"Invalid input path is a soft chain: {path}" |
| 186 | - ErrCode.UNAVAIL | 187 | + warnings.warn(msg) |
| 187 | - ) | ||
| 188 | - raise RuntimeError(msg) | ||
| 189 | return os.path.realpath(path) | 188 | return os.path.realpath(path) |
| 190 | 189 | ||
| 191 | 190 | ||
| @@ -204,11 +203,6 @@ class ProfilerPathManager: | |||
| 204 | ) | 203 | ) |
| 205 | return paths | 204 | return paths |
| 206 | 205 | ||
| 207 | - | ||
| 208 | - def path_is_other_writable(cls, path): | ||
| 209 | - stat_info = os.stat(path) | ||
| 210 | - return bool(stat_info.st_mode & 0o022) | ||
| 211 | - | ||
| 212 | 206 | ||
| 213 | def check_path_permission(cls, path): | 207 | def check_path_permission(cls, path): |
| 214 | file_stat = os.stat(path) | 208 | file_stat = os.stat(path) |
| @@ -145,12 +145,12 @@ class CANNFileParser: | |||
| 145 | if not self._cann_path: | 145 | if not self._cann_path: |
| 146 | raise RuntimeError("CANN Profiling data does not exist.") | 146 | raise RuntimeError("CANN Profiling data does not exist.") |
| 147 | if not FileManager.check_file_readable(self._cann_path): | 147 | if not FileManager.check_file_readable(self._cann_path): |
| 148 | - raise PermissionError( | 148 | + self.logger.warning( |
| 149 | f"Path '{self._cann_path}' owner is not readable. " | 149 | f"Path '{self._cann_path}' owner is not readable. " |
| 150 | f"Please execute 'chmod -R 755 '{self._cann_path}' '." | 150 | f"Please execute 'chmod -R 755 '{self._cann_path}' '." |
| 151 | ) | 151 | ) |
| 152 | if not FileManager.check_file_writable(self._cann_path): | 152 | if not FileManager.check_file_writable(self._cann_path): |
| 153 | - raise PermissionError( | 153 | + self.logger.warning( |
| 154 | f"Path '{self._cann_path}' owner is not writable. " | 154 | f"Path '{self._cann_path}' owner is not writable. " |
| 155 | f"Please execute 'chmod -R 755 '{self._cann_path}' '." | 155 | f"Please execute 'chmod -R 755 '{self._cann_path}' '." |
| 156 | ) | 156 | ) |
| @@ -120,15 +120,10 @@ class CANNExportParser(BaseParser): | |||
| 120 | def _check_profiler_path_parent_dir_invalid(self, paths: list): | 120 | def _check_profiler_path_parent_dir_invalid(self, paths: list): |
| 121 | for path in paths: | 121 | for path in paths: |
| 122 | if not FileManager.check_file_owner(path): | 122 | if not FileManager.check_file_owner(path): |
| 123 | - raise RuntimeError( | 123 | + self.logger.warning( |
| 124 | f"Path '{self._cann_path}' owner is neither root nor the current user. " | 124 | f"Path '{self._cann_path}' owner is neither root nor the current user. " |
| 125 | f"Please execute 'chown -R $(id -un) '{self._cann_path}' '." | 125 | f"Please execute 'chown -R $(id -un) '{self._cann_path}' '." |
| 126 | ) | 126 | ) |
| 127 | - if ProfilerPathManager.path_is_other_writable(path): | ||
| 128 | - raise RuntimeError( | ||
| 129 | - f"Path '{self._cann_path}' permission allow others users to write. " | ||
| 130 | - f"Please execute 'chmod -R 755 '{self._cann_path}' '." | ||
| 131 | - ) | ||
| 132 | return False | 127 | return False |
| 133 | 128 | ||
| 134 | def _check_msprof_path(self): | 129 | def _check_msprof_path(self): |
| @@ -167,14 +162,8 @@ class CANNExportParser(BaseParser): | |||
| 167 | + prof_error(ErrCode.PERMISSION) | 162 | + prof_error(ErrCode.PERMISSION) |
| 168 | + "\n" | 163 | + "\n" |
| 169 | ) | 164 | ) |
| 170 | - if ProfilerPathManager.path_is_other_writable(msprof_script_path): | ||
| 171 | - error_message += ( | ||
| 172 | - f"Path '{msprof_script_path}' permission allow others users to write. " | ||
| 173 | - f"Please execute 'chmod -R 755 '{msprof_script_path}' '" | ||
| 174 | - + prof_error(ErrCode.PERMISSION) | ||
| 175 | - ) | ||
| 176 | if error_message: | 165 | if error_message: |
| 177 | - raise RuntimeError(error_message) | 166 | + self.logger.warning(error_message) |
| 178 | 167 | ||
| 179 | def _get_msprof_script_path(self, script_path: str) -> str: | 168 | def _get_msprof_script_path(self, script_path: str) -> str: |
| 180 | msprof_path = os.path.realpath(self.msprof_path.strip()) | 169 | msprof_path = os.path.realpath(self.msprof_path.strip()) |
| @@ -79,12 +79,6 @@ class _KinetoProfile: | |||
| 79 | def export_chrome_trace(self, output_path: str): | 79 | def export_chrome_trace(self, output_path: str): |
| 80 | output_path = ProfilerPathManager.get_realpath(output_path) | 80 | output_path = ProfilerPathManager.get_realpath(output_path) |
| 81 | PathManager.check_input_file_path(output_path) | 81 | PathManager.check_input_file_path(output_path) |
| 82 | - file_name = os.path.basename(output_path) | ||
| 83 | - if not file_name.endswith(".json"): | ||
| 84 | - raise RuntimeError( | ||
| 85 | - "Invalid parameter output_path, which must be a json file." | ||
| 86 | - + prof_error(ErrCode.VALUE) | ||
| 87 | - ) | ||
| 88 | if not self.prof_if.prof_path: | 82 | if not self.prof_if.prof_path: |
| 89 | print_warn_msg("Invalid profiling path.") | 83 | print_warn_msg("Invalid profiling path.") |
| 90 | return | 84 | return |
| @@ -78,10 +78,10 @@ class PathManager: | |||
| 78 | cls.check_path_owner_consistent(path) | 78 | cls.check_path_owner_consistent(path) |
| 79 | if os.path.islink(path): | 79 | if os.path.islink(path): |
| 80 | msg = f"Invalid path is a soft chain: {path}" | 80 | msg = f"Invalid path is a soft chain: {path}" |
| 81 | - raise RuntimeError(msg + pta_error(ErrCode.UNAVAIL)) | 81 | + warnings.warn(msg) |
| 82 | if not os.access(path, os.W_OK): | 82 | if not os.access(path, os.W_OK): |
| 83 | msg = f"The path permission check failed: {path}" | 83 | msg = f"The path permission check failed: {path}" |
| 84 | - raise RuntimeError(msg + pta_error(ErrCode.PERMISSION)) | 84 | + warnings.warn(msg) |
| 85 | 85 | ||
| 86 | 86 | ||
| 87 | def check_directory_path_readable(cls, path): | 87 | def check_directory_path_readable(cls, path): |
| @@ -96,16 +96,16 @@ class PathManager: | |||
| 96 | cls.check_path_owner_consistent(path) | 96 | cls.check_path_owner_consistent(path) |
| 97 | if os.path.islink(path): | 97 | if os.path.islink(path): |
| 98 | msg = f"Invalid path is a soft chain: {path}" | 98 | msg = f"Invalid path is a soft chain: {path}" |
| 99 | - raise RuntimeError(msg + pta_error(ErrCode.UNAVAIL)) | 99 | + warnings.warn(msg) |
| 100 | if not os.access(path, os.R_OK): | 100 | if not os.access(path, os.R_OK): |
| 101 | msg = f"The path permission check failed: {path}" | 101 | msg = f"The path permission check failed: {path}" |
| 102 | - raise RuntimeError(msg + pta_error(ErrCode.PERMISSION)) | 102 | + warnings.warn(msg) |
| 103 | 103 | ||
| 104 | 104 | ||
| 105 | def remove_path_safety(cls, path: str): | 105 | def remove_path_safety(cls, path: str): |
| 106 | msg = f"Failed to remove path: {path}" | 106 | msg = f"Failed to remove path: {path}" |
| 107 | if os.path.islink(path): | 107 | if os.path.islink(path): |
| 108 | - raise RuntimeError(msg + pta_error(ErrCode.UNAVAIL)) | 108 | + warnings.warn(msg) |
| 109 | if not os.path.exists(path): | 109 | if not os.path.exists(path): |
| 110 | return | 110 | return |
| 111 | try: | 111 | try: |
| @@ -119,7 +119,7 @@ class PathManager: | |||
| 119 | def remove_file_safety(cls, file: str): | 119 | def remove_file_safety(cls, file: str): |
| 120 | msg = f"Failed to remove file: {file}" | 120 | msg = f"Failed to remove file: {file}" |
| 121 | if os.path.islink(file): | 121 | if os.path.islink(file): |
| 122 | - raise RuntimeError(msg) | 122 | + warnings.warn(msg) |
| 123 | if not os.path.exists(file): | 123 | if not os.path.exists(file): |
| 124 | return | 124 | return |
| 125 | try: | 125 | try: |
| @@ -133,7 +133,7 @@ class PathManager: | |||
| 133 | def make_dir_safety(cls, path: str): | 133 | def make_dir_safety(cls, path: str): |
| 134 | msg = f"Failed to make directory: {path}" | 134 | msg = f"Failed to make directory: {path}" |
| 135 | if os.path.islink(path): | 135 | if os.path.islink(path): |
| 136 | - raise RuntimeError(msg + pta_error(ErrCode.UNAVAIL)) | 136 | + warnings.warn(msg) |
| 137 | if os.path.exists(path): | 137 | if os.path.exists(path): |
| 138 | return | 138 | return |
| 139 | try: | 139 | try: |
| @@ -145,7 +145,7 @@ class PathManager: | |||
| 145 | def create_file_safety(cls, path: str): | 145 | def create_file_safety(cls, path: str): |
| 146 | msg = f"Failed to create file: {path}" | 146 | msg = f"Failed to create file: {path}" |
| 147 | if os.path.islink(path): | 147 | if os.path.islink(path): |
| 148 | - raise RuntimeError(msg + pta_error(ErrCode.UNAVAIL)) | 148 | + warnings.warn(msg) |
| 149 | if os.path.exists(path): | 149 | if os.path.exists(path): |
| 150 | return | 150 | return |
| 151 | try: | 151 | try: |
| @@ -156,21 +156,21 @@ class PathManager: | |||
| 156 | 156 | ||
| 157 | def _input_path_common_check(cls, path: str): | 157 | def _input_path_common_check(cls, path: str): |
| 158 | if len(path) > cls.MAX_PATH_LENGTH: | 158 | if len(path) > cls.MAX_PATH_LENGTH: |
| 159 | - raise RuntimeError("Length of input path exceeds the limit." + pta_error(ErrCode.PARAM)) | 159 | + warnings.warn("Length of input path exceeds the limit.") |
| 160 | 160 | ||
| 161 | if os.path.islink(path): | 161 | if os.path.islink(path): |
| 162 | msg = f"Invalid input path is a soft chain: {path}" | 162 | msg = f"Invalid input path is a soft chain: {path}" |
| 163 | - raise RuntimeError(msg + pta_error(ErrCode.UNAVAIL)) | 163 | + warnings.warn(msg) |
| 164 | 164 | ||
| 165 | pattern = r'(\.|/|_|-|\s|[~0-9a-zA-Z]|[\u4e00-\u9fa5])+' | 165 | pattern = r'(\.|/|_|-|\s|[~0-9a-zA-Z]|[\u4e00-\u9fa5])+' |
| 166 | if not re.fullmatch(pattern, path): | 166 | if not re.fullmatch(pattern, path): |
| 167 | msg = f"Invalid input path: {path}" | 167 | msg = f"Invalid input path: {path}" |
| 168 | - raise RuntimeError(msg + pta_error(ErrCode.PARAM)) | 168 | + warnings.warn(msg) |
| 169 | 169 | ||
| 170 | path_split_list = path.split("/") | 170 | path_split_list = path.split("/") |
| 171 | for name in path_split_list: | 171 | for name in path_split_list: |
| 172 | if len(name) > cls.MAX_FILE_NAME_LENGTH: | 172 | if len(name) > cls.MAX_FILE_NAME_LENGTH: |
| 173 | - raise RuntimeError("Length of input path exceeds the limit." + pta_error(ErrCode.PARAM)) | 173 | + warnings.warn("Length of input path exceeds the limit.") |
| 174 | 174 | ||
| 175 | 175 | ||
| 176 | def check_path_is_readable(cls, path: str): | 176 | def check_path_is_readable(cls, path: str): |