pytorch/torch/_dynamo
Shangdi Yu 4a6cf0a93e Fix dynamo stack trace (#165930)
Fixes #165911

- Add message to Attribute error so we see `  Developer debug context: raised exception AttributeError(["'Linear' object has no attribute 'w'"])` instead of just `Developer debug context: raised exception AttributeError([])`
- Add stack trace in `ObservedException` so we display the inner most error stack trace back to user code

Output:

```
/data/users/shangdiy/pytorch/torch/__init__.py:2641: UserWarning: You are calling torch.compile inside torch.export region. To capture an useful graph, we will implicitly switch to torch.compile(backend=eager)
  warnings.warn(
Traceback (most recent call last):
  File "/data/users/shangdiy/pytorch/torch/_dynamo/variables/user_defined.py", line 1385, in var_getattr
    subobj = self._getattr_static(name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/shangdiy/pytorch/torch/_dynamo/variables/user_defined.py", line 1256, in _getattr_static
    subobj = type(self.value).__getattribute__(self.value, name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Linear' object has no attribute 'w'

During handling of the above exception, another exception occurred:

torch._dynamo.exc.ObservedAttributeError: 'Linear' object has no attribute 'w'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/data/users/shangdiy/pytorch/test.py", line 34, in <module>
    mod = torch._dynamo.functional_export._dynamo_graph_capture_for_export(Model())(x)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/shangdiy/pytorch/torch/_dynamo/functional_export.py", line 481, in inner
    out = fullgraph_capture(
          ^^^^^^^^^^^^^^^^^^
  File "/data/users/shangdiy/pytorch/torch/_dynamo/convert_frame.py", line 1053, in fullgraph_capture
    return _fullgraph_capture_frame(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/shangdiy/pytorch/torch/_dynamo/convert_frame.py", line 1115, in _fullgraph_capture_frame
    raise e.with_traceback(None) from e.__cause__  # User compiler error
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
torch._dynamo.exc.Unsupported: Observed exception
  Explanation: Dynamo found no exception handler at the top-level compiled function when encountering an exception. Exception will propagate outside the compiled region.
  Hint: Dynamo has detected that tracing the code will result in an error when running in eager. Please double check that your code doesn't contain a similar error when actually running eager/uncompiled.
  Hint: It may be possible to write Dynamo tracing rules for this code. Please report an issue to PyTorch if you encounter this graph break often and it is causing performance issues.

  Developer debug context: raised exception AttributeError(["'Linear' object has no attribute 'w'"])

 For more details about this graph break, please visit: https://meta-pytorch.github.io/compile-graph-break-site/gb/gb0088.html

from user code:
   File "/data/users/shangdiy/pytorch/torch/_dynamo/functional_export.py", line 171, in forward
    res = self._export_root(*args, **kwargs)
  File "/data/users/shangdiy/pytorch/test.py", line 31, in forward
    weight = self.linear.w

Set TORCHDYNAMO_VERBOSE=1 for the internal stack trace (please do this especially if you're reporting a bug to PyTorch). For even more developer context, set TORCH_LOGS="+dynamo"

```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/165930
Approved by: https://github.com/anijain2305
2025-10-21 01:32:23 +00:00
..
backends Enable PLC1802 on ruff (#165813) 2025-10-18 05:44:14 +00:00
polyfills do not overguard when comparing lists (#165091) 2025-10-11 20:37:51 +00:00
repro Pyrefly suppressions 7/n (#164913) 2025-10-08 07:27:17 +00:00
variables Fix dynamo stack trace (#165930) 2025-10-21 01:32:23 +00:00
__init__.py [dynamo, nested graph breaks] fix nested step graph break related issues (#162737) 2025-10-08 22:02:52 +00:00
_trace_wrapped_higher_order_op.py Pyrefly suppressions 7/n (#164913) 2025-10-08 07:27:17 +00:00
aot_compile.py [precompile] Pass tensor_to_context to backend. (#165702) 2025-10-17 21:52:04 +00:00
bytecode_analysis.py Pyrefly suppressions 7/n (#164913) 2025-10-08 07:27:17 +00:00
bytecode_transformation.py [dynamo, nested graph breaks] fix nested step graph break related issues (#162737) 2025-10-08 22:02:52 +00:00
cache_size.py [Dynamo][Better Engineering] Add typing for comptime, cache, and convert_frame (#158379) 2025-07-18 02:11:57 +00:00
callback.py increment pending_callbacks_counter before initation the pt2 compile callbacks (#157185) 2025-06-30 01:23:59 +00:00
code_context.py
codegen.py Enable ruff rule E721 (#165162) 2025-10-13 01:48:55 +00:00
compiled_autograd.py [Fix] Adding missing f prefixes to formatted strings [1/N] (#164065) 2025-09-29 04:53:00 +00:00
comptime.py [Dynamo][Better Engineering] Add typing for comptime, cache, and convert_frame (#158379) 2025-07-18 02:11:57 +00:00
config.py [dynamo][ac] Config flag to allow eager and compile AC divergence for side-effects (#165775) 2025-10-17 22:04:19 +00:00
convert_frame.py Enable all flake8-logging-format rules (#164655) 2025-10-19 00:59:28 +00:00
create_parameter_op.py Pyrefly suppressions 7/n (#164913) 2025-10-08 07:27:17 +00:00
current_scope_id.py
debug_utils.py Pyrefly suppressions 7/n (#164913) 2025-10-08 07:27:17 +00:00
decorators.py [dynamo, nested graph breaks] fix nested step graph break related issues (#162737) 2025-10-08 22:02:52 +00:00
device_interface.py Pyrefly suppressions 7/n (#164913) 2025-10-08 07:27:17 +00:00
distributed.py Implement guard collectives (optimized version) (#156562) 2025-06-24 04:59:49 +00:00
eval_frame.py [dynamo] Add recompile reason for set_stance fail_on_recompile (#165445) 2025-10-19 21:12:19 +00:00
exc.py Fix dynamo stack trace (#165930) 2025-10-21 01:32:23 +00:00
external_utils.py Pyrefly suppressions 7/n (#164913) 2025-10-08 07:27:17 +00:00
funcname_cache.py
functional_export.py AOTI util deprecated flow using the new tracer (#165582) 2025-10-19 15:52:16 +00:00
graph_break_hints.py [dynamo] unimplemented -> unimplemented_v2 in variables/builder.py (#151044) 2025-04-11 09:07:01 +00:00
graph_break_registry.json [dynamo] Clean up assert in dynamo [1/N] (#165430) 2025-10-19 21:00:05 +00:00
graph_bytecode_inputs.py [user-cuda-streams] Pass streams/events to the graph via lookup table (#162899) 2025-10-14 05:43:19 +00:00
graph_deduplication.py Helper to augment graph with additional deps (#163959) 2025-09-30 04:53:58 +00:00
graph_region_tracker.py Enable all flake8-logging-format rules (#164655) 2025-10-19 00:59:28 +00:00
graph_utils.py [Intel GPU][pre_compile] Add XPU toolkit version and hardware info in compiled model check. (#162951) 2025-09-18 00:04:22 +00:00
guards.py [dynamo] Remove duplicated guards (#165806) 2025-10-20 05:50:33 +00:00
hooks.py Replace frame_traced_fn hook with get_traced_code() util (#155249) 2025-06-10 22:40:58 +00:00
logging.py Migrate from lru_cache to cache (#155613) 2025-06-11 19:44:18 +00:00
metrics_context.py Optimize dynamo typing (#147499) 2025-08-25 13:20:45 +00:00
mutation_guard.py Revert "[dynamo][executorch] Do not trace into exeuctorch LoweredBackendModule (#165126)" 2025-10-10 19:21:41 +00:00
output_graph.py Enable PLC1802 on ruff (#165813) 2025-10-18 05:44:14 +00:00
package.py Enable all flake8-logging-format rules (#164655) 2025-10-19 00:59:28 +00:00
pgo.py [PGO] log missing sources in allowlist (#164881) 2025-10-09 04:39:09 +00:00
precompile_context.py Enable all flake8-logging-format rules (#164655) 2025-10-19 00:59:28 +00:00
profiler.py Pyrefly suppressions 7/n (#164913) 2025-10-08 07:27:17 +00:00
replay_record.py [Dynamo][Better Engineering] Type coverage for torch/_dynamo/utils.py (#159580) 2025-08-04 21:51:53 +00:00
resume_execution.py [dynamo, nested graph breaks] fix nested step graph break related issues (#162737) 2025-10-08 22:02:52 +00:00
side_effects.py [dynamo][ac] Config flag to allow eager and compile AC divergence for side-effects (#165775) 2025-10-17 22:04:19 +00:00
source.py [dynamo, 3.14] fix BUILD_TUPLE with 0 args (#163818) 2025-09-30 17:42:40 +00:00
symbolic_convert.py Fix dynamo stack trace (#165930) 2025-10-21 01:32:23 +00:00
tensor_version_op.py [Dynamo][Better Engineering] Type devices, resume_execution and testing utils (#158593) 2025-07-18 18:22:06 +00:00
test_case.py Pyrefly suppressions 7/n (#164913) 2025-10-08 07:27:17 +00:00
test_dont_skip_tracing_functions.py [dynamo] context manager/decorator for dynamo config patching during tracing (#150586) 2025-04-23 09:12:13 +00:00
test_minifier_common.py Pyrefly suppressions 7/n (#164913) 2025-10-08 07:27:17 +00:00
testing.py [dynamo][logging] Add most recent bytecode to graph break with torch._dynamo.graph_break() and verbose (#164422) 2025-10-10 17:33:06 +00:00
trace_rules.py Introduce a generic API torch._C._accelerator_setAllocatorSettings (#165291) 2025-10-19 15:34:36 +00:00
types.py Implement guard collectives (optimized version) (#156562) 2025-06-24 04:59:49 +00:00
utils.py torch.compile: populate compiler_config (#165581) 2025-10-17 18:21:18 +00:00