class_docstr: Merge patch feature map.
  
  This layer groups feature map by kernel_size, and applies norm and linear
  layers to the grouped feature map. Our implementation uses `nn.Unfold` to
  merge patch, which is about 25% faster than original implementation.
  Instead, we need to modify pretrained models for compatibility.
  
  Args:
      in_channels (int): The num of input channels.
          to gets fully covered by filter and stride you specified..
          Default: True.
      out_channels (int): The num of output channels.
      kernel_size (int | tuple, optional): the kernel size in the unfold
          layer. Defaults to 2.
      stride (int | tuple, optional): the stride of the sliding blocks in the
          unfold layer. Default: None. (Would be set as `kernel_size`)
      padding (int | tuple | string ): The padding length of
          embedding conv. When it is a string, it means the mode
          of adaptive padding, support "same" and "corner" now.
          Default: "corner".
      dilation (int | tuple, optional): dilation parameter in the unfold
          layer. Default: 1.
      bias (bool, optional): Whether to add bias in linear layer or not.
          Defaults: False.
      norm_cfg (dict, optional): Config dict for normalization layer.
          Default: dict(type='LN').
      init_cfg (dict, optional): The extra config for initialization.
          Default: None.
class_docstr_tok: ["Merge","patch","feature","map",".","This","layer","groups","feature","map","by","kernel_size",",","and","applies","norm","and","linear","layers","to","the","grouped","feature","map",".","Our","implementation","uses","`","nn.Unfold","`","to","merge","patch",",","which","is","about","25","%","faster","than","original","implementation",".","Instead",",","we","need","to","modify","pretrained","models","for","compatibility",".","Args",":","in_channels","(","int",")",":","The","num","of","input","channels",".","to","gets","fully","covered","by","filter","and","stride","you","specified","..","Default",":","True",".","out_channels","(","int",")",":","The","num","of","output","channels",".","kernel_size","(","int","|","tuple",",","optional",")",":","the","kernel","size","in","the","unfold","layer",".","Defaults","to","2.","stride","(","int","|","tuple",",","optional",")",":","the","stride","of","the","sliding","blocks","in","the","unfold","layer",".","Default",":","None",".","(","Would","be","set","as","`","kernel_size","`",")","padding","(","int","|","tuple","|","string",")",":","The","padding","length","of","embedding","conv",".","When","it","is","a","string",",","it","means","the","mode","of","adaptive","padding",",","support","``","same","''","and","``","corner","''","now",".","Default",":","``","corner","''",".","dilation","(","int","|","tuple",",","optional",")",":","dilation","parameter","in","the","unfold","layer",".","Default",":","1.","bias","(","bool",",","optional",")",":","Whether","to","add","bias","in","linear","layer","or","not",".","Defaults",":","False",".","norm_cfg","(","dict",",","optional",")",":","Config","dict","for","normalization","layer",".","Default",":","dict","(","type='LN","'",")",".","init_cfg","(","dict",",","optional",")",":","The","extra","config","for","initialization",".","Default",":","None","."]
docstr: Args:
      x (Tensor): Has shape (B, H*W, C_in).
      input_size (tuple[int]): The spatial shape of x, arrange as (H, W).
          Default: None.
  
  Returns:
      tuple: Contains merged results and its spatial shape.
  
          - x (Tensor): Has shape (B, Merged_H * Merged_W, C_out)
          - out_size (tuple[int]): Spatial shape of x, arrange as
              (Merged_H, Merged_W).
code: def forward(self, x, input_size):
      """
      Args:
          x (Tensor): Has shape (B, H*W, C_in).
          input_size (tuple[int]): The spatial shape of x, arrange as (H, W).
              Default: None.
  
      Returns:
          tuple: Contains merged results and its spatial shape.
  
              - x (Tensor): Has shape (B, Merged_H * Merged_W, C_out)
              - out_size (tuple[int]): Spatial shape of x, arrange as
                  (Merged_H, Merged_W).
      """
      B, L, C = x.shape
      assert isinstance(input_size, Sequence), (
          f"Expect "
          f"input_size is "
          f"`Sequence` "
          f"but get {input_size}"
      )
  
      H, W = input_size
      assert L == H * W, "input feature has wrong size"
  
      x = x.view(B, H, W, C).permute([0, 3, 1, 2])  # B, C, H, W
      # Use nn.Unfold to merge patch. About 25% faster than original method,
      # but need to modify pretrained model for compatibility
  
      if self.adap_padding:
          x = self.adap_padding(x)
          H, W = x.shape[-2:]
  
      x = self.sampler(x)
      # if kernel_size=2 and stride=2, x should has shape (B, 4*C, H/2*W/2)
  
      out_h = (
          H
          + 2 * self.sampler.padding[0]
          - self.sampler.dilation[0] * (self.sampler.kernel_size[0] - 1)
          - 1
      ) // self.sampler.stride[0] + 1
      out_w = (
          W
          + 2 * self.sampler.padding[1]
          - self.sampler.dilation[1] * (self.sampler.kernel_size[1] - 1)
          - 1
      ) // self.sampler.stride[1] + 1
  
      output_size = (out_h, out_w)
      x = x.transpose(1, 2)  # B, H/2*W/2, 4*C
      x = self.norm(x) if self.norm else x
      x = self.reduction(x)
      return x, output_size
  
imports: import math
  import warnings
  from typing import Sequence
  import torch
  import torch.nn
  import torch.nn.functional
  import torch.utils.checkpoint
  from mmcv.cnn import Linear, build_activation_layer, build_norm_layer, xavier_init
  from mmcv.cnn.bricks.drop import build_dropout
  from mmcv.cnn.bricks.registry import FEEDFORWARD_NETWORK, TRANSFORMER_LAYER, TRANSFORMER_LAYER_SEQUENCE
  from mmcv.cnn.bricks.transformer import BaseTransformerLayer, TransformerLayerSequence, build_transformer_layer_sequence
  from mmcv.runner.base_module import BaseModule, Sequential
  from mmcv.utils import deprecated_api_warning, to_2tuple
  from torch.nn.init import normal_
  from ..builder import TRANSFORMER
label_desc: Use image node_id 2 for calling the PatchMerging obj's underlying member method code with example usage: obj.forward(x, input_size) and returns: x,  output_size