← Back to Pull requests

fix: validate weights, support zero-sized requests, and preserve event shape in EnsemblePosterior (#2227) #2228

Open
unknown opened this pull request with 362 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 25, 2026 08:52 UTC
Author

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() and sample_batched():
- Previously, requesting 0 samples (sample_shape=(0,) or num_samples=0) called torch.multinomial(self._weights, 0, replacement=True), which immediately crashed with RuntimeError: cannot sample n_sample <= 0 samples. Furthermore, _stack_and_shuffle crashed on empty sample lists.
- Now checks num_samples == 0 upfront and returns an empty tensor with the correct batch and event shape sample_shape + empty.shape[1:] from the component posterior without drawing.

2. Event shape preservation in sample():
- sample() previously called samples.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() used samples.reshape(sample_shape + samples.shape[1:]).
- Updated sample() to use samples.reshape(sample_shape + samples.shape[1:]), preserving multi-dimensional parameter event shapes and single sample shapes ().

3. Weights validation:
- Validates that weights length matches self.num_components, raising ValueError instead of silently ignoring components or failing with IndexError at sample time.
- Validates that weights are non-negative and sum to a positive value, avoiding division by zero and NaN weights.
- Uses torch.as_tensor(weights, dtype=torch.float32) instead of torch.tensor(weights) to prevent PyTorch's copy-construction UserWarning.

4. Spelling fix:
- Corrected variable typo posterior_indizes -> posterior_indices.

5. Unit tests:
- Added unit tests in tests/ensemble_test.py covering zero-sample requests in sample() and sample_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.py pass.

Checklist

  • I have read the contributing guide.

  • uv run pytest -n auto -m "not slow and not gpu" passes.

  • uv run pre-commit run --all-files passes (ruff and formatting).

  • uv run pyright sbi passes.

  • I added or updated tests for the changed behavior.

  • I used Google-style docstrings for new or changed public functions.

  • (If applicable) I reported how long new tests run and marked slow ones with 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.

    Diff: 362 lines (+271 / -91) across 4 files Reviewer: patelshivani2283-lab Review duration: 5545m
    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
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.6% (MEDIUM) · Higher = larger blast radius
    42.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.7% · Safe to merge
    22.7%
    Repository