def __unfold(self, tensor: torch.Tensor) -> torch.Tensor:
        """Unfold tensor into tiles.

        Args:
            tensor: Input tensor to tile

        Returns:
            torch.Tensor: Generated tiles
        """
        device = tensor.device
        batch, channels, image_h, image_w = tensor.shape

        self.num_patches_h = int((image_h - self.tile_size_h) / self.stride_h) + 1
        self.num_patches_w = int((image_w - self.tile_size_w) / self.stride_w) + 1

        tiles = torch.zeros(
            (
                self.num_patches_h,
                self.num_patches_w,
                batch,
                channels,
                self.tile_size_h,
                self.tile_size_w,
            ),
            device=device,
        )

        for (tile_i, tile_j), (loc_i, loc_j) in zip(
            product(range(self.num_patches_h), range(self.num_patches_w)),
            product(
                range(0, image_h - self.tile_size_h + 1, self.stride_h),
                range(0, image_w - self.tile_size_w + 1, self.stride_w),
            ),
            strict=True,
        ):
            tiles[tile_i, tile_j, :] = tensor[
                :,
                :,
                loc_i : (loc_i + self.tile_size_h),
                loc_j : (loc_j + self.tile_size_w),
            ]

        tiles = tiles.permute(2, 0, 1, 3, 4, 5)
        return tiles.contiguous().view(-1, channels, self.tile_size_h, self.tile_size_w)
Unfold tensor into tiles.

        Args:
            tensor: Input tensor to tile

        Returns:
            torch.Tensor: Generated tiles
        
src/anomalib/data/utils/tiler.py