pytorch - ✅(Solved) Fix [dynamo] Exception object interface (str, repr, args, __class__) not fully traced [1 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#177633Fetched 2026-04-08 00:52:49
View on GitHub
Comments
1
Participants
2
Timeline
110
Reactions
0
Timeline (top)
mentioned ×46subscribed ×46labeled ×8referenced ×3

Error Message

PYTORCH_TEST_WITH_DYNAMO=1 pytest
test/dynamo/cpython/3_13/test_baseexception.py::ExceptionClassTests::test_interface_single_arg
test/dynamo/cpython/3_13/test_baseexception.py::ExceptionClassTests::test_interface_multi_arg
test/dynamo/cpython/3_13/test_baseexception.py::ExceptionClassTests::test_interface_no_arg

Fix Action

Fix / Workaround

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 46 bits physical, 57 bits virtual Byte Order: Little Endian CPU(s): 160 On-line CPU(s) list: 0-159 Vendor ID: GenuineIntel Model name: Intel Xeon Processor (SapphireRapids) CPU family: 6 Model: 143 Thread(s) per core: 2 Core(s) per socket: 40 Socket(s): 2 Stepping: 4 BogoMIPS: 4200.00 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch cpuid_fault ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx_vnni avx512_bf16 wbnoinvd arat vnmi avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b fsrm md_clear serialize tsxldtrk amx_bf16 avx512_fp16 amx_tile amx_int8 arch_capabilities Virtualization: VT-x Hypervisor vendor: KVM Virtualization type: full L1d cache: 5 MiB (160 instances) L1i cache: 5 MiB (160 instances) L2 cache: 320 MiB (80 instances) L3 cache: 32 MiB (2 instances) NUMA node(s): 2 NUMA node0 CPU(s): 0-79 NUMA node1 CPU(s): 80-159 Vulnerability Gather data sampling: Not affected Vulnerability Indirect target selection: Mitigation; Aligned branch/return thunks Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Unknown: No mitigations Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS SW sequence; BHI SW loop, KVM SW loop Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected

PR fix notes

PR #177635: fix(dynamo): Support BaseException str/repr/args interface during tracing

Description (problem / solution / changelog)

Fixes https://github.com/pytorch/pytorch/issues/177633

Earlier Dynamo could not correctly trace the CPython BaseException interface, causing graph breaks or wrong results on exception objects. This change enables Dynamo to handle str(), repr(), .args, .class.name, and %-style string formatting for exceptions at trace time, matching CPython semantics.

Test Plan

PYTORCH_TEST_WITH_DYNAMO=1 pytest \
  test/dynamo/cpython/3_13/test_baseexception.py::ExceptionClassTests::test_interface_no_arg \
  test/dynamo/cpython/3_13/test_baseexception.py::ExceptionClassTests::test_interface_single_arg \
  test/dynamo/cpython/3_13/test_baseexception.py::ExceptionClassTests::test_interface_multi_arg \
  test/dynamo/cpython/3_13/test_enum.py::TestStrEnumClass::test_repr \
  test/dynamo/cpython/3_13/test_enum.py::TestStrEnumFunction::test_repr \
  test/dynamo/cpython/3_13/test_enum.py::TestMixedStrClass::test_repr \
  test/dynamo/cpython/3_13/test_enum.py::TestMixedStrFunction::test_repr \
  test/dynamo/cpython/3_13/test_float.py::GeneralFloatCases::test_error_message
  
pytest test/dynamo/test_exceptions.py -k "test_str_repr_exception" -v

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @Lucaskabela @jataylo @azahed98

Changed files

  • test/dynamo/test_exceptions.py (modified, +42/-0)
  • test/dynamo_expected_failures/CPython313-test_baseexception-ExceptionClassTests.test_interface_multi_arg (removed, +0/-0)
  • test/dynamo_expected_failures/CPython313-test_baseexception-ExceptionClassTests.test_interface_no_arg (removed, +0/-0)
  • test/dynamo_expected_failures/CPython313-test_baseexception-ExceptionClassTests.test_interface_single_arg (removed, +0/-0)
  • test/dynamo_expected_failures/CPython313-test_enum-TestMixedStrClass.test_repr (removed, +0/-0)
  • test/dynamo_expected_failures/CPython313-test_enum-TestMixedStrFunction.test_repr (removed, +0/-0)
  • test/dynamo_expected_failures/CPython313-test_enum-TestStrEnumClass.test_repr (removed, +0/-0)
  • test/dynamo_expected_failures/CPython313-test_enum-TestStrEnumFunction.test_repr (removed, +0/-0)
  • test/dynamo_expected_failures/CPython313-test_exceptions-ExceptionTests.test_unicode_error_str_does_not_crash (removed, +0/-0)
  • test/dynamo_expected_failures/CPython313-test_float-GeneralFloatCases.test_error_message (removed, +0/-0)
  • torch/_dynamo/symbolic_convert.py (modified, +2/-1)
  • torch/_dynamo/variables/builtin.py (modified, +29/-6)
  • torch/_dynamo/variables/misc.py (modified, +6/-2)

Code Example

PYTORCH_TEST_WITH_DYNAMO=1 pytest \
  test/dynamo/cpython/3_13/test_baseexception.py::ExceptionClassTests::test_interface_single_arg \
  test/dynamo/cpython/3_13/test_baseexception.py::ExceptionClassTests::test_interface_multi_arg \
  test/dynamo/cpython/3_13/test_baseexception.py::ExceptionClassTests::test_interface_no_arg
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Dynamo does not fully trace the standard exception-object interface. Several operations on exception instances like str(exc), repr(exc), exc.args, exc.class.name, and string % formatting used in assertion messages, either produce wrong results or cause graph breaks.

Failing CPython 3.13 conformance tests:

PYTORCH_TEST_WITH_DYNAMO=1 pytest \
  test/dynamo/cpython/3_13/test_baseexception.py::ExceptionClassTests::test_interface_single_arg \
  test/dynamo/cpython/3_13/test_baseexception.py::ExceptionClassTests::test_interface_multi_arg \
  test/dynamo/cpython/3_13/test_baseexception.py::ExceptionClassTests::test_interface_no_arg

Versions

Collecting environment information... PyTorch version: 2.12.0a0+git99bc3cc Is debug build: False CUDA used to build PyTorch: 12.8 ROCM used to build PyTorch: N/A

OS: Fedora Linux 41 (Container Image) (x86_64) GCC version: (GCC) 14.3.1 20251022 (Red Hat 14.3.1-4) Clang version: Could not collect CMake version: version 4.2.3 Libc version: glibc-2.40

Python version: 3.13.9 (main, Oct 14 2025, 00:00:00) [GCC 14.3.1 20250808 (Red Hat 14.3.1-3)] (64-bit runtime) Python platform: Linux-5.14.0-615.el9.x86_64-x86_64-with-glibc2.40 Is CUDA available: True CUDA runtime version: 12.8.93 CUDA_MODULE_LOADING set to: GPU models and configuration: GPU 0: NVIDIA H200 GPU 1: NVIDIA H200 GPU 2: NVIDIA H200 GPU 3: NVIDIA H200 GPU 4: NVIDIA H200 GPU 5: NVIDIA H200 GPU 6: NVIDIA H200 GPU 7: NVIDIA H200

Nvidia driver version: 580.82.07 cuDNN version: Probably one of the following: /usr/local/cuda-12.8/targets/x86_64-linux/lib/libcudnn.so.9.6.0 /usr/local/cuda-12.8/targets/x86_64-linux/lib/libcudnn_adv.so.9.6.0 /usr/local/cuda-12.8/targets/x86_64-linux/lib/libcudnn_cnn.so.9.6.0 /usr/local/cuda-12.8/targets/x86_64-linux/lib/libcudnn_engines_precompiled.so.9.6.0 /usr/local/cuda-12.8/targets/x86_64-linux/lib/libcudnn_engines_runtime_compiled.so.9.6.0 /usr/local/cuda-12.8/targets/x86_64-linux/lib/libcudnn_graph.so.9.6.0 /usr/local/cuda-12.8/targets/x86_64-linux/lib/libcudnn_heuristic.so.9.6.0 /usr/local/cuda-12.8/targets/x86_64-linux/lib/libcudnn_ops.so.9.6.0 Is XPU available: False HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True Caching allocator config: N/A

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 46 bits physical, 57 bits virtual Byte Order: Little Endian CPU(s): 160 On-line CPU(s) list: 0-159 Vendor ID: GenuineIntel Model name: Intel Xeon Processor (SapphireRapids) CPU family: 6 Model: 143 Thread(s) per core: 2 Core(s) per socket: 40 Socket(s): 2 Stepping: 4 BogoMIPS: 4200.00 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch cpuid_fault ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx_vnni avx512_bf16 wbnoinvd arat vnmi avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b fsrm md_clear serialize tsxldtrk amx_bf16 avx512_fp16 amx_tile amx_int8 arch_capabilities Virtualization: VT-x Hypervisor vendor: KVM Virtualization type: full L1d cache: 5 MiB (160 instances) L1i cache: 5 MiB (160 instances) L2 cache: 320 MiB (80 instances) L3 cache: 32 MiB (2 instances) NUMA node(s): 2 NUMA node0 CPU(s): 0-79 NUMA node1 CPU(s): 80-159 Vulnerability Gather data sampling: Not affected Vulnerability Indirect target selection: Mitigation; Aligned branch/return thunks Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Unknown: No mitigations Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS SW sequence; BHI SW loop, KVM SW loop Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected

Versions of relevant libraries: [pip3] numpy==2.4.3 [pip3] optree==0.19.0 [pip3] torch==2.12.0a0+git99bc3cc [pip3] triton==3.6.0

cc @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @amjames @Lucaskabela @jataylo

extent analysis

Fix Plan

To fix the issue with Dynamo not fully tracing the standard exception-object interface, we need to modify the Dynamo code to properly handle exception instances.

Here are the steps:

  • Modify the str(exc) and repr(exc) functions to return the correct string representation of the exception.
  • Update the exc.args and exc.__class__.__name__ attributes to return the correct values.
  • Fix the string % formatting used in assertion messages to handle exception instances correctly.

Example code:

class ExceptionHandler:
    def __init__(self, exc):
        self.exc = exc

    def __str__(self):
        return str(self.exc)

    def __repr__(self):
        return repr(self.exc)

    def get_args(self):
        return self.exc.args

    def get_class_name(self):
        return self.exc.__class__.__name__

# Usage
try:
    # Code that raises an exception
    raise ValueError("Test exception")
except Exception as e:
    handler = ExceptionHandler(e)
    print(str(handler))  # Correct string representation
    print(repr(handler))  # Correct repr representation
    print(handler.get_args())  # Correct exception arguments
    print(handler.get_class_name())  # Correct exception class name

Verification

To verify that the fix worked, run the failing CPython 3.13 conformance tests again:

PYTORCH_TEST_WITH_DYNAMO=1 pytest \
  test/dynamo/cpython/3.13/test_baseexception.py::ExceptionClassTests::test_interface_single_arg \
  test/dynamo/cpython/3.13/test_baseexception.py::ExceptionClassTests::test_interface_multi_arg \
  test/dynamo/cpython/3.13/test_baseexception.py::ExceptionClassTests::test_interface_no_arg

If the tests pass, it indicates that the fix was successful.

Extra Tips

  • Make sure to test the fix thoroughly to ensure that it does not introduce any new issues.
  • Consider adding additional test cases to cover different scenarios and exception types.
  • If you encounter any issues during the verification process, review the code changes and test results to identify the root cause of the problem.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING