Skip to content

Commit 6f815f6

Browse files
committed
Log external sampler and variables
1 parent 45371a9 commit 6f815f6

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

pymc/sampling/mcmc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ def _sample_external_nuts(
367367
)
368368
compile_kwargs["gradient_backend"] = nuts_kwargs.pop("gradient_backend")
369369

370+
if not quiet:
371+
_log.info(f"NUTS[{sampler}]: {model.free_RVs}")
372+
370373
if sampler == "nutpie":
371374
if not NUTPIE_INSTALLED:
372375
raise ImportError(

tests/sampling/test_mcmc_external.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import logging
1516
import unittest.mock as mock
1617
import warnings
1718

@@ -413,3 +414,37 @@ def test_falls_back_to_pymc_for_configured_nuts_step(self, patched_sampler):
413414
step = NUTS(potential=QuadPotentialDiag(np.ones(1)))
414415
sample(step=step, **self._BASE_KWARGS)
415416
patched_sampler.assert_not_called()
417+
418+
419+
class TestExternalSamplerLogging:
420+
"""`pm.sample` should announce which external NUTS backend is running."""
421+
422+
_BASE_KWARGS = {
423+
"tune": 1,
424+
"draws": 1,
425+
"chains": 1,
426+
"progressbar": False,
427+
"compute_convergence_checks": False,
428+
"random_seed": 42,
429+
}
430+
431+
@pytest.mark.parametrize("nuts_sampler", ["nutpie", "blackjax", "numpyro"])
432+
def test_logs_external_sampler_name(self, caplog, nuts_sampler):
433+
pytest.importorskip(nuts_sampler)
434+
with Model():
435+
Normal("x", 0, 1)
436+
HalfNormal("y", 1)
437+
with caplog.at_level(logging.INFO, logger="pymc"):
438+
sample(nuts_sampler=nuts_sampler, **self._BASE_KWARGS)
439+
440+
assert any(r.message == f"NUTS[{nuts_sampler}]: [x, y]" for r in caplog.records)
441+
442+
def test_quiet_suppresses_external_log(self, caplog):
443+
pytest.importorskip("nutpie")
444+
with Model():
445+
Normal("x", 0, 1)
446+
with caplog.at_level(logging.DEBUG, logger="pymc"):
447+
sample(nuts_sampler="nutpie", quiet=True, **self._BASE_KWARGS)
448+
449+
pymc_logs = [r for r in caplog.records if r.name.startswith("pymc")]
450+
assert pymc_logs == []

0 commit comments

Comments
 (0)