PyTorch Fixes Triton Autotuning Recursion Error for Large Expressions
Deeply nested 1000-term expressions were crashing Triton autotuning, now fixed.
PyTorch's Inductor compiler encountered a `RecursionError` during Triton autotuning when processing certain unbacked `tolist()` sums. The generated Triton source contained a deeply nested symbolic addition expression (up to 1000 terms), which Python parses as a deeply nested `BinOp` tree. Triton's dependency walker recursively visits this AST while building the precompile cache key, eventually exceeding the recursion limit and crashing before autotuning can finish.
The fix, merged in PR #185778, hoists wide size expressions before autotuning by leveraging PyTorch's precomputed-size mechanism. Instead of inlining the entire expression into the kernel source, the wrapper computes a single scalar host-side and passes it to the Triton kernel. This approach is local to size-like scalar expressions and avoids broader alternatives such as disabling compile-time autotuning or raising Python's recursion limit, which would only mask the root cause. Benchmarks on the reproduction case (`microbench_unbacked_tolist_sum`) show the fix passes the original failure, and compilation time for a similar `n=128` case improved slightly from 19.666s to 18.564s.
- Fixes a `RecursionError` in Triton autotuning caused by deeply nested 1000-term symbolic addition from unbacked `tolist()` values.
- Uses the existing precomputed-size mechanism to compute a single scalar host-side and pass it to the Triton kernel, avoiding AST depth issues.
- Benchmarks show a slight compilation time improvement (18.56s vs 19.67s) and the fix enables successful AOTI compilation for large unbacked sums.
Why It Matters
Enables PyTorch's AOTI compilation for models with large unbacked tolist() sums, preventing crashes during autotuning.