import torch


def subset_roll_to_front(tensor: torch.Tensor, subset_num_dims):
    """Transpose back indices to the front of `tensor`.

    Examples:
        >>> x = torch.rand(4, 5, 6, 7, 8, 9)
        >>> subset_roll_to_front(x, 2).size()
        torch.Size([8, 9, 4, 5, 6, 7])
    """
    subset_range = range(tensor.dim() - subset_num_dims, tensor.dim())
    complement_range = range(tensor.dim() - subset_num_dims)
    perm = list(subset_range) + list(complement_range)
    return torch.permute(tensor, perm)