code: def __init__(
      self,
      dim=384,
      key_dim=16,
      num_heads=8,
      attn_ratio=4,
      resolution=7,
      out_dim=None,
      act_layer=None,
  ):
      super().__init__()
      self.num_heads = num_heads
      self.scale = key_dim**-0.5
      self.key_dim = key_dim
      self.nh_kd = nh_kd = key_dim * num_heads
  
      self.resolution = resolution
  
      self.d = int(attn_ratio * key_dim)
      self.dh = int(attn_ratio * key_dim) * num_heads
      self.attn_ratio = attn_ratio
  
      if out_dim is not None:
          self.out_dim = out_dim
      else:
          self.out_dim = dim
  
      self.resolution2 = math.ceil(self.resolution / 2)
      self.q = LGQuery(
          dim,
          self.num_heads * self.key_dim,
          self.resolution,
          self.resolution2,
      )
  
      self.N = self.resolution**2
      self.N2 = self.resolution2**2
  
      self.k = nn.Sequential(
          nn.Conv2D(dim, self.num_heads * self.key_dim, 1),
          nn.BatchNorm2D(self.num_heads * self.key_dim),
      )
      self.v = nn.Sequential(
          nn.Conv2D(dim, self.num_heads * self.d, 1),
          nn.BatchNorm2D(self.num_heads * self.d),
      )
      self.v_local = nn.Sequential(
          nn.Conv2D(
              self.num_heads * self.d,
              self.num_heads * self.d,
              kernel_size=3,
              stride=2,
              padding=1,
              groups=self.num_heads * self.d,
          ),
          nn.BatchNorm2D(self.num_heads * self.d),
      )
  
      self.proj = nn.Sequential(
          act_layer(),
          nn.Conv2D(self.dh, self.out_dim, 1),
          nn.BatchNorm2D(self.out_dim),
      )
  
      points = list(
          itertools.product(
              range(self.resolution), range(self.resolution)
          )
      )
      points_ = list(
          itertools.product(
              range(self.resolution2), range(self.resolution2)
          )
      )
      N = len(points)
      N_ = len(points_)
      attention_offsets = {}
      idxs = []
      for p1 in points_:
          for p2 in points:
              size = 1
              offset = (
                  abs(
                      p1[0]
                      * math.ceil(self.resolution / self.resolution2)
                      - p2[0]
                      + (size - 1) / 2
                  ),
                  abs(
                      p1[1]
                      * math.ceil(self.resolution / self.resolution2)
                      - p2[1]
                      + (size - 1) / 2
                  ),
              )
              if offset not in attention_offsets:
                  attention_offsets[offset] = len(attention_offsets)
              idxs.append(attention_offsets[offset])
  
      self.register_buffer(
          "attention_biases", paddle.zeros([num_heads, 196])
      )
      self.register_buffer(
          "attention_bias_idxs",
          paddle.ones([49, 196], dtype=paddle.int64),
      )
  
      self.attention_biases_seg = self.create_parameter(
          shape=[num_heads, len(attention_offsets)],
          default_initializer=zeros_,
      )
  
      self.register_buffer(
          "attention_bias_idxs_seg",
          paddle.to_tensor(idxs, dtype=paddle.int64).reshape([N_, N]),
      )
  
imports: import math
  import itertools
  import paddle
  import paddle.nn
  import paddle.nn.functional
  from paddleseg.utils import utils
  from paddleseg.cvlibs import manager, param_init
  from paddleseg.models.backbones.transformer_utils import *
label_desc: Use image node_id 1 to create a new Attention4DDownsample object from inherited base classes: nn with example: obj = Attention4DDownsample(dim, key_dim, num_heads, attn_ratio, resolution, out_dim, act_layer)