Upstream commits
PyTorch
pytorch/pytorch
torch.compile fixes in Inductor, covering addmm and baddbmm decompositions and C++ buffer promotion, and Dynamo tracing of math functions and super().
7 commits
-
[dynamo] Support Class.mro() on user-defined classes (#196863)Calling `SomeClass.mro()` currently graph breaks in Dynamo with gb0156, while accessing `SomeClass.__mro__` is already supported. This adds a fast path for classes using the default `type.mro` implementation. When `type(cls) is type` and no class in the MRO defines `mro`, the call returns a new list built from `__mro__`, with a `DICT_NOT_CONTAINS("mro")` guard for each MRO entry. CPython recomputes `__mro__` using C3 linearization whenever the bases change, so the results remain consistent. Custom metaclasses continue to graph break, since guarding their behavior would require calling a potentially overridden `mro()` method. The change also fixes stale class lookups exposed by this path: * assigning `__bases__` during tracing now graph breaks instead of keeping outdated base information; * a `setattr` on a base class earlier in the frame is reflected in subsequent subclass lookups and `hasattr`, with descriptor binding and the appropriate guards, while a metaclass data descriptor still takes precedence; * `guard_mro_absent` no longer deduplicates guards solely by base class, which could leave shared bases insufficiently guarded. Towards #195901. Test plan My local build can't import main, so I ran the following tests on a 2026-09-24 tree (117c5e7e1e) with #197781, #198410 and main's `iter_mro_static_dicts` ported. Failures match the same tree without this change. python test/dynamo/test_misc.py python test/dynamo/test_tp_getattro.py python test/dynamo/test_repros.py python test/dynamo/test_functions.py python test/dynamo/test_dicts.py python test/dynamo/test_sets.py python test/dynamo/test_exceptions.py python test/dynamo/test_dynamic_shapes.py -k test_type_mro -k test_class_setattr python test/dynamo/test_nested_graph_breaks_wrapped.py -k test_type_mro -k test_class_setattr python test/dynamo/test_guard_manager.py PYTORCH_TEST_WITH_DYNAMO=1 python test/cpython/v3_13/test_typechecks.py PYTORCH_TEST_WITH_DYNAMO=1 python test/cpython/v3_13/test_descr.py PYTORCH_TEST_WITH_DYNAMO=1 python test/cpython/v3_13/test_enum.py PYTORCH_TEST_WITH_DYNAMO=1 python test/cpython/v3_13/test_super.py
-
[inductor] decompose_addmm skips non-default alpha and beta (#196588)Issue Fixes #196577 Summary `decompose_addmm` matched `aten.addmm` without declaring `beta` or `alpha`, and the pattern matcher only compares the kwargs a pattern declares, so `addmm(x, a, b, beta=0)` was rewritten to `a @ b + x`. The pattern now pins `beta=1, alpha=1`: nodes that omit them still match (the matcher fills in schema defaults), explicit `1.0` matches, and any other value or a symbolic coefficient goes through the regular addmm lowering, which already implements the scalars. Teaching the replacement about `beta`/`alpha` would duplicate that handling (including `beta == 0` ignoring a NaN/Inf input) in a pass meant for mem-bound shapes. The test is parametrized over the kwargs dict, so it covers nodes with neither, one, or both coefficients. Checklist * [x] Passes lint (`spin fixlint`) * [x] Added/updated tests * [ ] Updated documentation (if applicable) * [ ] Included benchmark results (for PRs impacting perf) BC-breaking? No
-
[inductor][cpp] Fix local buffer promotion for mutation outputs (#196639)Fixes #196570 Summary CPP outer-loop fusion can incorrectly consider a mutation output for local buffer promotion. Scheduler users are tracked under the mutation output name (`buf1` in the repro), while their `LoopBody` may still load the mutation's real buffer name (`buf0`). `is_all_write_read_contiguous()` assumed every user had a read under the output name and called `LoopBody.get_read_expr()`, which raises `KeyError` in this case. This change uses `get_all_read_expr()` and makes the buffer ineligible for promotion when a user does not load it by that name. Besides avoiding the crash, this is required for correctness: local buffer rewriting is name-based, so promoting `buf1` while its users continue loading `buf0` could redirect the mutation store to the local buffer while later reads still use the global buffer. Test Added `test_local_buffer_with_mutation_output`. The test uses `x.clone()` so that losing the mutation produces a clear numerical difference: the correct result is `softmax(x)`, while a lost mutation produces `softmax(2 * x)`. It also verifies that the intended outer-loop fusion/local-buffer path is exercised. Tested against CPU nightly `2.15.0.dev20260902+cpu`: - New regression test passes with the fix and fails with `KeyError: 'buf1'` without it. - `test_cpu_repro.py -k local_buffer -k outer_loop`: 9 tests pass. - Issue repro matches eager. - `lintrunner -a`: no issues.
-
[inductor] Ignore addmm and baddbmm bias when beta is zero (#196600)Issue Addresses the `beta == 0` part of #196596. `alpha == 0` has no documented contract and stays open there. Summary With `beta == 0`, `addmm` and `baddbmm` ignore `input`, NaN and inf included. Inductor still computed `beta * input` in three places, and since `0 * NaN` is NaN, a NaN bias reached the output: - the GEMM template epilogues (Triton, CPP, CUTLASS, CK); - the CPU and XPU branches of the addmm decomposition; - the `unfuse_bias_*` post_grad passes. Only CUDA addmm avoided this. It lowered to `alpha * mm` (#183511), which rounds the product to fp16 before applying alpha, so it can overflow where eager doesn't. `tuned_addmm` and `tuned_baddbmm` now handle `beta == 0` once, on every device. They pass the templates zeros in place of the input (`zero_addmm_input`), so each choice still scales its accumulator by alpha, as eager does. This replaces the `alpha * mm` path. ATen choices keep the real input, since ATen ignores it. The exception is baddbmm with the Triton template, because autotuning benchmarks every choice on the same inputs. The unfuse passes now leave `beta == 0` fused. The decomposition drops the term and checks the bias shape and device where eager does, which is everywhere except CUDA. `contiguous_addmm` (ROCm) ignores alpha and beta, so it is only offered when both are 1. Alternatives: short-circuiting the epilogue fixes only Triton. Using `alpha * mm` on every device keeps the fp16 overflow and adds a kernel. Test Plan Run on CPU (no local GPU) against a nightly build, with the changed inductor files overlaid on it: python test/inductor/test_mmdecomp.py python test/inductor/test_cpu_select_algorithm.py -k bmm -k addmm -k zero_beta python test/inductor/test_torchinductor_opinfo.py -k addmm -k baddbmm -k _bmm -k matmul python test/inductor/test_torchinductor.py -k addmm -k baddbmm -k bmm PYTHONPATH=test/inductor python -m unittest test_pattern_matcher -k zero_beta PYTHONPATH=test/inductor python -m unittest test_max_autotune.TestMaxAutotune -k 0d_bias Checklist - [x] Passes lint (`spin quicklint`) - [x] Added/updated tests - [ ] Updated documentation (not applicable) - [ ] Included benchmark results (not applicable) BC-breaking? No
-
[dynamo] Leave plain functions unbound when super() is bound to a class (#198435)When `super()` is bound to a class (e.g. zero-arg `super()` in a classmethod), CPython calls the descriptor with `obj=NULL`, so a plain function from the MRO stays unbound ([typeobject.c](https://github.com/python/cpython/blob/v3.13.0/Objects/typeobject.c#L11162-L11166)). Dynamo always passed `objvar` as the first argument, which can silently change the result: class Parent: def greet(*args): return len(args) class Child(Parent): @classmethod def greet(cls): return super().greet() # eager: 0, compiled: 1 `SuperVariable.call_method` now leaves the function unbound when `objvar` is a class. This enables `test_super.py::TestSuper::test_reassigned_new` and `test_mixed_staticmethod_hierarchy` and removes their expected-failure markers. In a metaclass method, `super(Meta, A)` binds to `A` even though `A` is a class. That case goes through the real `super()` fallback in `_resolved_getattr_and_source`, which already applies the descriptor, so it never reaches the new branch unbound. `test_super_metaclass_method` covers it, including a staticmethod on the parent metaclass that was previously called with `A` prepended. Towards #195901. Test Plan: python test/dynamo/test_repros.py -k test_super PYTORCH_TEST_WITH_DYNAMO=1 python test/cpython/v3_13/test_super.py
-
[dynamo] Support math.atan2/copysign/remainder on user-defined objects (#197498)Adds Dynamo support for `math.atan2`, `math.copysign`, and `math.remainder` with user-defined numeric objects. These functions share CPython's `math_2` helper, which converts arguments with `PyFloat_AsDouble` from left to right. The new handler mirrors that behavior using the existing `pyfloat_as_double` implementation and enables `test_math.py::MathTests::test_issue39871`. Removes the corresponding CPython 3.13 expected-failure marker. Towards #195901.
-
[dynamo] Support math.ceil/floor/trunc on user-defined objects (#196867)`math.ceil`, `math.floor`, and `math.trunc` currently graph break on user-defined objects because they cannot be converted to FX proxies. This follows the corresponding CPython `mathmodule.c` implementations: * `ceil` dispatches to `__ceil__`, then falls back through `PyFloat_AsDouble`. * `floor` dispatches to `__floor__`, then falls back through `PyFloat_AsDouble`. * `trunc` dispatches to `__trunc__` and raises `TypeError` when it is not defined. The `PyFloat_AsDouble` behavior is modeled in `object_protocol.py`, including `__float__` / `__index__` fallback, integer overflow, and float-subclass conversion without redispatching subclass overrides. Unlike `PyNumber_Float`, this path does not parse strings. The three functions are handled separately because their CPython implementations differ. They also preserve `METH_O` argument validation behavior. The CPython 3.13 `testCeil` and `testFloor` expected-failure markers are removed now that those tests pass under Dynamo. Towards #195901. Test plan python test/dynamo/test_functions.py -k math_ceil PYTORCH_TEST_WITH_DYNAMO=1 python test/cpython/v3_13/test_math.py The focused tests were also exercised on Python 3.12 and 3.13, and the affected files pass lint.
Updated daily, last on 2026-11-16. By Christian Aurich Zanettini Martins · All projects