import torch


def pseudo_huber_loss(truth, pred, scale=2.0):
    """Pseudo-Huber loss used in StereoNet.

    Described in 2019 Barron - A General and Adaptive Robust Loss Function.
    """
    diff2 = ((pred - truth) / scale)**2
    loss = torch.mean(torch.sqrt(diff2 + 1.0) - 1.0)
    return loss