jax.random
module#
Utilities for pseudo-random number generation.
The jax.random
package provides a number of routines for deterministic
generation of sequences of pseudorandom numbers.
Basic usage#
>>> seed = 1701
>>> num_steps = 100
>>> key = jax.random.PRNGKey(seed)
>>> for i in range(num_steps):
... key, subkey = jax.random.split(key)
... params = compiled_update(subkey, params, next(batches))
PRNG Keys#
Unlike the stateful pseudorandom number generators (PRNGs) that users of NumPy and
SciPy may be accustomed to, JAX random functions all require an explicit PRNG state to
be passed as a first argument.
The random state is described by two unsigned 32-bit integers that we call a key,
usually generated by the jax.random.PRNGKey()
function:
>>> from jax import random
>>> key = random.PRNGKey(0)
>>> key
Array([0, 0], dtype=uint32)
This key can then be used in any of JAX’s random number generation routines:
>>> random.uniform(key)
Array(0.41845703, dtype=float32)
Note that using a key does not modify it, so reusing the same key will lead to the same result:
>>> random.uniform(key)
Array(0.41845703, dtype=float32)
If you need a new random number, you can use jax.random.split()
to generate new subkeys:
>>> key, subkey = random.split(key)
>>> random.uniform(subkey)
Array(0.10536897, dtype=float32)
Advanced#
Design and Context#
TLDR: JAX PRNG = Threefry counter PRNG + a functional array-oriented splitting model
See docs/jep/263-prng.md for more details.
To summarize, among other requirements, the JAX PRNG aims to:
ensure reproducibility,
parallelize well, both in terms of vectorization (generating array values) and multi-replica, multi-core computation. In particular it should not use sequencing constraints between random function calls.
Advanced RNG configuration#
JAX provides several PRNG implementations (controlled by the jax_default_prng_impl flag).
default A counter-based PRNG built around the Threefry hash function.
experimental A PRNG that thinly wraps the XLA Random Bit Generator (RBG) algorithm. See TF doc.
“rbg” uses ThreeFry for splitting, and XLA RBG for data generation.
“unsafe_rbg” exists only for demonstration purposes, using RBG both for splitting (using an untested made up algorithm) and generating.
The random streams generated by these experimental implementations haven’t been subject to any empirical randomness testing (e.g. Big Crush). The random bits generated may change between JAX versions.
The possible reasons not use the default RNG are:
it may be slow to compile (specifically for Google Cloud TPUs)
it’s slower to execute on TPUs
it doesn’t support efficient automatic sharding / partitioning
Here is a short summary:
Property |
Threefry |
Threefry* |
rbg |
unsafe_rbg |
rbg** |
unsafe_rbg** |
---|---|---|---|---|---|---|
Fastest on TPU |
✅ |
✅ |
✅ |
✅ |
||
efficiently shardable (w/ pjit) |
✅ |
✅ |
✅ |
|||
identical across shardings |
✅ |
✅ |
✅ |
✅ |
||
identical across CPU/GPU/TPU |
✅ |
✅ |
||||
identical across JAX/XLA versions |
✅ |
✅ |
(*): with jax_threefry_partitionable=1 set (**): with XLA_FLAGS=–xla_tpu_spmd_rng_bit_generator_unsafe=1 set
The difference between “rbg” and “unsafe_rbg” is that while “rbg” uses a less robust/studied hash function for random value generation (but not for jax.random.split or jax.random.fold_in), “unsafe_rbg” additionally uses less robust hash functions for jax.random.split and jax.random.fold_in. Therefore less safe in the sense that the quality of random streams it generates from different keys is less well understood.
For more about jax_threefry_partitionable, see https://jax.readthedocs.io/en/latest/notebooks/Distributed_arrays_and_automatic_parallelization.html#generating-random-numbers
List of Available Functions#
|
Create a pseudo-random number generator (PRNG) key given an integer seed. |
|
Sample uniformly from the unit Lp ball. |
|
Sample Bernoulli random values with given shape and mean. |
|
Sample Beta random values with given shape and float dtype. |
|
Sample uniform bits in the form of unsigned integers. |
|
Sample random values from categorical distributions. |
|
Sample Cauchy random values with given shape and float dtype. |
|
Sample Chisquare random values with given shape and float dtype. |
|
Generates a random sample from a given array. |
|
Sample Dirichlet random values with given shape and float dtype. |
|
Sample from a double sided Maxwell distribution. |
|
Sample Exponential random values with given shape and float dtype. |
|
Sample F-distribution random values with given shape and float dtype. |
|
Folds in data to a PRNG key to form a new PRNG key. |
|
Sample Gamma random values with given shape and float dtype. |
|
Sample from the generalized normal distribution. |
|
Sample Geometric random values with given shape and float dtype. |
|
Sample Gumbel random values with given shape and float dtype. |
|
Sample Laplace random values with given shape and float dtype. |
|
Sample log-gamma random values with given shape and float dtype. |
|
Sample logistic random values with given shape and float dtype. |
|
Sample from a one sided Maxwell distribution. |
|
Sample multivariate normal random values with given mean and covariance. |
|
Sample standard normal random values with given shape and float dtype. |
|
Sample uniformly from the orthogonal group O(n). |
|
Sample Pareto random values with given shape and float dtype. |
|
Returns a randomly permuted array or range. |
|
Sample Poisson random values with given shape and integer dtype. |
|
Sample from a Rademacher distribution. |
|
Sample uniform random values in [minval, maxval) with given shape/dtype. |
|
Sample Rayleigh random values with given shape and float dtype. |
|
Shuffle the elements of an array uniformly at random along an axis. |
|
Splits a PRNG key into num new keys by adding a leading axis. |
|
Sample Student's t random values with given shape and float dtype. |
|
Sample truncated standard normal random values with given shape and dtype. |
|
Sample uniform random values in [minval, maxval) with given shape/dtype. |
|
Sample Wald random values with given shape and float dtype. |
|
Sample from a Weibull distribution. |