def METHOD_NAME(
    log_prob: Tensor,
    smoothing: Union[Tensor, float],
    *,
    axis: Optional[Dim] = None,
    exclude_labels: Optional[Sequence[int]] = None,
) -> Tensor:
    """
    :param log_prob: shape [...,D] (not necessarily the same as loss)
    :param smoothing: smoothing factor, for :func:`label_smoothing`
    :param axis: label axis. uses feature_dim by default
    :param exclude_labels: list of labels to exclude from smoothing (e.g. blank)
    Assume some cross-entropy-like loss:
      loss = - sum_i target_prob[i] * log_prob[i] .
    The sum is over the label indices i (corresponding to the ``axis`` argument).
    Then the gradient of loss w.r.t. log_prob[i] is:
      grad_logprob[i] loss = -target_prob[i] .
    We assume that the negative gradient is a probability distribution, and apply :func:`label_smoothing` on it.
    More specifically, we apply the same scale and shift as in the :func:`label_smoothing` function
    via :func:`scaled_gradient`.
    Just as a side remark: assume
      log_prob = log_softmax(z) .
    The gradient of log_softmax is:
      grad_z[j] log_prob[i] = delta(i==j) - softmax(z)[j] .
    Then the gradient w.r.t. z[j] is:
      grad_z[j] loss = sum_i (grad_logprob[i] loss) (grad_z[j] logprob[i])
                     = sum_i -target_prob[i] delta(i==j) + target_prob[i] softmax(z)[j]
                     = -target_prob[j] + (sum_i target_prob[i]) softmax(z)[j]
                     = softmax(z)[j] - target_prob[j]    # assuming (sum_i target_prob[i]) == 1
    """
    if not axis:
        assert log_prob.feature_dim
        axis = log_prob.feature_dim
    # See formula above for label_smoothing.
    dim = axis.dimension
    floor_prob = smoothing / (dim - 1)
    factor = 1.0 - dim * floor_prob
    if exclude_labels:
        indices = rf.range_over_dim(axis)
        mask = True
        for label in exclude_labels:
            mask = mask & (indices != label)
        factor = rf.where(mask, factor, 1.0)
        floor_prob = rf.where(mask, floor_prob, 0.0)
    # The gradient is expected to be the negative target prob, thus negative floor_prob.
    # The gradient is expected to be 0. for masked frames, thus the clipping logic.
    return rf.scaled_gradient_ext(log_prob, scale=factor, shift=-floor_prob, scale_shift_by_sum_over_axis=axis)