class_docstr: Compute contrastive loss between image and text pairs.
  
  Args:
      opts: command-line arguments
class_docstr_tok: ["Compute","contrastive","loss","between","image","and","text","pairs",".","Args",":","opts",":","command-line","arguments"]
docstr: Computes the contrast loss between image and text representations
  
  Args:
      prediction: A mapping of the form (string: Tensor). image and text are mandatory keys
  
  Shape:
      prediction["image"]: Shape is [N, d]
      prediction["text"]: Shape is [N, d]
      where N and d are batch size and feature dimensions, respectively.
  
  Returns:
      The output dictionary contains four keys (total_loss, image_loss, text_loss, logit_scale) and scalar
      loss value for each of these keys. total_loss is sum of image_loss and text_loss.
docstr_tok: ["Computes","the","contrast","loss","between","image","and","text","representations","Args",":","prediction",":","A","mapping","of","the","form","(","string",":","Tensor",")",".","image","and","text","are","mandatory","keys","Shape",":","prediction","[","``","image","''","]",":","Shape","is","[","N",",","d","]","prediction","[","``","text","''","]",":","Shape","is","[","N",",","d","]","where","N","and","d","are","batch","size","and","feature","dimensions",",","respectively",".","Returns",":","The","output","dictionary","contains","four","keys","(","total_loss",",","image_loss",",","text_loss",",","logit_scale",")","and","scalar","loss","value","for","each","of","these","keys",".","total_loss","is","sum","of","image_loss","and","text_loss","."]
code: def _forward_clip(
      self, prediction: Dict[str, Tensor], *args, **kwargs
  ) -> Dict[str, Tensor]:
      """
      Computes the contrast loss between image and text representations
  
      Args:
          prediction: A mapping of the form (string: Tensor). image and text are mandatory keys
  
      Shape:
          prediction["image"]: Shape is [N, d]
          prediction["text"]: Shape is [N, d]
          where N and d are batch size and feature dimensions, respectively.
  
      Returns:
          The output dictionary contains four keys (total_loss, image_loss, text_loss, logit_scale) and scalar
          loss value for each of these keys. total_loss is sum of image_loss and text_loss.
      """
  
      if not {"image", "text"}.issubset(prediction.keys()):
          logger.error(
              f"image and text are mandatory keys for {self.__class__.__name__}."
          )
  
      image_features = prediction.pop("image")
      text_features = prediction.pop("text")
      logit_scale = prediction.pop("logit_scale", 1.0)
  
      if image_features is None:
          logger.error(
              f"Image features can't be None in {self.__class__.__name__}"
          )
      if text_features is None:
          logger.error(
              f"Text features can't be None in {self.__class__.__name__}"
          )
  
      # Aggregate image and text features from all GPUs
      gathered_image_features, gathered_text_features = gather_features(
          image_features=image_features,
          text_features=text_features,
          use_distributed=self.use_distributed,
      )
  
      # compute logits
      # [N, d] x [G x d]^T --> [N, G], where G is global batch size
      logits_per_image = logit_scale * (
          image_features @ gathered_text_features.transpose(0, 1)
      )
      # [N, d] x [G, d]^T --> [N, G]
      logits_per_text = logit_scale * (
          text_features @ gathered_image_features.transpose(0, 1)
      )
  
      # generate labels
      num_logits = logits_per_image.shape[0]
      contrastive_labels = torch.arange(
          num_logits, device=logits_per_image.device, dtype=torch.long
      )
  
      # shift the labels by rank id
      contrastive_labels = contrastive_labels + (num_logits * self.rank)
  
      # compute cross entropy loss
      text_loss = (
          F.cross_entropy(logits_per_text, contrastive_labels) * 0.5
      )
      image_loss = (
          F.cross_entropy(logits_per_image, contrastive_labels) * 0.5
      )
      total_loss = image_loss + text_loss
      return {
          "total_loss": total_loss,
          "image_loss": image_loss,
          "text_loss": text_loss,
          "logit_scale": logit_scale,
      }
  
file_path: ml-cvnets/loss_fn/multi_modal_img_text/contrastive_loss_clip.py
imports: import argparse
  from typing import Any, Dict, Tuple
  import torch
  from torch import Tensor
  from torch.nn import functional
  from loss_fn import LOSS_REGISTRY
  from loss_fn.multi_modal_img_text.base_multi_modal_img_text_criteria import BaseMultiModalImageTextCriteria
  from utils import logger
  from utils.tensor_utils import gather_all_features
label_desc: Use image node_id 2 for calling the ContrastiveLossClip obj's underlying member method code with example usage: obj._forward_clip(prediction) and returns: dict