def smooth_l1(x1, x2, sigma):
    """Smooth L1 loss"""
    sigma2 = sigma ** 2

    diff = x1 - x2
    abs_diff = diff.abs()

    mask = (abs_diff.detach() < (1. / sigma2)).float()
    return mask * (sigma2 / 2.) * diff ** 2 + (1 - mask) * (abs_diff - 0.5 / sigma2)