def compute_distance(embedding: torch.Tensor, stats: list[torch.Tensor]) -> torch.Tensor:
        """Compute anomaly score for each patch position using Mahalanobis distance.

        Implements Equation (2) from Section III-C of the PaDiM paper to compute
        the distance between patch embeddings and their reference distributions.

        Args:
            embedding (torch.Tensor): Feature embeddings from the CNN backbone,
                shape ``(batch_size, n_features, height, width)``
            stats (list[torch.Tensor]): List containing mean and inverse covariance
                tensors for the multivariate Gaussian distributions

        Returns:
            torch.Tensor: Anomaly scores computed via Mahalanobis distance,
                shape ``(batch_size, 1, height, width)``
        """
        batch, channel, height, width = embedding.shape
        embedding = embedding.reshape(batch, channel, height * width)

        # calculate mahalanobis distances
        mean, inv_covariance = stats
        delta = (embedding - mean).permute(2, 0, 1)

        distances = (torch.matmul(delta, inv_covariance) * delta).sum(2).permute(1, 0)
        distances = distances.reshape(batch, 1, height, width)
        return distances.clamp(0).sqrt()
Compute anomaly score for each patch position using Mahalanobis distance.

        Implements Equation (2) from Section III-C of the PaDiM paper to compute
        the distance between patch embeddings and their reference distributions.

        Args:
            embedding (torch.Tensor): Feature embeddings from the CNN backbone,
                shape ``(batch_size, n_features, height, width)``
            stats (list[torch.Tensor]): List containing mean and inverse covariance
                tensors for the multivariate Gaussian distributions

        Returns:
            torch.Tensor: Anomaly scores computed via Mahalanobis distance,
                shape ``(batch_size, 1, height, width)``
        
src/anomalib/models/image/padim/anomaly_map.py