class_docstr: Transformer decoder consisting of *args.decoder_layers* layers. Each layer
  is a :class:`TransformerAANDecoderLayer`.
class_docstr_tok: ["Transformer","decoder","consisting","of","*","args.decoder_layers","*","layers",".","Each","layer","is","a",":","class",":","`","TransformerAANDecoderLayer","`","."]
code: def forward(
      self,
      prev_output_tokens,
      encoder_out=None,
      incremental_state=None,
      possible_translation_tokens=None,
      timestep=None,
  ):
      (encoder_x, src_tokens, encoder_padding_mask) = encoder_out
  
      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:]
  
          if self.onnx_trace:
              assert type(incremental_state) is list
              assert timestep is not None
  
              state_list = incremental_state
              incremental_state = {}
              state_index = 0
  
              for layer in self.layers:
                  prev_sum = state_list[state_index]
                  state_index += 1
                  prev_pos = timestep.float()
                  layer.avg_attn._set_input_buffer(
                      incremental_state,
                      {"prev_sum": prev_sum, "prev_pos": prev_pos},
                  )
  
                  if layer.encoder_attn is not None:
                      utils.set_incremental_state(
                          layer.encoder_attn,
                          incremental_state,
                          "prev_key",
                          state_list[state_index],
                      )
                      utils.set_incremental_state(
                          layer.encoder_attn,
                          incremental_state,
                          "prev_value",
                          state_list[state_index + 1],
                      )
                      state_index += 2
  
      # embed tokens and positions
      x = self.embed_scale * self.embed_tokens(prev_output_tokens)
  
      if self.project_in_dim is not None:
          x = self.project_in_dim(x)
  
      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)
      attn = None
  
      inner_states = [x]
  
      # decoder layers
      for layer in self.layers:
          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,
          )
          inner_states.append(x)
  
      if self.normalize:
          x = self.layer_norm(x)
  
      # 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)
  
      # 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
          )
  
      if self.adaptive_softmax is None:
          logits = F.linear(x, output_weights)
      else:
          assert (
              possible_translation_tokens is None
          ), "vocabulary reduction and adaptive softmax are incompatible!"
          logits = x
  
      if self.onnx_trace:
          state_outputs = []
          for layer in self.layers:
              saved_state = layer.avg_attn._get_input_buffer(
                  incremental_state
              )
              assert (
                  "prev_sum" in saved_state
              ), "No prev_sum found while tracing average attention layer!"
              # remove sequence axis
              prev_sum = saved_state["prev_sum"].squeeze(0)
              state_outputs.append(prev_sum)
  
              if layer.encoder_attn is not None:
                  # prev_key and prev_value remain unchanged
                  prev_key = state_list[len(state_outputs)]
                  prev_value = state_list[len(state_outputs) + 1]
                  state_outputs.extend([prev_key, prev_value])
  
          return (
              logits,
              attn,
              possible_translation_tokens,
              state_outputs,
          )
  
      return logits, attn, possible_translation_tokens
  
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, transformer
  from fairseq.modules import AdaptiveSoftmax, LearnedPositionalEmbedding, MultiheadAttention, SinusoidalPositionalEmbedding
  from pytorch_translate import transformer, vocab_reduction
  from pytorch_translate.average_attention import AverageAttention, AverageWindowAttention
  from pytorch_translate.utils import torch_find
label_desc: Use image node_id 3 for calling the TransformerAANDecoder 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, logits,  attn,  possible_translation_tokens,  state_outputs