class_docstr: Implements the Mask2Former head.
  
  See `Masked-attention Mask Transformer for Universal Image
  Segmentation <https://arxiv.org/pdf/2112.01527>`_ for details.
  
  Args:
      in_channels (list[int]): Number of channels in the input feature map.
      feat_channels (int): Number of channels for features.
      out_channels (int): Number of channels for output.
      num_things_classes (int): Number of things.
      num_stuff_classes (int): Number of stuff.
      num_queries (int): Number of query in Transformer decoder.
      pixel_decoder (:obj:`ConfigDict` or dict): Config for pixel
          decoder. Defaults to None.
      enforce_decoder_input_project (bool, optional): Whether to add
          a layer to change the embed_dim of tranformer encoder in
          pixel decoder to the embed_dim of transformer decoder.
          Defaults to False.
      transformer_decoder (:obj:`ConfigDict` or dict): Config for
          transformer decoder. Defaults to None.
      positional_encoding (:obj:`ConfigDict` or dict): Config for
          transformer decoder position encoding. Defaults to
          dict(num_feats=128, normalize=True).
      loss_cls (:obj:`ConfigDict` or dict): Config of the classification
          loss. Defaults to None.
      loss_mask (:obj:`ConfigDict` or dict): Config of the mask loss.
          Defaults to None.
      loss_dice (:obj:`ConfigDict` or dict): Config of the dice loss.
          Defaults to None.
      train_cfg (:obj:`ConfigDict` or dict, optional): Training config of
          Mask2Former head.
      test_cfg (:obj:`ConfigDict` or dict, optional): Testing config of
          Mask2Former head.
      init_cfg (:obj:`ConfigDict` or dict or list[:obj:`ConfigDict` or             dict], optional): Initialization config dict. Defaults to None.
docstr: Forward function.
  
  Args:
      x (list[Tensor]): Multi scale Features from the
          upstream network, each is a 4D-tensor.
      batch_data_samples (List[:obj:`DetDataSample`]): The Data
          Samples. It usually includes information such as
          `gt_instance`, `gt_panoptic_seg` and `gt_sem_seg`.
  
  Returns:
      tuple[list[Tensor]]: A tuple contains two elements.
  
          - cls_pred_list (list[Tensor)]: Classification logits                     for each decoder layer. Each is a 3D-tensor with shape                     (batch_size, num_queries, cls_out_channels).                     Note `cls_out_channels` should includes background.
          - mask_pred_list (list[Tensor]): Mask logits for each                     decoder layer. Each with shape (batch_size, num_queries,                     h, w).
