code: def __init__(
      self,
      dim,
      key_dim,
      num_heads=8,
      attn_ratio=4,
      activation=None,
      resolution=14,
  ):
      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.d = int(attn_ratio * key_dim)
      self.dh = int(attn_ratio * key_dim) * num_heads
      self.attn_ratio = attn_ratio
      self.h = self.dh + nh_kd * 2
      self.qkv = Linear_BN(dim, self.h)
      self.proj = nn.Sequential(
          activation(), Linear_BN(self.dh, dim, bn_weight_init=0)
      )
      points = list(
          itertools.product(range(resolution), range(resolution))
      )
      N = len(points)
      attention_offsets = {}
      idxs = []
      for p1 in points:
          for p2 in points:
              offset = (abs(p1[0] - p2[0]), abs(p1[1] - p2[1]))
              if offset not in attention_offsets:
                  attention_offsets[offset] = len(attention_offsets)
              idxs.append(attention_offsets[offset])
      self.attention_biases = self.create_parameter(
          shape=(num_heads, len(attention_offsets)),
          default_initializer=zeros_,
          attr=paddle.ParamAttr(regularizer=L2Decay(0.0)),
      )
      tensor_idxs = paddle.to_tensor(idxs, dtype="int64")
      self.register_buffer(
          "attention_bias_idxs", paddle.reshape(tensor_idxs, [N, N])
      )
  
imports: import itertools
  import math
  import warnings
  import paddle
  import paddle.nn
  import paddle.nn.functional
  from paddle.nn.initializer import Constant
  from paddle.nn.initializer import TruncatedNormal
  from paddle.regularizer import L2Decay
  from .vision_transformer import Identity
  from .vision_transformer import ones_
  from .vision_transformer import trunc_normal_
  from .vision_transformer import zeros_
label_desc: Use image node_id 1 to create a new Attention object from inherited base classes: nn with example: obj = Attention(dim, key_dim, num_heads, attn_ratio, activation, resolution)