[TORCH_TRACE] Record stack when no compile context is available (#122644)

This will help me track down those annoying unknown compile products.

Signed-off-by: Edward Z. Yang <ezyang@meta.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/122644
Approved by: https://github.com/jamesjwu
This commit is contained in:
Edward Z. Yang 2024-03-26 12:33:51 -04:00 committed by PyTorch MergeBot
parent f631586084
commit 852111e1c2
2 changed files with 11 additions and 0 deletions

View File

@ -72,6 +72,8 @@ class StructuredTraceTestingFormatter(logging.Formatter):
metadata["dynamo_start"]["stack"] = "STACK"
if "inductor_output_code" in metadata:
metadata["inductor_output_code"]["filename"] = "FILENAME"
if "stack" in metadata:
metadata["stack"] = "STACK"
if "compilation_metrics" in metadata:
metadata["compilation_metrics"] = "METRICS"

View File

@ -12,6 +12,9 @@ from importlib import __import__
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
from weakref import WeakSet
import torch._logging.structured
from torch.utils._traceback import CapturedTraceback
log = logging.getLogger(__name__)
# This is a synthetic logger which doesn't correspond to an actual logger,
@ -1063,6 +1066,12 @@ def trace_structured(
record["frame_id"] = trace_id.compile_id.frame_id
record["frame_compile_id"] = trace_id.compile_id.frame_compile_id
record["attempt"] = trace_id.attempt
else:
# Record the stack of the log call to better diagnose why we
# don't have a frame id for it
record["stack"] = torch._logging.structured.from_traceback(
CapturedTraceback.extract(skip=1).summary()
)
payload = payload_fn()
if payload is not None:
if not isinstance(payload, str):