jaxrens.sampling.mesh

Shared jax.shard_map mesh construction and pmap-compatibility shim.

Part of the pmap -> shard_map migration (see the design plan). Centralizes the Mesh construction that used to be duplicated ad hoc in nested_sampling.py and batch_descriptor.py, and provides pmap_like(), an adapter that lets existing per-device function bodies (written against jax.pmap’s calling convention) run unmodified under jax.shard_map.

Why the adapter is needed

jax.pmap(fn, axis_name=...) strips the mapped axis from every input leaf before calling fn and re-adds it (by stacking each device’s output) on the way out. jax.shard_map instead keeps a size-1 mapped axis in each device’s local view of a fully-partitioned array, and requires out_specs-consistent shapes explicitly. pmap_like() squeezes that size-1 axis off every input leaf and re-adds it to every output leaf, so callers can write fn exactly as they would for jax.pmap and swap the transform without touching the body. This has been verified bit-for-bit against jax.pmap for the collective ops jaxrens relies on (all_gather, axis_index, psum, including inside lax.scan/lax.while_loop bodies).

Functions

build_mesh

Return a 1-D jax.sharding.Mesh named axis_name over the first n_devices local devices.

pmap_like

Wrap fn so jax.shard_map reproduces jax.pmap's calling convention for it.