Skip to content

Make assume_pure able to work with functions that depends on random#9460

Merged
qihqi merged 2 commits intomasterfrom
hanq_assume_pure_random
Jul 10, 2025
Merged

Make assume_pure able to work with functions that depends on random#9460
qihqi merged 2 commits intomasterfrom
hanq_assume_pure_random

Conversation

@qihqi
Copy link
Copy Markdown
Collaborator

@qihqi qihqi commented Jul 9, 2025

Functions with randomness are not technically pure, so assuming them to pure has implication that now they lose the randomness.

The solution is to lift RNG seed an additional input so that the function's value can depend on that.

@qihqi qihqi requested review from lsy323 and yaoshiang July 9, 2025 17:36
Copy link
Copy Markdown
Collaborator

@yaoshiang yaoshiang left a comment

Choose a reason for hiding this comment

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

I refreshed my memory on Jax randomness. Indeed, there is no way to avoid passing a seed into a jax function / assume_pure decorated pytorch function.

As discussed, perhaps there's a way to store the state in the decorator's closure but outside the jitted function... I am not sure if that would work.

Comment thread test/test_assume_pure.py
Functions with randomness are not technically pure, so assuming them
to pure has implication that now they lose the randomness.

The solution is to lift RNG seed an additional input so that the
function's value can depend on that.
@qihqi qihqi force-pushed the hanq_assume_pure_random branch from a069da2 to dcebdb3 Compare July 9, 2025 23:07
@qihqi
Copy link
Copy Markdown
Collaborator Author

qihqi commented Jul 9, 2025

As discussed, perhaps there's a way to store the state in the decorator's closure but outside the jitted function... I am not sure if that would work.

Tried that, didnt made it work. Added more test on testing calling random many times inside of assume'd pure block.

@qihqi qihqi force-pushed the hanq_assume_pure_random branch from dcebdb3 to 534da4b Compare July 9, 2025 23:29
@qihqi qihqi merged commit 8999ba5 into master Jul 10, 2025
23 of 24 checks passed
@miladm
Copy link
Copy Markdown
Collaborator

miladm commented Aug 6, 2025

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.

3 participants