Repository navigation
Description
Bug description
ConditionedMDN raises ValueError: logits contains NaN or Inf values when the condition is far enough from the mixture component means. How far counts as "far enough" depends on the fitted precision factors, so the threshold is model-dependent: a 3-D MDN I trained here fails even at the posterior mode, while the linear-Gaussian MDN used in tests/linearGaussian_snpe_test.py does not fail at the condition it uses.
The reason is in condition_mog (sbi/utils/conditional_density_utils.py:298-301). It renormalizes the mixture weights like this:
new_mcs = torch.exp(logits + log_prob_y)
new_mcs = new_mcs / new_mcs.sum()
cond_logits = torch.log(new_mcs)log_prob_y is a log density. When the observation is not close to the component means it becomes very negative (-132 in my run), so torch.exp(log_prob_y) becomes exactly 0.0 in float32. Then torch.log(0) gives -inf, and if all components hit zero the division is 0/0, giving NaN. Either way cond_logits is invalid, and MoG rejects it.
MoG.condition (sbi/neural_nets/estimators/mog.py:407-412) does the exact same update correctly, using torch.logsumexp. Making condition_mog match it fixes every case I tested:
For one trained 3-D NPE + MDN posterior (BoxUniform(-2, 2), 2000 simulations, 30 epochs), conditioning two dimensions and pushing the condition away from the posterior mode:
| condition | current main |
with fix |
|---|---|---|
0 (mode) |
ValueError |
OK |
3 |
ValueError |
OK |
10 |
ValueError |
OK |
40 |
ValueError |
OK |
This is a single trained model, not a general threshold — the crossover point moves with the fitted precision factors.
log_weights = logits + log_prob_y
cond_logits = log_weights - torch.logsumexp(log_weights, dim=-1, keepdim=True)Same result wherever the current code works (difference 3.8e-06, float32 rounding), finite where it currently breaks. Existing tests still pass with the fix.
condition_mog has a single caller, ConditionedMDN.__init__ (sbi/analysis/conditional_density.py:212), so eval_conditional_density, conditional_corrcoeff and conditional pairplot are affected too when given a ConditionedMDN.
tests/linearGaussian_snpe_test.py::test_mdn_conditional_density passes on main and does not cover this: its MDN has component means close to the observation, so it never reaches the underflow region.
Minimal code example
import torch
from sbi.neural_nets.estimators.mog import MoG
from sbi.utils.conditional_density_utils import condition_mog
torch.manual_seed(0)
num_components, num_dim = 3, 2
logits = torch.zeros(1, num_components)
means = torch.tensor([[[-1.0, 8.0], [-1.0, 16.0], [-1.0, 24.0]]])
precfs = torch.eye(num_dim).expand(1, num_components, num_dim, num_dim) * 2.5
condition = torch.zeros(1, num_dim)
cond_logits, *_ = condition_mog(condition, [0], logits, means, precfs)
print("cond_logits:", cond_logits)
print("any NaN:", bool(torch.isnan(cond_logits).any()))
MoG(
logits=cond_logits,
means=means[:, :, [0]],
precisions=torch.eye(1).expand(1, num_components, 1, 1),
precision_factors=torch.eye(1).expand(1, num_components, 1, 1) * 2.5,
)
print("MoG accepted the logits")Error message or traceback
cond_logits: tensor([[nan, nan, nan]])
any NaN: True
Traceback (most recent call last):
File "minimal.py", line 26, in <module>
MoG(
File "<string>", line 8, in __init__
File "sbi/neural_nets/estimators/mog.py", line 97, in __post_init__
raise ValueError("logits contains NaN or Inf values")
ValueError: logits contains NaN or Inf values
## public API
Traceback (most recent call last):
File "public_api.py", line 21, in <module>
cm = ConditionedMDN(mdn, x_o, condition=torch.zeros(num_dim), dims_to_sample=[0])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "sbi/analysis/conditional_density.py", line 218, in __init__
self._mog = MoG(
^^^^
File "<string>", line 8, in __init__
File "sbi/neural_nets/estimators/mog.py", line 97, in __post_init__
raise ValueError("logits contains NaN or Inf values")
ValueError: logits contains NaN or Inf values
sbi version
0.27.0
Python version
Python 3.12.13
Device
CPU
Inference method
NPE
Single-round or multi-round
Single-round (amortized)
Additional context
No response
Activity
fix: normalize condition_mog mixture weights in log space
refactor: ConditionedMDN delegates to MoG.condition
fix: normalize condition_mog mixture weights in log space (sbi-dev#2026)