Developer Tools

PyTorch #186094 fixes AOTI cond predicate bug breaking C++ codegen

A missing SymPy dependency caused DCE to delete needed symbols—generated C++ wouldn't compile.

Deep Dive

PyTorch's AOTI (Ahead-of-Time Inductor) compiler transforms PyTorch models into optimized C++ ahead of time. For torch.cond, AOTI lowers the predicate to a SymPy boolean relation like `u0 > 0`, which can reference unbacked symbols—symbols without a concrete value yet. The scheduler tracks such dependencies to prevent dead code elimination (DCE) from removing the nodes that define these symbols. However, the constant-argument scanner only recognized SymTypes and sympy.Expr, missing sympy.Basic boolean relations. As a result, the cond node didn't report its use of `u0`, allowing DCE to remove the defining scalar node while C++ codegen still emitted `if (u0 > 0L)`, leaving an undeclared symbol.

PR #186094, authored by jansel and approved by larryliu0820, desertfire, and mlazos, fixes this by accepting `sympy.Basic` in `maybe_free_symbols()` and `maybe_free_unbacked_symbols()`. A benchmark over 200,000 calls shows the relational predicate case now finds `u0` (0.2959s vs 0.0548s missing it before), while expression scanning remains nearly identical (0.4289s vs 0.4324s). The fix preserves only the required definitions, avoiding over-retention that would mask deeper dependency bugs. Tests cover CPU and GPU AOTI variants, with stack-allocation and dual-wrapper cases registered as expected failures.

Key Points
  • Fixes GitHub issue #140842 by recognizing sympy.Basic boolean relations in AOTI's free-symbol scanners
  • Benchmark: relational predicate scan now detects `u0` in 0.2959s (old code missed it in 0.0548s) over 200K calls
  • 5 tests passed including CPU/GPU cond predicate cases; stack-allocation and dual-wrapper variants remain expected failures

Why It Matters

For PyTorch production users relying on AOTI, this fix eliminates intermittent C++ compilation failures in models using dynamic control flow.

📬 Get the top 10 AI stories daily