You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Posterior .to() methods fail on plain torch priors and drop cached state #2004
The .to() methods of the posterior classes have two gaps. #1972 worked around them in NeuralPosterior.__setstate__ by updating the device strings directly instead of calling self.to(). The gaps remain for users who call .to() themselves.
DirectPosterior.to() and VectorFieldPosterior.to() raise ValueError("Prior has no attribute to(device)") for plain torch priors such as MultivariateNormal. BasePotential.to() handles the same case with move_distribution_to_device().
Every posterior .to() re-runs super().__init__(). That drops the cached _map and resets _purpose. A user who calls map() and then .to() computes the MAP again.
🔄 Steps to Reproduce
import torch
from torch.distributions import MultivariateNormal
from sbi.inference import NPE
prior = MultivariateNormal(torch.zeros(2), torch.eye(2))
theta = prior.sample((200,))
x = theta + 0.1 * torch.randn_like(theta)
trainer = NPE(prior=prior, show_progress_bars=False)
trainer.append_simulations(theta, x).train(max_num_epochs=1)
posterior = trainer.build_posterior()
posterior.to("cpu")
# ValueError: Prior has no attribute to(device).
✅ Expected Behavior
.to() accepts every prior that process_prior() accepts. move_distribution_to_device() already covers plain torch distributions.
.to() changes device state only. The cached MAP and the other attributes stay.
Once both hold, NeuralPosterior.__setstate__ can call self.to(actual_device) and the attribute-by-attribute reconciliation from #1972 can go.
One more gap, found by the final review of #1972: RestrictedPrior has no .to() and caches its device in _device. move_distribution_to_device() cannot rebuild it, so it falls back to moving the tensors only. After torch.load(..., map_location="cpu") a posterior whose proposal is a RestrictedPrior reports cpu, but RestrictedPrior.sample() still ends with .to(self._device) and fails on a CPU-only machine.
This is not new in #1972, the same happens on main. A RestrictedPrior.to() that updates _device, moves _prior and forwards to the stored posterior would close it. The RestrictionEstimator it usually wraps caches _device too.
Thanks for offering to take this, @BHARATH0153. After a closer look I would like to keep this one with us. It overlaps with #1995, which @Jocho-Smith opened as part of the potentials and bind() work, and it changes the .to() method of every posterior class plus __setstate__. I will link the two issues and we do the refactor in that stream.
If you want a self-contained task, #1978 still has two open items in sbi/analysis/plot.py: a warning or error on unknown keys in diag_kwargs, upper_kwargs and lower_kwargs, and a docstring plus a deprecation message that point to a function that does not exist. Both are small, need a test each, and CI checks them fully. One PR for both would be great.
Hi mentors! I am a first-year Data Science & Applications student preparing for GSoC 2027. I am really interested in this library but I see there are no active 'good first issues' or documentation tasks left. Could you please guide me if any of these open bug issues can be broken down for a beginner, or if there is any codebase area where I can start helping?
@ansh751881-create thanks for your interest in sbi!
As of now, it is not clear whether sbi will take part in GSoC 2027.
Sorry, we don't have the capacity to break down issues for newcomers at the moment. But our contributing guide explains how we handle contributions, in case you want to come back to sbi later.
Activity
BHARATH0153 commentedon Sep 2, 2026
@janfb thanks! I will look into this
janfb commentedon Sep 3, 2026
One more gap, found by the final review of #1972:
RestrictedPriorhas no.to()and caches its device in_device.move_distribution_to_device()cannot rebuild it, so it falls back to moving the tensors only. Aftertorch.load(..., map_location="cpu")a posterior whoseproposalis aRestrictedPriorreportscpu, butRestrictedPrior.sample()still ends with.to(self._device)and fails on a CPU-only machine.This is not new in #1972, the same happens on main. A
RestrictedPrior.to()that updates_device, moves_priorand forwards to the stored posterior would close it. TheRestrictionEstimatorit usually wraps caches_devicetoo.janfb commentedon Sep 3, 2026
Thanks for offering to take this, @BHARATH0153. After a closer look I would like to keep this one with us. It overlaps with #1995, which @Jocho-Smith opened as part of the potentials and
bind()work, and it changes the.to()method of every posterior class plus__setstate__. I will link the two issues and we do the refactor in that stream.If you want a self-contained task, #1978 still has two open items in
sbi/analysis/plot.py: a warning or error on unknown keys indiag_kwargs,upper_kwargsandlower_kwargs, and a docstring plus a deprecation message that point to a function that does not exist. Both are small, need a test each, and CI checks them fully. One PR for both would be great.thanks, Jan
anshv0 commentedon Oct 5, 2026
Hi mentors! I am a first-year Data Science & Applications student preparing for GSoC 2027. I am really interested in this library but I see there are no active 'good first issues' or documentation tasks left. Could you please guide me if any of these open bug issues can be broken down for a beginner, or if there is any codebase area where I can start helping?
janfb commentedon Oct 6, 2026
@ansh751881-create thanks for your interest in sbi!
As of now, it is not clear whether sbi will take part in GSoC 2027.
Sorry, we don't have the capacity to break down issues for newcomers at the moment. But our contributing guide explains how we handle contributions, in case you want to come back to sbi later.
Cheers, Jan
fix: move posteriors to another device in place and keep their state (s…