BucketManager

class jaxrens.sampling.bucket_manager.BucketManager(ladder, offset, shrink_dwell=0)[source]

Bases: object

Outer-loop helper that owns low_count and the picker calls.

A single instance is used by _run_loop (one per NS run) and by initial_walk (one per burn-in invocation). The class is not JIT-traced — it is pure-Python control-flow glue that decides whether to grow / shrink / keep the max_neighbors static field on the population pytree.

low_count is the only mutable state: the count of consecutive successful iterations satisfying the hysteresis gap condition. It is reset to zero on (a) a gap violation, (b) any successful shrink, and (c) any overflow growth.

Parameters:
grow_if_overflow(ns_state, new_ns_state, *, label, iteration)[source]

Detect and handle a neighbor-bucket overflow.

Parameters:
  • ns_state (Any) – Pre-step state (the rollback target if overflow fired).

  • new_ns_state (Any) – Post-step state, which may carry population.overflow=True from the JIT kernel.

  • label (str) – Short tag used in the warning log, e.g. "iter" or "burn-in walk". Helps disambiguate the two call sites.

  • iteration (int) – Counter value (NS iteration or burn-in walk index).

Returns:

(state_to_use_next, retry_needed). When retry_needed is True, the caller must NOT advance its outer counter and should re-run the JIT kernel against state_to_use_next (which has a larger bucket). When False, state_to_use_next is just new_ns_state unchanged.

Return type:

tuple[Any, bool]

maybe_shrink(ns_state, *, iteration)[source]

Step the bucket down one entry when the dwell window is satisfied.

Reads ns_state.population.max_neighbor_count (the per-walker peak observed during the last successful step) and compares the next- smaller ladder entry against observed + offset. Increments low_count on a qualifying iteration, resets to zero on a violation, and shrinks when the counter reaches shrink_dwell.

shrink_dwell == 0 (default) bypasses the entire check so the method becomes a cheap no-op for users who never opted in.

Returns the (possibly updated) state. The caller assigns the result back to its working ns_state.

Parameters:
  • ns_state (Any)

  • iteration (int)

Return type:

Any