code: def get_rel_pos(q_size, k_size, rel_pos):
      """
      Get relative positional embeddings according to the relative positions of query and key sizes.
  
      Args:
          q_size (`int`):
              Size of query q.
          k_size (`int`):
              Size of key k.
          rel_pos (`torch.Tensor`):
              Relative position embeddings (num_embeddings, num_channels).
  
      Returns:
          Extracted positional embeddings according to relative positions.
      """
      max_rel_dist = int(2 * max(q_size, k_size) - 1)
      # Interpolate rel pos if needed.
      if rel_pos.shape[0] != max_rel_dist:
          # Interpolate rel position embeddings.
          rel_pos_resized = nn.functional.interpolate(
              rel_pos.reshape(1, rel_pos.shape[0], -1).permute(0, 2, 1),
              size=max_rel_dist,
              mode="linear",
          )
          rel_pos_resized = rel_pos_resized.reshape(
              -1, max_rel_dist
          ).permute(1, 0)
      else:
          rel_pos_resized = rel_pos
  
      # Scale the coords with short length if shapes for q and k are different.
      q_coords = torch.arange(q_size)[:, None] * max(
          k_size / q_size, 1.0
      )
      k_coords = torch.arange(k_size)[None, :] * max(
          q_size / k_size, 1.0
      )
      relative_coords = (q_coords - k_coords) + (k_size - 1) * max(
          q_size / k_size, 1.0
      )
  
      return rel_pos_resized[relative_coords.long()]
  
imports: import collections.abc
  import math
  from typing import Dict, List, Optional, Tuple, Union
  import torch
  import torch.utils.checkpoint
  from torch import nn
  from ...activations import ACT2FN
  from ...modeling_outputs import BackboneOutput, BaseModelOutput
  from ...modeling_utils import PreTrainedModel
  from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings
  from ...utils.backbone_utils import BackboneMixin
  from .configuration_vitdet import VitDetConfig
label_desc: Use image node_id 1 for calling a global function with example usage: get_rel_pos(q_size, k_size, rel_pos) and returns: rel_pos_resized