fix: validate weights, support zero-sized requests, and preserve event shape in EnsemblePosterior (#2227) #2228
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 · 362 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.353
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #2228"
general
weight: 0.35
patelshivani2283-lab
What does this PR do?
This PR addresses several edge-case crashes, shape inconsistencies, and input validation defects in
EnsemblePosterior:1. Zero-sample requests in
sample()andsample_batched():- Previously, requesting 0 samples (
sample_shape=(0,)ornum_samples=0) calledtorch.multinomial(self._weights, 0, replacement=True), which immediately crashed withRuntimeError: cannot sample n_sample <= 0 samples. Furthermore,_stack_and_shufflecrashed on empty sample lists.- Now checks
num_samples == 0upfront and returns an empty tensor with the correct batch and event shapesample_shape + empty.shape[1:]from the component posterior without drawing.2. Event shape preservation in
sample():-
sample()previously calledsamples.reshape(*sample_shape, -1), which flattened multi-dimensional parameter shapes (e.g. event shape(2, 3)) into a 1D tensor(sample_shape, 6).- In contrast,
sample_batched()usedsamples.reshape(sample_shape + samples.shape[1:]).- Updated
sample()to usesamples.reshape(sample_shape + samples.shape[1:]), preserving multi-dimensional parameter event shapes and single sample shapes().3. Weights validation:
- Validates that
weightslength matchesself.num_components, raisingValueErrorinstead of silently ignoring components or failing withIndexErrorat sample time.- Validates that weights are non-negative and sum to a positive value, avoiding division by zero and
NaNweights.- Uses
torch.as_tensor(weights, dtype=torch.float32)instead oftorch.tensor(weights)to prevent PyTorch's copy-constructionUserWarning.4. Spelling fix:
- Corrected variable typo
posterior_indizes->posterior_indices.5. Unit tests:
- Added unit tests in
tests/ensemble_test.pycovering zero-sample requests insample()andsample_batched(), multi-dimensional event shape preservation, mismatched weight lengths, negative weights, and zero-sum weights.Does this close any issues?
Fixes #2227
Anything else we should know?
All 24 tests in
tests/ensemble_test.pypass.Checklist
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.pytest.mark.slow.<!-- If you used an AI assistant for a substantial part of this pull request,
please tell us briefly how. -->
AI disclosure: An AI assistant was used to help isolate the edge cases, formulate test scenarios, and draft the pull request description.