Repository navigation
Closed
Labels
beginner friendlyGood for newcomersbugSomething isn't workinghackathonFor our in-person hackathons with maintainers present, not a general up-for-grabs listtestsEverything to do with the sbi test suite
Description
🐛 Bug Description
I was working on PR #1461 (Issue: #1428). I wanted to run all tests quickly (even if they fail) just to test the fixtures, so I set num_samples=10 and steps=5 for posterior sampling in test_npse_iid_inference. Everything seems to work fine for None and gaussian prior, but something weird happens for uniform.
🔄 Steps to Reproduce
Run the tests:
pytest tests/linearGaussian_npse_test.py::test_npse_iid_inference
Full tests code:
DEFAULT_USE = [
pytest.param(("ve", None), id="ve-None"),
pytest.param(("vp", "gaussian"), id="vp-gaussian"),
]
SLOW_COMBINATIONS = [
pytest.param(("vp", "uniform"), id="vp-uniform", marks=pytest.mark.slow),
pytest.param(("vp", None), id="vp-None", marks=pytest.mark.slow),
pytest.param(("ve", "gaussian"), id="ve-gaussian", marks=pytest.mark.slow),
pytest.param(("ve", "uniform"), id="ve-uniform", marks=pytest.mark.slow),
pytest.param(("subvp", "gaussian"), id="subvp-gaussian", marks=pytest.mark.slow),
pytest.param(("subvp", "uniform"), id="subvp-uniform", marks=pytest.mark.slow),
pytest.param(("subvp", None), id="subvp-None", marks=pytest.mark.slow),
]
@pytest.fixture(scope="module")
def npse_trained_model(request):
"""Module-scoped fixture that trains a score estimator for NPSE tests."""
sde_type, prior_type = request.param
num_dim = 2
num_simulations = 5
# likelihood_mean will be likelihood_shift+theta
likelihood_shift = -1.0 * ones(num_dim)
likelihood_cov = 0.3 * eye(num_dim)
if prior_type == "gaussian" or (prior_type is None):
prior_mean = zeros(num_dim)
prior_cov = eye(num_dim)
prior = MultivariateNormal(loc=prior_mean, covariance_matrix=prior_cov)
prior_npse = prior if prior_type is None else None
elif prior_type == "uniform":
prior = BoxUniform(-2 * ones(num_dim), 2 * ones(num_dim))
prior_npse = prior
# This check that our method to handle "general" priors works.
# i.e. if NPSE does not get a proper passed by the user.
inference = NPSE(prior_npse, show_progress_bars=True, sde_type=sde_type)
theta = prior.sample((num_simulations,))
x = linear_gaussian(theta, likelihood_shift, likelihood_cov)
score_estimator = inference.append_simulations(theta, x).train(
stop_after_epochs=200
)
return {
"score_estimator": score_estimator,
"inference": inference,
"prior": prior,
"likelihood_shift": likelihood_shift,
"likelihood_cov": likelihood_cov,
"prior_mean": prior_mean
if prior_type == "gaussian" or prior_type is None
else None,
"prior_cov": prior_cov
if prior_type == "gaussian" or prior_type is None
else None,
"num_dim": num_dim,
"x_o": zeros(num_dim),
"sde_type": sde_type,
"prior_type": prior_type,
}
@pytest.mark.parametrize(
"npse_trained_model",
DEFAULT_USE + SLOW_COMBINATIONS,
indirect=True, # So pytest knows to pass to the fixture
)
@pytest.mark.parametrize(
"iid_method, num_trial",
[
pytest.param("fnpe", 3, id="fnpe-2trials", marks=pytest.mark.slow),
pytest.param("gauss", 3, id="gauss-6trials", marks=pytest.mark.slow),
pytest.param("auto_gauss", 8, id="auto_gauss-8trials"),
pytest.param(
"auto_gauss", 16, id="auto_gauss-16trials", marks=pytest.mark.slow
),
pytest.param("jac_gauss", 8, id="jac_gauss-8trials", marks=pytest.mark.slow),
],
)
def test_npse_iid_inference(npse_trained_model, iid_method, num_trial):
"""Test whether NPSE infers well a simple example with available ground truth."""
num_samples = 10
# Extract data from fixture
score_estimator = npse_trained_model["score_estimator"]
inference = npse_trained_model["inference"]
prior = npse_trained_model["prior"]
likelihood_shift = npse_trained_model["likelihood_shift"]
likelihood_cov = npse_trained_model["likelihood_cov"]
prior_mean = npse_trained_model["prior_mean"]
prior_cov = npse_trained_model["prior_cov"]
num_dim = npse_trained_model["num_dim"]
sde_type = npse_trained_model["sde_type"]
prior_type = npse_trained_model["prior_type"]
x_o = zeros(num_trial, num_dim)
posterior = inference.build_posterior(score_estimator)
posterior.set_default_x(x_o)
samples = posterior.sample((num_samples,), iid_method=iid_method, steps=5)
if prior_type == "gaussian" or (prior_type is None):
gt_posterior = true_posterior_linear_gaussian_mvn_prior(
x_o, likelihood_shift, likelihood_cov, prior_mean, prior_cov
)
target_samples = gt_posterior.sample((num_samples,))
elif prior_type == "uniform":
target_samples = samples_true_posterior_linear_gaussian_uniform_prior(
x_o,
likelihood_shift,
likelihood_cov,
prior, # type: ignore
)
# Compute the c2st and assert it is near chance level of 0.5.
# Some degradation is expected, also because posterior get tighter which
# usually makes the c2st worse.
check_c2st(
samples,
target_samples,
alg=f"npse-{sde_type}-{prior_type}-{num_dim}-{iid_method}-{num_trial}iid-trials",
tol=0.05 * min(num_trial, 8),
)Reacted by Jan Teusen (né Boelts)
Metadata
Metadata
Assignees
Labels
beginner friendlyGood for newcomersbugSomething isn't workinghackathonFor our in-person hackathons with maintainers present, not a general up-for-grabs listtestsEverything to do with the sbi test suite






