docstr: Encode the source data to memory
  
  Parameters
  ----------
  src_data
      - layout = 'NT'
          Shape (batch_size, src_length)
      - layout = 'TN'
          Shape (src_length, batch_size)
  
  src_valid_length
      Shape (batch_size,)
  
  Returns
  -------
  enc_out
      - layout = 'NT'
          Shape (batch_size, src_length, C_out)
      - layout = 'TN'
          Shape (src_length, batch_size, C_out)
docstr_tok: ["Encode","the","source","data","to","memory","Parameters","--","--","--","--","--","src_data","-","layout","=","'NT'","Shape","(","batch_size",",","src_length",")","-","layout","=","'TN'","Shape","(","src_length",",","batch_size",")","src_valid_length","Shape","(","batch_size",",",")","Returns","--","--","--","-","enc_out","-","layout","=","'NT'","Shape","(","batch_size",",","src_length",",","C_out",")","-","layout","=","'TN'","Shape","(","src_length",",","batch_size",",","C_out",")"]
code: def encode(self, src_data, src_valid_length):
      """Encode the source data to memory
  
      Parameters
      ----------
      src_data
          - layout = 'NT'
              Shape (batch_size, src_length)
          - layout = 'TN'
              Shape (src_length, batch_size)
  
      src_valid_length
          Shape (batch_size,)
  
      Returns
      -------
      enc_out
          - layout = 'NT'
              Shape (batch_size, src_length, C_out)
          - layout = 'TN'
              Shape (src_length, batch_size, C_out)
  
      """
      src_data = self.src_embed_layer(src_data)
      if self.scaled_embed:
          src_data = src_data * _np.sqrt(self.enc_units)
      if self.pos_embed_type is not None:
          if self.layout == "NT":
              src_data = src_data + self.src_pos_embed_layer(
                  npx.arange_like(src_data, axis=1)
              )
          else:
              src_data = src_data + np.expand_dims(
                  self.src_pos_embed_layer(
                      npx.arange_like(src_data, axis=0)
                  ),
                  axis=1,
              )
  
      enc_out = self.encoder(src_data, src_valid_length)
      return enc_out
  
imports: import numpy
  import mxnet
  from mxnet import np, npx
  from mxnet import use_np
  from mxnet.gluon import nn, HybridBlock
  from typing import Optional, Tuple, List
  from ..utils.registry import Registry
  from ..attention_cell import MultiHeadAttentionCell, gen_self_attn_mask, gen_mem_attn_mask
  from ..layers import PositionalEmbedding, PositionwiseFFN, InitializerType
  from ..utils.config import CfgNode
  from ..sequence_sampler import BaseStepDecoder
label_desc: Use image node_id 5 for calling the TransformerModel obj's underlying member method code with example usage: obj.encode(src_data, src_valid_length) and returns: enc_out