astronomix._pallas_helpers#
Shared Pallas-backend utilities used across the FD and FV paths.
This module is the single place that:
- imports Pallas / Triton (and exposes pl is None if unavailable),
- normalises config.pallas_block_shape to a 3-tuple,
- builds Triton CompilerParams from config knobs,
- exposes the backend == PALLAS predicate,
- provides the _pallas_call_sharded multi-GPU wrapper that turns an
opaque
pl.pallas_callinto ashard_map+ ppermute halo-exchange body when the user runs on a multi-device mesh.
Every Pallas kernel module under astronomix should import from here so
new knobs / fallbacks only need to be added once.
Module Contents#
Functions#
Set the active mesh for Pallas kernel sharding. |
|
Run |
|
Same as |
|
Run |
API#
- astronomix._pallas_helpers.pallas_mesh_context(mesh)[source]#
Set the active mesh for Pallas kernel sharding.
time_integrationenters this context around the JIT trace whenever the user supplies ashardingargument. Inside the context every Pallas kernel that calls_pallas_call_shardedwill route through ashard_map+ ppermute halo exchange instead of the barepl.pallas_call.When
meshisNone(single-device run) or has size 1, the helper is a no-op — the kernel runs exactly as before.
- astronomix._pallas_helpers.diffable_pallas_call(state, params, *, pallas_branch, native_branch)[source]#
Run
pallas_branch(state, params)with a custom_jvp boundary that routes tangent computation throughnative_branch.Both branches must accept the same positional
(state, params)pair and produce the same pytree structure. Anything static (config, registered_variables, axis index, …) should be closed over.Outside of AD the call collapses to
pallas_branch(state, params)directly — no overhead. Underjax.jvp/jax.jacfwd/jax.grad/jax.vjp/jax.jacrevthe custom rule fires and the tangent goes throughnative_branch.
- astronomix._pallas_helpers.diffable_pallas_call_n(primals, *, pallas_branch, native_branch)[source]#
Same as
diffable_pallas_call()but takes a tuple of arbitrary differentiable primals (so callers with more than two diff args, e.g. extra rhs/accumulator buffers, can still get a custom_jvp boundary).
- astronomix._pallas_helpers.pallas_vjp_call(state, aux, *, pallas_forward, pallas_backward)[source]#
Run
pallas_forward(state, aux)with ajax.custom_vjpboundary whose reverse rule is a native Pallas adjoint kernelpallas_backward.Unlike
diffable_pallas_call()(which routes the tangent — and hence the transposed gradient — through native JAX), this keeps the entire backward pass on the Pallas/GPU backend:pallas_backward(state, aux, cot)returns the input cotangentd(loss)/d(state)directly from a hand-built adjoint kernel.Differentiates w.r.t.
stateonly.aux(e.g. the tracedSimulationParams) is threaded through the boundary and given a zero cotangent — it must be passed explicitly rather than closed over becausejax.custom_vjpcannot capture traced values in its forward/backward closures (only static/concrete data — config, axis — may be closed over by the two branches). Treating the physical constants as non-differentiable matches the inverse-problem regime (gradients w.r.t. the state, not params).NOTE:
jax.custom_vjpsupports reverse-mode only —jax.jvp/ forward-mode AD on this boundary raises. Use it for reverse-mode (jax.grad/differentiation_mode = BACKWARDS); for forward-mode keepdiffable_pallas_call().