Skip to content
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

Integrate PyMC samplers and clean up unsued MCMC sampler #1053

Merged
merged 44 commits into from
Apr 3, 2024

Conversation

famura
Copy link
Contributor

@famura famura commented Mar 20, 2024

What does this implement/fix? Explain your changes

Integrate the mighty MCMC samplers from PyMC.
Remove unused MCMC samplers.

Does this close any currently open issues?

Fixes #987 and partially #986

Any relevant code examples, logs, error output, etc?

...

Any other comments?

Currently the MCMC class sbi.samplers.sample.mcmc.mcmc.py is only used in the test

Checklist

Put an x in the boxes that apply. You can also fill these out after creating
the PR. If you're unsure about any of them, don't hesitate to ask. We're here to
help! This is simply a reminder of what we are going to look for before merging
your code.

  • I have read and understood the contribution
    guidelines
  • I agree with re-licensing my contribution from AGPLv3 to Apache-2.0.
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I have reported how long the new tests run and potentially marked them
    with pytest.mark.slow.
  • New and existing unit tests pass locally with my changes
  • I performed linting and formatting as described in the contribution
    guidelines
  • I rebased on main (or there are no conflicts with main)

@famura famura added enhancement New feature or request hackathon labels Mar 20, 2024
@famura famura linked an issue Mar 20, 2024 that may be closed by this pull request
@felixp8
Copy link
Contributor

felixp8 commented Mar 20, 2024

@famura it should be in decent shape for you to check over now

Copy link
Contributor Author

@famura famura left a comment

Choose a reason for hiding this comment

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

great progress

sbi/inference/posteriors/mcmc_posterior.py Show resolved Hide resolved
sbi/inference/posteriors/mcmc_posterior.py Outdated Show resolved Hide resolved
sbi/inference/posteriors/mcmc_posterior.py Show resolved Hide resolved
sbi/samplers/mcmc/__init__.py Outdated Show resolved Hide resolved
sbi/samplers/mcmc/pymc_utils.py Outdated Show resolved Hide resolved
sbi/samplers/mcmc/pymc_utils.py Outdated Show resolved Hide resolved
sbi/samplers/mcmc/pymc_utils.py Outdated Show resolved Hide resolved
sbi/samplers/mcmc/pymc_utils.py Outdated Show resolved Hide resolved
sbi/samplers/mcmc/pymc_utils.py Outdated Show resolved Hide resolved
tests/mcmc_test.py Show resolved Hide resolved
@famura
Copy link
Contributor Author

famura commented Mar 22, 2024

This will have a conflict with PR #1107 since that PR changes some tests that we are deleting here. However, I think that can be solved no matter which PR we merge first. I guess this one is more ready.

@felixp8

Copy link
Contributor

@felixp8 felixp8 left a comment

Choose a reason for hiding this comment

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

all looks good to me, but I'm biased :) would maybe be best to get another review also?

@famura famura requested a review from michaeldeistler March 25, 2024 09:56
Copy link
Contributor

@janfb janfb left a comment

Choose a reason for hiding this comment

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

Great work @famura @felixp8 👏
Added a couple of comments and questions.

sbi/inference/posteriors/mcmc_posterior.py Show resolved Hide resolved
sbi/inference/posteriors/mcmc_posterior.py Outdated Show resolved Hide resolved
sbi/inference/posteriors/mcmc_posterior.py Show resolved Hide resolved
sbi/inference/posteriors/mcmc_posterior.py Show resolved Hide resolved
sbi/inference/posteriors/mcmc_posterior.py Show resolved Hide resolved
sbi/inference/posteriors/mcmc_posterior.py Show resolved Hide resolved
sbi/samplers/mcmc/pymc_wrapper.py Show resolved Hide resolved
sbi/samplers/mcmc/pymc_wrapper.py Show resolved Hide resolved
sbi/samplers/mcmc/pymc_wrapper.py Show resolved Hide resolved
sbi/samplers/mcmc/pymc_wrapper.py Show resolved Hide resolved
famura added 2 commits March 28, 2024 10:40
# Conflicts:
#	tests/mcmc_slice_pyro/test_slice.py
#	tests/mcmc_test.py
#	tests/posterior_sampler_test.py
@famura
Copy link
Contributor Author

