docstr: Args:
      encoder_states: output of the embedding_layer (B x L_enc x H)
      encoder_mask: encoder inputs mask (B x L_enc)
      encoder_mems_list: list of the cached encoder hidden states
          for fast autoregressive generation which will be used instead
          of encoder_states as keys and values if not None
      return_mems: bool, whether to return outputs of all encoder layers
          or the last layer only
docstr_tok: ["Args",":","encoder_states",":","output","of","the","embedding_layer","(","B","x","L_enc","x","H",")","encoder_mask",":","encoder","inputs","mask","(","B","x","L_enc",")","encoder_mems_list",":","list","of","the","cached","encoder","hidden","states","for","fast","autoregressive","generation","which","will","be","used","instead","of","encoder_states","as","keys","and","values","if","not","None","return_mems",":","bool",",","whether","to","return","outputs","of","all","encoder","layers","or","the","last","layer","only"]
code: def forward(
      self,
      encoder_states,
      encoder_mask,
      encoder_mems_list=None,
      return_mems=False,
  ):
      """
      Args:
          encoder_states: output of the embedding_layer (B x L_enc x H)
          encoder_mask: encoder inputs mask (B x L_enc)
          encoder_mems_list: list of the cached encoder hidden states
              for fast autoregressive generation which will be used instead
              of encoder_states as keys and values if not None
          return_mems: bool, whether to return outputs of all encoder layers
              or the last layer only
      """
  
      encoder_attn_mask = form_attention_mask(encoder_mask, self.diag)
  
      memory_states = self._get_memory_states(
          encoder_states, encoder_mems_list, 0
      )
      cached_mems_list = [memory_states]
  
      for i, layer in enumerate(self.layers):
          encoder_states = layer(
              encoder_states, encoder_attn_mask, memory_states
          )
          memory_states = self._get_memory_states(
              encoder_states, encoder_mems_list, i + 1
          )
          cached_mems_list.append(memory_states)
  
      if self.final_layer_norm is not None:
          encoder_states = self.final_layer_norm(encoder_states)
          memory_states = self._get_memory_states(
              encoder_states, encoder_mems_list, i + 1
          )
          cached_mems_list.append(memory_states)
  
      if return_mems:
          return cached_mems_list
      else:
          return cached_mems_list[-1]
  
file_path: NeMo/nemo/collections/asr/modules/transformer/transformer_encoders.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 TransformerEncoder obj's underlying member method code with example usage: obj.forward(encoder_states, encoder_mask, encoder_mems_list, return_mems) and returns: cached_mems_list, cached_mems_list