import torch


def log1p_safe(x):
    """The same as torch.log1p(x), but clamps the input to prevent NaNs."""
    x = torch.as_tensor(x)
    return torch.log1p(torch.min(x, torch.tensor(33e37).to(x)))