class_docstr: RNN decoder with multihead attention. Attention is calculated using encoder
  output and output of decoder's first RNN layerself. Attention is applied
  after first RNN layer and concatenated to input of subsequent layers.
class_docstr_tok: ["RNN","decoder","with","multihead","attention",".","Attention","is","calculated","using","encoder","output","and","output","of","decoder","'s","first","RNN","layerself",".","Attention","is","applied","after","first","RNN","layer","and","concatenated","to","input","of","subsequent","layers","."]
code: def __init__(
      self,
      embed_tokens,
      encoder_hidden_dim,
      embed_dim,
      hidden_dim,
      out_embed_dim,
      cell_type,
      num_layers,
      dropout_in,
      dropout_out,
      attention_type,
      attention_heads,
      first_layer_attention,
      averaging_encoder,
  ):
      encoder_hidden_dim = max(1, encoder_hidden_dim)
      self.encoder_hidden_dim = encoder_hidden_dim
      self.embed_dim = embed_dim
      self.hidden_dim = hidden_dim
      self.out_embed_dim = out_embed_dim
      self.dropout_in = dropout_in
      self.dropout_out = dropout_out
      self.attention_type = attention_type
      self.attention_heads = attention_heads
      self.first_layer_attention = first_layer_attention
      self.embed_tokens = embed_tokens
  
      self.hidden_dim = hidden_dim
      self.averaging_encoder = averaging_encoder
  
      if cell_type == "lstm":
          cell_class = torch.nn.LSTMCell
      else:
          raise RuntimeError("Cell type not supported")
  
      self.change_hidden_dim = hidden_dim != encoder_hidden_dim
      if self.change_hidden_dim:
          hidden_init_fc_list = []
          cell_init_fc_list = []
          for _ in range(num_layers):
              hidden_init_fc_list.append(
                  nn.Linear(encoder_hidden_dim, hidden_dim)
              )
              cell_init_fc_list.append(
                  nn.Linear(encoder_hidden_dim, hidden_dim)
              )
          self.hidden_init_fc_list = nn.ModuleList(hidden_init_fc_list)
          self.cell_init_fc_list = nn.ModuleList(cell_init_fc_list)
      else:
          # Empty module lists to appease Torchscript
          self.hidden_init_fc_list = nn.ModuleList([])
          self.cell_init_fc_list = nn.ModuleList([])
  
      if attention_type == "dot":
          self.attention = DotAttention(
              decoder_hidden_state_dim=hidden_dim,
              context_dim=encoder_hidden_dim,
          )
      else:
          raise RuntimeError(
              f"Attention type {attention_type} not supported"
          )
  
      self.combined_output_and_context_dim = (
          self.attention.context_dim + hidden_dim
      )
  
      layers = []
      for layer in range(num_layers):
          if layer == 0:
              cell_input_dim = embed_dim
          else:
              cell_input_dim = hidden_dim
  
          # attention applied to first layer always.
          if self.first_layer_attention or layer == 0:
              cell_input_dim += self.attention.context_dim
          layers.append(
              cell_class(
                  input_size=cell_input_dim, hidden_size=hidden_dim
              )
          )
      self.layers = nn.ModuleList(layers)
      self.num_layers = len(layers)
  
      if self.combined_output_and_context_dim != out_embed_dim:
          self.additional_fc = nn.Linear(
              self.combined_output_and_context_dim, out_embed_dim
          )
      else:
          # Using identity layer in place of the bottleneck simplifies torchscript
          # compatibility.
          self.additional_fc = PlaceholderIdentity()
      log_class_usage(__class__)
  
imports: from typing import Dict, List, Optional, Tuple
  import torch
  import torch.nn.functional
  from fairseq import utils
  from pytext.config import ConfigBase
  from pytext.models.seq_models.base import PyTextSeq2SeqModule
  from pytext.utils.usage import log_class_usage
  from torch import nn
  from .attention import DotAttention
  from .base import PlaceholderIdentity, PyTextIncrementalDecoderComponent
label_desc: Use image node_id 2 to create a new RNNDecoderBase object from inherited base classes: PyTextIncrementalDecoderComponent with example: obj = RNNDecoderBase(embed_tokens, encoder_hidden_dim, embed_dim, hidden_dim, out_embed_dim, cell_type, num_layers, dropout_in, dropout_out, attention_type, attention_heads, first_layer_attention, averaging_encoder)