Walkers are JAX pytrees¶
A walker’s state — positions, cell, species, step sizes, energy,
ensemble parameters, and any move-specific extras — is a
jaxrens.state.mc_state.MCState dataclass registered with
jax.tree_util. Every leaf is a JAX array; every leading axis is a
batch axis. jit, vmap, and shard_map operate on the whole pytree
transparently, so going from one walker → many walkers → many
independent NS runs → many GPUs requires no code changes in the
inner kernels — only extra leading axes.
The tree¶
NSState is the top-level pytree the NS loop carries. Its single
non-trivial child is population, a batched
MCState whose leaves carry the
(K, …) per-walker arrays. Solid arrows are pytree leaves; dashed
arrows mark static_field() entries that live in aux data and
trigger a JIT recompile when they change.
flowchart LR
NS["NSState"]
NS --> P["population<br/>(MCState, batched)"]
NS --> LZ["log_evidence:<br/>scalar"]
NS --> IT["iteration:<br/>int32"]
NS --> KEY["rng_key:<br/>PRNGKey"]
NS -. static .-> NW["n_walkers: int"]
NS -. static .-> NA0["n_atoms: int"]
subgraph MC_leaves["MCState leaves (per-walker, batch K)"]
direction LR
subgraph mc_config["configuration"]
direction TB
POS["positions: (K, N, 3)"]
CELL["cell: (K, 3, 3)"]
TYP["types: (K, N)"]
EN["energy: (K,)"]
end
subgraph mc_mcmc["MCMC stats"]
direction TB
SS["step_sizes: (K, n_moves)"]
ACC["n_accepted: (K, n_moves)"]
PROP["n_proposed: (K, n_moves)"]
end
subgraph mc_misc["overflow / ensemble / extras"]
direction TB
MNC["max_neighbor_count: (K,)"]
OV["overflow: (K,) bool"]
EP["ensemble_params: dict<br/>(pressure / μ / target_comp / …)"]
EX["extras<br/>e.g. direction (galilean),<br/>momenta (HMC)"]
end
end
subgraph MC_aux["MCState aux (static_field)"]
direction TB
MN["max_neighbors: int"]
NA1["n_atoms: int"]
end
P --> MC_leaves
P -. static .-> MC_aux
classDef root fill:#fff7e0,stroke:#a07000,color:#222
classDef leafBox fill:#eef5ff,stroke:#1565c0,color:#222
classDef auxBox fill:#f5f5f5,stroke:#888,color:#222
classDef colBox fill:#fafdff,stroke:#9bb8d6,color:#222
class NS,P root
class MC_leaves leafBox
class MC_aux auxBox
class mc_config,mc_mcmc,mc_misc colBox
linkStyle 4,5,7 stroke:#888,color:#555
NSState sits on the left; its level-2 children stack vertically
in the next column; the batched MCState leaves form the right
panel split into three logical sub-columns — configuration
(positions / cell / types / energy), MCMC stats (step sizes,
accept / propose counters), and overflow / ensemble / extras
(neighbour-count + ensemble parameters + per-kernel extras). The
two dashed edges at the NSState and population level point to
aux blocks — static_field() entries that live outside the
pytree leaves and gate recompilation.
Dynamic class construction¶
MCState isn’t a fixed dataclass — it’s built on demand by
make_mc_state_class() based on the
move kernels in play. The galilean move needs a direction field;
HMC needs momentum; alchemical-morph moves need per-walker species
counts. build_mwg collects each kernel’s declared extra_state_fields
and generates an MCState subclass with exactly those fields + the
core. All subclasses share the same batch-axis convention, so the
NS loop is dtype-generic.
Two consequences:
You can add a new move with new per-walker state without touching
MCStatedirectly — just declareextra_state_fieldsin yourMoveKernel. See Moves and the Metropolis-within-Gibbs scheduler.Tests that mock
MCStateneed to match the extra-field set the real kernels would request;build_mwg(backend, descriptors)returns a ready-madeinit_fnthat does this for you.
Batch-axis shapes across the descriptors¶
Every array in the NS state has the same batch prefix — determined
by which BatchDescriptor
is dispatching the loop. The descriptor is attached to info["_batch"]
so callbacks can branch on it.
SingleRun— one process, one GPU, one NS instance.K = n_walkers,N = n_atoms.VmapRuns— one GPU,R = n_runsparallel NS instances. Used on CPU nodes or whenlen(jax.local_devices()) == 1.PmapVmapRuns—G × Preplicas distributed acrossGGPUs withP = n_per_gpureplicas on each. Execution isjax.jit(shard_map(vmap(...)))over a"gpu"-named device mesh.ShardedSingleRun— one NS run whoseKwalkers are split intoGchunks ofK / G, one per GPU (run.shard_n_gpu > 1). Population leaves are(G, K / G, N, 3); the leading axis is a sharding axis, not a replica axis, son_runs == 1. The step isns_step_sharded, which usesall_gather/psumcollectives over a"shard"-named mesh to act on the global population. This is for memory scaling when a heavy ML backend with a largen_livedoesn’t fit on one device. (The figure above shows only the three replica descriptors.)
The loop body is identical in all four. Only the descriptor’s
wrap_step, split_keys, and reduce_for_termination methods
differ. See sampling/batch_descriptor.py.
BatchDescriptor is a type alias
(SingleRun | VmapRuns | PmapVmapRuns | ShardedSingleRun), not a
base class. isinstance(x, BatchDescriptor) works, but you cannot
subclass it. The three replica descriptors share their shape helpers
through a private _UniformBatcher base, and ShardedSingleRun
stands alone because n_runs == prod(shape_prefix) doesn’t hold for
it.
Multi-device execution: shard_map¶
Both multi-device descriptors run on
jax.shard_map
rather than jax.pmap. Two pieces in
jaxrens.sampling.mesh make this work:
build_mesh(axis_name, n_devices)builds the 1-Djax.sharding.Meshover the firstn_deviceslocal devices. Each batcher builds its mesh once and caches it asbatcher.mesh. Every consumer (wrap_step,wrap_for_batch, theInterREManagerswap step) reuses that one object instead of building its own.pmap_like(fn, axis_name, mesh)wrapsfninshard_mapwithin_specs = out_specs = PartitionSpec(axis_name)on every leaf. It also squeezes the size-1 per-device axis off the inputs and adds it back to the outputs, sofnis written exactly as it would be forjax.pmap. The per-device bodies (ns_step_sharded, the swap kernels,adjust_step_size) are unchanged by the migration. Collectives inside them (all_gather,axis_index,psum) behave exactly as they did underpmap.
The practical differences from pmap:
Composes with
jax.jit. Every wrapped step is returned asjax.jit(shard_map(...)), and it can be nested inside other jitted code, whichpmapdoesn’t support cleanly.Per-argument partition specs. Where one uniform spec isn’t enough, the code calls
shard_mapdirectly.ShardedSingleRun.wrap_for_batch(..., replicated=...)marks arguments such as the coherent adaptation key as replicated (PartitionSpec()), soshard_maphands the same value to every device without a pre-broadcast. TheInterREManagerswap step returns its statistics dict without_specs=PartitionSpec(), so it comes back replicated instead of stackedGtimes.check_vmais off.shard_map’s varying-manual-axis type checker has nopmapequivalent. JAXRENS runs withcheck_vma=False(pmap’s “trust the caller” model), because the checker also traces into backend energy functions it doesn’t control. For example, NeuralIL’s cutofflax.condhas branches that type as varying vs. unvarying.wrap_for_batchstill takes acheck_vmaflag for callers whose body is known to type-check.
Why pytrees (and not, say, flat arrays)¶
Moves only see a single walker. A move kernel like
galileanis written as a pure function of oneMCState; it never sees batch axes.vmaplifts it into a batched kernel automatically.Adding a state field is local. A new move declares a new extra field. The rest of the library (I/O, callbacks, NS loop) never mentions it by name.
Static vs dynamic is an explicit decoration. If a field mutates during a run (step_size adapts; max_neighbors grows on overflow), it’s a leaf. If it’s a compile-time constant (n_atoms), it’s in aux data.
The pytree identity
is what makes all three descriptor modes equivalent at the kernel level — the leaves change shape, the tree structure and aux data do not.
Where this lives in the code¶
Concern |
File |
|---|---|
Dynamic |
|
Static field decorator |
|
Top-level NS state |
|
Batch-shape dispatch |
|
Pytree helpers (pack/unpack) |
|