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

Make potential_fn_gen and postprocess_fn_gen picklable #1302

Merged
merged 3 commits into from
Jan 23, 2022
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
2 changes: 0 additions & 2 deletions numpyro/infer/autoguide.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ def _create_plates(self, *args, **kwargs):
def __getstate__(self):
state = self.__dict__.copy()
state.pop("plates", None)
# TODO: Make potential_fn_gen pickable.
state.pop("_potential_fn_gen", None)
return state

@abstractmethod
Expand Down
1 change: 0 additions & 1 deletion numpyro/infer/barker.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,5 @@ def sample(self, state, model_args, model_kwargs):

def __getstate__(self):
state = self.__dict__.copy()
state["_postprocess_fn"] = None
state["_wa_update"] = None
return state
2 changes: 0 additions & 2 deletions numpyro/infer/hmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,8 +763,6 @@ def __getstate__(self):
state = self.__dict__.copy()
state["_sample_fn"] = None
state["_init_fn"] = None
state["_postprocess_fn"] = None
state["_potential_fn_gen"] = None
return state


Expand Down
2 changes: 0 additions & 2 deletions numpyro/infer/sa.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,4 @@ def __getstate__(self):
state = self.__dict__.copy()
state["_sample_fn"] = None
state["_init_fn"] = None
state["_postprocess_fn"] = None
state["_potential_fn_gen"] = None
return state
38 changes: 24 additions & 14 deletions numpyro/infer/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,16 @@ def _get_model_transforms(model, model_args=(), model_kwargs=None):
return inv_transforms, replay_model, has_enumerate_support, model_trace


def _partial_args_kwargs(fn, *args, **kwargs):
"""Returns a partial function of `fn` and args, kwargs."""
return partial(fn, args, kwargs)


def _drop_args_kwargs(fn, *args, **kwargs):
"""Returns the input function `fn`, ignoring args and kwargs."""
return fn


def get_potential_fn(
model,
inv_transforms,
Expand Down Expand Up @@ -480,20 +490,20 @@ def get_potential_fn(
`deterministic` sites in the model.
"""
if dynamic_args:

def potential_fn(*args, **kwargs):
return partial(potential_energy, model, args, kwargs, enum=enum)

def postprocess_fn(*args, **kwargs):
if replay_model:
# XXX: we seed to sample discrete sites (but not collect them)
model_ = seed(model.fn, 0) if enum else model
return partial(
constrain_fn, model_, args, kwargs, return_deterministic=True
)
else:
return partial(transform_fn, inv_transforms)

potential_fn = partial(
_partial_args_kwargs, partial(potential_energy, model, enum=enum)
)
if replay_model:
# XXX: we seed to sample discrete sites (but not collect them)
model_ = seed(model.fn, 0) if enum else model
postprocess_fn = partial(
_partial_args_kwargs,
partial(constrain_fn, model, return_deterministic=True),
)
else:
postprocess_fn = partial(
_drop_args_kwargs, partial(transform_fn, inv_transforms)
)
else:
model_kwargs = {} if model_kwargs is None else model_kwargs
potential_fn = partial(
Expand Down