class_docstr: HeadsDict class manages the construction and forward pass for
  multiple possible heads for multi-task learning.
  Construction from list or dict configs is supported,
  take a look at `build_heads_dict(head_configs, tasks, losses)`.
docstr: For a given task, compute the forward for each head
  associated with the task, compute the losses for
  each head, and sum the losses and scores
code: def forward(
      self,
      task: Optional[str],
      sequence: Tensor,
      sample_list: Dict[str, Tensor],
  ) -> Dict[str, Tensor]:
      """
      For a given task, compute the forward for each head
      associated with the task, compute the losses for
      each head, and sum the losses and scores
      """
      if isinstance(self.heads, nn.ModuleList):
          heads_modules_list = self.heads
          # list of losses, head_losses[i] is the loss name for outputs_list[i]
          head_losses = self.head_loss_names
          head_names = self.head_names
      else:
          heads_modules_list = self.heads[task]
          head_losses = self.head_loss_names[task]
          head_names = self.head_names[task]
  
      # list of dict( head outputs )
      outputs_list = [
          head(sequence, processed_sample_list=sample_list)
          for head in heads_modules_list
      ]
  
      assert len(head_losses) == len(outputs_list)
  
      # list of dict( losses, scores )
      processed_outputs_list = [
          self._process_head_output(
              outputs, loss_name, head_name, sample_list
          )
          for outputs, loss_name, head_name in zip(
              outputs_list, head_losses, head_names
          )
      ]
  
      def reduce_losses(accum_result, loss_dict):
          for loss_key, loss_val in loss_dict.items():
              if loss_key in accum_result:
                  accum_result[loss_key] += loss_val
              else:
                  accum_result[loss_key] = loss_val
  
      loss_result = {}
      for output in processed_outputs_list:
          reduce_losses(loss_result, output["losses"])
  
      results = {
          "losses": loss_result,
          "scores": sum(
              [
                  output.get("scores", 0)
                  for output in processed_outputs_list
              ]
          ),
      }
      return results
  
file_path: mmf/mmf/models/transformers/heads/utils.py
imports: import collections
  import collections.abc
  from typing import Dict, List, Optional, Union
  from mmf.common.registry import registry
  from torch import nn, Tensor
label_desc: Use image node_id 2 for calling the HeadsDict obj's underlying member method code with example usage: obj.forward(task, sequence, sample_list) and returns: results