Skip to content

Rewrite 1/x**(-a) -> 1 * x**a#2199

Draft
ricardoV94 wants to merge 2 commits into
pymc-devs:mainfrom
ricardoV94:div_neg_pow
Draft

Rewrite 1/x**(-a) -> 1 * x**a#2199
ricardoV94 wants to merge 2 commits into
pymc-devs:mainfrom
ricardoV94:div_neg_pow

Conversation

@ricardoV94

Copy link
Copy Markdown
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant