code: def create_training_instances(x):
      """Create `TrainingInstance`s from raw text.
  
      The expected input file format is the following:
  
      (1) One sentence per line. These should ideally be actual sentences, not
      entire paragraphs or arbitrary spans of text. (Because we use the
      sentence boundaries for the "next sentence prediction" task).
      (2) Blank lines between documents. Document boundaries are needed so
      that the "next sentence prediction" task doesn't span between documents.
  
      The function expect arguments packed in a tuple as described below.
  
      Parameters
      ----------
      input_files : list of str
          List of paths to input text files.
      tokenizer : BaseTokenizer
          The BERT tokenizer
      max_seq_length : int
          The hard limit of maximum sequence length of sentence pairs
      dupe_factor : int
          Duplication factor.
      short_seq_prob : float
          The probability of sampling sequences shorter than the max_seq_length.
      masked_lm_prob : float
          The probability of replacing texts with masks/random words/original words.
      max_predictions_per_seq : int
          The hard limit of the number of predictions for masked words
      whole_word_mask : bool
          Whether to do masking for whole words
      vocab : BERTVocab
          The BERTVocab
      nworker : int
          The number of processes to help processing texts in parallel
      worker_pool : multiprocessing.Pool
          Must be provided if nworker > 1. The caller is responsible for the destruction of
          the worker pool.
      output_file : str or None
          Path to the output file. If None, the result is not serialized. If provided,
          results are  stored in the order of (input_ids, segment_ids, masked_lm_positions,
          masked_lm_ids, masked_lm_weights, next_sentence_labels, valid_lengths).
  
      Returns
      -------
      A tuple of np.ndarray : input_ids, masked_lm_ids, masked_lm_positions, masked_lm_weights
                              next_sentence_labels, segment_ids, valid_lengths
      """
      (
          input_files,
          tokenizer,
          max_seq_length,
          short_seq_prob,
          masked_lm_prob,
          max_predictions_per_seq,
          whole_word_mask,
          vocab,
          dupe_factor,
          nworker,
          worker_pool,
          output_file,
          random_next_sentence,
      ) = x
  
      time_start = time.time()
      if nworker > 1:
          assert worker_pool is not None
  
      all_documents = [[]]
  
      for input_file in input_files:
          logging.debug("*** Tokenizing file %s***", input_file)
          with io.open(input_file, "r", encoding="utf-8") as reader:
              lines = reader.readlines()
              num_lines = len(lines)
              num_lines_per_worker = (
                  num_lines + nworker - 1
              ) // nworker
              process_args = []
  
              # tokenize in parallel
              for worker_idx in range(nworker):
                  start = worker_idx * num_lines_per_worker
                  end = min(
                      (worker_idx + 1) * num_lines_per_worker, num_lines
                  )
                  process_args.append((lines[start:end], tokenizer))
              if worker_pool:
                  tokenized_results = worker_pool.map(
                      tokenize_lines_fn, process_args
                  )
              else:
                  tokenized_results = [
                      tokenize_lines_fn(process_args[0])
                  ]
  
              for tokenized_result in tokenized_results:
                  for line in tokenized_result:
                      if not line:
                          if all_documents[-1]:
                              all_documents.append([])
                      else:
                          all_documents[-1].append(line)
  
      # remove the empty document if any
      all_documents = [x for x in all_documents if x]
      random.shuffle(all_documents)
  
      # generate training instances
      instances = []
      if worker_pool:
          process_args = []
          for document_index in range(len(all_documents)):
              process_args.append(
                  (
                      all_documents,
                      document_index,
                      max_seq_length,
                      short_seq_prob,
                      masked_lm_prob,
                      max_predictions_per_seq,
                      whole_word_mask,
                      vocab,
                      tokenizer,
                      random_next_sentence,
                  )
              )
          for _ in range(dupe_factor):
              instances_results = worker_pool.map(
                  create_instances_from_document, process_args
              )
              for instances_result in instances_results:
                  instances.extend(instances_result)
          random.shuffle(instances)
          npz_instances = worker_pool.apply(
              convert_to_npz, (instances, max_seq_length)
          )
      else:
          for _ in range(dupe_factor):
              for document_index in range(len(all_documents)):
                  instances.extend(
                      create_instances_from_document(
                          (
                              all_documents,
                              document_index,
                              max_seq_length,
                              short_seq_prob,
                              masked_lm_prob,
                              max_predictions_per_seq,
                              whole_word_mask,
                              vocab,
                              tokenizer,
                              random_next_sentence,
                          )
                      )
                  )
          random.shuffle(instances)
          npz_instances = convert_to_npz(instances, max_seq_length)
  
      (
          input_ids,
          masked_lm_ids,
          masked_lm_positions,
          masked_lm_weights,
          next_sentence_labels,
          segment_ids,
          valid_lengths,
      ) = npz_instances
  
      # write output to files. Used when pre-generating files
      if output_file:
          features = (
              input_ids,
              segment_ids,
              masked_lm_positions,
              masked_lm_ids,
              masked_lm_weights,
              next_sentence_labels,
              valid_lengths,
          )
          logging.debug(
              "*** Writing to output file %s ***", output_file
          )
          write_to_files_np(
              features,
              tokenizer,
              max_seq_length,
              max_predictions_per_seq,
              [output_file],
          )
          features = None
      else:
          features = (
              input_ids,
              masked_lm_ids,
              masked_lm_positions,
              masked_lm_weights,
              next_sentence_labels,
              segment_ids,
              valid_lengths,
          )
      time_end = time.time()
      logging.debug(
          "Process %d files took %.1f s",
          len(input_files),
          time_end - time_start,
      )
      return features
  
imports: import argparse
  import logging
  import io
  import os
  import glob
  import collections
  import warnings
  import random
  import time
  from multiprocessing import Pool
  import numpy
  from gluonnlp.models.bert import BertModel, BertForPretrain, get_pretrained_bert
  import gluonnlp
label_desc: Use image node_id 6 for calling a global function with example usage: create_training_instances(x) and returns: features