已合并
Suppress error printing for the pta in the python interactive environment. #30380
zhujiaxing1029创建于 2月2日
Suppress error printing for the pta in the python interactive environment. #30380
已合并
共 3 个文件变更+37-10
| @@ -66,6 +66,7 @@ import torch_npu.dynamo | |||
| 66 | import torch_npu._C | 66 | import torch_npu._C |
| 67 | import torch_npu._logging | 67 | import torch_npu._logging |
| 68 | from torch_npu.utils import patch_getenv | 68 | from torch_npu.utils import patch_getenv |
| 69 | +from torch_npu.utils.utils import _is_interactive_command_line | ||
| 69 | import torch_npu._afd | 70 | import torch_npu._afd |
| 70 | from torch_npu import profiler | 71 | from torch_npu import profiler |
| 71 | from torch_npu.npu.amp.sharded_grad_scaler import _ShardedGradScaler | 72 | from torch_npu.npu.amp.sharded_grad_scaler import _ShardedGradScaler |
| @@ -314,7 +315,7 @@ _inductor_register_device_op_overrides() | |||
| 314 | # Support stream into Dynamo charts | 315 | # Support stream into Dynamo charts |
| 315 | _patch_npu_trace_rules() | 316 | _patch_npu_trace_rules() |
| 316 | 317 | ||
| 317 | -if hasattr(sys, 'ps1'): | 318 | +if _is_interactive_command_line(): |
| 318 | os.environ["TASK_QUEUE_ENABLE"] = '0' | 319 | os.environ["TASK_QUEUE_ENABLE"] = '0' |
| 319 | warnings.warn("On the interactive interface, the value of TASK_QUEUE_ENABLE is set to 0 by default. \ | 320 | warnings.warn("On the interactive interface, the value of TASK_QUEUE_ENABLE is set to 0 by default. \ |
| 320 | Do not set it to 1 to prevent some unknown errors") | 321 | Do not set it to 1 to prevent some unknown errors") |
| @@ -3,6 +3,7 @@ import sys | |||
| 3 | import re | 3 | import re |
| 4 | import time | 4 | import time |
| 5 | from enum import Enum | 5 | from enum import Enum |
| 6 | +from torch_npu.utils.utils import _is_interactive_command_line | ||
| 6 | 7 | ||
| 7 | 8 | ||
| 8 | class _SubModuleID(Enum): | 9 | class _SubModuleID(Enum): |
| @@ -57,18 +58,19 @@ def _format_error_msg(submodule, error_code): | |||
| 57 | return rank | 58 | return rank |
| 58 | except Exception: | 59 | except Exception: |
| 59 | return -1 | 60 | return -1 |
| 61 | + | ||
| 60 | error_msg = "" | 62 | error_msg = "" |
| 61 | - if not get_env_compact_error_output(): | 63 | + if not get_env_compact_error_output() and not _is_interactive_command_line(): |
| 62 | error_msg += "\n[ERROR] {time} (PID:{pid}, Device:{device}, RankID:{rank}) {error_code} {submodule_name} {error_code_msg}" | 64 | error_msg += "\n[ERROR] {time} (PID:{pid}, Device:{device}, RankID:{rank}) {error_code} {submodule_name} {error_code_msg}" |
| 63 | 65 | ||
| 64 | return error_msg.format( | 66 | return error_msg.format( |
| 65 | - time=time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()), | 67 | + time=time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()), |
| 66 | - pid=os.getpid(), | 68 | + pid=os.getpid(), |
| 67 | - device=get_device_id(), | 69 | + device=get_device_id(), |
| 68 | - rank=get_rank_id(), | 70 | + rank=get_rank_id(), |
| 69 | - error_code="ERR{:0>2d}{:0>3d}".format(submodule.value, error_code.code,), | 71 | + error_code="ERR{:0>2d}{:0>3d}".format(submodule.value, error_code.code, ), |
| 70 | - submodule_name=submodule.name, | 72 | + submodule_name=submodule.name, |
| 71 | - error_code_msg=error_code.msg) | 73 | + error_code_msg=error_code.msg) |
| 72 | 74 | ||
| 73 | 75 | ||
| 74 | def pta_error(error: ErrCode) -> str: | 76 | def pta_error(error: ErrCode) -> str: |
| @@ -107,7 +109,7 @@ class _NPUExceptionHandler(object): | |||
| 107 | if self.exception and re.search(exception_pattern, self.exception): | 109 | if self.exception and re.search(exception_pattern, self.exception): |
| 108 | return True | 110 | return True |
| 109 | return False | 111 | return False |
| 110 | - | 112 | + |
| 111 | def set_force_stop_exception(self, flag): | 113 | def set_force_stop_exception(self, flag): |
| 112 | self.force_stop_flag = flag | 114 | self.force_stop_flag = flag |
| 113 | 115 | ||
| @@ -1,4 +1,5 @@ | |||
| 1 | import os | 1 | import os |
| 2 | +import sys | ||
| 2 | import time | 3 | import time |
| 3 | import warnings | 4 | import warnings |
| 4 | from warnings import _showwarnmsg_impl | 5 | from warnings import _showwarnmsg_impl |
| @@ -49,3 +50,26 @@ def _apply_npu_show_warning(): | |||
| 49 | _showwarnmsg_impl(msg) | 50 | _showwarnmsg_impl(msg) |
| 50 | 51 | ||
| 51 | warnings.showwarning = npu_show_warning | 52 | warnings.showwarning = npu_show_warning |
| 53 | + | ||
| 54 | + | ||
| 55 | +def _is_interactive_command_line(): | ||
| 56 | + # check whether it is standard python interactive environment | ||
| 57 | + if hasattr(sys, 'ps1'): | ||
| 58 | + return True | ||
| 59 | + | ||
| 60 | + # check whether it is IPython or Jupyter environment | ||
| 61 | + try: | ||
| 62 | + __IPYTHON__ # noqa: F821 | ||
| 63 | + return True | ||
| 64 | + except NameError: | ||
| 65 | + pass | ||
| 66 | + | ||
| 67 | + # check whether it is Python REPL mode | ||
| 68 | + if sys.flags.interactive: | ||
| 69 | + return True | ||
| 70 | + | ||
| 71 | + # check whether it is notebook mode | ||
| 72 | + if 'ipykernel' in sys.modules: | ||
| 73 | + return True | ||
| 74 | + | ||
| 75 | + return False | ||