to_pyro_distribution log_prob crashes for one observation without a plate
Maintainers usually reply within 6 days
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 45/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Domain
- backend, machine-learning, testing
Research direction
Read sbi/utils/pyroutils.py, especially ConditionalDensityEstimatorDistribution.log_prob(), and the existing cases in tests/pyroutils_test.py. Add coverage for a single observation without pyro.plate, checking that log_prob returns a scalar and that the reported failure is addressed; the issue also proposes an MCMC regression check. PR #2304 is already open against this issue, so check its status before starting.
Written by the indexing model from the issue text.
Description
Bug description
When using a trained likelihood in a Pyro model with a single observation and without pyro.plate, calling pyro.sample() with to_pyro_distribution(likelihood, theta) raises a TypeError. The same model works when the observation is wrapped inside pyro.plate("trials", 1).
The issue comes from ConditionalDensityEstimatorDistribution.log_prob() in sbi/utils/pyroutils.py, line 135:
return lp.reshape(*sample_shape, *self.batch_shape)
When there is only one observation and no plate, both sample_shape and self.batch_shape are empty, so the call becomes lp.reshape(), which raises an error because PyTorch expects a shape argument.
The existing tests in tests/pyroutils_test.py always use a plate, even for a single trial, so this case is not covered.
Minimal code example
import pyro
import pyro.distributions as pdist
import torch
from pyro.infer import MCMC, NUTS
from sbi.inference import NLE
from sbi.utils import BoxUniform
from sbi.utils.pyroutils import to_pyro_distribution
prior = BoxUniform(-torch.ones(2), torch.ones(2))
theta = prior.sample((500,))
x = theta + 0.1 * torch.randn_like(theta)
likelihood = NLE(prior).append_simulations(theta, x).train(max_num_epochs=5)
x_o = torch.tensor([0.5, -0.5])
def model():
th = pyro.sample("theta", pdist.Uniform(-torch.ones(2), torch.ones(2)).to_event(1))
pyro.sample("x", to_pyro_distribution(likelihood, th), obs=x_o)
MCMC(NUTS(model), num_samples=100, warmup_steps=50).run()
Error message
File "sbi/utils/pyroutils.py", line 135, in log_prob
return lp.reshape(*sample_shape, *self.batch_shape)
TypeError: reshape() missing 1 required positional arguments: "shape"
Expected behavior
The model should evaluate the log probability of a single observation without requiring a pyro.plate. The log probability should be a scalar with shape (), consistent with the behavior when using pyro.plate("trials", 1).
Proposed fix
Replace:
return lp.reshape(*sample_shape, *self.batch_shape)
with:
return lp.reshape(sample_shape + self.batch_shape)
Concatenating the two torch.Size objects always passes a valid shape to reshape(), including when both shapes are empty.
Verification
I tested the change locally. With a fully trained network (2000 simulations), the Pyro posterior mean was approximately (0.49, -0.48), which matches the posterior mean from sbi's own NLE posterior with the same network, approximately (0.49, -0.49).
Proposed regression test
A test in tests/pyroutils_test.py that:
- uses a single observation without
pyro.plate; - checks that
log_probreturns a scalar with shape(); - checks that MCMC runs without raising an exception.
Additional context
PR #2254 modifies the same function but does not change this reshape, so the issue remains with it.
Environment:
- sbi:
main(3968ef24) - Python:
3.12.16 - PyTorch:
2.15.0 - Pyro:
1.9.2 - Device: CPU
- Inference method: NLE (
to_pyro_distribution) - Rounds: single-round
I used an AI coding assistant to help identify and verify this bug.
- Dominant language
- Python
- Stars
- 894
- Forks
- 306
- Avg merge
- 5d 21h
- Merged PRs (30d)
- 61
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from sbi-dev/sbi
-
simulate_for_sbi(num_simulations=0) returns 1D empty theta/x tensors and breaks append_simulationsOpenbug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
sbi-dev/sbi#2310 · 3 comments ·
Maintainers usually reply within 6 days
-
bug: RestrictionEstimator.train(subsample_invalid_sims="auto") raises TypeErrorPossibly taken @Nitesh780 claimed this 2 days ago. Openbug
Difficulty 1/5 1-3 hours Newbie friendliness 85/100
sbi-dev/sbi#2309 · 1 comment ·
Maintainers usually reply within 6 days
-
TARP reference sampling fails when a parameter coordinate is fixedPossibly taken @Jayant-kernel claimed this 4 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
sbi-dev/sbi#2264 · 2 comments ·
Maintainers usually reply within 6 days
-
fix: broadcast UniformNormalConvolution batch shapePossibly taken @studentarijitpaul claimed this 32 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
sbi-dev/sbi#2151 · 2 comments ·
Maintainers usually reply within 6 days
-
bug
sbi-dev/sbi#2321 · 1 comment ·
Maintainers usually reply within 6 days
Similar issues
-
docs(contributing): the two things that silently block a PRPossibly taken A pull request linked to this issue is open or already merged. Openavailable documentation good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 95/100
Nixitov/Riptide-Native#944 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
JTHCode/secretguard#24 ·
Maintainers usually reply within 1 day
-
area: middleware documentation good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
good first issue
Difficulty 1/5 Under an hour Newbie friendliness 95/100
omarelshamy25x/shamy-hijri#63 ·
Maintainers usually reply within 1 day