PyTorch Dynamo gets proper power operator support in PR #186296
Power operations now work correctly in PyTorch's graph compiler
Pull request #186296 brings long-overdue support for power operators (`pow` and `ipow`) to PyTorch’s Dynamo compiler. Previously these operators were handled via an older `_handle_op_in_graph` table, which lacked proper integration with Dynamo’s variable tracking system. The PR rewires `operator.pow` and `operator.ipow` through the clean `ternary_op` and `ternary_iop` dispatch in `BuiltinVariable`, ensuring they benefit from Dynamo’s full compile-time optimization pipeline.
To handle Python’s ternary power semantics (which include a modulus argument in CPython’s `nb_power`), the implementation adds `nb_power_impl` (with a `reverse: bool` flag for forward/reflected operations) and `nb_inplace_power_impl` to all major variable trackers: `ConstantVariable`, `TensorVariable`, `SymNodeVariable`, and `UserDefinedVariable`. A separate `nb_power_z_impl` is introduced for the modulus ‘z-slot’ to avoid conflating it with the forward/reverse logic. The PR was co-authored by Claude Sonnet, reflecting growing AI-assisted development in PyTorch core.
- Wires `operator.pow` and `operator.ipow` through `ternary_op`/`ternary_iop` in `BuiltinVariable`, replacing the old `_handle_op_in_graph` table
- Adds `nb_power_impl` (with `reverse` flag) and `nb_inplace_power_impl` to `VariableTracker`, `ConstantVariable`, `TensorVariable`, `SymNodeVariable`, and `UserDefinedObjectVariable`
- Introduces `nb_power_z_impl` for the modulus slot in CPython’s ternary power, ensuring correct handling of the optional third argument
Why It Matters
Fixes power operator compilation in Dynamo, enabling faster and correct execution of models using `**` and `pow()`.