fix: handle scalar event shapes in vector field estimators, ODE solvers, and ratio estimators (#2253) #2254
All Vouch review confidence checks passed
Residual risk 0.23 = Change Risk (0.43) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 373 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.354
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #2254"
general
weight: 0.35
patelshivani2283-lab
What does this PR do?
Fixes a bug where components handling event shapes use Python negative slicing (
[:-dim]and[-dim:]) to separate batch dimensions from event dimensions.When
event_shape = torch.Size([])(common for 1D simulators or scalar conditions from PyTorch distributions likeNormalwheredim == 0):tensor.shape[:-0]evaluates totensor.shape[:0] == torch.Size([]), erroneously dropping the batch dimensions.tensor.shape[-0:]evaluates totensor.shape[0:] == tensor.shape, erroneously returning the full tensor shape instead of().Changes made:
[:-dim]and[-dim:]with[:tensor.dim() - dim]and[tensor.dim() - dim:]across:-
sbi/samplers/ode_solvers/zuko_ode.py(ZukoNeuralODE.get_distribution)-
sbi/neural_nets/ratio_estimators.py(RatioEstimator._get_shape_prefix)-
sbi/neural_nets/estimators/flowmatching_estimator.py(FlowMatchingEstimator.__call__)-
sbi/neural_nets/estimators/score_estimator.py(ConditionalScoreEstimator.__call__)-
sbi/inference/posteriors/vector_field_posterior.py(VectorFieldPosterior.sample_batched)-
sbi/samplers/score/diffuser.py(Diffuser.__init__)-
sbi/neural_nets/estimators/base.py(MaskedConditionalEstimator._check_input_shape)-
sbi/utils/pyroutils.py(EstimatorDistribution.__init__,_check_condition_shape, andlog_prob)RatioEstimator.combine_theta_and_xreceive a feature axis (unsqueeze(-1)) before concatenation so that (theta, x) observation pairs are properly aligned row-wise.xagainst batch shape inRatioEstimatorDistribution.log_prob.VectorFieldPosterior._sample_via_diffusionshape-independent via.reshape(*final_samples.shape[:2], -1).all(-1).-
tests/ratio_estimator_test.py:test_ratio_estimator_scalar_shapesandtest_ratio_estimator_combine_scalar_pairing-
tests/vf_estimator_test.py:test_zuko_ode_scalar_condition,test_vector_field_estimators_scalar_condition,test_masked_conditional_estimator_check_input_shape,test_pyroutils_scalar_condition_validation, andtest_diffusion_finite_samples_scalarDoes this close any issues?
Fixes #2253
Anything else we should know?
All tests pass (
tests/ratio_estimator_test.py,tests/vf_estimator_test.py,tests/potential_test.py), andruff check/ruff formatpass cleanly.Checklist
uv run pytest -n auto -m "not slow and not gpu"passes.uv run pre-commit run --all-filespasses (ruff and formatting).<!-- AI Assistant disclosure: Used an AI assistant to identify affected slicing sites across modules and develop regression tests. -->