famura commented Mar 28, 2024

@felixp8 please have a look at the state after the merge

@felixp8
Copy link
Contributor

felixp8 commented Apr 2, 2024

@famura all looks good to me! any thoughts from y'all on whether we should expose the multiprocessing start method (i.e. "spawn" vs "fork") as a parameter for Pyro and PyMC samplers as Jan mentions here? I don't really know what difference it makes tbh

Copy link
Contributor

@janfb janfb left a comment

Choose a reason for hiding this comment

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

Thanks for the updates!
I added a couple of suggestion for the thin doc strings. Sorry for the causing confusion here..

sbi/inference/posteriors/mcmc_posterior.py Outdated Show resolved Hide resolved
sbi/inference/posteriors/mcmc_posterior.py Outdated Show resolved Hide resolved
sbi/inference/posteriors/mcmc_posterior.py Outdated Show resolved Hide resolved
sbi/inference/posteriors/mcmc_posterior.py Outdated Show resolved Hide resolved
sbi/samplers/mcmc/slice_numpy.py Outdated Show resolved Hide resolved
sbi/samplers/mcmc/slice_numpy.py Outdated Show resolved Hide resolved
sbi/samplers/mcmc/slice_numpy.py Outdated Show resolved Hide resolved
@janfb
Copy link
Contributor

janfb commented Apr 3, 2024

@famura all looks good to me! any thoughts from y'all on whether we should expose the multiprocessing start method (i.e. "spawn" vs "fork") as a parameter for Pyro and PyMC samplers as Jan mentions here? I don't really know what difference it makes tbh

Forking can be 20x faster for multiprocessing but works only on unix systems:

The fork method is only supported on POSIX-based systems like Linux and macOS (not Windows), 
whereas the spawn start method is supported on all platforms. A major difference between the two
start methods is speed. It is generally considered that forking a process is faster than spawning a process

https://superfastpython.com/fork-faster-than-spawn/#:~:text=The%20fork%20method%20is%20only,faster%20than%20spawning%20a%20process.

Thus, I think it would be good to expose it so that users can use fork when they want.

@famura
Copy link
Contributor Author

famura commented Apr 3, 2024

... and it doesn't work with CUDA afaik

@felixp8
Copy link
Contributor

felixp8 commented Apr 3, 2024

makes sense, happy to quickly patch that in unless you're already on it @famura

@famura
Copy link
Contributor Author

famura commented Apr 3, 2024

no, please go ahead @felixp8

@felixp8
Copy link
Contributor

felixp8 commented Apr 3, 2024

added support for setting mp_context="fork", will debug test failures later this afternoon

Copy link

codecov bot commented Apr 3, 2024

Codecov Report

Attention: Patch coverage is 86.60714% with 15 lines in your changes are missing coverage. Please review.

Project coverage is 76.89%. Comparing base (4c2f71e) to head (707605c).
Report is 41 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1053      +/-   ##
==========================================
+ Coverage   76.37%   76.89%   +0.51%     
==========================================
  Files          84       89       +5     
  Lines        6507     6591      +84     
==========================================
+ Hits         4970     5068      +98     
+ Misses       1537     1523      -14     
Flag Coverage Δ
unittests 76.89% <86.60%> (+0.51%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
sbi/samplers/mcmc/__init__.py 100.00% <100.00%> (ø)
sbi/samplers/mcmc/slice_numpy.py 91.98% <100.00%> (+0.06%) ⬆️
sbi/inference/posteriors/mcmc_posterior.py 85.85% <89.74%> (+0.29%) ⬆️
sbi/samplers/mcmc/pymc_wrapper.py 84.28% <84.28%> (ø)

... and 13 files with indirect coverage changes

@janfb janfb merged commit 6e0e98a into main Apr 3, 2024
5 checks passed
@janfb janfb deleted the 987-pymc-interface branch April 3, 2024 19:33
@janfb janfb mentioned this pull request Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hackathon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add interface to PyMC MCMC samplers
3 participants