← Back to Pull requests

fix: handle scalar event shapes in vector field estimators, ODE solvers, and ratio estimators (#2253) #2254

Open
unknown opened this pull request with 373 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 28, 2026 08:34 UTC
Author

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 like Normal where dim == 0):

  • tensor.shape[:-0] evaluates to tensor.shape[:0] == torch.Size([]), erroneously dropping the batch dimensions.

  • tensor.shape[-0:] evaluates to tensor.shape[0:] == tensor.shape, erroneously returning the full tensor shape instead of ().
  • Changes made:


  • Replaced [:-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, and log_prob)
  • Ensured 1D scalar embeddings in RatioEstimator.combine_theta_and_x receive a feature axis (unsqueeze(-1)) before concatenation so that (theta, x) observation pairs are properly aligned row-wise.

  • Broadcast x against batch shape in RatioEstimatorDistribution.log_prob.

  • Made finiteness check in VectorFieldPosterior._sample_via_diffusion shape-independent via .reshape(*final_samples.shape[:2], -1).all(-1).

  • Added comprehensive unit tests in:

  • - tests/ratio_estimator_test.py: test_ratio_estimator_scalar_shapes and test_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, and test_diffusion_finite_samples_scalar

    Does 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), and ruff check / ruff format pass cleanly.

    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).

  • I added or updated tests for the changed behavior.

  • I used Google-style docstrings for new or changed public functions.
  • <!-- AI Assistant disclosure: Used an AI assistant to identify affected slicing sites across modules and develop regression tests. -->

    Diff: 373 lines (+279 / -94) across 4 files Reviewer: patelshivani2283-lab Review duration: 1243m
    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
    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.354
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #2254"
    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.8% (MEDIUM) · Higher = larger blast radius
    42.8%
    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.8% · Safe to merge
    22.8%
    Repository