Skip to content

Add var_names argument to sample #7206

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

Merged
merged 9 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion pymc/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

import numpy as np

from pytensor.tensor.variable import TensorVariable
from typing_extensions import TypeAlias

from pymc.backends.arviz import predictions_to_inference_data, to_inference_data
Expand Down Expand Up @@ -99,11 +100,12 @@ def _init_trace(
stats_dtypes: list[dict[str, type]],
trace: Optional[BaseTrace],
model: Model,
trace_vars: Optional[list[TensorVariable]] = None,
) -> BaseTrace:
"""Initializes a trace backend for a chain."""
strace: BaseTrace
if trace is None:
strace = NDArray(model=model)
strace = NDArray(model=model, vars=trace_vars)
elif isinstance(trace, BaseTrace):
if len(trace) > 0:
raise ValueError("Continuation of traces is no longer supported.")
Expand All @@ -123,6 +125,7 @@ def init_traces(
step: Union[BlockedStep, CompoundStep],
initial_point: Mapping[str, np.ndarray],
model: Model,
trace_vars: Optional[list[TensorVariable]] = None,
) -> tuple[Optional[RunType], Sequence[IBaseTrace]]:
"""Initializes a trace recorder for each chain."""
if HAS_MCB and isinstance(backend, Backend):
Expand All @@ -142,6 +145,7 @@ def init_traces(
chain_number=chain_number,
trace=backend,
model=model,
trace_vars=trace_vars,
)
for chain_number in range(chains)
]
Expand Down
10 changes: 7 additions & 3 deletions pymc/sampling/jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,15 +532,19 @@ def sample_jax_nuts(

model = modelcontext(model)

if var_names is None:
var_names = model.unobserved_value_vars
if var_names is not None:
filtered_var_names = [v for v in model.unobserved_value_vars if v.name in var_names]
else:
filtered_var_names = model.unobserved_value_vars

if nuts_kwargs is None:
nuts_kwargs = {}
else:
nuts_kwargs = nuts_kwargs.copy()

vars_to_sample = list(get_default_varnames(var_names, include_transformed=keep_untransformed))
vars_to_sample = list(
get_default_varnames(filtered_var_names, include_transformed=keep_untransformed)
)

(random_seed,) = _get_seeds_per_chain(random_seed, 1)

Expand Down
21 changes: 21 additions & 0 deletions pymc/sampling/mcmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ def _sample_external_nuts(
random_seed: Union[RandomState, None],
initvals: Union[StartDict, Sequence[Optional[StartDict]], None],
model: Model,
var_names: Optional[Sequence[str]],
progressbar: bool,
idata_kwargs: Optional[dict],
nuts_sampler_kwargs: Optional[dict],
Expand Down Expand Up @@ -292,6 +293,11 @@ def _sample_external_nuts(
"`idata_kwargs` are currently ignored by the nutpie sampler",
UserWarning,
)
if var_names is not None:
warnings.warn(
"`var_names` are currently ignored by the nutpie sampler",
UserWarning,
)
compiled_model = nutpie.compile_pymc_model(model)
t_start = time.time()
idata = nutpie.sample(
Expand Down Expand Up @@ -348,6 +354,7 @@ def _sample_external_nuts(
random_seed=random_seed,
initvals=initvals,
model=model,
var_names=var_names,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a warning about var_names not beeing used by nutpie like we have for some other arguments above?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, @aseyboldt how hard/reasonable is it to support this in nutpie?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it could be filtered in nutpie.sample._trace_to_arviz?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we want to filter during sampling already since RAM is usually the issue, not disk-space?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but there are no obvious hooks into the nutpie compiled model. It would require some changes on the nutpie side, by the looks of it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My top comment was to add a warning like these:

pymc/pymc/sampling/mcmc.py

Lines 283 to 294 in abe7bc9

if initvals is not None:
warnings.warn(
"`initvals` are currently not passed to nutpie sampler. "
"Use `init_mean` kwarg following nutpie specification instead.",
UserWarning,
)
if idata_kwargs is not None:
warnings.warn(
"`idata_kwargs` are currently ignored by the nutpie sampler",
UserWarning,
)

Not to try to monkey-patch nutpie from the outside

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once/if nutpie has similar functionality we can forward it from pymc?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go with the warning for now, and create an issue on nutpie for a solution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be too hard. Nutpie uses a numba function to compute all values that should appear in the trace (including the deterministics and transformed values). We should be able to just export a subset (code is around here: https://github.com/pymc-devs/nutpie/blob/main/python/nutpie/compile_pymc.py#L387)

progressbar=progressbar,
nuts_sampler=sampler,
idata_kwargs=idata_kwargs,
Expand All @@ -371,6 +378,7 @@ def sample(
random_seed: RandomState = None,
progressbar: bool = True,
step=None,
var_names: Optional[Sequence[str]] = None,
nuts_sampler: Literal["pymc", "nutpie", "numpyro", "blackjax"] = "pymc",
initvals: Optional[Union[StartDict, Sequence[Optional[StartDict]]]] = None,
init: str = "auto",
Expand Down Expand Up @@ -399,6 +407,7 @@ def sample(
random_seed: RandomState = None,
progressbar: bool = True,
step=None,
var_names: Optional[Sequence[str]] = None,
nuts_sampler: Literal["pymc", "nutpie", "numpyro", "blackjax"] = "pymc",
initvals: Optional[Union[StartDict, Sequence[Optional[StartDict]]]] = None,
init: str = "auto",
Expand Down Expand Up @@ -427,6 +436,7 @@ def sample(
random_seed: RandomState = None,
progressbar: bool = True,
step=None,
var_names: Optional[Sequence[str]] = None,
nuts_sampler: Literal["pymc", "nutpie", "numpyro", "blackjax"] = "pymc",
initvals: Optional[Union[StartDict, Sequence[Optional[StartDict]]]] = None,
init: str = "auto",
Expand Down Expand Up @@ -478,6 +488,8 @@ def sample(
A step function or collection of functions. If there are variables without step methods,
step methods for those variables will be assigned automatically. By default the NUTS step
method will be used, if appropriate to the model.
var_names : list of str, optional
Names of variables to be stored in the trace. Defaults to all free variables and deterministics.
nuts_sampler : str
Which NUTS implementation to run. One of ["pymc", "nutpie", "blackjax", "numpyro"].
This requires the chosen sampler to be installed.
Expand Down Expand Up @@ -680,6 +692,7 @@ def sample(
random_seed=random_seed,
initvals=initvals,
model=model,
var_names=var_names,
progressbar=progressbar,
idata_kwargs=idata_kwargs,
nuts_sampler_kwargs=nuts_sampler_kwargs,
Expand Down Expand Up @@ -722,12 +735,19 @@ def sample(
model.check_start_vals(ip)
_check_start_shape(model, ip)

if var_names is not None:
trace_vars = [v for v in model.unobserved_RVs if v.name in var_names]
assert len(trace_vars) == len(var_names), "Not all var_names were found in the model"
else:
trace_vars = None

# Create trace backends for each chain
run, traces = init_traces(
backend=trace,
chains=chains,
expected_length=draws + tune,
step=step,
trace_vars=trace_vars,
initial_point=ip,
model=model,
)
Expand All @@ -739,6 +759,7 @@ def sample(
"traces": traces,
"chains": chains,
"tune": tune,
"var_names": var_names,
"progressbar": progressbar,
"model": model,
"cores": cores,
Expand Down
9 changes: 9 additions & 0 deletions tests/sampling/test_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,15 @@ def test_sample_partially_observed():
assert idata.posterior["x"].shape == (1, 10, 3)


def test_sample_var_names():
with pm.Model() as model:
a = pm.Normal("a")
b = pm.Deterministic("b", a**2)
idata = pm.sample(10, tune=10, nuts_sampler="numpyro", var_names=["a"])
assert "a" in idata.posterior
assert "b" not in idata.posterior


@pytest.mark.parametrize("nuts_sampler", ("numpyro", "blackjax"))
def test_convergence_warnings(caplog, nuts_sampler):
with pm.Model() as m:
Expand Down
9 changes: 9 additions & 0 deletions tests/sampling/test_mcmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,15 @@ def test_no_init_nuts_compound(caplog):
assert "Initializing NUTS" not in caplog.text


def test_sample_var_names():
with pm.Model() as model:
a = pm.Normal("a")
b = pm.Deterministic("b", a**2)
idata = pm.sample(10, tune=10, var_names=["a"])
assert "a" in idata.posterior
assert "b" not in idata.posterior


class TestAssignStepMethods:
def test_bernoulli(self):
"""Test bernoulli distribution is assigned binary gibbs metropolis method"""
Expand Down