PyTorch Dynamo adds BoundMethodVariable to fix recursive method dispatch
Infinite recursion in PyTorch Dynamo fixed with new variable type for method calls
PyTorch's latest commit to its Dynamo JIT compiler introduces BoundMethodVariable, a new variable type designed to handle method dispatch on bound method objects. The patch addresses a critical infinite recursion bug that occurred when object_generic_getattr found a method with custom call_method logic. Previously, tp_descr_get_impl would call obj.getattro_impl, re-entering object_generic_getattr and looping indefinitely. Now, the system raises an _UnhandledDescriptorError instead; this error propagates to generic_getattr's NotImplementedError catch, falling back safely to GetAttrVariable.
The BoundMethodVariable models Python's method object with python_type, richcompare_impl, and hash_impl, ensuring accurate method dispatch. The call_function method now correctly routes through call_method on the bound object. Co-authored by Claude Code (Anthropic's AI assistant), the commit includes extensive test coverage: 47 OK in test_tp_getattro.py, 516 in test_functions.py, and 805 in test_misc.py. This bug fix is particularly valuable for production systems relying on PyTorch's eager-mode optimization, as it resolves a subtle source of crashes and performance regressions in method-heavy models.
- Introduces BoundMethodVariable to represent bound methods in PyTorch Dynamo
- Fixes infinite recursion in MemberDescriptorVariable and GetSetDescriptorVariable
- All 47 test cases in test_tp_getattro.py and 805 in test_misc.py pass with the change
Why It Matters
Prevents crashes in PyTorch's JIT compiler when handling method dispatch, improving model reliability.