Core concepts¶
Seven ideas hold up the rest of the library. Each maps to a small set of files and a configuration section; once you know these you can read everything else. Each has a dedicated subpage with the relevant mathematics, diagrams, and pointers into the code:
Schema → resolve → run — how a YAML file becomes a running NS job (schema → resolve → run).
The nested-sampling loop — the two-loop NS structure, prior-mass contraction, evidence estimator.
Walkers are JAX pytrees — how walkers live as JAX pytrees and how batch axes flow through
jit/vmap/shard_map.Moves and the Metropolis-within-Gibbs scheduler — individual move kernels and how MWG composes them into a single step function.
Backends and ensembles — the
EnergyBackendprotocol and how aHamiltonianadds NPT / semi-grand μPT (and soft-core) terms per call.Replica topology and inter-RE — how
n_total,n_gpu,n_per_gpuare derived and how inter-replica exchange (RENS) swaps work.Restart, resume, and the output-dir lifecycle — fresh-vs-restart lifecycle, output-dir gate,
--resumeauto-discovery, and the strict compatibility validator.
The summaries below are the 30-second version of each; follow the links for detail.
1. YAML becomes a run in three layers¶
A config file reaches the sampler through three layers with strictly
separated jobs: the schema (pydantic models in
jaxrens.cli.schema) validates shape, types and ranges without
importing JAX; the resolver (jaxrens.cli.resolve) turns the
validated spec into runtime objects — backend instance, initial walker
positions, move descriptors, per-replica ensemble params — without
importing pydantic; and the core consumes frozen dataclasses and
JAX arrays, knowing nothing about YAML.
The resolver splits further, into a plan phase that only makes
decisions (topology, divisibility, path existence) and a materialise
phase that builds the backend and places walkers. That is what lets
jaxrens validate be near-instant by default and pay the full startup
cost only under --full. Every field you can write is documented at
Configuration.
2. The two-loop NS structure¶
Nested sampling in jaxrens runs as an outer Python loop around
an inner lax.scan. The boundary is load-bearing:
Outer loop — step-size adaptation, move-kernel dispatch, overflow retry for MLIP neighbor-buffer resizing, termination checks, callback dispatch, I/O. Written in plain Python because it needs dynamic shapes and Python-side control flow.
Inner scan —
n_mcmc_stepsof MWG sampling per dead-point replacement. Fully JIT-compiled; shape-stable.
Code: jaxrens.sampling.nested_sampling, especially
run_ns, run_ns_parallel, run_ns_multi_gpu.
3. Walkers are JAX pytrees¶
The per-walker state is a MCState
dataclass — positions, cell, species, step-size, energy,
ensemble-params, plus any move-specific extra fields (e.g. a
momentum for HMC, a velocity direction for galilean). It’s
registered with JAX’s pytree machinery so jit, vmap, and shard_map
just work on it.
NS-level state (NSState) wraps the
batched walker population plus dead-point history, iteration
counter, log-evidence, and cumulative counters.
Static fields (compile-time constants like n_atoms, max_neighbors)
are tagged with static_field() so changing them triggers
recompilation, while dynamic fields are leaves.
4. Moves compose through MWG¶
Individual MCMC kernels live under
jaxrens.sampling.moves — random_walk, galilean, hmc,
single_atom, alchemical, swap, volume, shear, stretch,
replica_exchange. Each exposes a build_kernel(energy_fn, params, **kernel_kwargs) factory returning a step_fn(rng_key, state, likelihood_constraint) -> (state, MoveInfo).
A kernel is handed to the sampler wrapped in a
MoveKernel descriptor — a
dataclass carrying the kernel’s name, its build_kernel, the
kernel_kwargs to bake in, its weight and step_size, any
extra_state_fields it needs on MCState, and which state aspects it
mutates (so the constraint machinery knows which moves to gate).
build_mwg(backend, descriptors) in
jaxrens.sampling.mwg composes any list of them into a
single Metropolis-within-Gibbs step function, with per-move weights
controlling dispatch probabilities.
YAML: the moves: section is an ordered list; each entry’s type
selects the kernel and weight sets its MWG probability.
5. Backends and ensembles¶
An energy model is anything implementing the
EnergyBackend protocol:
(positions, species, cell, max_neighbors, ensemble_params) →
BackendResult, a NamedTuple whose
energy field is the only universally-meaningful one —
max_neighbor_count and overflow drive the neighbor-bucket manager,
and forces is filled only on the force path
(eval_energy_and_forces(): native forces
when the backend has them, autodiff otherwise).
Built-ins:
lj— Lennard-Jones with periodic cutoff.mace— MACE-JAX wrapper with supercell neighbor expansion.neuralil— NeuralIL with bucketed kernel compilation.nequix— Nequix, from a local checkpoint or a bundled model.jaxmd— jax-md analytic potentials (Tersoff, EAM), all-pairs.harmonic,double_well,gaussian_mixture— toy potentials for testing.
The sampler works with a
Hamiltonian: the bare backend
(the model) plus a flat list of additive energy terms. Each layer
contributes its own energy and forces. The ensemble correction is
one such term, EnsembleTerm:
NVT — no correction (no ensemble term).
NPT —
H = U + P·V.μPT / semi-grand —
H = U + P·V − μ·N.
ensemble_params is passed per call, so different replicas can run
at different pressures / chemical potentials without rebuilding the
Hamiltonian. The optional soft-core repulsion is another term,
SoftCoreTerm.
6. Replica axes: n_total, n_gpu, n_per_gpu¶
Multi-run dispatch activates when the YAML config implies more than one replica via a replica-differentiating list:
ensemble.pressure: [1, 2, 4, 8]→ 4 replicas at those pressures.inter_re.composition_targets: [[8, 0], [4, 4]]→ 2 replicas (XRENS).inter_re.chemical_potentials: [...]→ N replicas (semi-grand).
The resolver reads len(jax.local_devices()) at run time and splits
the replicas evenly:
n_total = len(list)— total replica count.n_gpu = len(jax.local_devices())— detected.n_per_gpu = n_total / n_gpu— derived (must divide evenly).
The NS state then has shape (n_gpu, n_per_gpu, n_walkers, ...) on
every dynamic field, and execution is shard_map(vmap(vmap(ns_step))) — a jax.shard_map over a
1-D "gpu" device mesh, wrapping a vmap over replicas and the
per-walker vmap inside ns_step.
See the Tutorials for a concrete multi-GPU example, and Configuration for the exact divisibility rules and every field that can drive a replica axis.
7. Restart, resume, and the output-dir gate¶
A run is either fresh or a restart, and the two are kept apart
deliberately. A fresh run refuses to start if working_dir already
holds artifacts under the same out_file_prefix, so a second run can
never silently append to or overwrite the first — pass --force to
clear them intentionally.
A restart either names its checkpoint explicitly
(init.restart_file) or lets --resume auto-discover the newest one
in working_dir. Either way a compatibility validator checks the
config against what the checkpoint was written with and refuses
mismatches that would corrupt the estimate, rather than continuing
from an inconsistent state. Burn-in is skipped on restart.
What’s where — quick map¶
I want to… |
Look in |
|---|---|
Read the YAML schema (fields, types, validation) |
|
Understand how a YAML becomes a runtime object |
|
Swap in a new energy model |
|
Add a new MCMC move |
|
Debug a run that looks wrong |
monitor log |
Post-process evidence / heat capacity |
|
Look up what a symbol ( |