Repository navigation
Description
Bug description
IntervalGuidanceConfig and IntervalGuidance in sbi/inference/potentials/vector_field_adaptor.py exhibit two critical bugs:
-
Ambiguous boolean evaluation for Tensor bounds in
IntervalGuidanceConfig.__post_init__:
IntervalGuidanceConfigdeclareslower_bound: Optional[Union[float, Tensor]]andupper_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_bounddirectly in Python'sif. Whenlower_boundorupper_boundis 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() -
In-place mutation of
config.maskcauses shape mismatch crash on subsequent evaluations:
Insideinterval_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.maskin-place on the user's config object. Ifconfig.maskhas shape(D,)and the first call has batch size$N_1$ ,config.maskis 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) -
Device / dtype mismatch:
Ifconfig.lower_bound,config.upper_bound, orconfig.maskare passed as Tensors on CPU whileinputis on CUDA / MPS (or has a non-default dtype like float64), binary operations andtorch.wherecrash 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