Skip to content

feat: JAX-native priors — xp dispatch on value_for / log_prior_from_value / vector_from_unit_vector - #1263

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/priors-jax-native
May 15, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/priors-jax-native

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Adds a xp=np kwarg to every concrete Prior.value_for, every Prior.log_prior_from_value, the underlying NormalMessage / TruncatedNormalMessage value_for, and Model.vector_from_unit_vector, so cube → physical mapping and prior log-density can run inside a jax.jit boundary without a jax.pure_callback host roundtrip. NumPy paths remain unchanged via xp=np defaults — all existing callers (Nautilus, Dynesty, Emcee, Zeus, EP) keep working untouched. Closes #1262 (Phase 0 of the nss_first_class_sampler roadmap).

API Changes

Added an xp=np keyword to Prior.value_for, Prior.log_prior_from_value, Model.vector_from_unit_vector, NormalMessage.value_for, and TruncatedNormalMessage.value_for. Default xp=np preserves every existing NumPy caller. Each of the five concrete Prior subclasses (UniformPrior, LogUniformPrior, GaussianPrior, TruncatedGaussianPrior, LogGaussianPrior) now overrides value_for with a closed-form JAX path that runs symbolically under jax.jit. NormalMessage.value_for replaces the legacy isinstance(unit, np.ndarray) runtime sniff with explicit xp dispatch.
See full details below.

Test Plan

  • pytest test_autofit — 1242 passed, 1 skipped on this branch
  • autofit_workspace_test/scripts/jax_assertions/priors_xp_dispatch.py — 24 new assertions pass (added in matching workspace_test PR)
  • autofit_workspace_test/scripts/jax_assertions/fitness_dispatch.py — still passes
  • autofit_workspace_test/scripts/jax_assertions/nested.py — still passes
  • autofit_workspace_test/scripts/jax_assertions/enable_pytrees.py — still passes
  • autofit_workspace_test/scripts/jax_assertions/pytrees.py — still passes
  • autofit_workspace_test/scripts/graphical/ep.py — full EP loop runs to completion
  • autofit_workspace/scripts/searches/{mcmc,mle,nest,start_point}.py — all pass
Full API Changes (for automation & release notes)

Added

  • Prior.value_for(unit, xp=np) — new xp kwarg on the base method (autofit/mapper/prior/abstract.py)
  • UniformPrior.value_for(unit, xp=np) — new xp kwarg
  • LogUniformPrior.value_for(unit, xp=np) — new xp kwarg
  • GaussianPrior.value_for(unit, xp=np) — new override (previously inherited from base)
  • TruncatedGaussianPrior.value_for(unit, xp=np) — new override
  • LogGaussianPrior.value_for(unit, xp=np) — new xp kwarg
  • AbstractPriorModel.vector_from_unit_vector(unit_vector, xp=np) — new xp kwarg
  • NormalMessage.value_for(unit, xp=np) — new xp kwarg replacing isinstance dispatch
  • TruncatedNormalMessage.value_for(unit, xp=np) — new xp kwarg

Changed Behaviour

  • Prior.value_for(unit, xp=jnp) — the five concrete subclasses now produce a JAX-traceable closed-form when xp is jax.numpy (UniformPrior linear, LogUniformPrior lower * (upper / lower) ** unit, GaussianPrior / LogGaussianPrior via jax.scipy.special.erfinv, TruncatedGaussianPrior via jax.scipy.stats.norm.{cdf,ppf}).
  • UniformPrior.log_prior_from_value(value, xp=jnp) — now returns xp.where(in_bounds, 0, -xp.inf) instead of 0.0.
  • Model.vector_from_unit_vector(unit, xp=jnp) — returns a 1-D xp.stack array instead of a Python list (NumPy path unchanged).
  • NormalMessage.value_for — explicit xp dispatch replacing the legacy isinstance(unit, np.ndarray) runtime sniff; behaviour is identical for both NumPy and JAX inputs.

Migration

None required. Default xp=np preserves all existing call sites. New callers wanting JAX traceability pass xp=jax.numpy explicitly:

Before (still works):

physical = model.vector_from_unit_vector(unit_vector)

After (new JAX path):

import jax, jax.numpy as jnp
@jax.jit
def cube_to_phys(unit):
    return model.vector_from_unit_vector(unit, xp=jnp)

🤖 Generated with Claude Code

…alue / vector_from_unit_vector

Phase 0 of the nss_first_class_sampler roadmap. Adds an xp=np kwarg to
Prior.value_for, Prior.log_prior_from_value, Model.vector_from_unit_vector,
NormalMessage.value_for, and TruncatedNormalMessage.value_for so the cube → physical
mapping and prior log-density can run inside jax.jit without a pure_callback host
roundtrip. NumPy paths preserved exactly via xp=np defaults.

Closes #1262

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label May 14, 2026
@Jammy2211
Jammy2211 merged commit cde3ba4 into main May 15, 2026
3 checks passed
@Jammy2211
Jammy2211 deleted the feature/priors-jax-native branch May 15, 2026 07:30
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: JAX-native priors — xp dispatch on value_for / log_prior_from_value / vector_from_unit_vector

1 participant