code: def forward(
      self,
      sequence_output: torch.Tensor,
      encoded_layers: Optional[List[torch.Tensor]] = None,
      processed_sample_list: Optional[
          Dict[str, Dict[str, torch.Tensor]]
      ] = None,
  ):
      start_token = {}
      end_token = {}
      prev_end_token = 0
      masks = []
      for modality in self.modalities:
          masks.append(processed_sample_list["masks"][modality])
          sz = processed_sample_list["masks"][modality].size()
          start_token[modality] = prev_end_token
          end_token[modality] = start_token[modality] + sz[1] - 1
          prev_end_token = end_token[modality] + 1
  
      pad_mask = torch.cat(masks, dim=1)
      processed_sample_list["refiner_outputs"] = {}
      processed_sample_list["refiner_outputs"][
          "fused_embedding"
      ] = self.pooler(encoded_layers, pad_mask)
      processed_sample_list["refiner_targets"] = {}
      for modality in self.modalities:
          modality_encodings = []
          tk_start = start_token[modality]
          tk_end = end_token[modality]
          for enc_layers in encoded_layers:
              modality_encodings.append(
                  enc_layers[:, tk_start : tk_end + 1, :]
              )
          modality_mask_encodings = pad_mask[:, tk_start : tk_end + 1]
          processed_sample_list["refiner_targets"][
              modality
          ] = self.pooler(modality_encodings, modality_mask_encodings)
  
      output_dict = {}
      prediction = self.cls(sequence_output)
      output_dict["logits"] = prediction
      output_dict["losses"] = {}
  
      fused_embedding = processed_sample_list["refiner_outputs"][
          "fused_embedding"
      ]
      refiner_reconstruct = {}
      for modality in processed_sample_list["refiner_targets"].keys():
          local_device = fused_embedding.device
          self.refiner_decoder[modality].to(local_device)
          refiner_reconstruct[modality] = self.refiner_decoder[
              modality
          ](fused_embedding)
          if isinstance(self.refiner_loss, torch.nn.CosineSimilarity):
              loss = self.weights[modality] * (
                  1.0
                  - torch.mean(
                      self.refiner_loss(
                          processed_sample_list["refiner_targets"][
                              modality
                          ],
                          refiner_reconstruct[modality],
                      )
                  )
              )
          elif isinstance(
              self.refiner_loss, RefinerContrastiveLoss
          ) or isinstance(self.refiner_loss, RefinerMSLoss):
              modality_targets = {}
              modality_targets["targets"] = processed_sample_list[
                  "refiner_targets"
              ][modality]
              refiner_modal_outputs = {}
              refiner_modal_outputs["scores"] = refiner_reconstruct[
                  modality
              ]
              loss = self.refiner_loss(
                  modality_targets, refiner_modal_outputs
              )
  
          else:
              loss = self.weights[modality] * self.refiner_loss(
                  processed_sample_list["refiner_targets"][modality],
                  refiner_reconstruct[modality],
              )
  
          if "current_loss" not in locals():
              current_loss = loss
          else:
              current_loss += loss
  
      output_dict["losses"][self.loss_name] = current_loss
      output_dict["fused_embedding"] = fused_embedding
      return output_dict
  
file_path: mmf/mmf/models/transformers/heads/refiner.py
imports: from dataclasses import dataclass, field
  from typing import Dict, List, Optional, Type
  import torch
  from mmf.common.registry import registry
  from mmf.models.transformers.base import BaseTransformerHead
  from mmf.modules.losses import RefinerContrastiveLoss, RefinerMSLoss
  from torch import nn
label_desc: Use image node_id 2 for calling the Refiner obj's underlying member method code with example usage: obj.forward(sequence_output, encoded_layers, processed_sample_list) and returns: output_dict