import numbers


def sum_rightmost(value, dim):
    """
    Sum out ``dim`` many rightmost dimensions of a given tensor.

    :param torch.autograd.Variable value: A tensor of ``.dim()`` at least ``dim``.
    :param int dim: The number of rightmost dims to sum out.
    """
    if dim == 0 or isinstance(value, numbers.Number):
        return value
    return value.contiguous().view(value.shape[:-dim] + (-1,)).sum(-1)