import torch


def diff(x: torch.Tensor, dim: int) -> torch.Tensor:
    """Take the finite difference of a tensor along an axis.
    Arguments:
        x (torch.Tensor):
            Input tensor of any dimension.
        axis (int):
            Axis on which to take the finite difference.
    Returns:
        d (torch.Tensor):
            Tensor with size less than x by 1 along the difference dimension.
    Raises:
        ValueError: Axis out of range for tensor.
    """
    length = x.shape[dim]
    return x.narrow(dim, 1, length-1) - x.narrow(dim, 0, length-1)