def reshape_embedding(embedding: torch.Tensor) -> torch.Tensor:
        """Reshape embedding tensor for patch-wise processing.

        Converts a 4D embedding tensor into a 2D matrix where each row represents
        a patch embedding vector.

        Args:
            embedding (torch.Tensor): Input embedding tensor of shape
                ``(batch_size, embedding_dim, height, width)``.

        Returns:
            torch.Tensor: Reshaped embedding tensor of shape
                ``(batch_size * height * width, embedding_dim)``.

        Example:
            >>> embedding = torch.randn(32, 512, 7, 7)
            >>> reshaped = PatchcoreModel.reshape_embedding(embedding)
            >>> reshaped.shape
            torch.Size([1568, 512])
        """
        embedding_size = embedding.size(1)
        return embedding.permute(0, 2, 3, 1).reshape(-1, embedding_size)
Reshape embedding tensor for patch-wise processing.

        Converts a 4D embedding tensor into a 2D matrix where each row represents
        a patch embedding vector.

        Args:
            embedding (torch.Tensor): Input embedding tensor of shape
                ``(batch_size, embedding_dim, height, width)``.

        Returns:
            torch.Tensor: Reshaped embedding tensor of shape
                ``(batch_size * height * width, embedding_dim)``.

        Example:
            >>> embedding = torch.randn(32, 512, 7, 7)
            >>> reshaped = PatchcoreModel.reshape_embedding(embedding)
            >>> reshaped.shape
            torch.Size([1568, 512])
        
src/anomalib/models/image/patchcore/torch_model.py