class_docstr: See :func:`shifted_window_attention`.
class_docstr_tok: ["See",":","func",":","`","shifted_window_attention","`","."]
code: def define_relative_position_index(self):
      # get pair-wise relative position index for each token inside the window
      coords_h = torch.arange(self.window_size[0])
      coords_w = torch.arange(self.window_size[1])
      coords = torch.stack(
          torch.meshgrid(coords_h, coords_w, indexing="ij")
      )  # 2, Wh, Ww
      coords_flatten = torch.flatten(coords, 1)  # 2, Wh*Ww
      relative_coords = (
          coords_flatten[:, :, None] - coords_flatten[:, None, :]
      )  # 2, Wh*Ww, Wh*Ww
      relative_coords = relative_coords.permute(
          1, 2, 0
      ).contiguous()  # Wh*Ww, Wh*Ww, 2
      relative_coords[:, :, 0] += (
          self.window_size[0] - 1
      )  # shift to start from 0
      relative_coords[:, :, 1] += self.window_size[1] - 1
      relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1
      relative_position_index = relative_coords.sum(
          -1
      ).flatten()  # Wh*Ww*Wh*Ww
      self.register_buffer(
          "relative_position_index", relative_position_index
      )
  
imports: import math
  from functools import partial
  from typing import Any, Callable, List, Optional
  import torch
  import torch.nn.functional
  from torch import nn, Tensor
  from ..ops.misc import MLP, Permute
  from ..ops.stochastic_depth import StochasticDepth
  from ..transforms._presets import ImageClassification, InterpolationMode
  from ..utils import _log_api_usage_once
  from ._api import register_model, Weights, WeightsEnum
  from ._meta import _IMAGENET_CATEGORIES
  from ._utils import _ovewrite_named_param, handle_legacy_interface
label_desc: Use image node_id 3 for calling the ShiftedWindowAttention obj's underlying member method code with example usage: obj.define_relative_position_index() without return types