Skip to content

Commit

Permalink
autoregressive: explicitly clone rng for reuse
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 614079072
  • Loading branch information
vanderplas authored and tensorflower-gardener committed Mar 9, 2024
1 parent 0435c36 commit 9a14b9b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
13 changes: 10 additions & 3 deletions tensorflow_probability/python/distributions/autoregressive.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,18 @@ def _sample_n(self, n, seed=None):
if num_steps_static is not None:
for _ in range(num_steps_static):
# pylint: disable=not-callable
samples = self.distribution_fn(samples).sample(seed=seed)
samples = self.distribution_fn(samples).sample(
seed=samplers.clone_seed(seed)
)
else:
# pylint: disable=not-callable
samples = tf.foldl(lambda s, _: self.distribution_fn(s).sample(seed=seed),
elems=tf.range(0, num_steps), initializer=samples)
samples = tf.foldl(
lambda s, _: self.distribution_fn(s).sample(
seed=samplers.clone_seed(seed)
),
elems=tf.range(0, num_steps),
initializer=samples,
)
return samples

def _log_prob(self, value):
Expand Down
11 changes: 11 additions & 0 deletions tensorflow_probability/python/internal/samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

__all__ = [
'categorical',
'clone_seed',
'fold_in',
'gamma',
'is_stateful_seed',
Expand Down Expand Up @@ -229,6 +230,16 @@ def split_seed(seed, n=2, salt=None, name=None):
return seeds


def clone_seed(seed):
"""Clones a seed so it can be reused without causing a JAX KeyReuseError."""
if JAX_MODE:
from jax import random as jaxrand # pylint: disable=g-import-not-at-top
if hasattr(jaxrand, 'clone'):
# JAX v0.4.26+
return jaxrand.clone(seed)
return seed


def categorical(
logits,
num_samples,
Expand Down

0 comments on commit 9a14b9b

Please sign in to comment.