class_docstr: Hybrid task cascade roi head including one bbox head and one mask head.
  
  https://arxiv.org/abs/1901.07518
  
  Args:
      num_stages (int): Number of cascade stages.
      stage_loss_weights (list[float]): Loss weight for every stage.
      semantic_roi_extractor (:obj:`ConfigDict` or dict, optional):
          Config of semantic roi extractor. Defaults to None.
      Semantic_head (:obj:`ConfigDict` or dict, optional):
          Config of semantic head. Defaults to None.
      interleaved (bool): Whether to interleaves the box branch and mask
          branch. If True, the mask branch can take the refined bounding
          box predictions. Defaults to True.
      mask_info_flow (bool): Whether to turn on the mask information flow,
          which means that feeding the mask features of the preceding stage
          to the current stage. Defaults to True.
docstr: Mask head forward function used only in training.
  
  Args:
      stage (int): The current stage in Cascade RoI Head.
      x (tuple[Tensor]): Tuple of multi-level img features.
      rois (Tensor): RoIs with the shape (n, 5) where the first
          column indicates batch id of each RoI.
      semantic_feat (Tensor, optional): Semantic feature. Defaults to
          None.
      training (bool): Mask Forward is different between training and
          testing. If True, use the mask forward in training.
          Defaults to True.
  
  Returns:
      dict: Usually returns a dictionary with keys:
  
          - `mask_preds` (Tensor): Mask prediction.
docstr_tok: ["Mask","head","forward","function","used","only","in","training",".","Args",":","stage","(","int",")",":","The","current","stage","in","Cascade","RoI","Head",".","x","(","tuple","[","Tensor","]",")",":","Tuple","of","multi-level","img","features",".","rois","(","Tensor",")",":","RoIs","with","the","shape","(","n",",","5",")","where","the","first","column","indicates","batch","id","of","each","RoI",".","semantic_feat","(","Tensor",",","optional",")",":","Semantic","feature",".","Defaults","to","None",".","training","(","bool",")",":","Mask","Forward","is","different","between","training","and","testing",".","If","True",",","use","the","mask","forward","in","training",".","Defaults","to","True",".","Returns",":","dict",":","Usually","returns","a","dictionary","with","keys",":","-","`","mask_preds","`","(","Tensor",")",":","Mask","prediction","."]
code: def _mask_forward(
      self,
      stage: int,
      x: Tuple[Tensor],
      rois: Tensor,
      semantic_feat: Optional[Tensor] = None,
      training: bool = True,
  ) -> Dict[str, Tensor]:
      """Mask head forward function used only in training.
  
      Args:
          stage (int): The current stage in Cascade RoI Head.
          x (tuple[Tensor]): Tuple of multi-level img features.
          rois (Tensor): RoIs with the shape (n, 5) where the first
              column indicates batch id of each RoI.
          semantic_feat (Tensor, optional): Semantic feature. Defaults to
              None.
          training (bool): Mask Forward is different between training and
              testing. If True, use the mask forward in training.
              Defaults to True.
  
      Returns:
          dict: Usually returns a dictionary with keys:
  
              - `mask_preds` (Tensor): Mask prediction.
      """
      mask_roi_extractor = self.mask_roi_extractor[stage]
      mask_head = self.mask_head[stage]
      mask_feats = mask_roi_extractor(
          x[: mask_roi_extractor.num_inputs], rois
      )
  
      # semantic feature fusion
      # element-wise sum for original features and pooled semantic features
      if self.with_semantic and "mask" in self.semantic_fusion:
          mask_semantic_feat = self.semantic_roi_extractor(
              [semantic_feat], rois
          )
          if mask_semantic_feat.shape[-2:] != mask_feats.shape[-2:]:
              mask_semantic_feat = F.adaptive_avg_pool2d(
                  mask_semantic_feat, mask_feats.shape[-2:]
              )
          mask_feats = mask_feats + mask_semantic_feat
  
      # mask information flow
      # forward all previous mask heads to obtain last_feat, and fuse it
      # with the normal mask feature
      if training:
          if self.mask_info_flow:
              last_feat = None
              for i in range(stage):
                  last_feat = self.mask_head[i](
                      mask_feats, last_feat, return_logits=False
                  )
              mask_preds = mask_head(
                  mask_feats, last_feat, return_feat=False
              )
          else:
              mask_preds = mask_head(mask_feats, return_feat=False)
  
          mask_results = dict(mask_preds=mask_preds)
      else:
          aug_masks = []
          last_feat = None
          for i in range(self.num_stages):
              mask_head = self.mask_head[i]
              if self.mask_info_flow:
                  mask_preds, last_feat = mask_head(
                      mask_feats, last_feat
                  )
              else:
                  mask_preds = mask_head(mask_feats)
          aug_masks.append(mask_preds)
  
          mask_results = dict(mask_preds=aug_masks)
  
      return mask_results
  
imports: from typing import Dict, List, Optional, Tuple
  import torch
  import torch.nn.functional
  from torch import Tensor
  from mmdet.models.test_time_augs import merge_aug_masks
  from mmdet.registry import MODELS
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox2roi
  from mmdet.utils import InstanceList, OptConfigType
  from ..layers import adaptive_avg_pool2d
  from ..task_modules.samplers import SamplingResult
  from ..utils import empty_instances, unpack_gt_instances
  from .cascade_roi_head import CascadeRoIHead
label_desc: Use image node_id 5 for calling the HybridTaskCascadeRoIHead obj's underlying member method code with example usage: obj._mask_forward(stage, x, rois, semantic_feat, training) and returns: mask_results