import torch


def softplus_inv(x, eps=1e-6, threshold=20.):
    """Compute the softplus inverse."""
    y = torch.zeros_like(x)

    idx = x < threshold
    # We deliberately ignore eps to avoid -inf
    y[idx] = torch.log(torch.exp(x[idx]) - 1)
    y[~idx] = x[~idx]

    return y