class_docstr: Transformer decoder of DDQ.
class_docstr_tok: ["Transformer","decoder","of","DDQ","."]
docstr: Get updated `self_attn_mask` for distinct queries selection, it is
  used in self attention layers of decoder.
  
  Args:
      reference_points (Tensor): The input reference of decoder,
          has shape (bs, num_queries, 4) with the last dimension
          arranged as (cx, cy, w, h).
      query (Tensor): The input query of decoder, has shape
          (bs, num_queries, dims).
      self_attn_mask (Tensor): The input self attention mask of
          last decoder layer, has shape (bs, num_queries_total,
          num_queries_total).
      layer_index (int): Last decoder layer index, used to get
          classification score of last layer output, for
          distinct queries selection.
  
  Returns:
      Tensor: `self_attn_mask` used in self attention layers
          of decoder, has shape (bs, num_queries_total,
          num_queries_total).
docstr_tok: ["Get","updated","`","self_attn_mask","`","for","distinct","queries","selection",",","it","is","used","in","self","attention","layers","of","decoder",".","Args",":","reference_points","(","Tensor",")",":","The","input","reference","of","decoder",",","has","shape","(","bs",",","num_queries",",","4",")","with","the","last","dimension","arranged","as","(","cx",",","cy",",","w",",","h",")",".","query","(","Tensor",")",":","The","input","query","of","decoder",",","has","shape","(","bs",",","num_queries",",","dims",")",".","self_attn_mask","(","Tensor",")",":","The","input","self","attention","mask","of","last","decoder","layer",",","has","shape","(","bs",",","num_queries_total",",","num_queries_total",")",".","layer_index","(","int",")",":","Last","decoder","layer","index",",","used","to","get","classification","score","of","last","layer","output",",","for","distinct","queries","selection",".","Returns",":","Tensor",":","`","self_attn_mask","`","used","in","self","attention","layers","of","decoder",",","has","shape","(","bs",",","num_queries_total",",","num_queries_total",")","."]
code: def select_distinct_queries(
      self,
      reference_points: Tensor,
      query: Tensor,
      self_attn_mask: Tensor,
      layer_index,
  ):
      """Get updated `self_attn_mask` for distinct queries selection, it is
      used in self attention layers of decoder.
  
      Args:
          reference_points (Tensor): The input reference of decoder,
              has shape (bs, num_queries, 4) with the last dimension
              arranged as (cx, cy, w, h).
          query (Tensor): The input query of decoder, has shape
              (bs, num_queries, dims).
          self_attn_mask (Tensor): The input self attention mask of
              last decoder layer, has shape (bs, num_queries_total,
              num_queries_total).
          layer_index (int): Last decoder layer index, used to get
              classification score of last layer output, for
              distinct queries selection.
  
      Returns:
          Tensor: `self_attn_mask` used in self attention layers
              of decoder, has shape (bs, num_queries_total,
              num_queries_total).
      """
      num_imgs = len(reference_points)
      dis_start, num_dis = self.cache_dict["dis_query_info"]
      # shape of self_attn_mask
      # (batch⋅num_heads, num_queries, embed_dims)
      dis_mask = self_attn_mask[
          :,
          dis_start : dis_start + num_dis,
          dis_start : dis_start + num_dis,
      ]
      # cls_branches from DDQDETRHead
      scores = (
          self.cache_dict["cls_branches"][layer_index](
              query[:, dis_start : dis_start + num_dis]
          )
          .sigmoid()
          .max(-1)
          .values
      )
      proposals = reference_points[:, dis_start : dis_start + num_dis]
      proposals = bbox_cxcywh_to_xyxy(proposals)
  
      attn_mask_list = []
      for img_id in range(num_imgs):
          single_proposals = proposals[img_id]
          single_scores = scores[img_id]
          attn_mask = ~dis_mask[img_id * self.cache_dict["num_heads"]][
              0
          ]
          # distinct query inds in this layer
          ori_index = attn_mask.nonzero().view(-1)
          _, keep_idxs = batched_nms(
              single_proposals[ori_index],
              single_scores[ori_index],
              torch.ones(len(ori_index)),
              self.cache_dict["dqs_cfg"],
          )
  
          real_keep_index = ori_index[keep_idxs]
  
          attn_mask = torch.ones_like(dis_mask[0]).bool()
          # such a attn_mask give best result
          # If it requires to keep index i, then all cells in row or column
          #   i should be kept in `attn_mask` . For example, if
          #   `real_keep_index` = [1, 4], and `attn_mask` size = [8, 8],
          #   then all cells at rows or columns [1, 4] should be kept, and
          #   all the other cells should be masked out. So the value of
          #  `attn_mask` should be:
          #
          # target\source   0 1 2 3 4 5 6 7
          #             0 [ 0 1 0 0 1 0 0 0 ]
          #             1 [ 1 1 1 1 1 1 1 1 ]
          #             2 [ 0 1 0 0 1 0 0 0 ]
          #             3 [ 0 1 0 0 1 0 0 0 ]
          #             4 [ 1 1 1 1 1 1 1 1 ]
          #             5 [ 0 1 0 0 1 0 0 0 ]
          #             6 [ 0 1 0 0 1 0 0 0 ]
          #             7 [ 0 1 0 0 1 0 0 0 ]
          attn_mask[real_keep_index] = False
          attn_mask[:, real_keep_index] = False
  
          attn_mask = attn_mask[None].repeat(
              self.cache_dict["num_heads"], 1, 1
          )
          attn_mask_list.append(attn_mask)
      attn_mask = torch.cat(attn_mask_list)
      self_attn_mask = copy.deepcopy(self_attn_mask)
      self_attn_mask[
          :,
          dis_start : dis_start + num_dis,
          dis_start : dis_start + num_dis,
      ] = attn_mask
      # will be used in loss and inference
      self.cache_dict["distinct_query_mask"].append(~attn_mask)
      return self_attn_mask
  
file_path: mmdetection/mmdet/models/layers/transformer/ddq_detr_layers.py
imports: import copy
  import torch
  from mmcv.ops import batched_nms
  from torch import Tensor, nn
  from mmdet.structures.bbox import bbox_cxcywh_to_xyxy
  from .deformable_detr_layers import DeformableDetrTransformerDecoder
  from .utils import MLP, coordinate_to_encoding, inverse_sigmoid
label_desc: Use image node_id 2 for calling the DDQTransformerDecoder obj's underlying member method code with example usage: obj.select_distinct_queries(reference_points, query, _attn_mask, layer_index) and returns: self_attn_mask