code: def cal_rel_pos_temporal(
      attn, q, has_cls_embed, q_shape, k_shape, rel_pos_t
  ):
      """
      Temporal 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
      dt = int(2 * max(q_t, k_t) - 1)
      # Intepolate rel pos if needed.
      rel_pos_t = get_rel_pos(rel_pos_t, dt)
  
      # Scale up rel pos if shapes for q and k are different.
      q_t_ratio = max(k_t / q_t, 1.0)
      k_t_ratio = max(q_t / k_t, 1.0)
      dist_t = (
          torch.arange(q_t)[:, None] * q_t_ratio
          - torch.arange(k_t)[None, :] * k_t_ratio
      )
      dist_t += (k_t - 1) * k_t_ratio
      Rt = rel_pos_t[dist_t.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)
      # [B, H, q_t, q_h, q_w, dim] -> [q_t, B, H, q_h, q_w, dim] -> [q_t, B*H*q_h*q_w, dim]
      r_q = r_q.permute(2, 0, 1, 3, 4, 5).reshape(
          q_t, B * n_head * q_h * q_w, dim
      )
  
      # [q_t, B*H*q_h*q_w, dim] * [q_t, dim, k_t] = [q_t, B*H*q_h*q_w, k_t] -> [B*H*q_h*q_w, q_t, k_t]
      rel = torch.matmul(r_q, Rt.transpose(1, 2)).transpose(0, 1)
      # [B*H*q_h*q_w, q_t, k_t] -> [B, H, q_t, q_h, q_w, k_t]
      rel = rel.view(B, n_head, q_h, q_w, q_t, k_t).permute(
          0, 1, 4, 2, 3, 5
      )
  
      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[:, :, :, :, :, :, 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 4 for calling a global function with example usage: cal_rel_pos_temporal(attn, q, has_cls_embed, q_shape, k_shape, rel_pos_t) and returns: attn