Skip to content

Refactor MiniBatch and stop using deprecated MRG sampler #6304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 12, 2022

Conversation

ferrine
Copy link
Member

@ferrine ferrine commented Nov 15, 2022

fix #4523, suspend #6273
closes #6277

@ricardoV94, I've figured out the root cause of the seeding bug

@ricardoV94
Copy link
Member

ricardoV94 commented Nov 15, 2022

@ferrine this does not fix #4523, VI is still using the MRG RandomStream. This can't be switched yet because of #6277

from aesara.sandbox.rng_mrg import MRG_RandomStream as RandomStream

pymc/pymc/aesaraf.py

Lines 872 to 895 in 5da2617

_at_rng = RandomStream()
def at_rng(random_seed=None):
"""
Get the package-level random number generator or new with specified seed.
Parameters
----------
random_seed: int
If not None
returns *new* aesara random generator without replacing package global one
Returns
-------
`aesara.tensor.random.utils.RandomStream` instance
`aesara.tensor.random.utils.RandomStream`
instance passed to the most recent call of `set_at_rng`
"""
if random_seed is None:
return _at_rng
else:
ret = RandomStream(random_seed)
return ret

@ricardoV94
Copy link
Member

It does fix some lack of seeding, for which I think we don't have an issue open

@codecov
Copy link

codecov bot commented Nov 15, 2022

Codecov Report

Merging #6304 (1218d0c) into main (5692fc0) will increase coverage by 0.07%.
The diff coverage is 97.81%.

❗ Current head 1218d0c differs from pull request most recent head 82dfbf6. Consider uploading reports for the commit 82dfbf6 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6304      +/-   ##
==========================================
+ Coverage   94.71%   94.79%   +0.07%     
==========================================
  Files         132      148      +16     
  Lines       26771    27488     +717     
==========================================
+ Hits        25357    26058     +701     
- Misses       1414     1430      +16     
Impacted Files Coverage Δ
pymc/distributions/__init__.py 100.00% <ø> (ø)
pymc/logprob/scan.py 97.35% <ø> (ø)
pymc/variational/approximations.py 87.72% <50.00%> (+0.05%) ⬆️
pymc/variational/opvi.py 87.10% <85.00%> (+0.02%) ⬆️
pymc/logprob/transforms.py 97.74% <93.10%> (-1.59%) ⬇️
pymc/distributions/distribution.py 97.46% <98.23%> (+2.28%) ⬆️
pymc/tests/distributions/test_distribution.py 98.69% <99.24%> (+0.48%) ⬆️
pymc/backends/arviz.py 96.37% <100.00%> (+5.65%) ⬆️
pymc/data.py 91.66% <100.00%> (+11.58%) ⬆️
pymc/distributions/logprob.py 100.00% <100.00%> (ø)
... and 38 more

@ricardoV94 ricardoV94 added VI Variational Inference enhancements v4 major Include in major changes release notes section labels Nov 15, 2022
pymc/data.py Outdated
from aesara.tensor.type import TensorType
from aesara.tensor.var import TensorConstant, TensorVariable

import pymc as pm

from pymc.aesaraf import convert_observed_data
from pymc.aesaraf import at_rng, convert_observed_data
Copy link
Member

Choose a reason for hiding this comment

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

We can also get rid of at_rng, as the seeds created by it will be overwritten in compile_pymc anyway. Just import RandomStream wherever it's needed.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok

@ricardoV94 ricardoV94 changed the title Fix rng streams Refactor MiniBatch and stop using deprecated MRG sampler Nov 15, 2022
@ricardoV94
Copy link
Member

I think I saw that kernel crash in my PR, perhaps an out of bounds indexing?

@ferrine
Copy link
Member Author

ferrine commented Nov 16, 2022

no idea why this windows test fails

@ferrine
Copy link
Member Author

ferrine commented Nov 16, 2022

hmm, that could be the random stream!

@ferrine ferrine added this to the v4.4.0 milestone Nov 16, 2022
@ferrine
Copy link
Member Author

ferrine commented Nov 17, 2022

