-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: restore blackjax>=1.6 compatibility for nuts_sampler="blackjax" #8373
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
Open
laishettikarthik-tech
wants to merge
13
commits into
pymc-devs:main
Choose a base branch
from
laishettikarthik-tech:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−4
Open
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1cff290
Update progress_bar handling in jax.py for blackjax
laishettikarthik-tech 49423d5
Implement tests for blackjax NUTS progress bar handling
laishettikarthik-tech 088c241
Remove unnecessary blank line in jax.py
laishettikarthik-tech e89bee5
Merge branch 'pymc-devs:main' into main
laishettikarthik-tech 5912a4f
Add progress bar support for blackjax sampling
laishettikarthik-tech 63b53b6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e3f24c9
Merge branch 'pymc-devs:main' into main
laishettikarthik-tech 945ea78
Merge branch 'pymc-devs:main' into main
laishettikarthik-tech 30c25e0
Improve progress bar integration in jax.py
laishettikarthik-tech 34ddf8f
fix: correct indentation on line 284
laishettikarthik-tech 0f550d9
style: apply ruff format
laishettikarthik-tech 0a13398
Fix progress bar leakage in window adaptation test
laishettikarthik-tech 28a0ed0
fix: remove duplicate pytensor import, restore missing ImputationWarn…
laishettikarthik-tech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -502,3 +502,79 @@ def test_convergence_warnings(caplog, nuts_sampler): | |
|
|
||
| [record] = caplog.records | ||
| assert re.match(r"There were \d+ divergences after tuning", record.message) | ||
|
|
||
|
|
||
| class TestBlackjaxProgressBarCompat: | ||
| """Regression tests for https://github.com/pymc-devs/pymc/issues/8367. | ||
|
|
||
| blackjax >= 1.6 removed the progress_bar parameter from | ||
| window_adaptation (unknown kwargs get forwarded straight into the NUTS | ||
| kernel and raise TypeError) and replaced the progress_bar module | ||
| (with its gen_scan_fn helper) with a context-manager function. | ||
| """ | ||
|
|
||
| def test_sample_blackjax_nuts_progressbar_false(self): | ||
| # This is the exact reproduction from the issue: progress_bar | ||
| # reaching window_adaptation used to raise | ||
| # "TypeError: ... got an unexpected keyword argument 'progress_bar'" | ||
| # on any blackjax >= 1.6. | ||
| with pm.Model(): | ||
| x = pm.Normal("x", 0.0, 1.0) | ||
| pm.Normal("obs", x, 1.0, observed=np.array([0.3, -0.1, 0.5])) | ||
| idata = pm.sample( | ||
| draws=10, | ||
| tune=10, | ||
| chains=1, | ||
| cores=1, | ||
| nuts_sampler="blackjax", | ||
| progressbar=False, | ||
| ) | ||
| assert idata.posterior["x"].shape == (1, 10) | ||
|
|
||
| def test_sample_blackjax_nuts_progressbar_true(self): | ||
| # Exercises the progress_bar=True path specifically, which on | ||
| # blackjax >= 1.6 (after fixing the TypeError above) used to hit a | ||
| # second break: AttributeError, since blackjax.progress_bar is no | ||
| # longer a module with a gen_scan_fn attribute. | ||
| with pm.Model(): | ||
| x = pm.Normal("x", 0.0, 1.0) | ||
| pm.Normal("obs", x, 1.0, observed=np.array([0.3, -0.1, 0.5])) | ||
| idata = pm.sample( | ||
| draws=10, | ||
| tune=10, | ||
| chains=1, | ||
| cores=1, | ||
| nuts_sampler="blackjax", | ||
| progressbar=True, | ||
| ) | ||
| assert idata.posterior["x"].shape == (1, 10) | ||
|
|
||
| def test_progress_bar_popped_before_window_adaptation(self): | ||
| # Directly asserts the ordering fix: progress_bar must never reach | ||
| # blackjax.window_adaptation's kwargs, regardless of blackjax | ||
| # version/API shape. | ||
| import blackjax | ||
|
|
||
| from pymc.sampling.jax import _blackjax_inference_loop | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check if local imports can be made global |
||
|
|
||
| original_window_adaptation = blackjax.window_adaptation | ||
|
|
||
| def spy_window_adaptation(*args, **kwargs): | ||
| assert "progress_bar" not in kwargs, "progress_bar leaked into window_adaptation kwargs" | ||
| return original_window_adaptation(*args, **kwargs) | ||
|
|
||
| with pm.Model() as model: | ||
| x = pm.Normal("x", 0.0, 1.0) | ||
| pm.Normal("obs", x, 1.0, observed=np.array([0.3, -0.1, 0.5])) | ||
| logp_fn = get_jaxified_logp(model) | ||
|
|
||
| with mock.patch("blackjax.window_adaptation", side_effect=spy_window_adaptation): | ||
| _blackjax_inference_loop( | ||
| seed=jax.random.PRNGKey(0), | ||
| init_position=[np.array(0.0)], | ||
| logp_fn=logp_fn, | ||
| draws=5, | ||
| tune=5, | ||
| target_accept=0.8, | ||
| progress_bar=False, | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On blackjax >= 1.6 this context manager is powered by
jaxtap, shipped asblackjax[progress]. On a plainblackjax==1.6.2install it raises:So
test_sample_blackjax_nuts_progressbar_truewill fail on any CI image that installs blackjax without the extra, and a user passingprogressbar=Truetrades aTypeErrorfor anImportError. A progress bar aborting a sampling run seems worse than losing the bar, so a fallback may be preferable to adding the extra as a test dependency (warningsis already imported at line 17):