Skip to content

Commit

Permalink
Fix chain detection in progress bar (#1077)
Browse files Browse the repository at this point in the history
* fixup progress bars with multiple chains

* add noteabout ipywidgets to docstring, add underscore to regex

* make CHAIN_RE private
  • Loading branch information
MarcoGorelli authored Jun 28, 2021
1 parent 86c65cf commit ab18282
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions numpyro/infer/mcmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://ipywidgets.readthedocs.io/en/latest/user_install.html>`_
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.
Expand Down
9 changes: 7 additions & 2 deletions numpyro/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit ab18282

Please sign in to comment.