code: def add_decomposed_rel_pos(
      attn, q, rel_pos_h, rel_pos_w, q_size, k_size
  ):
      """
      Args:
          attn (Tensor): attention map.
          q (Tensor):
              query q in the attention layer with shape (B, q_h * q_w, C).
          rel_pos_h (Tensor):
              relative position embeddings (Lh, C) for height axis.
          rel_pos_w (Tensor):
              relative position embeddings (Lw, C) for width axis.
          q_size (Tuple):
              spatial sequence size of query q with (q_h, q_w).
          k_size (Tuple):
              spatial sequence size of key k with (k_h, k_w).
  
      Returns:
          attn (Tensor): attention map with added relative positional embeddings.
      """
      q_h, q_w = q_size
      k_h, k_w = k_size
      Rh = get_rel_pos(q_h, k_h, rel_pos_h)
      Rw = get_rel_pos(q_w, k_w, rel_pos_w)
  
      B, _, dim = q.shape
      r_q = q.reshape(B, q_h, q_w, dim)
      rel_h = torch.einsum("bhwc,hkc->bhwk", r_q, Rh)
      rel_w = torch.einsum("bhwc,wkc->bhwk", r_q, Rw)
  
      attn = (
          attn.view(B, q_h, q_w, k_h, k_w)
          + rel_h[:, :, :, :, None]
          + rel_w[:, :, :, None, :]
      ).view(B, q_h * q_w, k_h * k_w)
  
      return attn
  
imports: import math
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import build_activation_layer, build_norm_layer
  from mmcv.cnn.bricks import DropPath
  from mmengine.logging import MMLogger
  from mmengine.model import BaseModule
  from mmengine.runner.checkpoint import CheckpointLoader
  from mmdet.registry import MODELS
label_desc: Use image node_id 3 for calling a global function with example usage: add_decomposed_rel_pos(attn, q, rel_pos_h, rel_pos_w, q_size, k_size) and returns: attn