def reduce_loss(loss, reduction):
    """Reduce loss as specified.

    Args:
        loss (Tensor): Elementwise loss tensor.
        reduction (str): Options are "none", "mean", "valid_mean" and "sum".

    Return:
        Tensor: Reduced loss tensor.
    """

    if reduction == 'mean':
        return loss.mean()
    elif reduction == 'valid_mean':
        valid_mask = loss > 0.0
        num_valid = valid_mask.sum().float().clamp_min(1.0)
        return loss.sum() / num_valid
    elif reduction == 'sum':
        return loss.sum()
    else:
        return loss