class_docstr: Second phase of decoder layer block
  This layer will take the input from the ecoder and phirst pass decoder.
  papers.nips.cc/paper/6775-deliberation-networks-sequence-generation-beyond-one-pass-decoding.pdf
docstr: Args:
      x (Tensor): input to the layer of shape `(seq_len, batch, embed_dim)`
      encoder_padding_mask (ByteTensor): binary ByteTensor of shape
          `(batch, src_len)` where padding elements are indicated by ``1``.
  
  Returns:
      encoded output of shape `(batch, src_len, embed_dim)`
docstr_tok: ["Args",":","x","(","Tensor",")",":","input","to","the","layer","of","shape","`","(","seq_len",",","batch",",","embed_dim",")","`","encoder_padding_mask","(","ByteTensor",")",":","binary","ByteTensor","of","shape","`","(","batch",",","src_len",")","`","where","padding","elements","are","indicated","by","``","1","``",".","Returns",":","encoded","output","of","shape","`","(","batch",",","src_len",",","embed_dim",")","`"]
code: def forward(
      self,
      x,
      encoder_out=None,
      encoder_padding_mask=None,
      decoder_out=None,
      incremental_state=None,
      prev_self_attn_state=None,
      prev_encoder_attn_state=None,
      prev_decoder_attn_state=None,
      self_attn_mask=None,
      self_attn_padding_mask=None,
  ):
      """
      Args:
          x (Tensor): input to the layer of shape `(seq_len, batch, embed_dim)`
          encoder_padding_mask (ByteTensor): binary ByteTensor of shape
              `(batch, src_len)` where padding elements are indicated by ``1``.
  
      Returns:
          encoded output of shape `(batch, src_len, embed_dim)`
      """
      residual = x
      x_self_attention = self.maybe_layer_norm(
          self.self_attn_layer_norm, x, before=True
      )
      if prev_self_attn_state is not None:
          if incremental_state is None:
              incremental_state = {}
          prev_key, prev_value = prev_self_attn_state
          saved_state = {"prev_key": prev_key, "prev_value": prev_value}
          self.self_attn._set_input_buffer(
              incremental_state, saved_state
          )
      x_self_attention, attn = self.self_attn(
          query=x,
          key=x,
          value=x,
          key_padding_mask=self_attn_padding_mask,
          incremental_state=incremental_state,
          need_weights=False,
          attn_mask=self_attn_mask,
      )
      x_self_attention = F.dropout(
          x_self_attention, p=self.dropout, training=self.training
      )
      x_self_attention = residual + x_self_attention
      x_self_attention = self.maybe_layer_norm(
          self.self_attn_layer_norm, x_self_attention, after=True
      )
  
      if self.encoder_attn is not None:
          residual = x
          x_encoder_attention = self.maybe_layer_norm(
              self.encoder_attn_layer_norm, x, before=True
          )
          if prev_encoder_attn_state is not None:
              if incremental_state is None:
                  incremental_state = {}
              prev_key, prev_value = prev_encoder_attn_state
              saved_state = {
                  "prev_key": prev_key,
                  "prev_value": prev_value,
              }
              self.encoder_attn._set_input_buffer(
                  incremental_state, saved_state
              )
          x_encoder_attention, attn = self.encoder_attn(
              query=x_encoder_attention,
              key=encoder_out,
              value=encoder_out,
              key_padding_mask=encoder_padding_mask,
              incremental_state=incremental_state,
              static_kv=True,
              need_weights=(not self.training and self.need_attn),
          )
          x_encoder_attention = F.dropout(
              x_encoder_attention,
              p=self.dropout,
              training=self.training,
          )
          x_encoder_attention = residual + x_encoder_attention
          x_encoder_attention = self.maybe_layer_norm(
              self.encoder_attn_layer_norm,
              x_encoder_attention,
              after=True,
          )
  
      if self.decoder_attn is not None:
          residual = x
          x_decoder_attention = self.maybe_layer_norm(
              self.decoder_attn_layer_norm, x, before=True
          )
          if prev_decoder_attn_state is not None:
              if incremental_state is None:
                  incremental_state = {}
              prev_key, prev_value = prev_decoder_attn_state
              saved_state = {
                  "prev_key": prev_key,
                  "prev_value": prev_value,
              }
              self.encoder_attn._set_input_buffer(
                  incremental_state, saved_state
              )
          x_decoder_attention, attn = self.decoder_attn(
              query=x_decoder_attention,
              key=decoder_out,
              value=decoder_out,
              incremental_state=incremental_state,
              static_kv=True,
              need_weights=(not self.training and self.need_attn),
          )
          x_decoder_attention = F.dropout(
              x_decoder_attention,
              p=self.dropout,
              training=self.training,
          )
          x_decoder_attention = residual + x_decoder_attention
          x_decoder_attention = self.maybe_layer_norm(
              self.encoder_attn_layer_norm,
              x_decoder_attention,
              after=True,
          )
      x = x_self_attention + x_encoder_attention + x_decoder_attention
  
      residual = x
      x = self.maybe_layer_norm(self.final_layer_norm, x, before=True)
      x = self.activation_fn(self.fc1(x))
      x = F.dropout(
          x, p=self.activation_dropout, training=self.training
      )
      x = self.fc2(x)
      x = F.dropout(x, p=self.dropout, training=self.training)
      x = residual + x
      x = self.maybe_layer_norm(self.final_layer_norm, x, after=True)
      if self.onnx_trace and incremental_state is not None:
          saved_state = self.self_attn._get_input_buffer(
              incremental_state
          )
          self_attn_state = (
              saved_state["prev_key"],
              saved_state["prev_value"],
          )
          return x, attn, self_attn_state
      return x, attn
  
imports: import math
  import torch
  import torch.nn
  import torch.nn.functional
  from fairseq import options, utils
  from fairseq.models import FairseqEncoderDecoderModel, FairseqIncrementalDecoder, register_model, register_model_architecture
  from fairseq.models.transformer import Embedding, Linear, TransformerDecoder, TransformerEncoder
  from fairseq.modules import AdaptiveSoftmax, LayerNorm, MultiheadAttention, PositionalEmbedding, SinusoidalPositionalEmbedding
label_desc: Use image node_id 3 for calling the TransformerDecoderLayerPhase2 obj's underlying member method code with example usage: obj.forward(x, encoder_out, encoder_padding_mask, decoder_out, incremental_state, prev__attn_state, prev_encoder_attn_state, prev_decoder_attn_state, _attn_mask, _attn_padding_mask) and returns: x,  attn, x,  attn,  self_attn_state