code: def test_patch_merging():
      # Test the model with int padding
      in_c = 3
      out_c = 4
      kernel_size = 3
      stride = 3
      padding = 1
      dilation = 1
      bias = False
      # test the case `pad_to_stride` is False
      patch_merge = PatchMerging(
          in_channels=in_c,
          out_channels=out_c,
          kernel_size=kernel_size,
          stride=stride,
          padding=padding,
          dilation=dilation,
          bias=bias,
      )
      B, L, C = 1, 100, 3
      input_size = (10, 10)
      x = torch.rand(B, L, C)
      x_out, out_size = patch_merge(x, input_size)
      assert x_out.size() == (1, 16, 4)
      assert out_size == (4, 4)
      # assert out size is consistent with real output
      assert x_out.size(1) == out_size[0] * out_size[1]
      in_c = 4
      out_c = 5
      kernel_size = 6
      stride = 3
      padding = 2
      dilation = 2
      bias = False
      patch_merge = PatchMerging(
          in_channels=in_c,
          out_channels=out_c,
          kernel_size=kernel_size,
          stride=stride,
          padding=padding,
          dilation=dilation,
          bias=bias,
      )
      B, L, C = 1, 100, 4
      input_size = (10, 10)
      x = torch.rand(B, L, C)
      x_out, out_size = patch_merge(x, input_size)
      assert x_out.size() == (1, 4, 5)
      assert out_size == (2, 2)
      # assert out size is consistent with real output
      assert x_out.size(1) == out_size[0] * out_size[1]
  
      # Test with adaptive padding
      for padding in ("same", "corner"):
          in_c = 2
          out_c = 3
          B = 2
  
          # test stride is 1
          input_size = (5, 5)
          kernel_size = (5, 5)
          stride = (1, 1)
          dilation = 1
          bias = False
          L = input_size[0] * input_size[1]
  
          x = torch.rand(B, L, in_c)
          patch_merge = PatchMerging(
              in_channels=in_c,
              out_channels=out_c,
              kernel_size=kernel_size,
              stride=stride,
              padding=padding,
              dilation=dilation,
              bias=bias,
          )
  
          x_out, out_size = patch_merge(x, input_size)
          assert x_out.size() == (B, 25, 3)
          assert out_size == (5, 5)
          assert x_out.size(1) == out_size[0] * out_size[1]
  
          # test kernel_size == stride
          input_size = (5, 5)
          kernel_size = (5, 5)
          stride = (5, 5)
          dilation = 1
          bias = False
          L = input_size[0] * input_size[1]
  
          x = torch.rand(B, L, in_c)
          patch_merge = PatchMerging(
              in_channels=in_c,
              out_channels=out_c,
              kernel_size=kernel_size,
              stride=stride,
              padding=padding,
              dilation=dilation,
              bias=bias,
          )
  
          x_out, out_size = patch_merge(x, input_size)
          assert x_out.size() == (B, 1, 3)
          assert out_size == (1, 1)
          assert x_out.size(1) == out_size[0] * out_size[1]
  
          # test kernel_size == stride
          input_size = (6, 5)
          kernel_size = (5, 5)
          stride = (5, 5)
          dilation = 1
          bias = False
          L = input_size[0] * input_size[1]
  
          x = torch.rand(B, L, in_c)
          patch_merge = PatchMerging(
              in_channels=in_c,
              out_channels=out_c,
              kernel_size=kernel_size,
              stride=stride,
              padding=padding,
              dilation=dilation,
              bias=bias,
          )
  
          x_out, out_size = patch_merge(x, input_size)
          assert x_out.size() == (B, 2, 3)
          assert out_size == (2, 1)
          assert x_out.size(1) == out_size[0] * out_size[1]
  
          # test different kernel_size with different stride
          input_size = (6, 5)
          kernel_size = (6, 2)
          stride = (6, 2)
          dilation = 1
          bias = False
          L = input_size[0] * input_size[1]
  
          x = torch.rand(B, L, in_c)
          patch_merge = PatchMerging(
              in_channels=in_c,
              out_channels=out_c,
              kernel_size=kernel_size,
              stride=stride,
              padding=padding,
              dilation=dilation,
              bias=bias,
          )
  
          x_out, out_size = patch_merge(x, input_size)
          assert x_out.size() == (B, 3, 3)
          assert out_size == (1, 3)
          assert x_out.size(1) == out_size[0] * out_size[1]
  
file_path: mmdetection/tests/test_models/test_layers/test_transformer.py
imports: import pytest
  import torch
  from mmengine.config import ConfigDict
  from mmdet.models.layers.transformer import AdaptivePadding, DDQTransformerDecoder, DetrTransformerDecoder, DetrTransformerEncoder, PatchEmbed, PatchMerging
label_desc: Use image node_id 3 for calling a global function with example usage: test_patch_merging() without return types