code: def resize_rel_pos_bias_table(
      rel_pos_bias,
      new_window_size: Tuple[int, int],
      new_bias_shape: Tuple[int, ...],
  ):
      """Resize relative position bias table using more advanced interpolation.
  
      Modified from code in Microsoft Unilm (https://github.com/microsoft/unilm) repo (BeiT, BeiT-v2, etc).
  
      https://github.com/microsoft/unilm/blob/5255d52de86dad642810f5849dd357769346c1d7/beit/run_class_finetuning.py#L351
  
      Args:
          rel_pos_bias:
          new_window_size:
          new_bias_shape:
  
      Returns:
  
      """
      if _USE_SCIPY:
          from scipy import interpolate
  
      dst_size = (
          new_window_size[0] * 2 - 1,
          new_window_size[1] * 2 - 1,
      )
      if rel_pos_bias.ndim == 3:
          # TF maxvit style (num_heads, H, W) bias shape, no extra tokens currently supported
          num_extra_tokens = 0
          _, dst_h, dst_w = new_bias_shape
          assert dst_h == dst_size[0] and dst_w == dst_size[1]
          num_attn_heads, src_h, src_w = rel_pos_bias.shape
          src_size = (src_h, src_w)
          has_flat_shape = False
      else:
          assert rel_pos_bias.ndim == 2
          # (num_pos, num_heads) (aka flat) bias shape
          dst_num_pos, _ = new_bias_shape
          src_num_pos, num_attn_heads = rel_pos_bias.shape
          num_extra_tokens = dst_num_pos - (dst_size[0] * dst_size[1])
          src_size = int((src_num_pos - num_extra_tokens) ** 0.5)
          src_size = (src_size, src_size)
          has_flat_shape = True
  
      if src_size[0] != dst_size[0] or src_size[1] != dst_size[1]:
          # print("Interpolating position from %dx%d to %dx%d" % (src_size[0], src_size[1], dst_size[0], dst_size[1]))
          if num_extra_tokens:
              extra_tokens = rel_pos_bias[-num_extra_tokens:, :]
              rel_pos_bias = rel_pos_bias[:-num_extra_tokens, :]
          else:
              extra_tokens = None
  
          def geometric_progression(a, r, n):
              return a * (1.0 - r**n) / (1.0 - r)
  
          def _calc(src, dst):
              left, right = 1.01, 1.5
              while right - left > 1e-6:
                  q = (left + right) / 2.0
                  gp = geometric_progression(1, q, src // 2)
                  if gp > dst // 2:
                      right = q
                  else:
                      left = q
  
              dis = []
              cur = 1
              for i in range(src // 2):
                  dis.append(cur)
                  cur += q ** (i + 1)
              r_ids = [-_ for _ in reversed(dis)]
              return r_ids + [0] + dis
  
          y = _calc(src_size[0], dst_size[0])
          x = _calc(src_size[1], dst_size[1])
          yx = [torch.tensor(y), torch.tensor(x)]
          # print("Original positions = %s" % str(x))
  
          ty = dst_size[0] // 2.0
          tx = dst_size[1] // 2.0
          dy = torch.arange(-ty, ty + 0.1, 1.0)
          dx = torch.arange(-tx, tx + 0.1, 1.0)
          dyx = torch.meshgrid([dy, dx])
          # print("Target positions = %s" % str(dx))
  
          all_rel_pos_bias = []
          for i in range(num_attn_heads):
              if has_flat_shape:
                  z = (
                      rel_pos_bias[:, i]
                      .view(src_size[0], src_size[1])
                      .float()
                  )
              else:
                  z = rel_pos_bias[i, :, :].float()
  
              if _USE_SCIPY:
                  # Original beit code uses scipy w/ cubic interpolation
                  f = interpolate.interp2d(
                      x, y, z.numpy(), kind="cubic"
                  )
                  r = (
                      torch.Tensor(f(dx, dy))
                      .contiguous()
                      .to(rel_pos_bias.device)
                  )
              else:
                  # Without scipy dependency, I've found a reasonably simple impl
                  # that supports uneven spaced interpolation pts with 'linear' interp.
                  # Results are comparable to scipy for model accuracy in most cases.
                  f = RegularGridInterpolator(yx, z)
                  r = f(dyx).contiguous().to(rel_pos_bias.device)
  
              if has_flat_shape:
                  r = r.view(-1, 1)
              all_rel_pos_bias.append(r)
  
          if has_flat_shape:
              rel_pos_bias = torch.cat(all_rel_pos_bias, dim=-1)
          else:
              rel_pos_bias = torch.cat(all_rel_pos_bias, dim=0)
  
          if extra_tokens is not None:
              assert has_flat_shape
              rel_pos_bias = torch.cat(
                  (rel_pos_bias, extra_tokens), dim=0
              )
  
      return rel_pos_bias
  
imports: import math
  import os
  from typing import Optional, Tuple
  import torch
  import torch.nn
  import torch.nn.functional
  from .interpolate import RegularGridInterpolator
  from .mlp import Mlp
  from .weight_init import trunc_normal_
label_desc: Use image node_id 4 for calling a global function with example usage: resize_rel_pos_bias_table(rel_pos_bias, new_window_size, new_bias_shape) and returns: rel_pos_bias, unknown, unknown