code: def compute_sim_matrix(self, data_loader, task_cfg):
      k_test = task_cfg.get("k_test")
  
      metric_logger = MetricLogger(delimiter="  ")
      header = "Evaluation:"
  
      logging.info("Computing features for evaluation...")
      start_time = time.time()
  
      texts = data_loader.dataset.text
      num_text = len(texts)
      text_bs = 256
      text_ids = []
      text_embeds = []
      text_feats = []
      text_atts = []
      for i in range(0, num_text, text_bs):
          text = texts[i : min(num_text, i + text_bs)]
          text_input = self.tokenizer(
              text,
              padding="max_length",
              truncation=True,
              max_length=self.max_txt_len,
              return_tensors="pt",
          ).to(self.device)
          text_output = self.text_encoder.forward_text(
              text_input,
              token_type_ids=torch.zeros(
                  text_input.input_ids.shape,
                  dtype=torch.long,
                  device=self.device,
              ),
          )
          text_feats.append(text_output.last_hidden_state.cpu())
          text_embed = F.normalize(
              self.text_proj(text_output.last_hidden_state[:, 0, :])
          )
          text_embeds.append(text_embed)
          text_ids.append(text_input.input_ids)
          text_atts.append(text_input.attention_mask)
  
      text_embeds = torch.cat(text_embeds, dim=0)
      text_ids = torch.cat(text_ids, dim=0)
      text_atts = torch.cat(text_atts, dim=0)
      text_feats = torch.cat(text_feats, dim=0)
  
      video_feats = []
      video_embeds = []
      for samples in data_loader:
          video = samples["video"]
  
          video = video.to(self.device)
          video_feat = self.visual_encoder.forward_features(video)
          video_embed = self.vision_proj(video_feat[:, 0, :])
          video_embed = F.normalize(video_embed, dim=-1)
  
          video_feats.append(video_feat.cpu())
          video_embeds.append(video_embed)
  
      video_feats = torch.cat(video_feats, dim=0)
      video_embeds = torch.cat(video_embeds, dim=0)
  
      sims_matrix = video_embeds @ text_embeds.t()
      score_matrix_v2t = torch.full(
          (len(data_loader.dataset.image), len(texts)), -100.0
      ).to(self.device)
  
      num_tasks = dist_utils.get_world_size()
      rank = dist_utils.get_rank()
      step = sims_matrix.size(0) // num_tasks + 1
      start = rank * step
      end = min(sims_matrix.size(0), start + step)
  
      # video-to-text
      for i, sims in enumerate(
          metric_logger.log_every(sims_matrix[start:end], 50, header)
      ):
          topk_sim, topk_idx = sims.topk(k=k_test, dim=0)
  
          video_feats_repeat = (
              video_feats[start + i]
              .repeat(k_test, 1, 1)
              .to(self.device)
          )
          video_atts_repeat = torch.ones(
              video_feats_repeat.size()[:-1], dtype=torch.long
          ).to(self.device)
  
          attention_mask = torch.cat(
              [text_atts[topk_idx], video_atts_repeat], dim=1
          )
          embedding_output = torch.cat(
              [
                  text_feats[topk_idx].to(self.device),
                  video_feats_repeat,
              ],
              dim=1,
          )
  
          output = self.text_encoder(
              encoder_embeds=embedding_output,
              attention_mask=attention_mask,
              return_dict=True,
              mode="fusion",
          )
  
          score = self.itm_head(output.last_hidden_state[:, 0, :])[:, 1]
          score_matrix_v2t[start + i, topk_idx] = score + topk_sim
  
      # text-to-video
      sims_matrix = sims_matrix.t()
      score_matrix_t2v = torch.full(
          (len(texts), len(data_loader.dataset.image)), -100.0
      ).to(self.device)
  
      step = sims_matrix.size(0) // num_tasks + 1
      start = rank * step
      end = min(sims_matrix.size(0), start + step)
  
      for i, sims in enumerate(
          metric_logger.log_every(sims_matrix[start:end], 50, header)
      ):
          topk_sim, topk_idx = sims.topk(k=k_test, dim=0)
  
          text_feats_repeat = (
              text_feats[start + i].repeat(k_test, 1, 1).to(self.device)
          )
          text_atts_repeat = (
              text_atts[start + i].repeat(k_test, 1).to(self.device)
          )
  
          video_atts = torch.ones(
              video_feats[topk_idx].size()[:-1], dtype=torch.long
          ).to(self.device)
  
          embedding_output = torch.cat(
              [
                  text_feats_repeat,
                  video_feats[topk_idx].to(self.device),
              ],
              dim=1,
          )
          attention_mask = torch.cat(
              [text_atts_repeat, video_atts], dim=1
          )
  
          output = self.text_encoder(
              encoder_embeds=embedding_output,
              attention_mask=attention_mask,
              return_dict=True,
              mode="fusion",
          )
  
          score = self.itm_head(output.last_hidden_state[:, 0, :])[:, 1]
          score_matrix_t2v[start + i, topk_idx] = score + topk_sim
  
      if dist_utils.is_dist_avail_and_initialized():
          dist.barrier()
          torch.distributed.all_reduce(
              score_matrix_v2t, op=torch.distributed.ReduceOp.SUM
          )
          torch.distributed.all_reduce(
              score_matrix_t2v, op=torch.distributed.ReduceOp.SUM
          )
  
      total_time = time.time() - start_time
      total_time_str = str(datetime.timedelta(seconds=int(total_time)))
      logging.info("Evaluation time {}".format(total_time_str))
  
      return (
          score_matrix_v2t.cpu().numpy(),
          score_matrix_t2v.cpu().numpy(),
      )
  
imports: import datetime
  import logging
  import time
  import lavis.common.dist_utils
  import numpy
  import torch
  import torch.distributed
  import torch.nn.functional
  from lavis.common.config import node_to_dict
  from lavis.common.dist_utils import get_rank
  from lavis.common.logger import MetricLogger
  from lavis.common.registry import registry
  from lavis.models.alpro_models import AlproBase
  from lavis.models.alpro_models.alpro_outputs import AlproIntermediateOutput, AlproOutput
  from lavis.models.base_model import all_gather_with_grad
  from lavis.models.med import XBertEncoder
  from lavis.models.timesformer.vit import TimeSformer
  from torch import nn
label_desc: Use image node_id 4 for calling the AlproRetrieval obj's underlying member method code with example usage: obj.compute_sim_matrix(data_loader, task_cfg) and returns: score_matrix_v2t,  score_matrix_t2v