PyTorch fixes adaptive_avg_pool2d crash with torch.compile dynamic shapes
SymPy symbolic window sizes broke dynamic compilation; PR #185369 fixes it.
PyTorch's PR #185369, titled "Fix dynamic output sizes for adaptive avg pool lowering," resolves a crash that occurred when compiling adaptive_avg_pool2d with torch.compile(dynamic=True). The Inductor lowering computed the maximum adaptive pooling window from input and output sizes. When the output_size was provided as symbolic Python arguments, that maximum became a SymPy expression. The lowering then used that expression in Python control flow for the large-window fallback decision and passed symbolic loop extents into the manually unrolled pooling loop, triggering a SymPy truth-value error before compilation completed.
The fix addresses this by guarding the large-window fallback predicate with sizevars.guard_or_true and guarding the small-window unrolled extents before using them as Python range bounds. This keeps the existing generated code strategy for static/small windows while making symbolic decisions explicit and sound. The PR includes a CPU regression test that compiles adaptive avg pool with dynamic output sizes, checks eager correctness across several output sizes, verifies frame reuse, exercises changing input shapes, and covers the large-window fallback branch. Benchmark results show negligible overhead: static CPU adaptive_avg_pool2d runs at 28.7 µs median vs 28.0 µs on main. The PR fixes issues #159550 and #185575 and was approved by yushangdi.
- Fixes SymPy truth-value crash in adaptive_avg_pool2d with torch.compile(dynamic=True)
- Uses sizevars.guard_or_true and guarded unrolled loop extents
- Regression test covers dynamic output sizes and frame reuse; static benchmark only 2.5% slower (28.7µs vs 28.0µs)
Why It Matters
Makes dynamic-shape compilation stable for adaptive pooling, a common op in vision models, without sacrificing static performance.