class_docstr: Multi-head Attention block with relative position embeddings.
class_docstr_tok: ["Multi-head","Attention","block","with","relative","position","embeddings","."]
code: def forward(self, hidden_state, output_attentions=False):
      batch_size, height, width, _ = hidden_state.shape
      # qkv with shape (3, batch_size, num_heads, height * width, num_channels)
      qkv = (
          self.qkv(hidden_state)
          .reshape(batch_size, height * width, 3, self.num_heads, -1)
          .permute(2, 0, 3, 1, 4)
      )
      # queries, keys and values have shape (batch_size * num_heads, height * width, num_channels)
      queries, keys, values = qkv.reshape(
          3, batch_size * self.num_heads, height * width, -1
      ).unbind(0)
  
      attention_scores = (queries * self.scale) @ keys.transpose(-2, -1)
  
      if self.use_relative_position_embeddings:
          attention_scores = add_decomposed_relative_positions(
              attention_scores,
              queries,
              self.rel_pos_h,
              self.rel_pos_w,
              (height, width),
              (height, width),
          )
  
      attention_probs = attention_scores.softmax(dim=-1)
  
      hidden_state = attention_probs @ values
      hidden_state = hidden_state.view(
          batch_size, self.num_heads, height, width, -1
      )
      hidden_state = hidden_state.permute(0, 2, 3, 1, 4)
      hidden_state = hidden_state.reshape(batch_size, height, width, -1)
      hidden_state = self.proj(hidden_state)
  
      if output_attentions:
          attention_probs = attention_probs.reshape(
              batch_size,
              self.num_heads,
              attention_probs.shape[-2],
              attention_probs.shape[-1],
          )
          outputs = (hidden_state, attention_probs)
      else:
          outputs = (hidden_state,)
  
      return outputs
  
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 2 for calling the VitDetAttention obj's underlying member method code with example usage: obj.forward(hidden_state, output_attentions) and returns: outputs