docstr: Forward function of Transformer encoder.
  
  Args:
      query (Tensor): The input query, has shape (bs, num_queries, dim).
      query_pos (Tensor): The positional encoding for query, has shape
          (bs, num_queries, dim).
      key_padding_mask (Tensor): The `key_padding_mask` of `self_attn`
          input. ByteTensor, has shape (bs, num_queries).
      spatial_shapes (Tensor): Spatial shapes of features in all levels,
          has shape (num_levels, 2), last dimension represents (h, w).
      level_start_index (Tensor): The start index of each level.
          A tensor has shape (num_levels, ) and can be represented
          as [0, h_0*w_0, h_0*w_0+h_1*w_1, ...].
      valid_ratios (Tensor): The ratios of the valid width and the valid
          height relative to the width and the height of features in all
          levels, has shape (bs, num_levels, 2).
      memory_text (Tensor, optional): Memory text. It has shape (bs,
          len_text, text_embed_dims).
      text_attention_mask (Tensor, optional): Text token mask. It has
          shape (bs,len_text).
      pos_text (Tensor, optional): The positional encoding for text.
          Defaults to None.
      text_self_attention_masks (Tensor, optional): Text self attention
          mask. Defaults to None.
      position_ids (Tensor, optional): Text position ids.
          Defaults to None.
docstr_tok: ["Forward","function","of","Transformer","encoder",".","Args",":","query","(","Tensor",")",":","The","input","query",",","has","shape","(","bs",",","num_queries",",","dim",")",".","query_pos","(","Tensor",")",":","The","positional","encoding","for","query",",","has","shape","(","bs",",","num_queries",",","dim",")",".","key_padding_mask","(","Tensor",")",":","The","`","key_padding_mask","`","of","`","self_attn","`","input",".","ByteTensor",",","has","shape","(","bs",",","num_queries",")",".","spatial_shapes","(","Tensor",")",":","Spatial","shapes","of","features","in","all","levels",",","has","shape","(","num_levels",",","2",")",",","last","dimension","represents","(","h",",","w",")",".","level_start_index","(","Tensor",")",":","The","start","index","of","each","level",".","A","tensor","has","shape","(","num_levels",",",")","and","can","be","represented","as","[","0",",","h_0","*","w_0",",","h_0","*","w_0+h_1","*","w_1",",","...","]",".","valid_ratios","(","Tensor",")",":","The","ratios","of","the","valid","width","and","the","valid","height","relative","to","the","width","and","the","height","of","features","in","all","levels",",","has","shape","(","bs",",","num_levels",",","2",")",".","memory_text","(","Tensor",",","optional",")",":","Memory","text",".","It","has","shape","(","bs",",","len_text",",","text_embed_dims",")",".","text_attention_mask","(","Tensor",",","optional",")",":","Text","token","mask",".","It","has","shape","(","bs",",","len_text",")",".","pos_text","(","Tensor",",","optional",")",":","The","positional","encoding","for","text",".","Defaults","to","None",".","text_self_attention_masks","(","Tensor",",","optional",")",":","Text","self","attention","mask",".","Defaults","to","None",".","position_ids","(","Tensor",",","optional",")",":","Text","position","ids",".","Defaults","to","None","."]
code: def forward(
      self,
      query: Tensor,
      query_pos: Tensor,
      key_padding_mask: Tensor,
      spatial_shapes: Tensor,
      level_start_index: Tensor,
      valid_ratios: Tensor,
      memory_text: Tensor = None,
      text_attention_mask: Tensor = None,
      pos_text: Tensor = None,
      text_self_attention_masks: Tensor = None,
      position_ids: Tensor = None,
  ):
      """Forward function of Transformer encoder.
  
      Args:
          query (Tensor): The input query, has shape (bs, num_queries, dim).
          query_pos (Tensor): The positional encoding for query, has shape
              (bs, num_queries, dim).
          key_padding_mask (Tensor): The `key_padding_mask` of `self_attn`
              input. ByteTensor, has shape (bs, num_queries).
          spatial_shapes (Tensor): Spatial shapes of features in all levels,
              has shape (num_levels, 2), last dimension represents (h, w).
          level_start_index (Tensor): The start index of each level.
              A tensor has shape (num_levels, ) and can be represented
              as [0, h_0*w_0, h_0*w_0+h_1*w_1, ...].
          valid_ratios (Tensor): The ratios of the valid width and the valid
              height relative to the width and the height of features in all
              levels, has shape (bs, num_levels, 2).
          memory_text (Tensor, optional): Memory text. It has shape (bs,
              len_text, text_embed_dims).
          text_attention_mask (Tensor, optional): Text token mask. It has
              shape (bs,len_text).
          pos_text (Tensor, optional): The positional encoding for text.
              Defaults to None.
          text_self_attention_masks (Tensor, optional): Text self attention
              mask. Defaults to None.
          position_ids (Tensor, optional): Text position ids.
              Defaults to None.
      """
      output = query
      reference_points = self.get_encoder_reference_points(
          spatial_shapes, valid_ratios, device=query.device
      )
      if self.text_layers:
          # generate pos_text
          bs, n_text, _ = memory_text.shape
          if pos_text is None and position_ids is None:
              pos_text = (
                  torch.arange(n_text, device=memory_text.device)
                  .float()
                  .unsqueeze(0)
                  .unsqueeze(-1)
                  .repeat(bs, 1, 1)
              )
              pos_text = get_text_sine_pos_embed(
                  pos_text, num_pos_feats=256, exchange_xy=False
              )
          if position_ids is not None:
              pos_text = get_text_sine_pos_embed(
                  position_ids[..., None],
                  num_pos_feats=256,
                  exchange_xy=False,
              )
  
      # main process
      for layer_id, layer in enumerate(self.layers):
          if self.fusion_layers:
              output, memory_text = self.fusion_layers[layer_id](
                  visual_feature=output,
                  lang_feature=memory_text,
                  attention_mask_v=key_padding_mask,
                  attention_mask_l=text_attention_mask,
              )
          if self.text_layers:
              text_num_heads = self.text_layers[
                  layer_id
              ].self_attn_cfg.num_heads
              memory_text = self.text_layers[layer_id](
                  query=memory_text,
                  query_pos=(
                      pos_text if pos_text is not None else None
                  ),
                  attn_mask=~text_self_attention_masks.repeat(
                      text_num_heads, 1, 1
                  ),  # note we use ~ for mask here
                  key_padding_mask=None,
              )
          output = layer(
              query=output,
              query_pos=query_pos,
              reference_points=reference_points,
              spatial_shapes=spatial_shapes,
              level_start_index=level_start_index,
              key_padding_mask=key_padding_mask,
          )
      return output, memory_text
  
imports: import torch
  import torch.nn
  from mmcv.cnn import build_norm_layer
  from mmcv.cnn.bricks.transformer import FFN, MultiheadAttention
  from mmcv.ops import MultiScaleDeformableAttention
  from mmengine.model import ModuleList
  from torch import Tensor
  from mmdet.models.utils.vlfuse_helper import SingleScaleBiAttentionBlock
  from mmdet.utils import ConfigType, OptConfigType
  from .deformable_detr_layers import DeformableDetrTransformerDecoderLayer, DeformableDetrTransformerEncoder, DeformableDetrTransformerEncoderLayer
  from .detr_layers import DetrTransformerEncoderLayer
  from .dino_layers import DinoTransformerDecoder
  from .utils import MLP, get_text_sine_pos_embed
label_desc: Use image node_id 3 for calling the GroundingDinoTransformerEncoder obj's underlying member method code with example usage: obj.forward(query, query_pos, key_padding_mask, spatial_shapes, level_start_index, valid_ratios, memory_text, text_attention_mask, pos_text, text__attention_masks, position_ids) and returns: output,  memory_text