code: def cal_rel_pos_spatial(
      attn, q, k, has_cls_embed, q_shape, k_shape, rel_pos_h, rel_pos_w
  ):
      """
      Decomposed Spatial Relative Positional Embeddings.
      """
      sp_idx = 1 if has_cls_embed else 0
      q_t, q_h, q_w = q_shape
      k_t, k_h, k_w = k_shape
      dh = int(2 * max(q_h, k_h) - 1)
      dw = int(2 * max(q_w, k_w) - 1)
  
      # Scale up rel pos if shapes for q and k are different.
      q_h_ratio = max(k_h / q_h, 1.0)
      k_h_ratio = max(q_h / k_h, 1.0)
      dist_h = (
          torch.arange(q_h)[:, None] * q_h_ratio
          - torch.arange(k_h)[None, :] * k_h_ratio
      )
      dist_h += (k_h - 1) * k_h_ratio
      q_w_ratio = max(k_w / q_w, 1.0)
      k_w_ratio = max(q_w / k_w, 1.0)
      dist_w = (
          torch.arange(q_w)[:, None] * q_w_ratio
          - torch.arange(k_w)[None, :] * k_w_ratio
      )
      dist_w += (k_w - 1) * k_w_ratio
  
      # Intepolate rel pos if needed.
      rel_pos_h = get_rel_pos(rel_pos_h, dh)
      rel_pos_w = get_rel_pos(rel_pos_w, dw)
      Rh = rel_pos_h[dist_h.long()]
      Rw = rel_pos_w[dist_w.long()]
  
      B, n_head, q_N, dim = q.shape
  
      r_q = q[:, :, sp_idx:].reshape(B, n_head, q_t, q_h, q_w, dim)
      rel_h_q = torch.einsum(
          "bythwc,hkc->bythwk", r_q, Rh
      )  # [B, H, q_t, qh, qw, k_h]
      rel_w_q = torch.einsum(
          "bythwc,wkc->bythwk", r_q, Rw
      )  # [B, H, q_t, qh, qw, k_w]
  
      attn[:, :, sp_idx:, sp_idx:] = (
          attn[:, :, sp_idx:, sp_idx:].view(
              B, -1, q_t, q_h, q_w, k_t, k_h, k_w
          )
          + rel_h_q[:, :, :, :, :, None, :, None]
          + rel_w_q[:, :, :, :, :, None, None, :]
      ).view(B, -1, q_t * q_h * q_w, k_t * k_h * k_w)
  
      return attn
  
file_path: SlowFast/slowfast/models/attention.py
imports: import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from torch.nn.init import trunc_normal_
  from slowfast.models.common import DropPath, Mlp
label_desc: Use image node_id 3 for calling a global function with example usage: cal_rel_pos_spatial(attn, q, k, has_cls_embed, q_shape, k_shape, rel_pos_h, rel_pos_w) and returns: attn