class_docstr: Implement query generator of the Contrastive denoising (CDN) proposed in
  `DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object
  Detection <https://arxiv.org/abs/2203.03605>`_
  
  Code is modified from the `official github repo
  <https://github.com/IDEA-Research/DINO>`_.
  
  Args:
      num_classes (int): Number of object classes.
      embed_dims (int): The embedding dimensions of the generated queries.
      num_matching_queries (int): The queries number of the matching part.
          Used for generating dn_mask.
      label_noise_scale (float): The scale of label noise, defaults to 0.5.
      box_noise_scale (float): The scale of box noise, defaults to 1.0.
      group_cfg (:obj:`ConfigDict` or dict, optional): The config of the
          denoising queries grouping, includes `dynamic`, `num_dn_queries`,
          and `num_groups`. Two grouping strategies, 'static dn groups' and
          'dynamic dn groups', are supported. When `dynamic` is `False`,
          the `num_groups` should be set, and the number of denoising query
          groups will always be `num_groups`. When `dynamic` is `True`, the
          `num_dn_queries` should be set, and the group number will be
          dynamic to ensure that the denoising queries number will not exceed
          `num_dn_queries` to prevent large fluctuations of memory. Defaults
          to `None`.
docstr: Generate attention mask to prevent information leakage from
  different denoising groups and matching parts.
  
  .. code:: text
  
                  0 0 0 0 1 1 1 1 0 0 0 0 0
                  0 0 0 0 1 1 1 1 0 0 0 0 0
                  0 0 0 0 1 1 1 1 0 0 0 0 0
                  0 0 0 0 1 1 1 1 0 0 0 0 0
                  1 1 1 1 0 0 0 0 0 0 0 0 0
                  1 1 1 1 0 0 0 0 0 0 0 0 0
                  1 1 1 1 0 0 0 0 0 0 0 0 0
                  1 1 1 1 0 0 0 0 0 0 0 0 0
                  1 1 1 1 1 1 1 1 0 0 0 0 0
                  1 1 1 1 1 1 1 1 0 0 0 0 0
                  1 1 1 1 1 1 1 1 0 0 0 0 0
                  1 1 1 1 1 1 1 1 0 0 0 0 0
                  1 1 1 1 1 1 1 1 0 0 0 0 0
   max_num_target |_|           |_________| num_matching_queries
                  |_____________| num_denoising_queries
  
         1 -> True  (Masked), means 'can not see'.
         0 -> False (UnMasked), means 'can see'.
  
  Args:
      max_num_target (int): The max target number of the input batch
          samples.
      num_groups (int): The number of denoising query groups.
      device (obj:`device` or str): The device of generated mask.
  
  Returns:
      Tensor: The attention mask to prevent information leakage from
      different denoising groups and matching parts, will be used as
      `self_attn_mask` of the `decoder`, has shape (num_queries_total,
      num_queries_total), where `num_queries_total` is the sum of
      `num_denoising_queries` and `num_matching_queries`.
