diff --git a/numpyro/infer/mcmc.py b/numpyro/infer/mcmc.py index 58d5eaf39..3b9afb185 100644 --- a/numpyro/infer/mcmc.py +++ b/numpyro/infer/mcmc.py @@ -204,6 +204,11 @@ class MCMC(object): .. note:: Setting `progress_bar=False` will improve the speed for many cases. But it might require more memory than the other option. + .. note:: If setting `num_chains` greater than `1` in a Jupyter Notebook, then you will need to + have installed `ipywidgets `_ + in the environment from which you launced Jupyter in order for the progress bars to render + correctly. + :param MCMCKernel sampler: an instance of :class:`~numpyro.infer.mcmc.MCMCKernel` that determines the sampler for running MCMC. Currently, only :class:`~numpyro.infer.hmc.HMC` and :class:`~numpyro.infer.hmc.NUTS` are available. diff --git a/numpyro/util.py b/numpyro/util.py index 9405caa86..26b751ae6 100644 --- a/numpyro/util.py +++ b/numpyro/util.py @@ -21,6 +21,7 @@ from jax.tree_util import tree_flatten, tree_map _DISABLE_CONTROL_FLOW_PRIM = False +_CHAIN_RE = re.compile(r"(?<=_)\d+$") # e.g. get '3' from 'TFRT_CPU_3' def set_rng_seed(rng_seed): @@ -189,12 +190,16 @@ def progress_bar_factory(num_samples, num_chains): tqdm_bars[chain].set_description("Compiling.. ", refresh=True) def _update_tqdm(arg, transform, device): - chain = int(str(device)[4:]) + chain_match = _CHAIN_RE.search(str(device)) + assert chain_match + chain = int(chain_match.group()) tqdm_bars[chain].set_description(f"Running chain {chain}", refresh=False) tqdm_bars[chain].update(arg) def _close_tqdm(arg, transform, device): - chain = int(str(device)[4:]) + chain_match = _CHAIN_RE.search(str(device)) + assert chain_match + chain = int(chain_match.group()) tqdm_bars[chain].update(arg) finished_chains.append(chain) if len(finished_chains) == num_chains: