class_docstr: Transformer decoder.
class_docstr_tok: ["Transformer","decoder","."]
docstr: For self-attention, initial (prev_key, prev_value) are dummy tensors
  with a zero-size sequence dimension.
  For encoder-decoder attention, key and value are computed once from
  the encoder outputs and stay the same throughout decoding.
docstr_tok: ["For","self-attention",",","initial","(","prev_key",",","prev_value",")","are","dummy","tensors","with","a","zero-size","sequence","dimension",".","For","encoder-decoder","attention",",","key","and","value","are","computed","once","from","the","encoder","outputs","and","stay","the","same","throughout","decoding","."]
code: def _init_prev_states(self, encoder_out):
      """
      For self-attention, initial (prev_key, prev_value) are dummy tensors
      with a zero-size sequence dimension.
      For encoder-decoder attention, key and value are computed once from
      the encoder outputs and stay the same throughout decoding.
      """
      encoder_x, src_tokens, encoder_padding_mask = encoder_out
      batch_size = torch.onnx.operators.shape_as_tensor(encoder_x)[1]
      states = []
      for i, layer in enumerate(self.layers):
          if hasattr(self, "decoder_layers_to_keep") and (
              i not in self.decoder_layers_to_keep.keys()
          ):
              continue
          if self.aan:
              # (bsz, channel)
              prev_sum = torch.zeros([1, layer.avg_attn.embed_dim])
              prev_pos = torch.zeros([1, 1])
              states.extend([prev_sum, prev_pos])
          else:
              # dummy initial (prev_key, prev_value) for self-attention
              for _ in range(2):
                  dummy_state_shape = torch.cat(
                      [
                          batch_size.view(1),
                          torch.LongTensor([layer.self_attn.num_heads]),
                          torch.LongTensor([0]),
                          torch.LongTensor([layer.self_attn.head_dim]),
                      ]
                  )
                  dummy_state = torch.zeros(
                      [
                          1,
                          layer.self_attn.num_heads,
                          0,
                          layer.self_attn.head_dim,
                      ]
                  )
                  reshaped_dummy_state = (
                      torch.onnx.operators.reshape_from_tensor_shape(
                          dummy_state, dummy_state_shape
                      )
                  )
                  states.append(reshaped_dummy_state)
  
          # (key, value) for encoder-decoder attention computed from encoder
          # output and remain the same throughout decoding
          key = layer.encoder_attn.k_proj(encoder_x)
          value = layer.encoder_attn.v_proj(encoder_x)
  
          # (key, value) kept in shape (bsz, num_heads, seq_len, head_dim)
          # to avoid repeated transpose operations
          seq_len, batch_size_int, _ = encoder_x.shape
          num_heads = layer.encoder_attn.num_heads
          head_dim = layer.encoder_attn.head_dim
          key = (
              key.view(seq_len, batch_size_int * num_heads, head_dim)
              .transpose(0, 1)
              .view(batch_size_int, num_heads, seq_len, head_dim)
          )
          value = (
              value.view(seq_len, batch_size_int * num_heads, head_dim)
              .transpose(0, 1)
              .view(batch_size_int, num_heads, seq_len, head_dim)
          )
  
          states.extend([key, value])
      return states
  
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 7 for calling the TransformerDecoder obj's underlying member method code with example usage: obj._init_prev_states(encoder_out) and returns: states