Skip to content

Posterior .to() methods fail on plain torch priors and drop cached state #2004

Closed

Description

@janfb

🐛 Bug Description

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.

  1. 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().
  2. 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.

📌 Additional Context

  • Found while reviewing fix: reconcile posterior device after mapped load #1972. The .to() methods live in
    sbi/inference/posteriors/*_posterior.py; move_distribution_to_device() is in
    sbi/utils/user_input_checks_utils.py.
  • VIPosterior.to() and BasePotential.to() already do it right: they move state in place
    and do not re-run __init__.

Activity

  1. added
    bug
    on Sep 2, 2026
  2. BHARATH0153 commented on Sep 2, 2026

    @BHARATH0153
    Contributor

    @janfb thanks! I will look into this

  3. janfb commented on Sep 3, 2026

    @janfb
    ContributorAuthor

    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.

  4. janfb commented on Sep 3, 2026

    @janfb
    ContributorAuthor

    @janfb thanks! I will look into this

    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.

    thanks, Jan

  5. self-assigned this
    on Sep 24, 2026
  6. anshv0 commented on Oct 5, 2026

    @anshv0

    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?

  7. janfb commented on Oct 6, 2026

    @janfb
    ContributorAuthor

    @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

  8. added a commit that references this issue on Nov 11, 2026
    c36f471
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions