← Back to Pull requests

fix: support tensor bounds and prevent in-place mask mutation in IntervalGuidance (#2241) #2242

Open
unknown opened this pull request with 383 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 27, 2026 06:08 UTC
Author

What does this PR do?

Fixes two issues in IntervalGuidanceConfig and IntervalGuidance in sbi/inference/potentials/vector_field_adaptor.py:

1. Tensor bounds & scalar precision support: IntervalGuidanceConfig types lower_bound and upper_bound as Optional[Union[float, Tensor]]. However, self.lower_bound >= self.upper_bound raised RuntimeError: Boolean value of Tensor with more than one value is ambiguous when multidimensional tensors were passed. __post_init__ now compares Python scalars directly (preserving precision for tight intervals such as 1.0 and 1.0 + 1e-8) and converts non-scalars to tensors on matching devices using (lb >= ub).any(), correctly supporting floats, 0-d tensors, mixed-device tensors, and multidimensional tensors.
2. Prevent in-place mask mutation: interval_fn previously assigned config.mask = config.mask.unsqueeze(0).expand_as(out) in-place on the dataclass instance, corrupting config.mask from (D,) to (N1, D). Any subsequent evaluation with a different batch size $N_2$ (e.g. during ODE integration or multi-batch evaluation) crashed with RuntimeError: expand(...): the number of sizes provided (2) must be greater or equal to the number of dimensions in the tensor (3). It now uses a local mask tensor and broadcasts with mask.expand_as(out) without mutating config.mask.
3. Device & dtype preservation: Ensures tensor bounds and mask are moved to device=input.device and dtype=input.dtype to avoid device/dtype mismatch crashes when evaluating on GPU or with non-default dtypes.

Does this close any issues?

Fixes #2241

Anything else we should know?

  • Added test_interval_guidance_config_tensor_bounds: validates valid/invalid tensor and scalar bounds (including tight intervals like 1.0 and 1.0 + 1e-8) and bounds requirement.

  • Added test_interval_guidance_with_mask_multiple_batch_sizes: verifies evaluations across different batch sizes (10, 5, 1) without mask mutation or runtime errors.

  • Added test_interval_guidance_mask_semantics: verifies penalties are applied only to masked dimensions.

  • All new tests pass in 0.25 s (pytest tests/score_samplers_test.py -k "test_interval_guidance").

  • Existing interval guidance tests in tests/linearGaussian_vector_field_test.py pass.

  • ruff check and ruff format --check pass on changed files.
  • 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 (verified on CI Linting workflow).

  • 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.
  • An AI coding assistant was used to assist in identifying edge cases and writing tests. All changes and math were reviewed and verified locally.

    Diff: 383 lines (+287 / -96) across 4 files Reviewer: patelshivani2283-lab Review duration: 2828m
    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 · 383 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.356
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #2242"
    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: 43.0% (MEDIUM) · Higher = larger blast radius
    43.0%
    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.9% · Safe to merge
    22.9%
    Repository