class_docstr: Decoder with general structure of Chen et al., The Best of Both Worlds:
  Combining Recent Advances in Neural Machine Translation, 2018.
  https://arxiv.org/abs/1804.09849
class_docstr_tok: ["Decoder","with","general","structure","of","Chen","et","al.",",","The","Best","of","Both","Worlds",":","Combining","Recent","Advances","in","Neural","Machine","Translation",",","2018.","https",":","\/\/arxiv.org\/abs\/1804.09849"]
code: def _forward_given_embeddings(
      self,
      embed_out,
      prev_output_tokens,
      encoder_out,
      incremental_state=None,
      possible_translation_tokens=None,
      timestep=None,
  ):
      x = embed_out
      (
          encoder_x,
          src_tokens,
          encoder_padding_mask,
      ) = self._unpack_encoder_out(encoder_out)
      bsz, seqlen = prev_output_tokens.size()
  
      state_outputs = []
      if incremental_state is not None:
          prev_states = utils.get_incremental_state(
              self, incremental_state, "cached_state"
          )
          if prev_states is None:
              prev_states = self._init_prev_states(encoder_out)
  
          # final 2 states of list are projected key and value
          saved_state = {
              "prev_key": prev_states[-2],
              "prev_value": prev_states[-1],
          }
          self.attention._set_input_buffer(
              incremental_state, saved_state
          )
  
      if incremental_state is not None:
          # first num_layers pairs of states are (prev_hidden, prev_cell)
          # for each layer
          h_prev = prev_states[0]
          c_prev = prev_states[1]
      else:
          h_prev = self._init_hidden(encoder_out, bsz).type_as(x)
          c_prev = torch.zeros([1, bsz, self.lstm_units]).type_as(x)
  
      x = self._concat_latent_code(x, encoder_out)
      x, (h_next, c_next) = self.initial_rnn_layer(x, (h_prev, c_prev))
      if incremental_state is not None:
          state_outputs.extend([h_next, c_next])
  
      x = F.dropout(x, p=self.dropout, training=self.training)
  
      if self.proj_encoder_layer is not None:
          encoder_x = self.proj_encoder_layer(encoder_x)
  
      attention_in = x
      if self.proj_layer is not None:
          attention_in = self.proj_layer(x)
  
      attention_out, attention_weights = self.attention(
          query=attention_in,
          key=encoder_x,
          value=encoder_x,
          key_padding_mask=encoder_padding_mask,
          incremental_state=incremental_state,
          static_kv=True,
          need_weights=(not self.training),
      )
  
      for i, layer in enumerate(self.extra_rnn_layers):
          residual = x
          rnn_input = torch.cat([x, attention_out], dim=2)
          rnn_input = self._concat_latent_code(rnn_input, encoder_out)
  
          if incremental_state is not None:
              # first num_layers pairs of states are (prev_hidden, prev_cell)
              # for each layer
              h_prev = prev_states[2 * i + 2]
              c_prev = prev_states[2 * i + 3]
          else:
              h_prev = self._init_hidden(encoder_out, bsz).type_as(x)
              c_prev = torch.zeros([1, bsz, self.lstm_units]).type_as(x)
  
          x, (h_next, c_next) = layer(rnn_input, (h_prev, c_prev))
          if incremental_state is not None:
              state_outputs.extend([h_next, c_next])
          x = F.dropout(x, p=self.dropout, training=self.training)
          x = x + residual
  
      x = torch.cat([x, attention_out], dim=2)
      x = self._concat_latent_code(x, encoder_out)
      if self.bottleneck_layer is not None:
          x = self.bottleneck_layer(x)
  
      # T x B x C -> B x T x C
      x = x.transpose(0, 1)
  
      if (
          self.vocab_reduction_module is not None
          and possible_translation_tokens is None
      ):
          decoder_input_tokens = prev_output_tokens.contiguous()
          possible_translation_tokens = self.vocab_reduction_module(
              src_tokens, decoder_input_tokens=decoder_input_tokens
          )
  
      output_weights = self.embed_out
      if possible_translation_tokens is not None:
          output_weights = output_weights.index_select(
              dim=0, index=possible_translation_tokens
          )
  
      logits = F.linear(x, output_weights)
  
      if incremental_state is not None:
          # encoder projections can be reused at each incremental step
          state_outputs.extend([prev_states[-2], prev_states[-1]])
          utils.set_incremental_state(
              self, incremental_state, "cached_state", state_outputs
          )
  
      return logits, attention_weights, possible_translation_tokens
  
imports: import torch
  import torch.nn
  import torch.nn.functional
  from fairseq import utils
  from fairseq.models import FairseqEncoderDecoderModel, FairseqIncrementalDecoder, register_model, register_model_architecture, transformer
  from fairseq.modules import MultiheadAttention
  from pytorch_translate import multilingual_model, transformer, vocab_reduction
  from pytorch_translate.utils import torch_find
label_desc: Use image node_id 10 for calling the HybridRNNDecoder obj's underlying member method code with example usage: obj._forward_given_embeddings(embed_out, prev_output_tokens, encoder_out, incremental_state, possible_translation_tokens, timestep) and returns: logits,  attention_weights,  possible_translation_tokens