InterREManager

class jaxrens.sampling.inter_re_manager.InterREManager(swap_kernel, batcher, backend, re_interval=1, n_swap_cycles=1)[source]

Bases: object

Manages inter-replica-exchange swap passes in the NS outer loop.

Constructed once before the loop starts. _run_loop calls fires(i) to decide whether to fire on iteration i, then calls apply(ns_state, rng_key) to run the swap pass.

Parameters:
  • swap_kernel (SwapKernel) – SwapKernel instance, typically from make_swap_kernel().

  • batcher (BatchDescriptor) – BatchDescriptor controlling the execution mode.

  • backend (Any) – Energy backend, passed to kernels that re-evaluate energies (XRENSSwap).

  • re_interval (int) – Fire a swap pass every this many NS iterations. 0 → never fire.

  • n_swap_cycles (int) – Number of even+odd swap phases per fire.

fires(iteration)[source]

Return True iff a swap pass should fire on this iteration.

Rules: re_interval > 0 AND iteration > 0 AND iteration % re_interval == 0. Iteration 0 is skipped to match the adapt-step firing convention.

Parameters:

iteration (int) – Current NS iteration index (Python int).

Returns:

True when a swap should be attempted.

Return type:

bool

property is_active: bool

True iff this manager will actually do work.

SingleRun descriptors return False (no batched population to swap). VmapRuns / PmapVmapRuns return True when re_interval > 0.

apply(ns_state, rng_key)[source]

Run one inter-RE swap pass.

For SingleRun: returns state unchanged with zero stats. For VmapRuns: operates on (n_runs, K, ...) population directly. For PmapVmapRuns: all_gathers across the pmap axis, swaps, re-shards.

The swap-acceptance constraint is read off ns_state.emax — the per-replica NS contour the most recent ns_step culled at. This is algorithm state set by ns_step, not a value re-derived from pop.energy: recomputing here would be strictly tighter post-MCMC and would reject otherwise-legal swaps.

Parameters:
  • ns_state (NSState) – Current NSState (single, vmapped, or pmap-vmapped).

  • rng_key (Key[jaxlib._jax.Array, '']) – Scalar PRNG key for swap randomness.

Returns:

  • new_ns_state: Updated NSState after swaps.

  • swap_stats: Dict with keys {"n_swap_pairs_attempted": int, "n_swap_pairs_accepted": int, "acceptance_rate": float, "n_energy_evals": int, "n_grad_evals": int}. All zeros for SingleRun (no-op).

  • new_rng_key: Advanced PRNG key carry (scalar).

Return type:

Tuple (new_ns_state, swap_stats, new_rng_key) where