code: def resize_last_dim_to_most(layer_matrix, last_dim_size):
      if layer_matrix.shape[-1] == last_dim_size:
          return layer_matrix
      else:
          cls_token, attn = layer_matrix[..., 1], layer_matrix[..., 1:]
          if attn.shape[-1] > last_dim_size - 1:
              assert attn.shape[-1] % (last_dim_size - 1) == 0
          else:
              assert (last_dim_size - 1) % attn.shape[-1] == 0
          factor = (last_dim_size - 1) / attn.shape[-1]
          attn = torch.nn.functional.interpolate(
              attn, size=last_dim_size - 1, mode="nearest"
          )
          attn = attn * factor
          return torch.cat([cls_token.unsqueeze(dim=-1), attn], dim=-1)
  
imports: import math
  from functools import partial
  from typing import Tuple, List
  import numpy
  import torch
  from torch import nn
  from torch.nn.init import trunc_normal_
  from einops import rearrange
  from towhee.models.layers.layers_with_relprop import GELU, Linear, Dropout, Conv2d, Conv3d, Softmax, Sigmoid, LayerNorm, Einsum, Add, Clone, MaxPool3d, IndexSelect
label_desc: Use image node_id 2 for calling a global function with example usage: resize_last_dim_to_most(layer_matrix, last_dim_size) and returns: layer_matrix, torch