Skip to content
Open
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: 1 addition & 1 deletion pymc/sampling/mcmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ def _iter_sample(
if draws < 1:
raise ValueError("Argument `draws` must be greater than 0.")

step.set_rng(rng)
step.setup_chain(rng, tune, draws - tune)

point = start
if isinstance(trace, _ZarrChainBase):
Expand Down
2 changes: 1 addition & 1 deletion pymc/sampling/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def _recv_msg(self):

def _start_loop(self):
zarr_recording = self._zarr_recording
self._step_method.set_rng(self._rng)
self._step_method.setup_chain(self._rng, self._tune, self._draws - self._tune)

draw = 0
tuning = True
Expand Down
2 changes: 1 addition & 1 deletion pymc/sampling/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def _prepare_iter_population(
chainstep = CompoundStep([copy(m) for m in step.methods])
else:
chainstep = copy(step)
chainstep.set_rng(rng)
chainstep.setup_chain(rng, tune, draws - tune)
# link population samplers to the shared population state
for sm in chainstep.methods if isinstance(step, CompoundStep) else [chainstep]:
if isinstance(sm, PopulationArrayStepShared):
Expand Down
18 changes: 12 additions & 6 deletions pymc/step_methods/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,13 @@ def stop_tuning(self):
if hasattr(self, "tune"):
self.tune = False

def set_rng(self, rng: RandomGenerator):
def setup_chain(self, rng: RandomGenerator, tune: int, draws: int) -> None:
"""Prepare the step method for sampling one chain.

Called once per chain, right before that chain starts sampling.
Can be called multiple times, meaning downstream method
should handle multiple, possibly repeated calls.
"""
self.rng = get_random_generator(rng, copy=False)


Expand Down Expand Up @@ -297,6 +303,11 @@ def reset_tuning(self):
if hasattr(method, "reset_tuning"):
method.reset_tuning()

def setup_chain(self, rng: RandomGenerator, tune: int, draws: int) -> None:
_rngs = get_random_generator(rng, copy=False).spawn(len(self.methods))
for method, _rng in zip(self.methods, _rngs):
method.setup_chain(_rng, tune, draws)

@property
def sampling_state(self) -> DataClassState:
return CompoundStepState(methods=[method.sampling_state for method in self.methods])
Expand All @@ -313,11 +324,6 @@ def sampling_state(self, state: DataClassState):
def vars(self) -> list[Variable]:
return [var for method in self.methods for var in method.vars]

def set_rng(self, rng: RandomGenerator):
_rngs = get_random_generator(rng, copy=False).spawn(len(self.methods))
for method, _rng in zip(self.methods, _rngs):
method.set_rng(_rng)

def _progressbar_config(self, n_chains=1):
from functools import reduce

Expand Down
6 changes: 3 additions & 3 deletions pymc/step_methods/hmc/base_hmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from pymc.step_methods.state import dataclass_state
from pymc.step_methods.step_sizes import DualAverageAdaptation, StepSizeState
from pymc.tuning import guess_scaling
from pymc.util import RandomGenerator, get_random_generator, get_value_vars_from_user_vars
from pymc.util import RandomGenerator, get_value_vars_from_user_vars

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -297,6 +297,6 @@ def reset(self, start=None):
self.tune = True
self.potential.reset()

def set_rng(self, rng: RandomGenerator):
self.rng = get_random_generator(rng, copy=False)
def setup_chain(self, rng: RandomGenerator, tune: int, draws: int) -> None:
super().setup_chain(rng, tune, draws)
self.potential.set_rng(self.rng.spawn(1)[0])