class_docstr: Implements the decoder in PETR transformer.
  
  Args:
      return_intermediate (bool): Whether to return intermediate outputs.
      coder_norm_cfg (dict): Config of last normalization layer. Default：
          `LN`.
class_docstr_tok: ["Implements","the","decoder","in","PETR","transformer",".","Args",":","return_intermediate","(","bool",")",":","Whether","to","return","intermediate","outputs",".","coder_norm_cfg","(","dict",")",":","Config","of","last","normalization","layer",".","Default\uff1a","`","LN","`","."]
docstr: Forward function for `TransformerDecoder`.
  
  Args:
      query (Tensor): Input query with shape (num_query, bs, embed_dims).
      reference_points (Tensor): The reference points of offset,
          has shape (bs, num_query, K*2).
      valid_ratios (Tensor): The radios of valid points on the feature
          map, has shape (bs, num_levels, 2).
      kpt_branches: (obj:`nn.LayerList`): Used for refining the
          regression results. Only would be passed when `with_box_refine`
          is True, otherwise would be passed a `None`.
  
  Returns:
      tuple (Tensor): Results with shape [1, num_query, bs, embed_dims] when
          return_intermediate is `False`, otherwise it has shape
          [num_layers, num_query, bs, embed_dims] and
          [num_layers, bs, num_query, K*2].
docstr_tok: ["Forward","function","for","`","TransformerDecoder","`",".","Args",":","query","(","Tensor",")",":","Input","query","with","shape","(","num_query",",","bs",",","embed_dims",")",".","reference_points","(","Tensor",")",":","The","reference","points","of","offset",",","has","shape","(","bs",",","num_query",",","K","*","2",")",".","valid_ratios","(","Tensor",")",":","The","radios","of","valid","points","on","the","feature","map",",","has","shape","(","bs",",","num_levels",",","2",")",".","kpt_branches",":","(","obj",":","`","nn.LayerList","`",")",":","Used","for","refining","the","regression","results",".","Only","would","be","passed","when","`","with_box_refine","`","is","True",",","otherwise","would","be","passed","a","`","None","`",".","Returns",":","tuple","(","Tensor",")",":","Results","with","shape","[","1",",","num_query",",","bs",",","embed_dims","]","when","return_intermediate","is","`","False","`",",","otherwise","it","has","shape","[","num_layers",",","num_query",",","bs",",","embed_dims","]","and","[","num_layers",",","bs",",","num_query",",","K","*","2","]","."]
code: def forward(
      self,
      query,
      *args,
      reference_points=None,
      valid_ratios=None,
      kpt_branches=None,
      **kwargs,
  ):
      """Forward function for `TransformerDecoder`.
  
      Args:
          query (Tensor): Input query with shape (num_query, bs, embed_dims).
          reference_points (Tensor): The reference points of offset,
              has shape (bs, num_query, K*2).
          valid_ratios (Tensor): The radios of valid points on the feature
              map, has shape (bs, num_levels, 2).
          kpt_branches: (obj:`nn.LayerList`): Used for refining the
              regression results. Only would be passed when `with_box_refine`
              is True, otherwise would be passed a `None`.
  
      Returns:
          tuple (Tensor): Results with shape [1, num_query, bs, embed_dims] when
              return_intermediate is `False`, otherwise it has shape
              [num_layers, num_query, bs, embed_dims] and
              [num_layers, bs, num_query, K*2].
      """
      output = query
      intermediate = []
      intermediate_reference_points = []
      for lid, layer in enumerate(self.layers):
          if reference_points.shape[-1] == self.num_keypoints * 2:
              reference_points_input = (
                  reference_points[:, :, None]
                  * valid_ratios.tile((1, 1, self.num_keypoints))[
                      :, None
                  ]
              )
          else:
              assert reference_points.shape[-1] == 2
              reference_points_input = (
                  reference_points[:, :, None] * valid_ratios[:, None]
              )
          output = layer(
              output,
              *args,
              reference_points=reference_points_input,
              **kwargs,
          )
  
          if kpt_branches is not None:
              tmp = kpt_branches[lid](output)
              if reference_points.shape[-1] == self.num_keypoints * 2:
                  new_reference_points = tmp + inverse_sigmoid(
                      reference_points
                  )
                  new_reference_points = F.sigmoid(new_reference_points)
              else:
                  raise NotImplementedError
              reference_points = new_reference_points.detach()
  
          if self.return_intermediate:
              intermediate.append(output)
              intermediate_reference_points.append(reference_points)
  
      if self.return_intermediate:
          return paddle.stack(intermediate), paddle.stack(
              intermediate_reference_points
          )
  
      return output, reference_points
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import math
  import numpy
  import paddle
  import paddle.nn
  import paddle.nn.functional
  from paddle import ParamAttr
  from ppdet.core.workspace import register
  from ..layers import MultiHeadAttention, _convert_attention_mask
  from .utils import _get_clones
  from ..initializer import linear_init_, normal_, constant_, xavier_uniform_
label_desc: Use image node_id 2 for calling the PETR_TransformerDecoder obj's underlying member method code with example usage: obj.forward(query) and returns: output,  reference_points, paddle,  paddle