Skip to content

Commit

Permalink
Node.stack_trace should have innermost frame last
Browse files Browse the repository at this point in the history
ghstack-source-id: 8a1b4d307733ab7b9982a3c483a22960323abbe7
Pull Request resolved: #95592
  • Loading branch information
SherlockNoMad committed Feb 27, 2023
1 parent d3e1f16 commit fa8858b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
4 changes: 3 additions & 1 deletion torch/_dynamo/output_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,10 +818,12 @@ def create_proxy(
while tx:
frame_summaries.append(tx.frame_summary())
tx = getattr(tx, "parent", None)
# Reverse the frame_summaries, such that the innermost frame is at the last
frame_summaries.reverse()

# official from_list stub doesn't have new-style type
msgs = traceback.StackSummary.from_list(frame_summaries).format() # type: ignore[arg-type]
rv.node.stack_trace = " | ".join(msgs)
rv.node.stack_trace = "".join(msgs)

return rv

Expand Down
26 changes: 10 additions & 16 deletions torch/fx/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,26 +451,20 @@ def append_stacktrace_summary(node : Node):
prev_stacktrace = node.stack_trace

lines = node.stack_trace.strip().split('\n')
idx = 0
while idx < len(lines):
# stacktrace should have innermost frame last, so we
# iterate backwards to find the first line that starts
# with 'File '
summary_str = ""
for idx in range(len(lines) - 2, -1, -1):
line = lines[idx].strip()
if line.startswith('File '):
break
idx += 1

summary_lines = []
if idx + 1 < len(lines):
matches = pattern.match(lines[idx].strip())
matches = pattern.match(line)
if matches:
file = matches.group(1)
lineno = matches.group(2)
lineage = f'File: {file}:{lineno}'
summary_lines.append(lineage)

code = f"code: {lines[idx + 1].strip()}"
summary_lines.append(code)

summary_str = ', '.join(summary_lines)
# next line should be the code
code = lines[idx + 1].strip()
summary_str = f'File: {file}:{lineno}, code: {code}'
break
body.append(f'\n# {summary_str}\n')
elif prev_stacktrace != "":
prev_stacktrace = ""
Expand Down
10 changes: 7 additions & 3 deletions torch/fx/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,13 @@ def update_kwarg(self, key : str, arg : Argument) -> None:
def stack_trace(self) -> Optional[str]:
"""
Return the Python stack trace that was recorded during tracing, if any.
This property is usually populated by `Tracer.create_proxy`. To record
stack traces during tracing for debug purposes, set
`record_stack_traces = True` on the `Tracer` instance.
When traced with fx.Tracer, this property is usually populated by
`Tracer.create_proxy`. To record stack traces during tracing for debug purposes,
set `record_stack_traces = True` on the `Tracer` instance.
When traced with dynamo, this property will be populated by default by
`OutputGraph.create_proxy`.
stack_trace would have the innermost frame at the end of the string.
"""
return self.meta.get("stack_trace", None)

Expand Down
4 changes: 2 additions & 2 deletions torch/fx/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ def create_proxy(self, kind: str, target: Target, args: Tuple[Any, ...], kwargs:
if self.record_stack_traces and not proxy.node.stack_trace:
user_frame = self._find_user_frame()
if user_frame:
walk_stack_gen = traceback.walk_stack(user_frame)
summary = traceback.StackSummary.extract(walk_stack_gen) # type: ignore[arg-type]
summary = traceback.extract_stack(user_frame)
tb_lines = summary.format()
# stack_trace would have innermost frame at the bottom
proxy.node.stack_trace = ''.join(tb_lines)

return proxy
Expand Down

0 comments on commit fa8858b

Please sign in to comment.