SIGN IN SIGN UP

[dynamo] Support math.sumprod with non-constant arguments (#198685)

Part of #195901. This is the sumprod row I picked up in https://github.com/pytorch/pytorch/issues/195901#issuecomment-5839701902. Opening as a draft: the change and the text below were drafted with AI assistance (Claude) and the tests were run locally, and I still need to finish reviewing it before marking it ready.

> math.sumprod is registered as an in-graph function, so constant arguments are folded, but anything else (Fractions built in the frame, generators, user iterators, objects with `__mul__`) falls through to `create_proxy` and graph breaks with "Failed to convert args/kwargs to proxy".
>
> This adds a `math.sumprod` handler next to `handle_radians` (guarded with `hasattr(math, "sumprod")` for 3.12+). It keeps constant folding when all args are constants and otherwise inlines `polyfills.sumprod`, which follows the generic path of CPython's `math_sumprod_impl`: both iterators are advanced every step, a length mismatch raises `ValueError("Inputs are not the same length")`, and the total uses plain binary add. Keyword arguments and a wrong positional count raise the same TypeErrors as CPython. Same shape as the `math.prod` polyfill in #198492.
>
> Behavior note: the polyfill does not reproduce CPython's extended-precision float path, so any list containing a non-constant element is accumulated plainly for the whole call, float constants in it included (same as the `sum` polyfill in `polyfills/builtins.py`). Those inputs graph broke before this change.

## Test Plan

> Run on CPython 3.13.13 with the 2.15.0.dev20260925 CPU nightly (same `polyfills/__init__.py` and `variables/torch.py` as current main), overlaying the two changed files.

```
python test/dynamo/test_functions.py -k sumprod
```
main: 4 errors (`Unsupported: Failed to convert args/kwargs to proxy`). With this change: 4 passed.

```
python test/dynamo/test_functions.py -k math
python test/dynamo/test_functions.py
```
main: `-k math` has the same 4 errors. With this change: `-k math` 94 passed, full file OK (skipped=8, expected failures=2).

```
PYTORCH_TEST_WITH_DYNAMO=1 python test/cpython/v3_13/test_math.py
```
Same on both: 84 ran, OK (skipped=21). `testSumProd` still stops at `Decimal(3.5)`, so its expected-failure file stays. With the two Decimal rows removed locally it passes with this change and fails on main with the proxy graph break.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/198685
Approved by: https://github.com/jansel

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
E
Eugenio "Jay" Zuccarelli committed
22669c3e651acf2c35aa1f629a0e261a9617c902
Parent: 617a6f7
Committed by PyTorch MergeBot <pytorchmergebot@users.noreply.github.com> on 10/1/2026, 2:11:25 AM