Repository navigation
fix: process_prior failing for independent priors with non-float32 types - #2064
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2064 +/- ##
==========================================
+ Coverage 89.89% 89.91% +0.01%
==========================================
Files 142 142
Lines 14646 14650 +4
==========================================
+ Hits 13166 13172 +6
+ Misses 1480 1478 -2
Flags with carried forward coverage won't be shown. Click here to find out more.
|
… shape defaults Warn once per prior that sbi casts non-float32 priors to float32, so users do not expect float64 precision. Add mixed-dtype and multivariate cases, a shape and warning test, and document the None defaults of PytorchReturnTypeWrapper.
|
@ThomasGesseyJonesPX thanks a lot for fixing this, and for the clear reproducer and the honest AI note! To your question: I like changing the default of the wrapper. It also fixes a quiet bug for single priors, where a float64 multivariate prior came out with the wrong event shape. I added a small commit on top. sbi casts the samples and log probs of non-float32 priors to float32. Until now this happened silently, so users could think they get float64 precision. It now warns when it casts. I also added tests for mixed dtypes and multivariate priors, and documented the new defaults in the wrapper docstring. While checking this I found a separate bug: the combined prior returns the wrong shape for Thanks again for fixing this, Jan |
What does this PR do?
process_priorcurrently throws an error when it is given multiple independent priors (as a list) with non-float32dtypes. This is because thebatch_shapeandevent_shapeof each of the component independent priors is not passed to itsPytorchReturnTypeWrapper, e.g., it becomes a scalar, which leads to an assertion error when these are encapsulated inMultipleIndependent. This PR addresses this by havingPytorchReturnTypeWrapperdefault to thebatch_shapeandevent_shapeof the given prior rather thantorch.Size().A minimal example of the issue can be seen with the following code
which outputs an
AssertionErrorwhich incorrectly points the user toward an issue with them using scalar distributions.Does this close any issues?
No, I could not find any matching issues.
Anything else we should know?
The proposed fix works by changing the default behaviour of
PytorchReturnTypeWrapper. This can be avoided by manually passing thebatch_shapeandevent_shapewithinprocess_pytorch_prior. I believe this is the only place its actually used, so I felt changing the default behaviour made more intuitive sense, but would be keen to get a maintainers opinion on which of the two approaches would be better.The test added to avoid regressions intentionally does not use
Uniformas the conversion toBoxUniformconverts these priors to float32 and avoids the bug as a result.Checklist
Put an
xin the boxes that apply. If you are unsure about any of them, justask - we are happy to help.
uv run pytest -n auto -m "not slow and not gpu"passes.uv run pre-commit run --all-filespasses (ruff and formatting).uv run pyright sbipasses.with
pytest.mark.slow.AI usage declaration: the bug was originally encountered by a colleague of mine, who then used Claude to trace this to the
sbipackage and to provide me with instructions for replication and the minimal example. All the code in this PR to address the issue was manually written.