import torch


def smooth_L1(ti, ti_star):
    """
    smooth L1 function:
        0.5 * (x^2) if abs(x) < 1
        abs(x) - 0.5 otherwise

    Params:
        ti: shape([N])
        ti_star: shape([N])
    
    Return: score: shape([N])
    """
    abs_sub = torch.abs(ti - ti_star)
    
    smaller_than_1 = torch.where(abs_sub < 1)
    greater_than_1 = torch.where(abs_sub >= 1)
    
    abs_sub[smaller_than_1] = torch.pow(abs_sub[smaller_than_1], 2) / 2
    abs_sub[greater_than_1] = abs_sub[greater_than_1] - 0.5
        
    return abs_sub