Skip to content

Subtensor lift: reason about advanced indices jointly when gating#2251

Merged
ricardoV94 merged 1 commit into
pymc-devs:mainfrom
ricardoV94:proper_idx_gating
Jul 10, 2026
Merged

Subtensor lift: reason about advanced indices jointly when gating#2251
ricardoV94 merged 1 commit into
pymc-devs:mainfrom
ricardoV94:proper_idx_gating

Conversation

@ricardoV94

@ricardoV94 ricardoV94 commented Jun 21, 2026

Copy link
Copy Markdown
Member

We had heuristics that work per-axis, but we should reason about the advanced indices together since they interact. E.g., tril_indices, would fail the per-axis checks, as each index (row, col) is larger than the respective axis and contains duplicates. But jointly they index less than the full array and don't have duplicates, so we want to lift it through Elemwise and the like.

Shows up in pymc-devs/pymc#8297

@ricardoV94
ricardoV94 force-pushed the proper_idx_gating branch 3 times, most recently from 483c1b9 to f63da68 Compare June 25, 2026 12:38
@ricardoV94
ricardoV94 marked this pull request as ready for review June 25, 2026 13:06
@ricardoV94
ricardoV94 force-pushed the proper_idx_gating branch 3 times, most recently from f7ceb7d to fe342d6 Compare June 27, 2026 12:58
continue
if not _index_provably_not_larger(idx, inp.type.shape[axis], fgraph):
return None
adv_indices = [

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be called adv_indices and input_static_dim

@ricardoV94
ricardoV94 force-pushed the proper_idx_gating branch 4 times, most recently from da593b2 to a727559 Compare July 2, 2026 14:17
@ricardoV94 ricardoV94 added the bug Something isn't working label Jul 3, 2026
@ricardoV94
ricardoV94 marked this pull request as draft July 5, 2026 08:23
@ricardoV94
ricardoV94 force-pushed the proper_idx_gating branch 3 times, most recently from 557c648 to 756c14b Compare July 7, 2026 09:00
@ricardoV94
ricardoV94 marked this pull request as ready for review July 7, 2026 09:21
Generalize the duplicate-free reasoning used to gate the advanced-index
write rewrites from a per-axis check to a joint one over the whole advanced
index group, sharing the logic across subtensor.py and subtensor_lift.py.

- _index_provably_unique: per-axis uniqueness, now also proving single-signed
  aranges and views (Reshape/DimShuffle) that preserve the value multiset.
- _indices_jointly_unique: distinct joint coordinate tuples via all-axes
  uniqueness, a single Nonzero (e.g. tril_indices), or jointly-unique
  constants where no single axis is unique on its own.
- _indices_provably_not_larger: bound a gather by the indexed axes' size
  using static shapes, per-axis bounds, or joint uniqueness.
@ricardoV94
ricardoV94 force-pushed the proper_idx_gating branch from 756c14b to 3814ee9 Compare July 7, 2026 09:34
if _is_provably_positive(step): # ascending
if _is_provably_non_negative(start): # all >= 0
return True
if stop_c is not None and stop_c <= 0: # all <= -1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stop_c can't be symbolic? why not _is_provably_negative(stop_c) here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cuz we don't have a _is_provably_negative helper :)

and step_c is not None
and step_c != 0
):
n = max(0, -(-(stop_c - start_c) // step_c)) # length, via ceil division

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

push in some of these minus signs

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no bug ever came from simple algebra

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its ugly

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ricardoV94
ricardoV94 merged commit f871581 into pymc-devs:main Jul 10, 2026
66 checks passed
@ricardoV94
ricardoV94 deleted the proper_idx_gating branch July 10, 2026 08:05
@ricardoV94 ricardoV94 added maintenance and removed bug Something isn't working labels Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants