Update context in unimplemented_v2 when exception bubbles up to the interpreter (#158924)

Before:
```
.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:
```

After:
```
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 TypeError([ConstantVariable(str: "unhashable type: <class 'torch._dynamo.variables.dicts.SetVariable'>")])
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/158924
Approved by: https://github.com/williamwen42, https://github.com/zou3519
This commit is contained in:
Guilherme Leobas
2025-07-24 10:08:26 -03:00
committed by PyTorch MergeBot
parent 8573a2beda
commit de85ee73ae
4 changed files with 23 additions and 20 deletions

View File

@ -602,7 +602,7 @@ Observed exception
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 ExceptionVariable(<class 'RuntimeError'>)
Developer debug context: raised exception RuntimeError([ConstantVariable(str: 'test')])
For more details about this graph break, please visit: https://pytorch-labs.github.io/compile-graph-break-site/gb/gb0088.html

View File

@ -851,7 +851,7 @@
"GB0088": [
{
"Gb_type": "Observed exception",
"Context": "str(raised_exception)",
"Context": "raised exception {curr_exc.python_type_name()}({curr_exc.args})",
"Explanation": "observed_exn_gb_explanation",
"Hints": [
"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."

View File

@ -1914,6 +1914,21 @@ class InstructionTranslatorBase(
self.call_function(fn, [typ, val, tb], {})
def exception_handler(self, raised_exception):
def bubble_exception_to_interpreter():
# Bubble the exception to the interpreter
curr_exc = self.exn_vt_stack.get_current_exception()
dynamo_exc = exc.get_dynamo_observed_exception(curr_exc.python_type())
assert isinstance(raised_exception, dynamo_exc) # sanity check
unimplemented_v2(
gb_type="Observed exception",
context=f"raised exception {curr_exc.python_type_name()}({curr_exc.args})",
explanation=observed_exn_gb_explanation,
hints=[
*graph_break_hints.USER_ERROR,
*graph_break_hints.SUPPORTABLE,
],
)
observed_exn_gb_explanation = (
"Dynamo found no exception handler at the top-level compiled function "
"when encountering an exception. Exception will propagate outside the compiled region."
@ -1945,15 +1960,7 @@ class InstructionTranslatorBase(
# instruction translator. We use special exception for this.
self.stack.clear()
if type(self) is InstructionTranslator:
unimplemented_v2(
gb_type="Observed exception",
context=str(raised_exception),
explanation=observed_exn_gb_explanation,
hints=[
*graph_break_hints.USER_ERROR,
*graph_break_hints.SUPPORTABLE,
],
)
bubble_exception_to_interpreter()
raise raised_exception
else:
if len(self.block_stack):
@ -2025,15 +2032,7 @@ class InstructionTranslatorBase(
# instruction translator. We use special exception for this.
self.stack.clear()
if type(self) is InstructionTranslator:
unimplemented_v2(
gb_type="Observed exception",
context=str(raised_exception),
explanation=observed_exn_gb_explanation,
hints=[
*graph_break_hints.USER_ERROR,
*graph_break_hints.SUPPORTABLE,
],
)
bubble_exception_to_interpreter()
raise raised_exception
def PUSH_EXC_INFO(self, inst):

View File

@ -1576,6 +1576,10 @@ class UserDefinedExceptionObjectVariable(UserDefinedObjectVariable):
def __context__(self):
return self.exc_vt.__context__
@property
def args(self):
return self.exc_vt.args
def set_context(self, context: "variables.ExceptionVariable"):
return self.exc_vt.set_context(context)