Repository navigation
deprecation: attach observations to potentials only with bind() - #2248
Conversation
Passing `x_o` to a potential constructor or to a `*_estimator_based_potential` factory now warns with a FutureWarning, also as `None`. The factories pass `x_o` through, so `BasePotential.__init__` is the one place that warns. The warning asks to pass the arguments after `prior` by keyword: sbi 0.29 replaces `x_o` with a keyword-only marker, so that old positional calls fail instead of shifting into `enable_transform` or `device`. The passed `x_o` still binds until then. A posterior that uses a default `x` it took from a bound potential warns that this stops in 0.29. The warning fires on use, not on construction, because posteriors write their bound potential back to `potential_fn` and building a new posterior from it is fine. The ensemble and VI posteriors read `default_x` once per call, so each use warns once. sbi no longer passes `x_o` internally. The duplicate "reinitialize the potential" checks go, so an unbound potential raises the one `bind()` message; the vector-field potential raised an `AttributeError` before. `ConditionedPotential.return_x_o()` delegates instead of reading `_x_o`. The `set_x()` warnings name sbi 0.29. `_stacklevel_to_caller()` moves to `sbi.utils.sbiutils` so potentials and posteriors attribute the warnings to the user's line. Tests, the sampler-interface how-to, the conditional-distributions and training-interface tutorials and the decision-making example use `bind()` or `set_default_x()`. Part of #1994.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #2248 +/- ##
==========================================
+ Coverage 88.88% 90.81% +1.93%
==========================================
Files 149 149
Lines 16574 17481 +907
==========================================
+ Hits 14731 15876 +1145
+ Misses 1843 1605 -238
Flags with carried forward coverage won't be shown. Click here to find out more.
|
dgedon
left a comment
There was a problem hiding this comment.
LGTM, accepted with some small optional edits inline below
| assert torch.isfinite(constructed(zeros(1, 2))).all() | ||
|
|
||
|
|
||
| def test_posterior_warns_when_it_uses_x_of_bound_potential(): |
There was a problem hiding this comment.
Could we also test that set_default_x() silences this warning? The same goes for EnsemblePosterior.set_default_x() and the VIPosterior.set_q() copy.
| @property | ||
| def default_x(self) -> Optional[Tensor]: | ||
| """Return default x used by `.sample(), .log_prob` as conditioning context.""" | ||
| if self._x_from_potential: |
There was a problem hiding this comment.
Reading default_x can now warn, so the code reads it once per call. Could we add a short comment here so later edits keep that?
| """Return default x used by `.sample(), .log_prob` as conditioning context.""" | ||
| if self._x_from_potential: | ||
| warn( | ||
| "This posterior uses the `x` bound to its potential as its default " |
There was a problem hiding this comment.
The second sentence of this warning is hard to parse. Could we simplify it?
|
|
The posterior warning now says in two sentences that the posterior falls back to the `x` bound to its potential and that sbi 0.29 removes this fallback. A comment on `default_x` notes that reading it can warn, so callers read it once per call. The GPU-only device test passed `x_o` to `EnsemblePotential`, which CI does not run; it binds now. The posterior warning test also checks that `set_default_x()` silences the warning and that a VI copy keeps its source's state.
|
Thanks for the review! All four points are in now. The warning reads: "This posterior has no default The ensemble needs no test of its own: its |
Part of #1994. The removal in sbi 0.29 follows in a second PR.
Summary
A potential gets its observation in two ways today:
x_oin its constructor or factory, orbind(x_o). A posterior also takes its defaultxfrom a bound potential. This PR deprecates both, so thatbind()is the only way to attach an observation andxflows one way, from the posterior to the potential. Both go in sbi 0.29, together withset_x(), which already warns.x_oin any potential constructor or*_estimator_based_potential, alsox_o=Nonex_oto a potential's constructor is deprecated and will be removed in sbi 0.29. …"xit took from a bound potentialx, so it uses thexbound to its potential (bybind()or by an earlier posterior call). …"Both warnings are
FutureWarnings and point to the user's line. The second one fires only when the posterior uses thatx, so building a posterior from a bound potential and then callingset_default_x()stays silent. In 0.29 the arguments afterpriorbecome keyword-only, so an old positionalx_ofails with aTypeErrorinstead of shifting into the next argument. That is why an explicitx_o=Nonewarns too, and why the warning asks for keyword arguments. Until 0.29 the behaviour does not change: a passedx_ostill binds, and the posterior still uses it as its defaultx.A custom subclass of
BasePotentialdropsx_ofrom its__init__and fromsuper().__init__(). If it derives state fromx_o, it does so where it readsself.x_o. TheBinomialGammaPotentialof the decision-making tutorial shows how.Also in this PR:
x_o, so users see only warnings about their own code.potential_fn.bind(x_o)." The vector-field potential raised anAttributeErrorbefore.set_x()warnings name sbi 0.29.CustomPotentialWrapperuses the basebind()._stacklevel_to_caller()moves from the trainers tosbi/utils/sbiutils.py, so that potentials and posteriors can use it.bind()and whenset_default_x(). The tutorials and tests use the new form.Evidence
test_x_o_at_construction_warns_and_binds: constructorx_owarns and still binds.test_factory_x_o_warns_also_when_none: a factory warns on an explicitNone.test_posterior_warns_when_it_uses_x_of_bound_potential: building the posterior and passingxis silent; using thexof the bound potential warns.Merge Danger
Door: two-way. The PR adds warnings and changes no results.
Blast Radius: small. Users who pass
x_oto a potential see aFutureWarning. Code that runs with warnings as errors fails until it moves tobind().