fix: handle scalar event_shape () in reshape_to_{batch,sample_batch}_event (#2235) #2236
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.45) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 412 lines diff across 5 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.360
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #2236"
general
weight: 0.35
patelshivani2283-lab
What does this PR do?
Fixes #2235 —
reshape_to_batch_eventandreshape_to_sample_batch_eventinsbi/neural_nets/estimators/shape_handling.pyraise a spuriousRuntimeErrorwhenevent_shapeistorch.Size([])(scalar observations/parameters), breaking callers that handle scalar events (such asMarginalTraineron 1-D data).Root cause
Python's negative-index slice edge case:
When
event_shape = torch.Size([]),event_shape_dim = 0, andtensor.shape[-0:]returns the full shape instead of the expected empty slice. The trailing-shape check then always fails.Downstream impact & Caller Hardening
1. Scalar event handling: Callers such as
MarginalTraineron 1-D data (input_shape == torch.Size([])) crashed during estimation and sampling.2. Batch dimension validation: In
likelihood_based_potential.pyandnpe_b.py, callers passingtheta.shape[1:]to the reshape helpers previously raised by accident whenthetawas 1-D unbatched. With scalar events now supported, explicittheta.dim() < 2guards were added to ensure unbatchedthetacontinues to raise an informativeValueError:Fix
1. Add a one-branch guard for
event_shape_dim == 0in both reshape utilities:The existing Simformer special-case for
event_shape == torch.Size([0])is left untouched.2. Add explicit batch dimension validation in
_log_likelihoods_over_trials(likelihood_based_potential.py) and_log_prob_proposal_posterior(npe_b.py).Tests added
tests/density_estimator_test.py:- 4 parametrized cases for
reshape_to_sample_batch_eventwithevent_shape = ()covering scalar, 1-D batch, 1-D sample, and 2-D inputs.- Standalone
test_reshape_to_batch_event_scalar_eventcovering a 1-D batch and a 0-D scalar input.tests/marginal_estimator_test.py:-
test_marginal_trainer_1d_data: trainsMarginalTraineron 1-D scalar data, verifying scalarinput_shape == torch.Size([]), sampling shape(10, 1), and log probability shape(10,).tests/potential_test.py:-
test_likelihood_based_potential_raises_on_unbatched_theta: assertsLikelihoodBasedPotentialraisesValueErroron unbatched theta.tests/linearGaussian_snpe_test.py:-
test_npe_b_log_prob_proposal_posterior_raises_on_unbatched_theta: assertsNPE_Bproposal-posterior evaluation raisesValueErroron unbatched theta.Checklist
pytest tests/density_estimator_test.py tests/marginal_estimator_test.py tests/potential_test.py tests/linearGaussian_snpe_test.py)ruff checkandruff format --checkpass with no errors