PyTorch fixes SymPy power bug in Inductor value range analysis
A subtle bug in PyTorch's Inductor caused 'failed while executing pow_by_natural' warnings for negative exponents.
A subtle but impactful bug in PyTorch's Inductor compiler has been fixed with PR #185786, authored by jansel. The issue stemmed from the SymPy interpreter used for value range analysis, which is critical for optimizations like shape inference and loop unrolling. The interpreter routed every sympy.Pow through a function called pow_by_natural, which is only valid when the exponent is known to be a nonnegative integer. However, SymPy represents reciprocal expressions such as x**-1 as Pow. When the value range analysis encountered a positive base with exponent range [-1, -1], pow_by_natural tried to clamp the exponent to [0, ∞], producing an empty range and the repeated 'failed while executing pow_by_natural' warnings reported in issues #148003 and #136628.
The fix is surgical: it routes sympy.Pow to pow_by_natural only when the exponent is known to be a nonnegative integer. All other SymPy powers—including reciprocals and unknown-sign exponents—now use the general pow handler, which is conservative for value range analysis. This preserves the existing precise natural-power path while avoiding invalid assumptions. Benchmark results show the fix not only eliminates errors but also slightly improves performance: median execution time for natural_const_exp dropped from ~99.9μs to ~79.2μs, and for symbolic exponent from ~79.7μs to ~58.9μs. The fix is production-ready, with tests added for negative powers and lintrunner passing. This PR makes PyTorch's Inductor more robust for models that include division or reciprocal operations.
- Bug: pow_by_natural handler incorrectly applied to all SymPy powers, including reciprocals (x**-1), causing empty ranges and warnings.
- Fix: Route only nonnegative integer exponents to pow_by_natural; all others use a general conservative handler.
- Performance: natural_const_exp benchmark improved from ~99.9μs to ~79.2μs; addresses issues #148003 and #136628.
Why It Matters
Ensures robust value range analysis in PyTorch Inductor, preventing compiler errors for models using reciprocal operations.