PyTorch Dynamo rewrites object dispatch using CPython sub-structs
New PR slims down Python method checks by directly reading CPython internal tables.
PyTorch's dynamo JIT compiler just got a core-level performance optimization. Pull request #190415, authored by guilhermeleobas with help from Claude Opus 4.8, rewrites how object protocol methods—like subscript, item access, and numeric operations—are dispatched during graph tracing. Previously, Dynamo used a set of `type_implements_*` predicates to determine whether a Python object supported a given protocol (e.g., mapping, sequence). This approach required evaluating multiple conditions for each slot check, adding overhead.
The fix is elegant: instead of checking predicates, the PR reads the raw CPython `PyNumberMethods`, `PySequenceMethods`, and `PyMappingMethods` sub-structs directly from the `VariableTracker`. Each filled slot is represented as a `Slot`—a late-binding callable to the raw slot function. The dispatch site then collapses to the canonical CPython form: check if the slot pointer is non-null, then call it. This reduces complexity and better matches the underlying interpreter's behavior. A few predicates are retained for low-level checks like `pyindex_check`, truthiness handling (`nb_bool`), and generic binary/ternary operators. The change passes existing test suites for misc, nb_index, and tp_getattro. It also depends on two preceding PRs (#190257, #190259), indicating a larger cleanup effort.
- Drops type_implements_* predicates in favor of direct CPython sub-struct reads (PyNumberMethods, PySequenceMethods, PyMappingMethods).
- Each supported slot becomes a late-binding Slot callable, simplifying dispatch to a null-check and call.
- Retains low-level type predicates and binary op dispatchers where unbound-method identity is needed for reflected operations.
Why It Matters
Aligns Dynamo with CPython internals, reducing overhead and enabling more precise optimization in PyTorch's JIT.