code: def cmc_calculation(
      distance_matrix: torch.Tensor,
      query_ids: torch.Tensor,
      k: int = 5,
  ) -> Tuple[float, float]:
      """Compute Cumulative Matching Characteristics metric.
  
      Args:
          distance_matrix: pairwise distance matrix between embeddings of gallery and query sets
          query_ids: labels for the query data (assuming the same as gallery)
          k: parameter for top k retrieval
  
      Returns: cmc-top1, cmc-top5
  
      """
      distance_matrix = copy.deepcopy(distance_matrix)
      query_ids = copy.deepcopy(query_ids)
  
      distance_matrix.fill_diagonal_(float("inf"))
      _, indices = torch.sort(distance_matrix)
      labels = query_ids.unsqueeze(dim=0).repeat(query_ids.shape[0], 1)
      sorted_labels = torch.gather(labels, 1, indices)
      top_1 = (
          sorted_labels[:, 0] == query_ids
      ).sum() / query_ids.shape[0]
      top_k = (sorted_labels[:, :k] == query_ids.unsqueeze(1)).sum(
          dim=1
      ).clamp(max=1).sum() / query_ids.shape[0]
  
      return top_1, top_k
  
imports: import argparse
  import copy
  from typing import Dict, Tuple
  import numpy
  import torch
  from sklearn.metrics import average_precision_score
  from torch.nn import functional
  from metrics import METRICS_REGISTRY
  from metrics.metric_base import EpochMetric
  from utils import logger
  from utils.ddp_utils import is_master
  from utils.registry import Registry
label_desc: Use image node_id 3 for calling a global function with example usage: cmc_calculation(distance_matrix, query_ids, k) and returns: top_1,  top_k