docstr_tok: ["Generate","attention","mask","to","prevent","information","leakage","from","different","denoising","groups","and","matching","parts",".","..","code",":",":","text","0","0","0","0","1","1","1","1","0","0","0","0","0","0","0","0","0","1","1","1","1","0","0","0","0","0","0","0","0","0","1","1","1","1","0","0","0","0","0","0","0","0","0","1","1","1","1","0","0","0","0","0","1","1","1","1","0","0","0","0","0","0","0","0","0","1","1","1","1","0","0","0","0","0","0","0","0","0","1","1","1","1","0","0","0","0","0","0","0","0","0","1","1","1","1","0","0","0","0","0","0","0","0","0","1","1","1","1","1","1","1","1","0","0","0","0","0","1","1","1","1","1","1","1","1","0","0","0","0","0","1","1","1","1","1","1","1","1","0","0","0","0","0","1","1","1","1","1","1","1","1","0","0","0","0","0","1","1","1","1","1","1","1","1","0","0","0","0","0","max_num_target","|_|","|_________|","num_matching_queries","|_____________|","num_denoising_queries","1","-",">","True","(","Masked",")",",","means","'can","not","see","'",".","0","-",">","False","(","UnMasked",")",",","means","'can","see","'",".","Args",":","max_num_target","(","int",")",":","The","max","target","number","of","the","input","batch","samples",".","num_groups","(","int",")",":","The","number","of","denoising","query","groups",".","device","(","obj",":","`","device","`","or","str",")",":","The","device","of","generated","mask",".","Returns",":","Tensor",":","The","attention","mask","to","prevent","information","leakage","from","different","denoising","groups","and","matching","parts",",","will","be","used","as","`","self_attn_mask","`","of","the","`","decoder","`",",","has","shape","(","num_queries_total",",","num_queries_total",")",",","where","`","num_queries_total","`","is","the","sum","of","`","num_denoising_queries","`","and","`","num_matching_queries","`","."]
code: def generate_dn_mask(
      self,
      max_num_target: int,
      num_groups: int,
      device: Union[torch.device, str],
  ) -> Tensor:
      """Generate attention mask to prevent information leakage from
      different denoising groups and matching parts.
  
      .. code:: text
  
                      0 0 0 0 1 1 1 1 0 0 0 0 0
                      0 0 0 0 1 1 1 1 0 0 0 0 0
                      0 0 0 0 1 1 1 1 0 0 0 0 0
                      0 0 0 0 1 1 1 1 0 0 0 0 0
                      1 1 1 1 0 0 0 0 0 0 0 0 0
                      1 1 1 1 0 0 0 0 0 0 0 0 0
                      1 1 1 1 0 0 0 0 0 0 0 0 0
                      1 1 1 1 0 0 0 0 0 0 0 0 0
                      1 1 1 1 1 1 1 1 0 0 0 0 0
                      1 1 1 1 1 1 1 1 0 0 0 0 0
                      1 1 1 1 1 1 1 1 0 0 0 0 0
                      1 1 1 1 1 1 1 1 0 0 0 0 0
                      1 1 1 1 1 1 1 1 0 0 0 0 0
       max_num_target |_|           |_________| num_matching_queries
                      |_____________| num_denoising_queries
  
             1 -> True  (Masked), means 'can not see'.
             0 -> False (UnMasked), means 'can see'.
  
      Args:
          max_num_target (int): The max target number of the input batch
              samples.
          num_groups (int): The number of denoising query groups.
          device (obj:`device` or str): The device of generated mask.
  
      Returns:
          Tensor: The attention mask to prevent information leakage from
          different denoising groups and matching parts, will be used as
          `self_attn_mask` of the `decoder`, has shape (num_queries_total,
          num_queries_total), where `num_queries_total` is the sum of
          `num_denoising_queries` and `num_matching_queries`.
      """
      num_denoising_queries = int(max_num_target * 2 * num_groups)
      num_queries_total = (
          num_denoising_queries + self.num_matching_queries
      )
      attn_mask = torch.zeros(
          num_queries_total,
          num_queries_total,
          device=device,
          dtype=torch.bool,
      )
      # Make the matching part cannot see the denoising groups
      attn_mask[num_denoising_queries:, :num_denoising_queries] = True
      # Make the denoising groups cannot see each other
      for i in range(num_groups):
          # Mask rows of one group per step.
          row_scope = slice(
              max_num_target * 2 * i, max_num_target * 2 * (i + 1)
          )
          left_scope = slice(max_num_target * 2 * i)
          right_scope = slice(
              max_num_target * 2 * (i + 1), num_denoising_queries
          )
          attn_mask[row_scope, right_scope] = True
          attn_mask[row_scope, left_scope] = True
      return attn_mask
  
imports: import warnings
  from typing import Tuple, Union
  import torch
  from mmengine.model import BaseModule
  from torch import Tensor, nn
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox_xyxy_to_cxcywh
  from mmdet.utils import OptConfigType
  from .deformable_detr_layers import DeformableDetrTransformerDecoder
  from .utils import MLP, coordinate_to_encoding, inverse_sigmoid
label_desc: Use image node_id 7 for calling the CdnQueryGenerator obj's underlying member method code with example usage: obj.generate_dn_mask(max_num_target, num_groups, device) and returns: attn_mask