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

transfer_states_to_host convenience function #1707

Merged
merged 1 commit into from
Dec 24, 2023
Merged
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
9 changes: 8 additions & 1 deletion numpyro/infer/mcmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import numpy as np

from jax import jit, lax, local_device_count, pmap, random, vmap
from jax import jit, lax, local_device_count, pmap, random, vmap, device_get
import jax.numpy as jnp
from jax.tree_util import tree_flatten, tree_map

Expand Down Expand Up @@ -721,6 +721,13 @@ def print_summary(self, prob=0.9, exclude_deterministic=True):
"Number of divergences: {}".format(jnp.sum(extra_fields["diverging"]))
)

def transfer_states_to_host(self):
"""
Reduce the memory footprint of collected samples by transfering them to the host device.
"""
self._states = device_get(self._states)
self._states_flat = device_get(self._states_flat)

def __getstate__(self):
state = self.__dict__.copy()
state["_cache"] = {}
Expand Down
Loading