class_docstr: Transformer decoder as in Vaswani et al, 2017: Attention is all you need.
  In training, computation scores for each position of the known target sequence are computed in parallel,
  yielding most of the speedup.
  At inference time, the decoder block is evaluated again and again over a maximum length input sequence that is
  initially filled with zeros and grows during beam search with predicted tokens. Appropriate masking at every
  time-step ensures correct self-attention scores and is updated with every step.
  
  :param config: Transformer configuration.
  :param inference_only: Only use the model for inference enabling some optimizations,
                         such as disabling the auto-regressive mask.
  :param dtype: Torch data type for parameters.
  :param clamp_to_dtype: Avoid -inf/inf by clamping outputs to min/max finite
                         values for their dtype.
docstr: Returns the initial states given encoder output. States for teacher-forced training are encoder outputs
  and a valid length mask for encoder outputs.
  At inference, this method returns the following state tuple:
  valid length bias, step state,
  [projected encoder attention keys, projected encoder attention values] * num_layers,
  [autoregressive state dummies] * num_layers.
  
  :param encoder_outputs: Encoder outputs. Shape: (batch, source_length, encoder_dim).
  :param encoder_valid_length: Valid lengths of encoder outputs. Shape: (batch, 2).
  :param target_embed: Target-side embedding layer output. Shape: (batch, target_length, target_embedding_dim).
  :return: Initial states.
docstr_tok: ["Returns","the","initial","states","given","encoder","output",".","States","for","teacher-forced","training","are","encoder","outputs","and","a","valid","length","mask","for","encoder","outputs",".","At","inference",",","this","method","returns","the","following","state","tuple",":","valid","length","bias",",","step","state",",","[","projected","encoder","attention","keys",",","projected","encoder","attention","values","]","*","num_layers",",","[","autoregressive","state","dummies","]","*","num_layers",".",":","param","encoder_outputs",":","Encoder","outputs",".","Shape",":","(","batch",",","source_length",",","encoder_dim",")",".",":","param","encoder_valid_length",":","Valid","lengths","of","encoder","outputs",".","Shape",":","(","batch",",","2",")",".",":","param","target_embed",":","Target-side","embedding","layer","output",".","Shape",":","(","batch",",","target_length",",","target_embedding_dim",")",".",":","return",":","Initial","states","."]
code: def init_state_from_encoder(
      self,
      encoder_outputs: pt.Tensor,
      encoder_valid_length: Optional[pt.Tensor] = None,
      target_embed: Optional[pt.Tensor] = None,
  ) -> List[pt.Tensor]:
      """
      Returns the initial states given encoder output. States for teacher-forced training are encoder outputs
      and a valid length mask for encoder outputs.
      At inference, this method returns the following state tuple:
      valid length bias, step state,
      [projected encoder attention keys, projected encoder attention values] * num_layers,
      [autoregressive state dummies] * num_layers.
  
      :param encoder_outputs: Encoder outputs. Shape: (batch, source_length, encoder_dim).
      :param encoder_valid_length: Valid lengths of encoder outputs. Shape: (batch, 2).
      :param target_embed: Target-side embedding layer output. Shape: (batch, target_length, target_embedding_dim).
      :return: Initial states.
      """
      source_max_len = encoder_outputs.size()[1]
      # (batch * heads, 1, source_max_len)
      source_mask = layers.prepare_source_length_mask(
          encoder_valid_length,
          self.config.attention_heads,
          source_max_len,
          mask_prepended_tokens=self.config.block_prepended_cross_attention,
      )
      if (
          target_embed is None
      ):  # Inference: initial step = 0. Shape: (batch_size, 1)
          steps = pt.zeros_like(encoder_valid_length[:, :1])
          # Shape: (batch, heads, 1, src_max_len)
          source_mask = source_mask.view(
              -1, self.config.attention_heads, 1, source_max_len
          )
      else:  # Training: steps up to target length. Shape: (1, target_length)
          target_length = target_embed.size()[1]
          steps = pt.arange(
              0, target_length, device=target_embed.device
          ).unsqueeze(0)
          source_mask = source_mask.expand(
              -1, target_length, -1
          )  # Shape: (batch * heads, trg_max_len, src_max_len)
          # Shape: (batch, heads, trg_max_len, src_max_len)
          source_mask = source_mask.view(
              -1,
              self.config.attention_heads,
              target_length,
              source_max_len,
          )
  
      if self.inference_only:
          # Encoder projection caching, therefore we don't pass the encoder_outputs
          states = [steps, source_mask]
          encoder_outputs_t = encoder_outputs.transpose(
              1, 0
          )  # time-major layout
          for layer in self.layers:
              enc_att_kv = layer.enc_attention.ff_kv(encoder_outputs_t)
              states.append(enc_att_kv)
      else:
          # NO encoder projection caching
          states = [steps, encoder_outputs.transpose(1, 0), source_mask]
  
      _batch_size = encoder_outputs.size()[0]
      _device = encoder_outputs.device
      _dtype = encoder_outputs.dtype
      dummy_autoregr_states = [
          pt.zeros(
              layer.get_states_shape(_batch_size),
              device=_device,
              dtype=_dtype,
          )
          for layer in self.layers
          for _ in range(layer.num_state_tensors)
      ]
  
      states += dummy_autoregr_states
      return states
  
imports: import logging
  from abc import abstractmethod
  from itertools import islice
  from typing import Dict, List, Optional, Tuple, Type, Union
  import torch
  from .None import constants
  from .None import layers
  from .None import transformer
  from .transformer import TransformerConfig
label_desc: Use image node_id 4 for calling the TransformerDecoder obj's underlying member method code with example usage: obj.init_state_from_encoder(encoder_outputs, encoder_valid_length, target_embed) and returns: states