docstr_tok: ["Forward","function",".","Args",":","x","(","list","[","Tensor","]",")",":","Multi","scale","Features","from","the","upstream","network",",","each","is","a","4D-tensor",".","batch_data_samples","(","List","[",":","obj",":","`","DetDataSample","`","]",")",":","The","Data","Samples",".","It","usually","includes","information","such","as","`","gt_instance","`",",","`","gt_panoptic_seg","`","and","`","gt_sem_seg","`",".","Returns",":","tuple","[","list","[","Tensor","]","]",":","A","tuple","contains","two","elements",".","-","cls_pred_list","(","list","[","Tensor",")","]",":","Classification","logits","for","each","decoder","layer",".","Each","is","a","3D-tensor","with","shape","(","batch_size",",","num_queries",",","cls_out_channels",")",".","Note","`","cls_out_channels","`","should","includes","background",".","-","mask_pred_list","(","list","[","Tensor","]",")",":","Mask","logits","for","each","decoder","layer",".","Each","with","shape","(","batch_size",",","num_queries",",","h",",","w",")","."]
code: def forward(
      self, x: List[Tensor], batch_data_samples: SampleList
  ) -> Tuple[List[Tensor]]:
      """Forward function.
  
          Args:
              x (list[Tensor]): Multi scale Features from the
                  upstream network, each is a 4D-tensor.
              batch_data_samples (List[:obj:`DetDataSample`]): The Data
                  Samples. It usually includes information such as
                  `gt_instance`, `gt_panoptic_seg` and `gt_sem_seg`.
  
          Returns:
              tuple[list[Tensor]]: A tuple contains two elements.
  
                  - cls_pred_list (list[Tensor)]: Classification logits \
                      for each decoder layer. Each is a 3D-tensor with shape \
                      (batch_size, num_queries, cls_out_channels). \
                      Note `cls_out_channels` should includes background.
                  - mask_pred_list (list[Tensor]): Mask logits for each \
                      decoder layer. Each with shape (batch_size, num_queries, \
                      h, w).
          """
      batch_size = x[0].shape[0]
      mask_features, multi_scale_memorys = self.pixel_decoder(x)
      # multi_scale_memorys (from low resolution to high resolution)
      decoder_inputs = []
      decoder_positional_encodings = []
      for i in range(self.num_transformer_feat_level):
          decoder_input = self.decoder_input_projs[i](
              multi_scale_memorys[i]
          )
          # shape (batch_size, c, h, w) -> (batch_size, h*w, c)
          decoder_input = decoder_input.flatten(2).permute(0, 2, 1)
          level_embed = self.level_embed.weight[i].view(1, 1, -1)
          decoder_input = decoder_input + level_embed
          # shape (batch_size, c, h, w) -> (batch_size, h*w, c)
          mask = decoder_input.new_zeros(
              (batch_size,) + multi_scale_memorys[i].shape[-2:],
              dtype=torch.bool,
          )
          decoder_positional_encoding = (
              self.decoder_positional_encoding(mask)
          )
          decoder_positional_encoding = (
              decoder_positional_encoding.flatten(2).permute(0, 2, 1)
          )
          decoder_inputs.append(decoder_input)
          decoder_positional_encodings.append(
              decoder_positional_encoding
          )
      # shape (num_queries, c) -> (batch_size, num_queries, c)
      query_feat = self.query_feat.weight.unsqueeze(0).repeat(
          (batch_size, 1, 1)
      )
      query_embed = self.query_embed.weight.unsqueeze(0).repeat(
          (batch_size, 1, 1)
      )
  
      cls_pred_list = []
      mask_pred_list = []
      cls_pred, mask_pred, attn_mask = self._forward_head(
          query_feat, mask_features, multi_scale_memorys[0].shape[-2:]
      )
      cls_pred_list.append(cls_pred)
      mask_pred_list.append(mask_pred)
  
      for i in range(self.num_transformer_decoder_layers):
          level_idx = i % self.num_transformer_feat_level
          # if a mask is all True(all background), then set it all False.
          mask_sum = (
              attn_mask.sum(-1) != attn_mask.shape[-1]
          ).unsqueeze(-1)
          attn_mask = attn_mask & mask_sum
          # cross_attn + self_attn
          layer = self.transformer_decoder.layers[i]
          query_feat = layer(
              query=query_feat,
              key=decoder_inputs[level_idx],
              value=decoder_inputs[level_idx],
              query_pos=query_embed,
              key_pos=decoder_positional_encodings[level_idx],
              cross_attn_mask=attn_mask,
              query_key_padding_mask=None,
              # here we do not apply masking on padded region
              key_padding_mask=None,
          )
          cls_pred, mask_pred, attn_mask = self._forward_head(
              query_feat,
              mask_features,
              multi_scale_memorys[
                  (i + 1) % self.num_transformer_feat_level
              ].shape[-2:],
          )
  
          cls_pred_list.append(cls_pred)
          mask_pred_list.append(mask_pred)
  
      return cls_pred_list, mask_pred_list
  
imports: import copy
  from typing import List, Tuple
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import Conv2d
  from mmcv.ops import point_sample
  from mmengine.model import ModuleList, caffe2_xavier_init
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.structures import SampleList
  from mmdet.utils import ConfigType, OptConfigType, OptMultiConfig, reduce_mean
  from ..layers import Mask2FormerTransformerDecoder, SinePositionalEncoding
  from ..utils import get_uncertain_point_coords_with_randomness
  from .anchor_free_head import AnchorFreeHead
  from .maskformer_head import MaskFormerHead
label_desc: Use image node_id 6 for calling the Mask2FormerHead obj's underlying member method code with example usage: obj.forward(x, batch_data_samples) and returns: cls_pred_list,  mask_pred_list