Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing nested sampling #1871

Merged
merged 7 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ jobs:
- name: Test with pytest
run: |
pytest -vs --durations=20 test/infer/test_mcmc.py
pytest -vs --durations=20 test/infer --ignore=test/infer/test_mcmc.py
pytest -vs --durations=20 test/infer --ignore=test/infer/test_mcmc.py --ignore=test/contrib/test_nested_sampling.py
renecotyfanboy marked this conversation as resolved.
Show resolved Hide resolved
pytest -vs --durations=20 test/contrib --ignore=test/contrib/stochastic_support/test_dcc.py
- name: Test x64
run: |
Expand All @@ -118,6 +118,9 @@ jobs:
- name: Test custom prng
run: |
JAX_ENABLE_CUSTOM_PRNG=1 pytest -vs test/infer/test_mcmc.py
- name: Test nested sampling
run: |
JAX_ENABLE_X64=1 pytest -vs test/contrib/test_nested_sampling.py
renecotyfanboy marked this conversation as resolved.
Show resolved Hide resolved


examples:
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ funsor
ipython
jax
jaxlib
jaxns==2.4.8
jaxns==2.6.3
Jinja2
matplotlib
multipledispatch
Expand Down
2 changes: 2 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

hmc(None, None)

autodoc_mock_imports = ["jaxns"]

# -- Project information -----------------------------------------------------

project = "NumPyro"
Expand Down
2 changes: 2 additions & 0 deletions examples/gaussian_shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def main(args):

if __name__ == "__main__":
assert numpyro.__version__.startswith("0.15.3")

parser = argparse.ArgumentParser(description="Nested sampler for Gaussian shells")
parser.add_argument("-n", "--num-samples", nargs="?", default=10000, type=int)
parser.add_argument("--num-warmup", nargs="?", default=1000, type=int)
Expand All @@ -133,6 +134,7 @@ def main(args):
parser.add_argument("--device", default="cpu", type=str, help='use "cpu" or "gpu".')
args = parser.parse_args()

numpyro.enable_x64()
numpyro.set_platform(args.device)

main(args)
13 changes: 7 additions & 6 deletions numpyro/contrib/nested_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

from functools import singledispatch

import jax
from jax import random, tree
import jax.numpy as jnp

try:
import jaxns # noqa: F401
from jaxns import (
DefaultNestedSampler,
Model,
Prior,
TerminationCondition,
Expand All @@ -17,11 +18,13 @@
resample,
summary,
)
from jaxns.public import DefaultNestedSampler
from jaxns.utils import NestedSamplerResults

except ImportError as e:
raise ImportError(
"To use this module, please install `jaxns` package. It can be"
f"{e} \n "
f"To use this module, please install `jaxns>2.5` package. It can be"
" installed with `pip install jaxns` with python>=3.8"
) from e

Expand Down Expand Up @@ -142,9 +145,7 @@ class NestedSampler:
:param dict termination_kwargs: keyword arguments to terminate the sampler. Please
refer to the upstream :meth:`jaxns.NestedSampler.__call__` method.

**Example**

.. doctest::
Example::

>>> from jax import random
>>> import jax.numpy as jnp
Expand Down Expand Up @@ -258,7 +259,7 @@ def prior_model():

default_constructor_kwargs = dict(
num_live_points=model.U_ndims * 25,
num_parallel_workers=1,
devices=jax.devices(),
max_samples=1e4,
)
default_termination_kwargs = dict(dlogZ=1e-4)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"flax",
"funsor>=0.4.1",
"graphviz",
"jaxns==2.4.8",
"jaxns==2.6.3",
"matplotlib",
"optax>=0.0.6",
"pylab-sdk", # jaxns dependency
Expand Down
18 changes: 14 additions & 4 deletions test/contrib/test_nested_sampling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright Contributors to the Pyro project.
# SPDX-License-Identifier: Apache-2.0

import os

import numpy as np
from numpy.testing import assert_allclose
import pytest
Expand All @@ -11,15 +13,23 @@
import numpyro

try:
from numpyro.contrib.nested_sampling import NestedSampler, UniformReparam
if os.environ.get("JAX_ENABLE_X64"):
renecotyfanboy marked this conversation as resolved.
Show resolved Hide resolved
from numpyro.contrib.nested_sampling import NestedSampler, UniformReparam

except ImportError:
pytestmark = pytest.mark.skip(reason="jaxns is not installed")

import numpyro.distributions as dist
from numpyro.distributions.transforms import AffineTransform, ExpTransform

pytestmark = pytest.mark.filterwarnings(
"ignore:jax.tree_.+ is deprecated:FutureWarning"
)
pytestmark = [
pytest.mark.filterwarnings("ignore:jax.tree_.+ is deprecated:FutureWarning"),
renecotyfanboy marked this conversation as resolved.
Show resolved Hide resolved
pytest.mark.filterwarnings("ignore:JAX x64"),
pytest.mark.skipif(
not os.environ.get("JAX_ENABLE_X64"),
reason="test suite for jaxns requires double precision",
),
]


# Test helper to extract a few central moments from samples.
Expand Down
Loading