Skip to content

GlobalMutualInformationLoss returns NaN for constant B-spline inputs #9018

Description

@yinkev

Describe the bug

GlobalMutualInformationLoss(kernel_type="b-spline") returns a non-finite loss for valid constant prediction or target tensors. Constant tensors have zero intensity range, so the B-spline bin width becomes zero and normalization produces NaNs.

Two related boundary failures exist in the same path:

  • num_bins <= 4 cannot accommodate the implementation's two-bin padding on each side, leaving a zero or negative interior denominator.
  • In float16, the cubic B-spline distance polynomial can overflow before masking.

To Reproduce

import torch
from monai.losses import GlobalMutualInformationLoss

pred = torch.zeros((1, 1, 8, 8), requires_grad=True)
target = torch.ones_like(pred)
loss = GlobalMutualInformationLoss(kernel_type="b-spline")
result = loss(pred, target)
print(result, torch.isfinite(result))

On the current dev revision, result is non-finite.

Expected behavior

Valid constant tensors should produce a finite loss and finite gradients. B-spline configurations with too few bins should fail at construction with a clear ValueError.

Screenshots

Not applicable.

Environment

MONAI version: 1.6.0rc1+46.g3ee058bd
MONAI revision: 3ee058bdd16dd4a566d23d3f84687c3c35268a36
Python version: 3.13.13
PyTorch version: 2.12.0
NumPy version: 2.4.6
System: macOS 27.0 arm64
CUDA available: False

Additional context

A focused fix and regression coverage are proposed in #9022.

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