Skip to content

Add asumption for unique indices#2225

Merged
ricardoV94 merged 1 commit into
pymc-devs:mainfrom
ricardoV94:assume_unique_indices
Jun 15, 2026
Merged

Add asumption for unique indices#2225
ricardoV94 merged 1 commit into
pymc-devs:mainfrom
ricardoV94:assume_unique_indices

Conversation

@ricardoV94

Copy link
Copy Markdown
Member

This allows symbolic indices to be lifted towards the inputs. There is no cleaner / structural way to do this in PyTensor

Comment thread tests/tensor/rewriting/test_subtensor.py Outdated
def _has_unique_indices(fgraph, idx) -> bool:
"""Whether ``idx``'s entries are provably duplicate-free: a constant with
unique entries, or a variable asserted ``unique_indices`` by the user."""
return _constant_has_unique_indices(idx) or check_assumption(

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.

Do you have a sense of which of these is cheaper? It's the assumption check if the Feature is already attached, but in live code i don't know.

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.

I don't think it matters, people aren't going to put asumptions on constant indices (no need anyway)? Either way after the first check it's cached

@ricardoV94 ricardoV94 force-pushed the assume_unique_indices branch from 2956155 to e565cff Compare June 15, 2026 13:39
Assert that *x* is (or is not) a selection matrix
permutation : bool, optional
Assert that *x* is (or is not) a permutation matrix
unique_indices : bool, optional

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.

Should we reject this flag on non-1d inputs for now? Otherwise we need to know which axis/axes have unique indices, and we don't have the machinery for parameterized assumptions yet. If you want to add it though I'd be happy, I want it for matrix rank among other things.

@ricardoV94 ricardoV94 Jun 15, 2026

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 assumes unique over the whole data, I don't care about dims

@ricardoV94 ricardoV94 Jun 15, 2026

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.

(not yet), the guard against repeated computations still needs some work, but I don't think people are gonna do (symbolic) advanced matrix indexing and want it to lift, they could always do flat and reshape

@ricardoV94 ricardoV94 merged commit 9842a1b into pymc-devs:main Jun 15, 2026
66 checks passed
@ricardoV94 ricardoV94 deleted the assume_unique_indices branch June 15, 2026 15:38
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