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_prev_states(
      self,
      encoder_out: Dict[str, torch.Tensor],
      incremental_state: Dict[str, torch.Tensor],
  ) -> None:
      encoder_output = encoder_out["unpacked_output"]
      final_hiddens = encoder_out["final_hiddens"]
      prev_cells = encoder_out["final_cells"]
      if self.averaging_encoder:
          # Use mean encoder hidden states
          prev_hiddens = torch.stack(
              [torch.mean(encoder_output, 0)] * self.num_layers, dim=0
          )
      else:
          # Simply return the final state of each layer
          prev_hiddens = final_hiddens
  
      if self.change_hidden_dim:
          transformed_hiddens: List[torch.Tensor] = []
          transformed_cells: List[torch.Tensor] = []
          i: int = 0
          for hidden_init_fc, cell_init_fc in zip(
              self.hidden_init_fc_list, self.cell_init_fc_list
          ):
              transformed_hiddens.append(
                  hidden_init_fc(prev_hiddens[i])
              )
              transformed_cells.append(cell_init_fc(prev_cells[i]))
              i += 1
          use_hiddens = torch.stack(transformed_hiddens, dim=0)
          use_cells = torch.stack(transformed_cells, dim=0)
      else:
          use_hiddens = prev_hiddens
          use_cells = prev_cells
  
      assert self.attention.context_dim
      initial_attn_context = torch.zeros(
          self.attention.context_dim, device=encoder_output.device
      )
      batch_size = encoder_output.size(1)
  
      self.set_incremental_state(
          incremental_state, "cached_hiddens", use_hiddens
      )
      self.set_incremental_state(
          incremental_state, "cached_cells", use_cells
      )
      self.set_incremental_state(
          incremental_state,
          "cached_feeds",
          initial_attn_context.expand(
              batch_size, self.attention.context_dim
          ),
      )
  
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 6 for calling the RNNDecoderBase obj's underlying member method code with example usage: obj._init_prev_states(encoder_out, incremental_state) without return types