pytorch/torch/_dynamo/variables
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
..
__init__.py [user-streams] Move stream code to streams module (#163027) 2025-10-14 05:43:19 +00:00
base.py [dynamo] Clean up assert in dynamo [1/N] (#165430) 2025-10-19 21:00:05 +00:00
builder.py [Bugfix][Dynamo] Fix Sparse tensors by graph break in Dynamo (#164873) 2025-10-16 15:06:20 +00:00
builtin.py [dynamo] Clean up assert in dynamo [1/N] (#165430) 2025-10-19 21:00:05 +00:00
constant.py [dynamo] Clean up assert in dynamo [1/N] (#165430) 2025-10-19 21:00:05 +00:00
ctx_manager.py [user-streams] Move stream code to streams module (#163027) 2025-10-14 05:43:19 +00:00
dicts.py [dynamo] Clean up assert in dynamo [1/N] (#165430) 2025-10-19 21:00:05 +00:00
distributed.py Fixing get_local_rank() variable missing when compiled (#165432) 2025-10-16 18:20:34 +00:00
functions.py Patch the flex_attention._get_mod_type to not use inspect.signature when computing num_positional_args (an alternative fix for flex attention graph break on create_block_mask) (#164923) 2025-10-17 17:44:45 +00:00
higher_order_ops.py Enable all flake8-logging-format rules (#164655) 2025-10-19 00:59:28 +00:00
iter.py [dynamo, nested graph breaks] move cell codegen before side effects codegen (#160601) 2025-10-08 22:02:52 +00:00
lazy.py [Dynamo][Logging] Add sources/types to LazyVariableTracker logging (#165402) 2025-10-15 23:23:09 +00:00
lists.py [dynamo] Clean up assert in dynamo [1/N] (#165430) 2025-10-19 21:00:05 +00:00
misc.py Revert "[dynamo][misc] Replace UserFunctionVariable with VariableTracker build (#165707)" 2025-10-20 17:28:58 +00:00
nn_module.py Fix dynamo stack trace (#165930) 2025-10-21 01:32:23 +00:00
optimizer.py [Dynamo] Don't guard data ptrs by default with mark_static_address (#162208) 2025-09-12 07:15:10 +00:00
script_object.py [dynamo] Replace unimplemented with unimplemented_v2 in torch/_dynamo/variables/script_object.py (#159343) 2025-08-01 21:30:41 +00:00
sdpa.py More ruff SIM fixes (#164695) 2025-10-09 03:24:50 +00:00
streams.py [RFC] Add pyrefly to lintrunner (#165179) 2025-10-16 20:07:09 +00:00
tensor.py Fix issues with generalized_scatter and setitem allocated unbacked symbols. (#164341) 2025-10-18 03:20:30 +00:00
torch_function.py [dynamo] Be consistent with UserMethodVariable source (#160155) 2025-08-09 04:16:14 +00:00
torch.py [dynamo] Clean up assert in dynamo [1/N] (#165430) 2025-10-19 21:00:05 +00:00
user_defined.py Fix dynamo stack trace (#165930) 2025-10-21 01:32:23 +00:00