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

Resolve: Add progress bar to run_inference_algorithm #614

Merged
merged 3 commits into from
Dec 9, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions blackjax/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from jax.tree_util import tree_leaves

from blackjax.base import Info, State
from blackjax.progress_bar import progress_bar_scan
from blackjax.types import Array, ArrayLikeTree, ArrayTree, PRNGKey


Expand Down Expand Up @@ -144,6 +145,7 @@
initial_state_or_position,
inference_algorithm,
num_steps,
progress_bar: bool = False,
) -> tuple[State, State, Info]:
"""Wrapper to run an inference algorithm.

Expand Down Expand Up @@ -176,9 +178,16 @@
keys = split(rng_key, num_steps)

@jit
def one_step(state, rng_key):
def _one_step(state, xs):
_, rng_key = xs
state, info = inference_algorithm.step(rng_key, state)
return state, (state, info)

final_state, (state_history, info_history) = lax.scan(one_step, initial_state, keys)
if progress_bar:
one_step = progress_bar_scan(num_steps)(_one_step)

Check warning on line 187 in blackjax/util.py

View check run for this annotation

Codecov / codecov/patch

blackjax/util.py#L187

Added line #L187 was not covered by tests
else:
one_step = _one_step

xs = (jnp.arange(num_steps), keys)
final_state, (state_history, info_history) = lax.scan(one_step, initial_state, xs)
return final_state, state_history, info_history
Loading