def generate_fake_anomalies_joined(
        features: torch.Tensor,
        embeddings: torch.Tensor,
        memory_torch_original: torch.Tensor,
        mask: torch.Tensor,
        strength: torch.Tensor,
    ) -> torch.Tensor:
        """Generate quantized anomalies.

        Args:
            features (torch.Tensor): Features on which the anomalies will be generated.
            embeddings (torch.Tensor): Embeddings to use to generate the anomalies.
            memory_torch_original (torch.Tensor): Weight of embeddings.
            mask (torch.Tensor): Original anomaly mask.
            strength (float): Strength of generated anomaly.

        Returns:
            torch.Tensor: Anomalous embedding.
        """
        random_embeddings = torch.zeros(
            (embeddings.shape[0], embeddings.shape[2] * embeddings.shape[3], memory_torch_original.shape[1]),
        )
        inputs = features.permute(0, 2, 3, 1).contiguous()

        for k in range(embeddings.shape[0]):
            memory_torch = memory_torch_original
            flat_input = inputs[k].view(-1, memory_torch.shape[1])

            distances_b = (
                torch.sum(flat_input**2, dim=1, keepdim=True)
                + torch.sum(memory_torch**2, dim=1)
                - 2 * torch.matmul(flat_input, memory_torch.t())
            )

            percentage_vectors = strength[k]
            topk = max(1, min(int(percentage_vectors * memory_torch.shape[0]) + 1, memory_torch.shape[0] - 1))
            _, topk_indices = torch.topk(distances_b, topk, dim=1, largest=False)
            topk_indices = topk_indices[:, int(memory_torch.shape[0] * 0.05) :]
            topk = topk_indices.shape[1]

            random_indices_hik = torch.randint(topk, size=(topk_indices.shape[0],))
            random_indices_t = topk_indices[torch.arange(random_indices_hik.shape[0]), random_indices_hik]
            random_embeddings[k] = memory_torch[random_indices_t, :]
        random_embeddings = random_embeddings.reshape(
            (random_embeddings.shape[0], embeddings.shape[2], embeddings.shape[3], random_embeddings.shape[2]),
        )
        random_embeddings_tensor = random_embeddings.permute(0, 3, 1, 2).cuda()

        down_ratio_y = int(mask.shape[2] / embeddings.shape[2])
        down_ratio_x = int(mask.shape[3] / embeddings.shape[3])
        anomaly_mask = torch.nn.functional.max_pool2d(mask, (down_ratio_y, down_ratio_x)).float()

        return anomaly_mask * random_embeddings_tensor + (1.0 - anomaly_mask) * embeddings
Generate quantized anomalies.

        Args:
            features (torch.Tensor): Features on which the anomalies will be generated.
            embeddings (torch.Tensor): Embeddings to use to generate the anomalies.
            memory_torch_original (torch.Tensor): Weight of embeddings.
            mask (torch.Tensor): Original anomaly mask.
            strength (float): Strength of generated anomaly.

        Returns:
            torch.Tensor: Anomalous embedding.
        
src/anomalib/models/image/dsr/torch_model.py