MCPcopy Create free account
hub / github.com/pytorch/pytorch / ExceptionWrapper

Class ExceptionWrapper

torch/_utils.py:690–722  ·  view source on GitHub ↗

r"""Wraps an exception plus traceback to communicate across threads

Source from the content-addressed store, hash-verified

688
689
690class ExceptionWrapper:
691 r"""Wraps an exception plus traceback to communicate across threads"""
692
693 def __init__(self, exc_info=None, where="in background"):
694 # It is important that we don't store exc_info, see
695 # NOTE [ Python Traceback Reference Cycle Problem ]
696 if exc_info is None:
697 exc_info = sys.exc_info()
698 self.exc_type = exc_info[0]
699 self.exc_msg = "".join(traceback.format_exception(*exc_info))
700 self.where = where
701
702 def reraise(self):
703 r"""Reraises the wrapped exception in the current thread"""
704 # Format a message such as: "Caught ValueError in DataLoader worker
705 # process 2. Original Traceback:", followed by the traceback.
706 msg = f"Caught {self.exc_type.__name__} {self.where}.\nOriginal {self.exc_msg}"
707 if self.exc_type == KeyError:
708 # KeyError calls repr() on its argument (usually a dict key). This
709 # makes stack traces unreadable. It will not be changed in Python
710 # (https://bugs.python.org/issue2651), so we work around it.
711 msg = KeyErrorMessage(msg)
712 elif getattr(self.exc_type, "message", None):
713 # Some exceptions have first argument as non-str but explicitly
714 # have message field
715 raise self.exc_type(message=msg)
716 try:
717 exception = self.exc_type(msg)
718 except TypeError:
719 # If the exception takes multiple arguments, don't try to
720 # instantiate since we don't know how to
721 raise RuntimeError(msg) from None
722 raise exception
723
724
725def _get_available_device_type():

Callers 4

do_one_stepFunction · 0.90
_worker_loopFunction · 0.90
_workerFunction · 0.90
runMethod · 0.90

Calls

no outgoing calls

Tested by 1

runMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…