import torch


def soft_relu(x):
    """Compute log(1 + exp(x)) with numerical stability.
    
    Can be used for getting differentiable nonnegative outputs.
    Might also be useful in other cases, e.g.:  
        log(sigmoid(x)) = x - soft_relu(x) = - soft_relu(-x).
        log(1 - sigmoid(x)) = - soft_relu(x)
    """
    return ((-x.abs()).exp() + 1.0).log() + torch.clip(x, min=0.0)