class_docstr: Transformer decoder.
class_docstr_tok: ["Transformer","decoder","."]
code: def forward(
      self,
      prev_output_tokens,
      encoder_out,
      incremental_state=None,
      possible_translation_tokens=None,
      timestep=None,
  ):
      (encoder_x, src_tokens, encoder_padding_mask) = encoder_out
  
      # embed positions
      positions = self.embed_positions(
          prev_output_tokens,
          incremental_state=incremental_state,
          timestep=timestep,
      )
  
      if incremental_state is not None:
          prev_output_tokens = prev_output_tokens[:, -1:]
          positions = positions[:, -1:]
  
      # embed tokens and positions
      x = self.embed_scale * self.embed_tokens(prev_output_tokens)
      x += positions
      x = F.dropout(x, p=self.dropout, training=self.training)
  
      # B x T x C -> T x B x C
      x = x.transpose(0, 1)
  
      # decoder layers
      state_outputs = []  # onnx_trace only
      attn = None
  
      if self.onnx_trace:
          for i, layer in enumerate(self.layers):
              if hasattr(self, "decoder_layers_to_keep"):
                  if i not in self.decoder_layers_to_keep.keys():
                      continue
                  else:
                      i = self.decoder_layers_to_keep[i]
              # (prev_key, prev_value)
              self_attn_input = incremental_state[4 * i : 4 * i + 2]
              attn_state = incremental_state[4 * i + 2 : 4 * i + 4]
              x, attn, self_attn_out = layer(
                  x,
                  encoder_x,
                  encoder_padding_mask,
                  incremental_state={},
                  prev_self_attn_state=self_attn_input
                  if len(incremental_state) > 0
                  else None,
                  prev_attn_state=attn_state
                  if len(incremental_state) > 0
                  else None,
              )
              state_outputs.extend(self_attn_out)
              state_outputs.extend(attn_state)  # unchanged
      else:
          for i, layer in enumerate(self.layers):
              if hasattr(self, "decoder_layers_to_keep"):
                  dropout_probability = random.uniform(0, 1)
                  if (
                      self.training
                      and dropout_probability < self.decoder_layerdrop
                      or (
                          not self.training
                          and i
                          not in self.decoder_layers_to_keep.keys()
                      )
                  ):
                      continue
              x, attn, _ = layer(
                  x,
                  encoder_x,
                  encoder_padding_mask,
                  incremental_state,
                  self_attn_mask=self.buffered_future_mask(x)
                  if incremental_state is None
                  else None,
              )
  
      # T x B x C -> B x T x C
      x = x.transpose(0, 1)
  
      if self.bottleneck_layer is not None:
          x = self.bottleneck_layer(x)
  
      if self.adaptive_softmax is not None:
          return x, attn, None
  
      # project back to size of vocabulary
      if self.share_input_output_embed:
          output_weights = self.embed_tokens.weight
      else:
          output_weights = self.embed_out
  
      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
          )
      if possible_translation_tokens is not None:
          output_weights = output_weights.index_select(
              dim=0, index=possible_translation_tokens
          )
  
      # Use quantizable self.output_projection if vocab reduction is not used
      if self.vocab_reduction_module is None:
          logits = self.output_projection(x)
      else:
          logits = F.linear(x, output_weights)
  
      if self.onnx_trace:
          return (
              logits,
              attn,
              possible_translation_tokens,
              state_outputs,
          )
  
      return logits, attn, possible_translation_tokens
  
imports: import math
  import random
  import torch
  import torch.nn
  import torch.nn.functional
  from fairseq import options, utils
  from fairseq.models import FairseqEncoder, FairseqEncoderDecoderModel, FairseqIncrementalDecoder, register_model, register_model_architecture, transformer
  from fairseq.modules import AdaptiveSoftmax, LayerNorm, MultiheadAttention, SinusoidalPositionalEmbedding
  from pytorch_translate import utils, vocab_reduction
  from pytorch_translate.average_attention import AverageAttention
  from pytorch_translate.common_layers import TransformerEmbedding, TransformerEncoderGivenEmbeddings, TransformerTokenEmbedding, VariableTracker
  from pytorch_translate.semi_supervised import SemiSupervisedModel
  from pytorch_translate.utils import torch_find
label_desc: Use image node_id 3 for calling the TransformerDecoder obj's underlying member method code with example usage: obj.forward(prev_output_tokens, encoder_out, incremental_state, possible_translation_tokens, timestep) and returns: logits,  attn,  possible_translation_tokens, x,  attn,  None, logits,  attn,  possible_translation_tokens,  state_outputs