def make_masks(grid_size: tuple[int, int], kernel_size: int, stride: int = 1) -> torch.Tensor:
    """Make masks to select patches from a feature map using sliding windows.

    Creates a set of masks for selecting patches from a feature map in a sliding window
    fashion. Each column in the returned tensor represents one mask. A mask consists of
    indices indicating which patches are covered by that sliding window position.

    The number of masks equals the number of possible sliding window positions that fit
    in the feature map given the kernel size and stride.

    Args:
        grid_size (tuple[int, int]): Height and width of the feature map grid as
            ``(H, W)``.
        kernel_size (int): Size of the sliding window kernel in number of patches.
        stride (int, optional): Stride of the sliding window in number of patches.
            Defaults to ``1``.

    Returns:
        torch.Tensor: Set of masks with shape ``(n_patches_per_mask, n_masks)``. Each
            column represents indices of patches covered by one sliding window position.

    Raises:
        ValueError: If any dimension of ``grid_size`` is smaller than ``kernel_size``.

    Examples:
        Create masks for a 3x3 grid with kernel size 2 and stride 1:

        >>> make_masks((3, 3), 2)
        tensor([[0, 1, 3, 4],
                [1, 2, 4, 5],
                [3, 4, 6, 7],
                [4, 5, 7, 8]], dtype=torch.int32)

        Create masks for a 4x4 grid with kernel size 2 and stride 1:

        >>> make_masks((4, 4), 2)
        tensor([[ 0,  1,  2,  4,  5,  6,  8,  9, 10],
                [ 1,  2,  3,  5,  6,  7,  9, 10, 11],
                [ 4,  5,  6,  8,  9, 10, 12, 13, 14],
                [ 5,  6,  7,  9, 10, 11, 13, 14, 15]], dtype=torch.int32)

        Create masks for a 4x4 grid with kernel size 2 and stride 2:

        >>> make_masks((4, 4), 2, stride=2)
        tensor([[ 0,  2,  8, 10],
                [ 1,  3,  9, 11],
                [ 4,  6, 12, 14],
                [ 5,  7, 13, 15]], dtype=torch.int32)

    Note:
        The returned masks can be used with :func:`visual_association_score` to compute
        scores for sliding window positions.
    """
    if any(dim < kernel_size for dim in grid_size):
        msg = (
            "Each dimension of the grid size must be greater than or equal to "
            f"the kernel size. Got grid size {grid_size} and kernel size {kernel_size}."
        )
        raise ValueError(msg)
    height, width = grid_size
    grid = torch.arange(height * width).reshape(1, height, width)
    return nn.functional.unfold(grid.float(), kernel_size=kernel_size, stride=stride).int()
Make masks to select patches from a feature map using sliding windows.

    Creates a set of masks for selecting patches from a feature map in a sliding window
    fashion. Each column in the returned tensor represents one mask. A mask consists of
    indices indicating which patches are covered by that sliding window position.

    The number of masks equals the number of possible sliding window positions that fit
    in the feature map given the kernel size and stride.

    Args:
        grid_size (tuple[int, int]): Height and width of the feature map grid as
            ``(H, W)``.
        kernel_size (int): Size of the sliding window kernel in number of patches.
        stride (int, optional): Stride of the sliding window in number of patches.
            Defaults to ``1``.

    Returns:
        torch.Tensor: Set of masks with shape ``(n_patches_per_mask, n_masks)``. Each
            column represents indices of patches covered by one sliding window position.

    Raises:
        ValueError: If any dimension of ``grid_size`` is smaller than ``kernel_size``.

    Examples:
        Create masks for a 3x3 grid with kernel size 2 and stride 1:

        >>> make_masks((3, 3), 2)
        tensor([[0, 1, 3, 4],
                [1, 2, 4, 5],
                [3, 4, 6, 7],
                [4, 5, 7, 8]], dtype=torch.int32)

        Create masks for a 4x4 grid with kernel size 2 and stride 1:

        >>> make_masks((4, 4), 2)
        tensor([[ 0,  1,  2,  4,  5,  6,  8,  9, 10],
                [ 1,  2,  3,  5,  6,  7,  9, 10, 11],
                [ 4,  5,  6,  8,  9, 10, 12, 13, 14],
                [ 5,  6,  7,  9, 10, 11, 13, 14, 15]], dtype=torch.int32)

        Create masks for a 4x4 grid with kernel size 2 and stride 2:

        >>> make_masks((4, 4), 2, stride=2)
        tensor([[ 0,  2,  8, 10],
                [ 1,  3,  9, 11],
                [ 4,  6, 12, 14],
                [ 5,  7, 13, 15]], dtype=torch.int32)

    Note:
        The returned masks can be used with :func:`visual_association_score` to compute
        scores for sliding window positions.
    
src/anomalib/models/image/winclip/utils.py