Skip to content

IntervalGuidance crashes on multidimensional Tensor bounds and mutates mask in-place #2241

Backlog

Description

@patelshivani2283-lab

Bug description

IntervalGuidanceConfig and IntervalGuidance in sbi/inference/potentials/vector_field_adaptor.py exhibit two critical bugs:

  1. Ambiguous boolean evaluation for Tensor bounds in IntervalGuidanceConfig.__post_init__:
    IntervalGuidanceConfig declares lower_bound: Optional[Union[float, Tensor]] and upper_bound: Optional[Union[float, Tensor]]. However, its validation check:

    if (
        self.lower_bound is not None
        and self.upper_bound is not None
        and self.lower_bound >= self.upper_bound
    ):

    evaluates self.lower_bound >= self.upper_bound directly in Python's if. When lower_bound or upper_bound is a Tensor with more than 1 element (e.g. lower_bound=torch.zeros(3), upper_bound=torch.ones(3) for per-dimension constraints), this raises:
    RuntimeError: Boolean value of Tensor with more than one value is ambiguous. Use a.any() or a.all()

  2. In-place mutation of config.mask causes shape mismatch crash on subsequent evaluations:
    Inside interval_fn:

    if config.mask is not None:
        if config.mask.shape != out.shape:
            config.mask = config.mask.unsqueeze(0).expand_as(out)
        out = torch.where(config.mask, out, torch.zeros_like(out))

    This mutates config.mask in-place on the user's config object. If config.mask has shape (D,) and the first call has batch size $N_1$, config.mask is mutated to shape $(N_1, D)$. Any subsequent evaluation with a different batch size $N_2 \neq N_1$ (e.g. during adaptive ODE integration, batched sampling, or multiple calls with different batch sizes) triggers:
    config.mask.unsqueeze(0) -> shape $(1, N_1, D)$, which fails to .expand_as(out) (shape $(N_2, D)$), raising:
    RuntimeError: expand(...): the number of sizes provided (2) must be greater or equal to the number of dimensions in the tensor (3)

  3. Device / dtype mismatch:
    If config.lower_bound, config.upper_bound, or config.mask are passed as Tensors on CPU while input is on CUDA / MPS (or has a non-default dtype like float64), binary operations and torch.where crash with device/dtype mismatch.

Minimal code example

import torch
from sbi.inference.potentials.vector_field_adaptor import IntervalGuidanceConfig

# Bug 1: Tensor bounds crash in __post_init__
config = IntervalGuidanceConfig(
    lower_bound=torch.zeros(3),
    upper_bound=torch.ones(3),
)

And for Bug 2:

import torch
from sbi.inference.potentials.vector_field_adaptor import (
    IntervalGuidance,
    IntervalGuidanceConfig,
)
from tests.score_samplers_test import _build_gaussian_score_estimator

score_fn = _build_gaussian_score_estimator("vp", (2,), torch.zeros(2), torch.ones(2))
config = IntervalGuidanceConfig(
    lower_bound=0.0,
    upper_bound=1.0,
    mask=torch.tensor([True, False]),
)
guidance = IntervalGuidance(score_fn.vector_field_estimator, prior=None, config=config)

# Call 1: batch_size = 10 (succeeds, but mutates config.mask to (10, 2))
guidance(torch.randn(10, 2), condition=torch.zeros(10, 1), time=torch.tensor([0.5]))

# Call 2: batch_size = 5 (crashes due to in-place mask mutation!)
guidance(torch.randn(5, 2), condition=torch.zeros(5, 1), time=torch.tensor([0.5]))

Error message or traceback

Bug 1:

RuntimeError: Boolean value of Tensor with more than one value is ambiguous. Use a.any() or a.all()

Bug 2:

RuntimeError: expand(CPUBoolType{[1, 10, 2]}, size=[5, 2]): the number of sizes provided (2) must be greater or equal to the number of dimensions in the tensor (3)

sbi version

0.27.0 (main branch)

Python version

3.12

Device

CPU / CUDA / MPS

Inference method

NPSE / FMPE / Vector field methods with Interval Guidance

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions