class_docstr: Window based multi-head self attention (W-MSA) module with relative position bias.
  It supports both of shifted and non-shifted window.
  
  Args:
      dim (int): Number of input channels.
      window_size (tuple[int]): The height and width of the window.
      num_heads (int): Number of attention heads.
      qkv_bias (bool, optional):  If True, add a learnable bias to query, key, value. Default: True
      attn_drop_ratio (float, optional): Dropout ratio of attention weight. Default: 0.0
      proj_drop_ratio (float, optional): Dropout ratio of output. Default: 0.0
class_docstr_tok: ["Window","based","multi-head","self","attention","(","W-MSA",")","module","with","relative","position","bias",".","It","supports","both","of","shifted","and","non-shifted","window",".","Args",":","dim","(","int",")",":","Number","of","input","channels",".","window_size","(","tuple","[","int","]",")",":","The","height","and","width","of","the","window",".","num_heads","(","int",")",":","Number","of","attention","heads",".","qkv_bias","(","bool",",","optional",")",":","If","True",",","add","a","learnable","bias","to","query",",","key",",","value",".","Default",":","True","attn_drop_ratio","(","float",",","optional",")",":","Dropout","ratio","of","attention","weight",".","Default",":","0.0","proj_drop_ratio","(","float",",","optional",")",":","Dropout","ratio","of","output",".","Default",":","0.0"]
code: def build(self, input_shape):
      # define a parameter table of relative position bias
      # [2*Mh-1 * 2*Mw-1, nH]
      self.relative_position_bias_table = self.add_weight(
          shape=[
              (2 * self.window_size[0] - 1)
              * (2 * self.window_size[1] - 1),
              self.num_heads,
          ],
          initializer=initializers.TruncatedNormal(stddev=0.02),
          trainable=True,
          dtype=tf.float32,
          name="relative_position_bias_table",
      )
  
      coords_h = np.arange(self.window_size[0])
      coords_w = np.arange(self.window_size[1])
      coords = np.stack(
          np.meshgrid(coords_h, coords_w, indexing="ij")
      )  # [2, Mh, Mw]
      coords_flatten = np.reshape(coords, [2, -1])  # [2, Mh*Mw]
      # [2, Mh*Mw, 1] - [2, 1, Mh*Mw]
      relative_coords = (
          coords_flatten[:, :, None] - coords_flatten[:, None, :]
      )  # [2, Mh*Mw, Mh*Mw]
      relative_coords = np.transpose(
          relative_coords, [1, 2, 0]
      )  # [Mh*Mw, Mh*Mw, 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
      )  # [Mh*Mw, Mh*Mw]
  
      self.relative_position_index = tf.Variable(
          tf.convert_to_tensor(relative_position_index),
          trainable=False,
          dtype=tf.int64,
          name="relative_position_index",
      )
  
imports: import tensorflow
  from tensorflow.keras import Model, layers, initializers
  import numpy
label_desc: Use image node_id 2 for calling the WindowAttention obj's underlying member method code with example usage: obj.build(input_shape) without return types