The issue that blocks the PR seems to be in aesara or I'm doing something wrong aesara-devs/aesara#1301

@ferrine
Copy link
Member Author

ferrine commented Nov 17, 2022

Maybe @brandonwillard or @ricardoV94 have any insights?

@michaelosthege
Copy link
Member

@ferrine are some of these changes independent and could be merged via a separate PR already?

@ricardoV94
Copy link
Member

Not really... The change led to some scan issues that need to be addressed otherwise it would lead to a regression

@michaelosthege michaelosthege modified the milestones: v4.4.0, v5.0.0 Nov 19, 2022
@ferrine ferrine changed the title Refactor MiniBatch and stop using deprecated MRG sampler WIP Refactor MiniBatch and stop using deprecated MRG sampler Nov 21, 2022
@ferrine ferrine marked this pull request as draft November 21, 2022 12:12
@ferrine
Copy link
Member Author

ferrine commented Nov 21, 2022

Marked this as draft util we figure out the scan issue

pymc/aesaraf.py Outdated
from aesara.scalar.basic import Cast
from aesara.tensor.basic import _as_tensor_variable
from aesara.tensor.elemwise import Elemwise
from aesara.tensor.random import RandomStream
Copy link
Member

Choose a reason for hiding this comment

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

Can we remove the whole at_rng and set_rng related utilities below from this file?

https://github.com/pymc-devs/pymc/blob/12f4cd73bae95c42a28c2f07f6aab60071fd2ca1/pymc/aesaraf.py#L872-L912

Copy link
Member

Choose a reason for hiding this comment

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

set_at_rng is called by pm.sampling.parallel._Process._start_loop.

In the interest of the v5 release I think we should take care of that later

Copy link
Member

Choose a reason for hiding this comment

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

We can, but it's only used by VI

@ricardoV94 ricardoV94 changed the title WIP Refactor MiniBatch and stop using deprecated MRG sampler Refactor MiniBatch and stop using deprecated MRG sampler Nov 29, 2022
@ricardoV94
Copy link
Member

@ferrine What do you think of doing the Minibatch API breaking change for V5, and fixing the MRG later, as that is not user facing anyway.

@ferrine
Copy link
Member Author

ferrine commented Dec 9, 2022

@ferrine What do you think of doing the Minibatch API breaking change for V5, and fixing the MRG later, as that is not user facing anyway.

That makes to me, we better get back to it later, than get stuck right now. It is also reasonable to improve minibatch API and VI experience later and reduce to something working now

@ferrine ferrine marked this pull request as ready for review December 9, 2022 16:53
@ferrine ferrine force-pushed the fix-rng-streams branch 2 times, most recently from a3093be to 9a19ef4 Compare December 10, 2022 18:50
@michaelosthege
Copy link
Member

I marked the VI test_profile tests as XFAIL and fixed flakyness of the Moyal logcdf test which is also currently ❌ on main.

@michaelosthege michaelosthege force-pushed the fix-rng-streams branch 2 times, most recently from 75b697b to f3dde69 Compare December 11, 2022 14:20
@michaelosthege
Copy link
Member

@ferrine I squashed the commits so we can rebase-merge this.

It looks like you have not set up your @pymc-devs.org email address in your GitHub account. That's why your pic doesn't shown on the commits..

Copy link
Member

@michaelosthege michaelosthege left a comment

Choose a reason for hiding this comment

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

🥳🚀

@ricardoV94
Copy link
Member

@ferrine can you have one last look and merge it if you agree with the changes?

@ferrine
Copy link
Member Author

ferrine commented Dec 12, 2022

I had one more look at the PR changes and I don't have anything to add to it. I'll merge once tests pass.

@michaelosthege michaelosthege merged commit adf6fff into main Dec 12, 2022
@michaelosthege michaelosthege deleted the fix-rng-streams branch December 12, 2022 13:04
@michaelosthege
Copy link
Member

Just fixuped the edit of the comment into the moyal logcdf commit.

Didn't want to waste CI, so I merged it right away =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancements major Include in major changes release notes section v4 VI Variational Inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use RandomVariables for Minibatch VI using deprecated Aesara MRG sampler
3 participants