mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
AOTAutograd retraces graph module produced by torch dynamo, this PR preserves the stack trace in the original fx.Node. Differential Revision: [D38595638](https://our.internmc.facebook.com/intern/diff/D38595638) Pull Request resolved: https://github.com/pytorch/pytorch/pull/83050 Approved by: https://github.com/ezyang, https://github.com/voznesenskym
56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
import traceback
|
|
from contextlib import contextmanager
|
|
from typing import Optional, List
|
|
from ._compatibility import compatibility
|
|
|
|
__all__ = ['override_stack_trace', 'append_stack_trace', 'format_stack', 'is_stack_trace_overridden']
|
|
|
|
|
|
current_stack: List[str] = []
|
|
is_overridden = False
|
|
|
|
|
|
@compatibility(is_backward_compatible=False)
|
|
@contextmanager
|
|
def override_stack_trace():
|
|
global is_overridden
|
|
|
|
saved_is_overridden = is_overridden
|
|
try:
|
|
is_overridden = True
|
|
yield
|
|
finally:
|
|
is_overridden = saved_is_overridden
|
|
|
|
|
|
@compatibility(is_backward_compatible=False)
|
|
@contextmanager
|
|
def append_stack_trace(stack : Optional[str]):
|
|
"""
|
|
The content of stack here is an entire stacktraces as a string
|
|
"""
|
|
global current_stack
|
|
|
|
if is_overridden and stack:
|
|
try:
|
|
current_stack.append(stack)
|
|
yield
|
|
finally:
|
|
current_stack.pop()
|
|
else:
|
|
yield
|
|
|
|
|
|
@compatibility(is_backward_compatible=False)
|
|
def format_stack() -> List[str]:
|
|
if is_overridden:
|
|
return current_stack
|
|
else:
|
|
# fallback to traceback.format_stack()
|
|
return traceback.format_stack()
|
|
|
|
|
|
@compatibility(is_backward_compatible=False)
|
|
def is_stack_trace_overridden() -> bool:
|
|
return is_overridden
|