Rewrite 1/x**(-a) -> 1 * x**a#2199
Draft
ricardoV94 wants to merge 2 commits into
Draft
Conversation
ricardoV94
commented
Jun 8, 2026
Member
- Centralize cached constant predicates in constant_props module
- Rewrite division by a reciprocal or negative power as multiplication
Move the tag-cached, constant-data predicates into a new dependency-free leaf module `pytensor.tensor.constant_props`: - `constant_indices_are_unique` and `constant_is_arange` (from `rewriting/subtensor.py`) - `constant_is_all_negative` (new; replaces the inline all-negative scan in `local_sign_div`) These only inspect constant data, so the module imports nothing beyond numpy and `Constant`. The recursive, graph-walking sign analysis `is_provably_positive` / `is_provably_non_negative` stays in `subtensor` with the ops it recurses over, and is renamed to drop the leading underscore now that it is imported across modules.
Add two specialize rewrites: - `local_div_reciprocal_to_mul`: `A / reciprocal(B) -> A * B` and `A / y ** (-p) -> A * y ** p`. The first form is left opaque by the AlgebraicCanonizer and is what `local_pow_specialize` emits for negative constant exponents (e.g. `y ** -2 -> reciprocal(sqr(y))`), which previously left a redundant reciprocal and division. - `local_reciprocal_neg_pow_to_pow`: `reciprocal(y ** (-p)) -> y ** p`, covering `1 / y ** (-p)`, which `local_div_to_reciprocal` canonicalizes to a reciprocal before it can reach the division rewrite. Both only fire when the exponent is recognisably negative (a `neg` node or an all-negative constant), so a positive exponent is never flipped.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.