class_docstr: The RoIHead for `Sparse R-CNN: End-to-End Object Detection with
  Learnable Proposals <https://arxiv.org/abs/2011.12450>`_
  and `Instances as Queries <http://arxiv.org/abs/2105.01928>`_
  
  Args:
      num_stages (int): Number of stage whole iterative process.
          Defaults to 6.
      stage_loss_weights (Tuple[float]): The loss
          weight of each stage. By default all stages have
          the same weight 1.
      bbox_roi_extractor (:obj:`ConfigDict` or dict): Config of box
          roi extractor.
      mask_roi_extractor (:obj:`ConfigDict` or dict): Config of mask
          roi extractor.
      bbox_head (:obj:`ConfigDict` or dict): Config of box head.
      mask_head (:obj:`ConfigDict` or dict): Config of mask head.
      train_cfg (:obj:`ConfigDict` or dict, Optional): Configuration
          information in train stage. Defaults to None.
      test_cfg (:obj:`ConfigDict` or dict, Optional): Configuration
          information in test stage. Defaults to None.
      init_cfg (:obj:`ConfigDict` or dict or list[:obj:`ConfigDict` or \
          dict]): Initialization config dict. Defaults to None.
docstr: Run forward function and calculate loss for mask head in training.
  
  Args:
      stage (int): The current stage in Cascade RoI Head.
      x (tuple[Tensor]): Tuple of multi-level img features.
      bbox_results (dict): Results obtained from `bbox_loss`.
      batch_gt_instances (list[:obj:`InstanceData`]): Batch of
          gt_instance. It usually includes ``bboxes``, ``labels``, and
          ``masks`` attributes.
      rcnn_train_cfg (obj:ConfigDict): `train_cfg` of RCNN.
  
  Returns:
      dict: Usually returns a dictionary with keys:
  
      - `mask_preds` (Tensor): Mask prediction.
      - `loss_mask` (dict): A dictionary of mask loss components.
docstr_tok: ["Run","forward","function","and","calculate","loss","for","mask","head","in","training",".","Args",":","stage","(","int",")",":","The","current","stage","in","Cascade","RoI","Head",".","x","(","tuple","[","Tensor","]",")",":","Tuple","of","multi-level","img","features",".","bbox_results","(","dict",")",":","Results","obtained","from","`","bbox_loss","`",".","batch_gt_instances","(","list","[",":","obj",":","`","InstanceData","`","]",")",":","Batch","of","gt_instance",".","It","usually","includes","``","bboxes","``",",","``","labels","``",",","and","``","masks","``","attributes",".","rcnn_train_cfg","(","obj",":","ConfigDict",")",":","`","train_cfg","`","of","RCNN",".","Returns",":","dict",":","Usually","returns","a","dictionary","with","keys",":","-","`","mask_preds","`","(","Tensor",")",":","Mask","prediction",".","-","`","loss_mask","`","(","dict",")",":","A","dictionary","of","mask","loss","components","."]
code: def mask_loss(
      self,
      stage: int,
      x: Tuple[Tensor],
      bbox_results: dict,
      batch_gt_instances: InstanceList,
      rcnn_train_cfg: ConfigDict,
  ) -> dict:
      """Run forward function and calculate loss for mask head in training.
  
      Args:
          stage (int): The current stage in Cascade RoI Head.
          x (tuple[Tensor]): Tuple of multi-level img features.
          bbox_results (dict): Results obtained from `bbox_loss`.
          batch_gt_instances (list[:obj:`InstanceData`]): Batch of
              gt_instance. It usually includes ``bboxes``, ``labels``, and
              ``masks`` attributes.
          rcnn_train_cfg (obj:ConfigDict): `train_cfg` of RCNN.
  
      Returns:
          dict: Usually returns a dictionary with keys:
  
          - `mask_preds` (Tensor): Mask prediction.
          - `loss_mask` (dict): A dictionary of mask loss components.
      """
      attn_feats = bbox_results["attn_feats"]
      sampling_results = bbox_results["sampling_results"]
  
      pos_rois = bbox2roi([res.pos_priors for res in sampling_results])
  
      attn_feats = torch.cat(
          [
              feats[res.pos_inds]
              for (feats, res) in zip(attn_feats, sampling_results)
          ]
      )
      mask_results = self._mask_forward(stage, x, pos_rois, attn_feats)
  
      mask_loss_and_target = self.mask_head[stage].loss_and_target(
          mask_preds=mask_results["mask_preds"],
          sampling_results=sampling_results,
          batch_gt_instances=batch_gt_instances,
          rcnn_train_cfg=rcnn_train_cfg,
      )
      mask_results.update(mask_loss_and_target)
  
      return mask_results
  
imports: from typing import List, Tuple
  import torch
  from mmengine.config import ConfigDict
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.models.task_modules.samplers import PseudoSampler
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox2roi
  from mmdet.utils import ConfigType, InstanceList, OptConfigType
  from ..utils.misc import empty_instances, unpack_gt_instances
  from .cascade_roi_head import CascadeRoIHead
label_desc: Use image node_id 5 for calling the SparseRoIHead obj's underlying member method code with example usage: obj.mask_loss(stage, x, bbox_results, batch_gt_instances, rcnn_train_cfg) and returns: mask_results