class_docstr: An implementation of Spatial Reduction Attention of PVT.
  
  This module is modified from MultiheadAttention which is a module from
  mmcv.cnn.bricks.transformer.
  
  Args:
      embed_dims (int): The embedding dimension.
      num_heads (int): Parallel attention heads.
      attn_drop (float): A Dropout layer on attn_output_weights.
          Default: 0.0.
      proj_drop (float): A Dropout layer after `nn.MultiheadAttention`.
          Default: 0.0.
      dropout_layer (obj:`ConfigDict`): The dropout_layer used
          when adding the shortcut. Default: None.
      batch_first (bool): Key, Query and Value are shape of
          (batch, n, embed_dim)
          or (n, batch, embed_dim). Default: False.
      qkv_bias (bool): enable bias for qkv if True. Default: True.
      norm_cfg (dict): Config dict for normalization layer.
          Default: dict(type='LN').
      sr_ratio (int): The ratio of spatial reduction of Spatial Reduction
          Attention of PVT. Default: 1.
      init_cfg (obj:`mmengine.ConfigDict`): The Config for initialization.
          Default: None.
class_docstr_tok: ["An","implementation","of","Spatial","Reduction","Attention","of","PVT",".","This","module","is","modified","from","MultiheadAttention","which","is","a","module","from","mmcv.cnn.bricks.transformer",".","Args",":","embed_dims","(","int",")",":","The","embedding","dimension",".","num_heads","(","int",")",":","Parallel","attention","heads",".","attn_drop","(","float",")",":","A","Dropout","layer","on","attn_output_weights",".","Default",":","0.0.","proj_drop","(","float",")",":","A","Dropout","layer","after","`","nn.MultiheadAttention","`",".","Default",":","0.0.","dropout_layer","(","obj",":","`","ConfigDict","`",")",":","The","dropout_layer","used","when","adding","the","shortcut",".","Default",":","None",".","batch_first","(","bool",")",":","Key",",","Query","and","Value","are","shape","of","(","batch",",","n",",","embed_dim",")","or","(","n",",","batch",",","embed_dim",")",".","Default",":","False",".","qkv_bias","(","bool",")",":","enable","bias","for","qkv","if","True",".","Default",":","True",".","norm_cfg","(","dict",")",":","Config","dict","for","normalization","layer",".","Default",":","dict","(","type='LN","'",")",".","sr_ratio","(","int",")",":","The","ratio","of","spatial","reduction","of","Spatial","Reduction","Attention","of","PVT",".","Default",":","1.","init_cfg","(","obj",":","`","mmengine.ConfigDict","`",")",":","The","Config","for","initialization",".","Default",":","None","."]
code: def __init__(
      self,
      embed_dims,
      num_heads,
      attn_drop=0.0,
      proj_drop=0.0,
      dropout_layer=None,
      batch_first=True,
      qkv_bias=True,
      norm_cfg=dict(type="LN"),
      sr_ratio=1,
      init_cfg=None,
  ):
      super().__init__(
          embed_dims,
          num_heads,
          attn_drop,
          proj_drop,
          batch_first=batch_first,
          dropout_layer=dropout_layer,
          bias=qkv_bias,
          init_cfg=init_cfg,
      )
  
      self.sr_ratio = sr_ratio
      if sr_ratio > 1:
          self.sr = Conv2d(
              in_channels=embed_dims,
              out_channels=embed_dims,
              kernel_size=sr_ratio,
              stride=sr_ratio,
          )
          # The ret[0] of build_norm_layer is norm name.
          self.norm = build_norm_layer(norm_cfg, embed_dims)[1]
  
      # handle the BC-breaking from https://github.com/open-mmlab/mmcv/pull/1418 # noqa
      from mmdet import digit_version, mmcv_version
  
      if mmcv_version < digit_version("1.3.17"):
          warnings.warn(
              "The legacy version of forward function in"
              "SpatialReductionAttention is deprecated in"
              "mmcv>=1.3.17 and will no longer support in the"
              "future. Please upgrade your mmcv."
          )
          self.forward = self.legacy_forward
  
imports: import math
  import warnings
  from collections import OrderedDict
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import Conv2d, build_activation_layer, build_norm_layer
  from mmcv.cnn.bricks.drop import build_dropout
  from mmcv.cnn.bricks.transformer import MultiheadAttention
  from mmengine.logging import MMLogger
  from mmengine.model import BaseModule, ModuleList, Sequential, constant_init, normal_init, trunc_normal_init
  from mmengine.model.weight_init import trunc_normal_
  from mmengine.runner.checkpoint import CheckpointLoader, load_state_dict
  from torch.nn.modules.utils import _pair
  from mmdet.registry import MODELS
  from ..layers import PatchEmbed, nchw_to_nlc, nlc_to_nchw
label_desc: Use image node_id 1 to create a new SpatialReductionAttention object from inherited base classes: MultiheadAttention with example: obj = SpatialReductionAttention(embed_dims, num_heads, attn_drop, proj_drop, dropout_layer, batch_first, qkv_bias, norm_cfg, sr_ratio, init_cfg)