已合并
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
已合并
zhujiaxing1029创建于 2月2日
3 个文件变更+37-10
Mtorch_npu/__init__.py+2-1
@@ -66,6 +66,7 @@ import torch_npu.dynamo
66import torch_npu._C66import torch_npu._C
67import torch_npu._logging67import torch_npu._logging
68from torch_npu.utils import patch_getenv68from torch_npu.utils import patch_getenv
69+from torch_npu.utils.utils import _is_interactive_command_line
69import torch_npu._afd70import torch_npu._afd
70from torch_npu import profiler71from torch_npu import profiler
71from torch_npu.npu.amp.sharded_grad_scaler import _ShardedGradScaler72from torch_npu.npu.amp.sharded_grad_scaler import _ShardedGradScaler
@@ -314,7 +315,7 @@ _inductor_register_device_op_overrides()
314# Support stream into Dynamo charts315# 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")
Mtorch_npu/utils/_error_code.py+11-9
@@ -3,6 +3,7 @@ import sys
3import re3import re
4import time4import time
5from enum import Enum5from enum import Enum
6+from torch_npu.utils.utils import _is_interactive_command_line
6 7 
7 8 
8class _SubModuleID(Enum):9class _SubModuleID(Enum):
@@ -57,18 +58,19 @@ def _format_error_msg(submodule, error_code):
57 return rank58 return rank
58 except Exception:59 except Exception:
59 return -160 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 
74def pta_error(error: ErrCode) -> str:76def 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 True110 return True
109 return False111 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 = flag114 self.force_stop_flag = flag
113 115 
Mtorch_npu/utils/utils.py+24-0
@@ -1,4 +1,5 @@
1import os1import os
2+import sys
2import time3import time
3import warnings4import warnings
4from warnings import _showwarnmsg_impl5from 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_warning52 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