class_docstr: Multi-headed attention from 'Attention Is All You Need' paper.
code: def __init__(
      self,
      config,
      embed_dim: int,
      num_heads: int,
      kdim: int = None,
      vdim: int = None,
      dropout: float = 0.0,
      is_decoder: bool = False,
      bias: bool = True,
      is_cross_attention: bool = False,
  ):
      super().__init__()
      self.embed_dim = embed_dim
      self.kdim = kdim if kdim is not None else embed_dim
      self.vdim = vdim if vdim is not None else embed_dim
      self.num_heads = num_heads
      self.dropout = dropout
      self.head_dim = embed_dim // num_heads
      if not (self.head_dim * num_heads == self.embed_dim):
          raise ValueError(
              f"embed_dim must be divisible by num_heads (got `embed_dim`: {self.embed_dim} and `num_heads`:"
              f" {num_heads})."
          )
      self.scaling = self.head_dim**-0.5
      self.is_decoder = is_decoder
  
      self.k_proj = nn.Linear(self.kdim, embed_dim, bias=bias)
      self.v_proj = nn.Linear(self.vdim, embed_dim, bias=bias)
      self.q_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
  
      self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
  
imports: import copy
  import math
  import random
  from typing import Optional, Tuple
  import torch
  from torch import nn
  from torch.nn import CrossEntropyLoss
  from ...activations import ACT2FN
  from ...modeling_outputs import BaseModelOutputWithPastAndCrossAttentions, CausalLMOutputWithCrossAttentions
  from ...modeling_utils import PreTrainedModel
  from ...utils import add_start_docstrings, logging, replace_return_docstrings
  from .configuration_trocr import TrOCRConfig
label_desc: Use image node_id 1 to create a new TrOCRAttention object from inherited base classes: nn with example: obj = TrOCRAttention(config, embed_dim, num_heads, kdim, vdim, dropout, is_decoder, bias, is_cross_attention)