class_docstr: Efficient self-attention mechanism with reduction of the sequence [PvT paper](https://arxiv.org/abs/2102.12122).
class_docstr_tok: ["Efficient","self-attention","mechanism","with","reduction","of","the","sequence","[","PvT","paper","]","(","https",":","\/\/arxiv.org\/abs\/2102.12122",")","."]
code: def forward(
      self,
      hidden_states: torch.Tensor,
      height: int,
      width: int,
      output_attentions: bool = False,
  ) -> Tuple[torch.Tensor]:
      query_layer = self.transpose_for_scores(self.query(hidden_states))
  
      if self.sequences_reduction_ratio > 1:
          batch_size, seq_len, num_channels = hidden_states.shape
          # Reshape to (batch_size, num_channels, height, width)
          hidden_states = hidden_states.permute(0, 2, 1).reshape(
              batch_size, num_channels, height, width
          )
          # Apply sequence reduction
          hidden_states = self.sequence_reduction(hidden_states)
          # Reshape back to (batch_size, seq_len, num_channels)
          hidden_states = hidden_states.reshape(
              batch_size, num_channels, -1
          ).permute(0, 2, 1)
          hidden_states = self.layer_norm(hidden_states)
  
      key_layer = self.transpose_for_scores(self.key(hidden_states))
      value_layer = self.transpose_for_scores(self.value(hidden_states))
  
      # Take the dot product between "query" and "key" to get the raw attention scores.
      attention_scores = torch.matmul(
          query_layer, key_layer.transpose(-1, -2)
      )
  
      attention_scores = attention_scores / math.sqrt(
          self.attention_head_size
      )
  
      # Normalize the attention scores to probabilities.
      attention_probs = nn.functional.softmax(attention_scores, dim=-1)
  
      # This is actually dropping out entire tokens to attend to, which might
      # seem a bit unusual, but is taken from the original Transformer paper.
      attention_probs = self.dropout(attention_probs)
  
      context_layer = torch.matmul(attention_probs, value_layer)
  
      context_layer = context_layer.permute(0, 2, 1, 3).contiguous()
      new_context_layer_shape = context_layer.size()[:-2] + (
          self.all_head_size,
      )
      context_layer = context_layer.view(new_context_layer_shape)
  
      outputs = (
          (context_layer, attention_probs)
          if output_attentions
          else (context_layer,)
      )
  
      return outputs
  
imports: import collections
  import math
  from typing import Iterable, Optional, Tuple, Union
  import torch
  import torch.nn.functional
  import torch.utils.checkpoint
  from torch import nn
  from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
  from ...activations import ACT2FN
  from ...modeling_outputs import BaseModelOutput, ImageClassifierOutput
  from ...modeling_utils import PreTrainedModel
  from ...pytorch_utils import find_pruneable_heads_and_indices, prune_linear_layer
  from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
  from .configuration_pvt import PvtConfig
label_desc: Use image node_id 3 for calling the PvtEfficientSelfAttention obj's underlying member method code with example usage: obj.forward(hidden_states, height, width, output_attentions) and returns: outputs