PmapVmapRuns

class jaxrens.sampling.batch_descriptor.PmapVmapRuns(n_gpu, n_per_gpu, n_runs=0, shape_prefix=())[source]

Bases: _UniformBatcher

Descriptor for shard_map(vmap(...)) multi-GPU multi-run NS.

Shape convention: (G, P, K, ...) where

  • G = n_gpu — sharded axis (one shard per GPU device).

  • P = n_per_gpu — vmap axis (independent NS runs per GPU).

  • K — walker axis (already handled inside ns_step via vmap).

For n_gpu=1 this degenerates to (1, P, K, ...) and runs on the single available device, which is useful for testing.

wrap_step composes a "gpu"-axis jax.shard_map (G axis) over jax.vmap (P axis), with one move-schedule key shared by every run on every device (simplified; squeezing of the G axis omitted):

per_run  = lambda s, mk: ns_step_fn(
    s, step_fn, n_mcmc_steps, n_extra, move_key=mk)
sharded  = jax.shard_map(            # ns_states: P("gpu"), mk: P()
    jax.vmap(per_run, in_axes=(0, None)), mesh=mesh,
    in_specs=(P("gpu"), P()), out_specs=P("gpu"))
jit_step = jax.jit(
    lambda s: sharded(s, _shared_move_key(s.rng_key)))

Unlike jax.pmap, shard_map composes cleanly with jax.jit, so the result is explicitly jit-wrapped.

split_keys produces (G, P, n_sub_keys)-shaped key arrays via two nested splits: first (G,) per-GPU keys, then (P, n_sub_keys) per GPU via jax.vmap.

reduce_for_termination takes the worst-of across both G and P axes (same as VmapRuns but over a 2-D input rather than 1-D).

Variables:
  • n_gpu (int) – Number of GPU devices (G).

  • n_per_gpu (int) – Number of NS runs per GPU (P).

  • n_runs (int) – Total runs: n_gpu * n_per_gpu.

  • shape_prefix (tuple[int, ]) – (n_gpu, n_per_gpu) — leading shape of all batched arrays.

Parameters:
property is_batched: bool

Always True — multiple NS runs are distributed across G×P.

property mesh

The "gpu"-axis jax.sharding.Mesh for this topology.

Built once per instance (not per wrap_step/wrap_for_batch call) and shared by every consumer — InterREManager pulls this same object rather than building its own equivalent-but-distinct mesh.

wrap_step(ns_step_fn, step_fn, n_mcmc_steps, n_extra)[source]

Return a shard_map(vmap(…)) NS step callable (pmap-equivalent).

The returned callable has signature:

pmap_step(ns_states)  ->  (ns_states, infos)

where ns_states has leading shape (G, P, ...).

Built on jax.shard_map (via wrap_for_batch()) rather than jax.pmap — this is the "gpu"-axis leg of the pmap -> shard_map migration. Unlike jax.pmap, shard_map composes with jax.jit, so the returned callable is explicitly jit-wrapped.

Parameters:
  • ns_step_fn (callable) – Raw ns_step (or compatible replacement).

  • step_fn (callable) – MCMC step function captured in the closure (static for vmap).

  • n_mcmc_steps (int) – Number of MCMC steps per walker (static).

  • n_extra (int) – Number of additional walkers per iteration (static).

Returns:

jax.jit(shard_map(vmap(per_run))) with ns_states sharded over G and one globally shared move_key replicated to every device (see _wrap_batched_step()).

Return type:

callable

split_keys(rng_key, n_sub_keys)[source]

Split a per-replica PRNG key array into (G, P, n_sub_keys).

Input shape equals shape_prefix ((G, P) here), output prepends an n_sub_keys axis at the end. Implemented as a 2-D vmap (one for each prefix axis) of jax.random.split.

Parameters:
  • rng_key (jax.Array) – Shape (G, P) — one key per replica. Same convention as NSState.rng_key for PmapVmapRuns.

  • n_sub_keys (int) – Number of sub-keys per replica.

Returns:

Shape (G, P, n_sub_keys) with typed-key dtype.

Return type:

jax.Array

reduce_for_termination(log_evidence, hmax)[source]

Worst-of reduction across both G and P axes.

Parameters:
Returns:

Worst-case scalars across all runs on all GPUs.

Return type:

(float(jnp.min(log_evidence)), float(jnp.max(hmax)))