adjust_step_size

jaxrens.sampling.adaptation.stepsize_handler.adjust_step_size(population, move_fn, step_size, emax, rng_key, n_samples, min_rate, max_rate, adjust_factor, max_step_size, max_rounds, *, trial_batch_size=None, axis_name=None)[source]

Adjust step size for one move type until acceptance rate is in window.

Pure JAX function. JIT-compilable. Vmappable over runs.

Iterates a bisection-like loop: sample walkers → run trial moves → measure acceptance rate → scale step size up/down. Stops when the rate falls within [min_rate, max_rate] or brackets are detected. Under vmap-over-runs, lax.while_loop runs until all replicas converge or max_rounds is hit; per-replica convergence is masked by the loop’s any-active semantics combined with the per-replica converged carry.

Parameters:
  • population (Any) – Batched MCState, shape (n_walkers, …) on every field.

  • move_fn (Callable) – Single-move step function (static under JIT). Signature: move_fn(state, key, constraint) -> (state, MoveInfo).

  • step_size (Float[jaxlib._jax.Array, '']) – Current step size (scalar under each replica).

  • emax (Float[jaxlib._jax.Array, '']) – Likelihood constraint — max energy in population (scalar).

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

  • n_samples (int) – Walkers to sample per trial round (static).

  • min_rate (float) – Target acceptance window (static).

  • max_rate (float) – Target acceptance window (static).

  • adjust_factor (float) – Multiplicative step-size factor (static).

  • max_step_size (float) – Upper bound on step size (static).

  • max_rounds (int) – Maximum bisection rounds (static).

  • trial_batch_size (int | None) – Optional chunk size for the trial vmap (kwarg-only, static). None (default) → jax.vmap(trial_one) over all n_samples walkers. When set to an int, the trial vmap is replaced by jax.lax.map(..., batch_size=trial_batch_size) so peak memory scales with the chunk rather than n_samples — needed when the move’s backward tape exceeds device memory. If n_samples is not a multiple of trial_batch_size the trial population is padded with copies of the last sample and the pad is sliced off before rate/count aggregation.

  • axis_name (str | None) – Mesh axis name to run under, when this function is called inside a jax.shard_map (PmapVmapRuns’s "gpu"-axis wrap; see jaxrens.sampling.mesh). None (default) for SingleRun/VmapRuns, which never run inside a mapped context. When set, every while_loop carry entry downstream of rate/reasons/the per-round eval counts (rate_prev, converged, counts, cap_hits, floor_hits, saw_too_high, saw_too_low, cumulative eval counters) is pre-cast to the varying VMA type via jax.lax.pcast before the loop, so its input/ output carry types match under shard_map’s stricter checking — nothing in this function ever collective-reduces those, unlike adjust_step_size_sharded(). Cast broadly rather than cherry-picked: an earlier, narrower version (only the entries one toy harmonic+random_walk test happened to surface as mismatched) passed that test but still failed on a real LJ/NPT pipeline run, where counts (not in the narrower list) also turned out to need it. jax.lax.pcast errors outside a mapped context, which is why this can’t be applied unconditionally.

Returns:

(new_step_size, final_rate, final_counts, n_rounds, converged, cap_hits, floor_hits, bracket_detected, trial_n_evaluations, trial_n_grad_evaluations).

Return type:

tuple[Float[jaxlib._jax.Array, ’’], Float[jaxlib._jax.Array, ’’], Int[jaxlib._jax.Array, ’4’], Int[jaxlib._jax.Array, ’’], Bool[jaxlib._jax.Array, ’’], Int[jaxlib._jax.Array, ’’], Int[jaxlib._jax.Array, ’’], Bool[jaxlib._jax.Array, ’’], Int[jaxlib._jax.Array, ’’], Int[jaxlib._jax.Array, ’’]]