Activity
vagechirkov commentedon Mar 18, 2025
@manuelgloeckler @janfb
[-]Incorrect number of posterior samples[/-][+]tests: incorrect number of posterior samples[/+]vagechirkov commentedon Mar 21, 2025
I think to reproduce this problem, one can try to simply run sampling with a very low number of samples and the uniform prior. It may just be a sampling logging bug.
vamshi567bh commentedon Feb 13, 2026
@janfb can I take this
janfb commentedon Feb 21, 2026
@BharathC0 sorry for the late reply! Great, have you reproduced the error locally? And what would be your suggest for fixing this?
Jocho-Smith commentedon Feb 27, 2026
Hey everyone, I want to work on this issue. @manuelgloeckler confirmed it is fair if I do so now.
@BharathC0 if you still want to join in, I'm sure we can collaborate on it somehow.
manuelgloeckler commentedon Feb 27, 2026
Sorry @BharathC0 but I will reassign this issue to @Jocho-Smith, given that its already some time passed without an PR and you are already involved in another "good-first-issue". Hope that fine :)
Jocho-Smith commentedon Mar 3, 2026
I'm not sure if I can reproduce the error. Here is the code that should reproduce it to my current knowledge:
I ran this with two parameter settings for
num_samples, steps,(100, 10)and(10, 5). Here is the output I get@vagechirkov do I understand you correctly, that the tqdm style iteration log should look differently?
manuelgloeckler commentedon Mar 3, 2026
Hey @Jocho-Smith ,
I think your current reproducer is missing the
iidsetting i.e. it currently only works with a single observation but this issue is aboutauto_gauss_iid_8_iid_trialtests. For NPSE there happens quite a lot of thinks, in the IID case so this should be checked i.e. in your current example use:I am also not exactly sure, the incorrect number of posterior sample issue exits here i.e. Based on the limited traceback from @vagechirkov , I can see that the test did fail due to high C2ST (i.e. bad approximation). But this is totally expected due to
step=5(i.e. only 5 diffusion steps) yielding a bad approximation.I think what is confusing are the multiple progress bars with different number of samples. Specifically
auto_gaussdoes one initial sampling run to estimate its hyperparameters here. It might makes sense to surpress the progress bar that might appears there.Jocho-Smith commentedon Mar 12, 2026
Thanks for the clarification @manuelgloeckler !
Here is how I adopted your suggestion in a test file:
I ran this with
pytest -s tests/test_npse_auto_gauss_iid.py, I can still confirm, that there is no shape miss match. I think the initial confusion was indeed only b/c of the status bar. If you still think this should be tested, I can add this test via a PR.Also I spent some time investigating the multiple progress bars that appear when using
iid_method="auto_gauss"and tried to trace where they originate from.From debugging the call stack, the sampling pipeline looks roughly like this:
posterior.sampleaccept_reject_sample(called irregardless of the value ofreject_outside_prior)_sample_via_diffusion(passed as input viaproposalforaccept_reject_sample)Diffuser.run(here is the actual status bar)The diffusion sampler (
Diffuser.run) is therefore used as the proposal distribution inside rejection sampling, meaning it can be called multiple times during a singleposterior.samplecall.In practice this leads to output like:
These progress indicators correspond to different parts of the algorithm:
Diffuser.run66it [...]bar comes from something else. It is not the "Drawing {num_samples} samples for" bar insideaccept_reject_sample. That is beforeDiffuser.run.To sum this up: I tried understanding it and I tried multiple runs where I just 'grid searched' different combinations of values for
show_progress_bars(within different abstraction layers in the code) and I failed to make it happen. Either you see both bars or none of them.The only way I see to realize this is to additionally keep track of when/why
Diffuser.runis called to differentiate between the different calls. I doubt though, that this is worth the hustle.Hope there is some value in what I did here :D
manuelgloeckler commentedon Mar 12, 2026
Thanks @Jocho-Smith for investigating this in detail! I agree with your evaluation and I do not think a test is required.
I hence will close this issue here. For the progress bar problem I would create a new "enhancement" request for this pointing to your analysis here. I think one consideration would be to just remove
Diffuser.runprogressbar, by I will move this to the discussion to the new issue #1811.fix: do not let the rejection bars overshoot their total
fix: suppress nested progress bars via thread-local context manager (#…