code: def __init__(
      self,
      in_channels=512,
      hidden_dim: int = 512,
      dim_proj: int = 512,
      num_queries: int = 101,
      max_token_num: int = 77,
      nheads: int = 8,
      dim_feedforward: int = 2048,
      decoder_layers: int = 9,
      pre_norm: bool = False,
      mask_dim: int = 512,
      task: str = "semseg",
      captioning_step: int = 50,
  ):
      super().__init__()
  
      # positional encoding
      self.pe_layer = PositionEmbeddingSine(
          hidden_dim // 2, normalize=True
      )
  
      # define transformer decoder here
      self.num_heads = nheads
      self.num_layers = decoder_layers
      self.max_token_num = max_token_num
      self.transformer_self_attention_layers = nn.ModuleList()
      self.transformer_cross_attention_layers = nn.ModuleList()
      self.transformer_ffn_layers = nn.ModuleList()
  
      for _ in range(self.num_layers):
          self.transformer_self_attention_layers.append(
              SelfAttentionLayer(
                  d_model=hidden_dim,
                  nhead=nheads,
                  dropout=0.0,
                  normalize_before=pre_norm,
              )
          )
  
          self.transformer_cross_attention_layers.append(
              CrossAttentionLayer(
                  d_model=hidden_dim,
                  nhead=nheads,
                  dropout=0.0,
                  normalize_before=pre_norm,
              )
          )
  
          self.transformer_ffn_layers.append(
              FFNLayer(
                  d_model=hidden_dim,
                  dim_feedforward=dim_feedforward,
                  dropout=0.0,
                  normalize_before=pre_norm,
              )
          )
  
      self.decoder_norm = nn.LayerNorm(hidden_dim)
  
      self.num_queries = num_queries
      # learnable query features
      self.query_feat = nn.Embedding(num_queries, hidden_dim)
      # learnable query p.e.
      self.query_embed = nn.Embedding(num_queries, hidden_dim)
  
      # level embedding (always use 3 scales)
      self.num_feature_levels = 3
      self.level_embed = nn.Embedding(
          self.num_feature_levels, hidden_dim
      )
      self.input_proj = nn.ModuleList()
  
      for _ in range(self.num_feature_levels):
          if in_channels != hidden_dim:
              self.input_proj.append(
                  Conv2d(in_channels, hidden_dim, kernel_size=1)
              )
          else:
              self.input_proj.append(nn.Sequential())
  
      self.task = task
  
      # output FFNs
      self.lang_encoder = LanguageEncoder()
  
      self.mask_embed = MLP(hidden_dim, hidden_dim, mask_dim, 3)
      self.class_embed = nn.Parameter(torch.empty(hidden_dim, dim_proj))
  
      # for caption and ref-caption
      self.caping_embed = nn.Parameter(
          torch.empty(hidden_dim, dim_proj)
      )
      self.pos_embed_caping = nn.Embedding(max_token_num, hidden_dim)
      self.captioning_step = captioning_step
  
      # register self_attn_mask to avoid information leakage,
      # it includes interaction between object query, class query and
      # caption query
      self_attn_mask = torch.zeros(
          (1, num_queries + max_token_num, num_queries + max_token_num)
      ).bool()
      # object+class query does not attend with caption query.
      self_attn_mask[:, :num_queries, num_queries:] = True
      # caption query only attend with previous token.
      self_attn_mask[:, num_queries:, num_queries:] = torch.triu(
          torch.ones((1, max_token_num, max_token_num)), diagonal=1
      ).bool()
      # object query does not attend with class query.
      self_attn_mask[
          :, : num_queries - 1, num_queries - 1 : num_queries
      ] = True
      # class query does not attend with object query.
      self_attn_mask[
          :, num_queries - 1 : num_queries, : num_queries - 1
      ] = True
      self.register_buffer("self_attn_mask", self_attn_mask)
  
file_path: mmdetection/projects/XDecoder/xdecoder/transformer_decoder.py
imports: import torch
  from torch import nn
  from torch.nn import functional
  from mmdet.registry import MODELS
  from .language_model import LanguageEncoder
  from .transformer_blocks import MLP, Conv2d, CrossAttentionLayer, FFNLayer, PositionEmbeddingSine, SelfAttentionLayer
  from .utils import is_lower_torch_version
label_desc: Use image node_id 1 to create a new XDecoderTransformerDecoder object from inherited base classes: nn with example: obj = XDecoderTransformerDecoder(in_channels, hidden_dim, dim_proj, num_queries, max_token_num, nheads, dim_feedforward, decoder_layers, pre_norm, mask_dim, task, captioning_step)