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: Collate generated queries to obtain batched dn queries.
  
  The strategy for query collation is as follow:
  
  .. code:: text
  
              input_queries (num_target_total, query_dim)
      P_A1 P_B1 P_B2 N_A1 N_B1 N_B2 P'A1 P'B1 P'B2 N'A1 N'B1 N'B2
        |________ group1 ________|    |________ group2 ________|
                                   |
                                   V
                P_A1 Pad0 N_A1 Pad0 P'A1 Pad0 N'A1 Pad0
                P_B1 P_B2 N_B1 N_B2 P'B1 P'B2 N'B1 N'B2
                 |____ group1 ____| |____ group2 ____|
       batched_queries (batch_size, max_num_target, query_dim)
  
      where query_dim is 4 for bbox and self.embed_dims for label.
      Notation: _-group 1; '-group 2;
                A-Sample1(has 1 target); B-sample2(has 2 targets)
  
  Args:
      input_label_query (Tensor): The generated label queries of all
          targets, has shape (num_target_total, embed_dims) where
          `num_target_total = sum(num_target_list)`.
      input_bbox_query (Tensor): The generated bbox queries of all
          targets, has shape (num_target_total, 4) with the last
          dimension arranged as (cx, cy, w, h).
      batch_idx (Tensor): The batch index of the corresponding sample
          for each target, has shape (num_target_total).
      batch_size (int): The size of the input batch.
      num_groups (int): The number of denoising query groups.
  
  Returns:
      tuple[Tensor]: Output batched label and bbox queries.
      - batched_label_query (Tensor): The output batched label queries,
        has shape (batch_size, max_num_target, embed_dims).
      - batched_bbox_query (Tensor): The output batched bbox queries,
        has shape (batch_size, max_num_target, 4) with the last dimension
        arranged as (cx, cy, w, h).
docstr_tok: ["Collate","generated","queries","to","obtain","batched","dn","queries",".","The","strategy","for","query","collation","is","as","follow",":","..","code",":",":","text","input_queries","(","num_target_total",",","query_dim",")","P_A1","P_B1","P_B2","N_A1","N_B1","N_B2","P'A1","P'B1","P'B2","N'A1","N'B1","N'B2","|________","group1","________|","|________","group2","________|","|","V","P_A1","Pad0","N_A1","Pad0","P'A1","Pad0","N'A1","Pad0","P_B1","P_B2","N_B1","N_B2","P'B1","P'B2","N'B1","N'B2","|____","group1","____|","|____","group2","____|","batched_queries","(","batch_size",",","max_num_target",",","query_dim",")","where","query_dim","is","4","for","bbox","and","self.embed_dims","for","label",".","Notation",":","_-group","1",";","'-group","2",";","A-Sample1","(","has","1","target",")",";","B-sample2","(","has","2","targets",")","Args",":","input_label_query","(","Tensor",")",":","The","generated","label","queries","of","all","targets",",","has","shape","(","num_target_total",",","embed_dims",")","where","`","num_target_total","=","sum","(","num_target_list",")","`",".","input_bbox_query","(","Tensor",")",":","The","generated","bbox","queries","of","all","targets",",","has","shape","(","num_target_total",",","4",")","with","the","last","dimension","arranged","as","(","cx",",","cy",",","w",",","h",")",".","batch_idx","(","Tensor",")",":","The","batch","index","of","the","corresponding","sample","for","each","target",",","has","shape","(","num_target_total",")",".","batch_size","(","int",")",":","The","size","of","the","input","batch",".","num_groups","(","int",")",":","The","number","of","denoising","query","groups",".","Returns",":","tuple","[","Tensor","]",":","Output","batched","label","and","bbox","queries",".","-","batched_label_query","(","Tensor",")",":","The","output","batched","label","queries",",","has","shape","(","batch_size",",","max_num_target",",","embed_dims",")",".","-","batched_bbox_query","(","Tensor",")",":","The","output","batched","bbox","queries",",","has","shape","(","batch_size",",","max_num_target",",","4",")","with","the","last","dimension","arranged","as","(","cx",",","cy",",","w",",","h",")","."]
code: def collate_dn_queries(
      self,
      input_label_query: Tensor,
      input_bbox_query: Tensor,
      batch_idx: Tensor,
      batch_size: int,
      num_groups: int,
  ) -> Tuple[Tensor]:
      """Collate generated queries to obtain batched dn queries.
  
      The strategy for query collation is as follow:
  
      .. code:: text
  
                  input_queries (num_target_total, query_dim)
          P_A1 P_B1 P_B2 N_A1 N_B1 N_B2 P'A1 P'B1 P'B2 N'A1 N'B1 N'B2
            |________ group1 ________|    |________ group2 ________|
                                       |
                                       V
                    P_A1 Pad0 N_A1 Pad0 P'A1 Pad0 N'A1 Pad0
                    P_B1 P_B2 N_B1 N_B2 P'B1 P'B2 N'B1 N'B2
                     |____ group1 ____| |____ group2 ____|
           batched_queries (batch_size, max_num_target, query_dim)
  
          where query_dim is 4 for bbox and self.embed_dims for label.
          Notation: _-group 1; '-group 2;
                    A-Sample1(has 1 target); B-sample2(has 2 targets)
  
      Args:
          input_label_query (Tensor): The generated label queries of all
              targets, has shape (num_target_total, embed_dims) where
              `num_target_total = sum(num_target_list)`.
          input_bbox_query (Tensor): The generated bbox queries of all
              targets, has shape (num_target_total, 4) with the last
              dimension arranged as (cx, cy, w, h).
          batch_idx (Tensor): The batch index of the corresponding sample
              for each target, has shape (num_target_total).
          batch_size (int): The size of the input batch.
          num_groups (int): The number of denoising query groups.
  
      Returns:
          tuple[Tensor]: Output batched label and bbox queries.
          - batched_label_query (Tensor): The output batched label queries,
            has shape (batch_size, max_num_target, embed_dims).
          - batched_bbox_query (Tensor): The output batched bbox queries,
            has shape (batch_size, max_num_target, 4) with the last dimension
            arranged as (cx, cy, w, h).
      """
      device = input_label_query.device
      num_target_list = [
          torch.sum(batch_idx == idx) for idx in range(batch_size)
      ]
      max_num_target = max(num_target_list)
      num_denoising_queries = int(max_num_target * 2 * num_groups)
  
      map_query_index = torch.cat(
          [
              torch.arange(num_target, device=device)
              for num_target in num_target_list
          ]
      )
      map_query_index = torch.cat(
          [
              map_query_index + max_num_target * i
              for i in range(2 * num_groups)
          ]
      ).long()
      batch_idx_expand = batch_idx.repeat(2 * num_groups, 1).view(-1)
      mapper = (batch_idx_expand, map_query_index)
  
      batched_label_query = torch.zeros(
          batch_size,
          num_denoising_queries,
          self.embed_dims,
          device=device,
      )
      batched_bbox_query = torch.zeros(
          batch_size, num_denoising_queries, 4, device=device
      )
  
      batched_label_query[mapper] = input_label_query
      batched_bbox_query[mapper] = input_bbox_query
      return batched_label_query, batched_bbox_query
  
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 6 for calling the CdnQueryGenerator obj's underlying member method code with example usage: obj.collate_dn_queries(input_label_query, input_bbox_query, batch_idx, batch_size, num_groups) and returns: batched_label_query,  batched_bbox_query