docstr: Args:
      decoder_states: output of the embedding layer (B x L_dec x H)
      decoder_mask: decoder inputs mask (B x L_dec)
      encoder_states: output of the encoder (B x L_enc x H)
      encoder_mask: encoder inputs mask (B x L_enc)
      decoder_mems_list: list of the cached decoder hidden states
          for fast autoregressive generation which will be used instead
          of decoder_states as keys and values if not None
      return_mems: bool, whether to return outputs of all decoder layers
          or the last layer only
      return_mems_as_list: bool, when True, mems returned are as a list; otherwise mems are Tensor
docstr_tok: ["Args",":","decoder_states",":","output","of","the","embedding","layer","(","B","x","L_dec","x","H",")","decoder_mask",":","decoder","inputs","mask","(","B","x","L_dec",")","encoder_states",":","output","of","the","encoder","(","B","x","L_enc","x","H",")","encoder_mask",":","encoder","inputs","mask","(","B","x","L_enc",")","decoder_mems_list",":","list","of","the","cached","decoder","hidden","states","for","fast","autoregressive","generation","which","will","be","used","instead","of","decoder_states","as","keys","and","values","if","not","None","return_mems",":","bool",",","whether","to","return","outputs","of","all","decoder","layers","or","the","last","layer","only","return_mems_as_list",":","bool",",","when","True",",","mems","returned","are","as","a","list",";","otherwise","mems","are","Tensor"]
code: def forward(
      self,
      decoder_states,
      decoder_mask,
      encoder_states,
      encoder_mask,
      decoder_mems_list=None,
      return_mems=False,
      return_mems_as_list=True,
  ):
      """
      Args:
          decoder_states: output of the embedding layer (B x L_dec x H)
          decoder_mask: decoder inputs mask (B x L_dec)
          encoder_states: output of the encoder (B x L_enc x H)
          encoder_mask: encoder inputs mask (B x L_enc)
          decoder_mems_list: list of the cached decoder hidden states
              for fast autoregressive generation which will be used instead
              of decoder_states as keys and values if not None
          return_mems: bool, whether to return outputs of all decoder layers
              or the last layer only
          return_mems_as_list: bool, when True, mems returned are as a list; otherwise mems are Tensor
      """
      decoder_attn_mask = form_attention_mask(
          decoder_mask, diagonal=self.diagonal
      )
      encoder_attn_mask = form_attention_mask(encoder_mask)
      memory_states = self._get_memory_states(
          decoder_states, decoder_mems_list, 0
      )
      if return_mems_as_list:
          cached_mems_list = [memory_states]
      else:
          cached_mems_list = memory_states.unsqueeze(0)
  
      for i, layer in enumerate(self.layers):
          decoder_states = layer(
              decoder_states,
              decoder_attn_mask,
              memory_states,
              encoder_states,
              encoder_attn_mask,
          )
          memory_states = self._get_memory_states(
              decoder_states, decoder_mems_list, i + 1
          )
          if return_mems_as_list:
              cached_mems_list.append(memory_states)
          else:
              cached_mems_list = torch.cat(
                  (cached_mems_list, memory_states.unsqueeze(0)), dim=0
              )
  
      if self.final_layer_norm is not None:
          decoder_states = self.final_layer_norm(decoder_states)
          memory_states = self._get_memory_states(
              decoder_states, decoder_mems_list, i + 2
          )
          if return_mems_as_list:
              cached_mems_list.append(memory_states)
          else:
              cached_mems_list = torch.cat(
                  (cached_mems_list, memory_states.unsqueeze(0)), dim=0
              )
  
      if return_mems:
          return cached_mems_list
      else:
          return cached_mems_list[-1]
  
file_path: NeMo/nemo/collections/asr/modules/transformer/transformer_decoders.py
imports: import copy
  import torch
  import torch.nn
  from nemo.collections.asr.modules.transformer.transformer_modules import MultiHeadAttention, PositionWiseFF
  from nemo.collections.common.parts import form_attention_mask
label_desc: Use image node_id 3 for calling the TransformerDecoder obj's underlying member method code with example usage: obj.forward(decoder_states, decoder_mask, encoder_states, encoder_mask, decoder_mems_list, return_mems, return_mems_as_list) and returns: cached_mems_list, cached_mems_list