docstr: Generate the representation given the inputs.
  
  This is used in training or fine-tuning a bert model.
  
  Parameters
  ----------
  inputs
      - layout = 'NT'
          Shape (batch_size, seq_length)
      - layout = 'TN'
          Shape (seq_length, batch_size)
  
  token_types
      If the inputs contain two sequences, we will set different token types for the first
       sentence and the second sentence.
  
      - layout = 'NT'
          Shape (batch_size, seq_length)
      - layout = 'TN'
          Shape (seq_length, batch_size)
  
  valid_length
      The valid length of each sequence
      Shape (batch_size,)
  masked_positions
      The masked position of the sequence
      Shape (batch_size, num_masked_positions).
  
  Returns
  -------
  contextual_embedding
      - layout = 'NT'
          Shape (batch_size, seq_length, units).
      - layout = 'TN'
          Shape (seq_length, batch_size, units).
  
  pooled_out
      Shape (batch_size, units)
  nsp_score :
      Shape (batch_size, 2)
  mlm_scores :
      Shape (batch_size, num_masked_positions, vocab_size)
docstr_tok: ["Generate","the","representation","given","the","inputs",".","This","is","used","in","training","or","fine-tuning","a","bert","model",".","Parameters","--","--","--","--","--","inputs","-","layout","=","'NT'","Shape","(","batch_size",",","seq_length",")","-","layout","=","'TN'","Shape","(","seq_length",",","batch_size",")","token_types","If","the","inputs","contain","two","sequences",",","we","will","set","different","token","types","for","the","first","sentence","and","the","second","sentence",".","-","layout","=","'NT'","Shape","(","batch_size",",","seq_length",")","-","layout","=","'TN'","Shape","(","seq_length",",","batch_size",")","valid_length","The","valid","length","of","each","sequence","Shape","(","batch_size",",",")","masked_positions","The","masked","position","of","the","sequence","Shape","(","batch_size",",","num_masked_positions",")",".","Returns","--","--","--","-","contextual_embedding","-","layout","=","'NT'","Shape","(","batch_size",",","seq_length",",","units",")",".","-","layout","=","'TN'","Shape","(","seq_length",",","batch_size",",","units",")",".","pooled_out","Shape","(","batch_size",",","units",")","nsp_score",":","Shape","(","batch_size",",","2",")","mlm_scores",":","Shape","(","batch_size",",","num_masked_positions",",","vocab_size",")"]
code: def forward(
      self, inputs, token_types, valid_length, masked_positions
  ):
      """Generate the representation given the inputs.
  
      This is used in training or fine-tuning a bert model.
  
      Parameters
      ----------
      inputs
          - layout = 'NT'
              Shape (batch_size, seq_length)
          - layout = 'TN'
              Shape (seq_length, batch_size)
  
      token_types
          If the inputs contain two sequences, we will set different token types for the first
           sentence and the second sentence.
  
          - layout = 'NT'
              Shape (batch_size, seq_length)
          - layout = 'TN'
              Shape (seq_length, batch_size)
  
      valid_length
          The valid length of each sequence
          Shape (batch_size,)
      masked_positions
          The masked position of the sequence
          Shape (batch_size, num_masked_positions).
  
      Returns
      -------
      contextual_embedding
          - layout = 'NT'
              Shape (batch_size, seq_length, units).
          - layout = 'TN'
              Shape (seq_length, batch_size, units).
  
      pooled_out
          Shape (batch_size, units)
      nsp_score :
          Shape (batch_size, 2)
      mlm_scores :
          Shape (batch_size, num_masked_positions, vocab_size)
      """
      contextual_embeddings, pooled_out = self.backbone_model(
          inputs, token_types, valid_length
      )
      nsp_score = self.nsp_classifier(pooled_out)
      if self.layout == "NT":
          mlm_features = select_vectors_by_position(
              contextual_embeddings, masked_positions
          )
      else:
          mlm_features = select_vectors_by_position(
              np.swapaxes(contextual_embeddings, 0, 1), masked_positions
          )
      mlm_scores = self.mlm_decoder(mlm_features)
      return contextual_embeddings, pooled_out, nsp_score, mlm_scores
  
imports: import os
  from typing import Tuple
  import mxnet
  from mxnet import use_np, np, npx
  from mxnet.gluon import HybridBlock, nn
  from .base import BACKBONE_REGISTRY
  from .transformer import TransformerEncoderLayer
  from ..base import get_model_zoo_home_dir, get_repo_model_zoo_url, get_model_zoo_checksum_dir
  from ..utils.config import CfgNode
  from ..utils.misc import load_checksum_stats, download
  from ..utils.registry import Registry
  from ..initializer import TruncNorm
  from ..attention_cell import MultiHeadAttentionCell, gen_self_attn_mask
  from ..layers import get_activation, PositionalEmbedding, PositionwiseFFN, InitializerType
  from ..op import select_vectors_by_position
  from ..data.tokenizers import HuggingFaceWordPieceTokenizer
label_desc: Use image node_id 3 for calling the BertForPretrain obj's underlying member method code with example usage: obj.forward(inputs, token_types, valid_length, masked_positions) and returns: contextual_embeddings,  pooled_out,  nsp_score,  mlm_scores