text: # coding=utf-8
  # Copyright 2018 The Google AI Language Team Authors and The HugginFace Inc. team.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  """BERT finetuning runner."""
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import csv
  import os
  import logging
  import argparse
  import random
  from tqdm import tqdm, trange
  import socket
  
  import numpy as np
  import torch
  from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
  from torch.utils.data.distributed import DistributedSampler
  import torch.multiprocessing as mp
  
  from pytorch_pretrained_bert.tokenization import BertTokenizer
  from pytorch_pretrained_bert.modeling import BertForSequenceClassification
  from pytorch_pretrained_bert.optimization import BertAdam
  from pytorch_pretrained_bert.file_utils import PYTORCH_PRETRAINED_BERT_CACHE
  #from azureml_bert_util import *
  #from azureml.core.run import Run
  from pretrain.configuration import BertJobConfiguration
  from transformers.modeling_electra import ElectraModel, ElectraConfig
  from pytorch_pretrained_bert.modeling import BertModel, BertConfig
  
  from models import ElectraForSequenceClassification, ElectraForSequenceRegression, BertForSequenceRegression 
  
  import rutils
  
  #logging.basicConfig(format = '%(asctime)s - %(levelname)s - %(name)s -   %(message)s', 
  #                    datefmt = '%m/%d/%Y %H:%M:%S',
  #                    level = logging.INFO)
  #logger = logging.getLogger(__name__)
  
  def warmup_linear(x, warmup=0.002):
      if x < warmup:
          return x/warmup
      return 1.0 - x
  
  
  def adjust_gradient_accumulation_steps(x, initial_steps, target_steps, warmup):
      return min(max(int(x/warmup*target_steps), initial_steps), target_steps)
  
  
  class InputExample(object):
      """A single training/test example for simple sequence classification."""
  
      def __init__(self, guid, text_a, text_b=None, label=None):
          """Constructs a InputExample.
  
          Args:
              guid: Unique id for the example.
              text_a: string. The untokenized text of the first sequence. For single
              sequence tasks, only this sequence must be specified.
              text_b: (Optional) string. The untokenized text of the second sequence.
              Only must be specified for sequence pair tasks.
              label: (Optional) string. The label of the example. This should be
              specified for train and dev examples, but not for test examples.
          """
          self.guid = guid
          self.text_a = text_a
          self.text_b = text_b
          self.label = label
  
  
  class InputFeatures(object):
      """A single set of features of data."""
  
      def __init__(self, input_ids, input_mask, segment_ids, label_id):
          self.input_ids = input_ids
          self.input_mask = input_mask
          self.segment_ids = segment_ids
          self.label_id = label_id
  
  
  class DataProcessor(object):
      """Base class for data converters for sequence classification data sets."""
  
      def get_train_examples(self, data_dir):
          """Gets a collection of `InputExample`s for the train set."""
          raise NotImplementedError()
  
      def get_dev_examples(self, data_dir):
          """Gets a collection of `InputExample`s for the dev set."""
          raise NotImplementedError()
  
      def get_labels(self):
          """Gets the list of labels for this data set."""
          raise NotImplementedError()
  
      @classmethod
      def _read_tsv(cls, input_file, quotechar=None):
          """Reads a tab separated value file."""
          with open(input_file, "r") as f:
              reader = csv.reader(f, delimiter="\t", quotechar=quotechar)
              lines = []
              for line in reader:
                  lines.append(line)
              return lines
  
  
  class RTEProcessor(DataProcessor):
      """Processor for the RTE data set (GLUE version)."""
      def get_train_examples(self, data_dir):
          """See base class."""
          #logger.info("LOOKING AT {}".format(os.path.join(data_dir, "train.tsv")))
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
  
      def get_dev_examples(self, data_dir):
          """See base class."""
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")
  
      def get_labels(self):
          """See base class."""
          return ["entailment", "not_entailment"]
  
      def _create_examples(self, lines, set_type):
          """Creates examples for the training and dev sets."""
          examples = []
          for (i, line) in enumerate(lines):
              if i == 0:
                  continue
              guid = "%s-%s" % (set_type, i)
              text_a = line[1]
              text_b = line[2]
              label = line[3]
              examples.append(
                  InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
          return examples
  
  
  class STSBProcessor(DataProcessor):
      """Processor for the STS-B data set (GLUE version)."""
      def get_train_examples(self, data_dir):
          """See base class."""
          #logger.info("LOOKING AT {}".format(os.path.join(data_dir, "train.tsv")))
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
  
      def get_dev_examples(self, data_dir):
          """See base class."""
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")
  
      def get_labels(self):
          """See base class. Regression task, return None as label_list"""
          return None
  
      def _create_examples(self, lines, set_type):
          """Creates examples for the training and dev sets."""
          examples = []
          for (i, line) in enumerate(lines):
              if i == 0:
                  continue
              guid = "%s-%s" % (set_type, i)
              text_a = line[7]
              text_b = line[8]
              label = str(float(line[9]))
              examples.append(
                  InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
          return examples
  
  
  class SST2Processor(DataProcessor):
      """Processor for the SST-2 data set (GLUE version)."""
      def get_train_examples(self, data_dir):
          """See base class."""
          #logger.info("LOOKING AT {}".format(os.path.join(data_dir, "train.tsv")))
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
  
      def get_dev_examples(self, data_dir):
          """See base class."""
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")
  
      def get_labels(self):
          """See base class."""
          return ["0", "1"]
  
      def _create_examples(self, lines, set_type):
          """Creates examples for the training and dev sets."""
          examples = []
          for (i, line) in enumerate(lines):
              if i == 0:
                  continue
              guid = "%s-%s" % (set_type, i)
              text_a = line[0]
              text_b = None
              label = line[1]
              examples.append(
                  InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
          return examples
  
  class QNLIProcessor(DataProcessor):
      """Processor for the QNLI data set (GLUE version)."""
      def get_train_examples(self, data_dir):
          """See base class."""
          #logger.info("LOOKING AT {}".format(os.path.join(data_dir, "train.tsv")))
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
  
      def get_dev_examples(self, data_dir):
          """See base class."""
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")
  
      def get_labels(self):
          """See base class."""
          return ["entailment", "not_entailment"]
  
      def _create_examples(self, lines, set_type):
          """Creates examples for the training and dev sets."""
          examples = []
          for (i, line) in enumerate(lines):
              if i == 0:
                  continue
              guid = "%s-%s" % (set_type, i)
              text_a = line[1]
              text_b = line[2]
              label = line[3]
              examples.append(
                  InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
          return examples
  
  class QQPProcessor(DataProcessor):
      """Processor for the QQP data set (GLUE version)."""
      def get_train_examples(self, data_dir):
          """See base class."""
          #print("LOOKING AT {}".format(os.path.join(data_dir, "train.tsv")))
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
  
      def get_dev_examples(self, data_dir):
          """See base class."""
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")
  
      def get_labels(self):
          """See base class."""
          return ["0", "1"]
  
      def _create_examples(self, lines, set_type):
          """Creates examples for the training and dev sets."""
          examples = []
          for (i, line) in enumerate(lines):
              if i == 0:
                  continue
              if len(line) < 6:
                  continue
              guid = "%s-%s" % (set_type, i)
              text_a = line[3]
              text_b = line[4]
              label = line[5]
              examples.append(
                  InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
          return examples
  
  
  class MrpcProcessor(DataProcessor):
      """Processor for the MRPC data set (GLUE version)."""
  
      def get_train_examples(self, data_dir):
          """See base class."""
          #logger.info("LOOKING AT {}".format(os.path.join(data_dir, "train.tsv")))
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
  
      def get_dev_examples(self, data_dir):
          """See base class."""
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")
  
      def get_labels(self):
          """See base class."""
          return ["0", "1"]
  
      def _create_examples(self, lines, set_type):
          """Creates examples for the training and dev sets."""
          examples = []
          for (i, line) in enumerate(lines):
              if i == 0:
                  continue
              guid = "%s-%s" % (set_type, i)
              text_a = line[3]
              text_b = line[4]
              label = line[0]
              examples.append(
                  InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
          return examples
  
  
  class MnliProcessor(DataProcessor):
      """Processor for the MultiNLI data set (GLUE version)."""
  
      def get_train_examples(self, data_dir):
          """See base class."""
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
  
      def get_dev_examples(self, data_dir):
          """See base class."""
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "dev_matched.tsv")),
              "dev_matched")
  
      def get_labels(self):
          """See base class."""
          return ["contradiction", "entailment", "neutral"]
  
      def _create_examples(self, lines, set_type):
          """Creates examples for the training and dev sets."""
          examples = []
          for (i, line) in enumerate(lines):
              if i == 0:
                  continue
              #print(len(line), i, line)
              guid = "%s-%s" % (set_type, line[0])
              text_a = line[8]
              text_b = line[9]
              label = line[-1]
              examples.append(
                  InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
          return examples
  
  
  class ColaProcessor(DataProcessor):
      """Processor for the CoLA data set (GLUE version)."""
  
      def get_train_examples(self, data_dir):
          """See base class."""
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
  
      def get_dev_examples(self, data_dir):
          """See base class."""
          return self._create_examples(
              self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")
  
      def get_labels(self):
          """See base class."""
          return ["0", "1"]
  
      def _create_examples(self, lines, set_type):
          """Creates examples for the training and dev sets."""
          examples = []
          for (i, line) in enumerate(lines):
              guid = "%s-%s" % (set_type, i)
              text_a = line[3]
              label = line[1]
              examples.append(
                  InputExample(guid=guid, text_a=text_a, text_b=None, label=label))
          return examples
  
  
  def convert_examples_to_features(examples, label_list, max_seq_length, tokenizer, logger):
      """Loads a data file into a list of `InputBatch`s."""
  
      if label_list is not None:
        label_map = {}
        for (i, label) in enumerate(label_list):
          label_map[label] = i
  
      features = []
      for (ex_index, example) in enumerate(examples):
          tokens_a = tokenizer.tokenize(example.text_a)
  
          tokens_b = None
          if example.text_b:
              tokens_b = tokenizer.tokenize(example.text_b)
  
          if tokens_b:
              # Modifies `tokens_a` and `tokens_b` in place so that the total
              # length is less than the specified length.
              # Account for [CLS], [SEP], [SEP] with "- 3"
              _truncate_seq_pair(tokens_a, tokens_b, max_seq_length - 3)
          else:
              # Account for [CLS] and [SEP] with "- 2"
              if len(tokens_a) > max_seq_length - 2:
                  tokens_a = tokens_a[0:(max_seq_length - 2)]
  
          # The convention in BERT is:
          # (a) For sequence pairs:
          #  tokens:   [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
          #  type_ids: 0   0  0    0    0     0       0 0    1  1  1  1   1 1
          # (b) For single sequences:
          #  tokens:   [CLS] the dog is hairy . [SEP]
          #  type_ids: 0   0   0   0  0     0 0
          #
          # Where "type_ids" are used to indicate whether this is the first
          # sequence or the second sequence. The embedding vectors for `type=0` and
          # `type=1` were learned during pre-training and are added to the wordpiece
          # embedding vector (and position vector). This is not *strictly* necessary
          # since the [SEP] token unambigiously separates the sequences, but it makes
          # it easier for the model to learn the concept of sequences.
          #
          # For classification tasks, the first vector (corresponding to [CLS]) is
          # used as as the "sentence vector". Note that this only makes sense because
          # the entire model is fine-tuned.
          tokens = []
          segment_ids = []
          tokens.append("[CLS]")
          segment_ids.append(0)
          for token in tokens_a:
              tokens.append(token)
              segment_ids.append(0)
          tokens.append("[SEP]")
          segment_ids.append(0)
  
          if tokens_b:
              for token in tokens_b:
                  tokens.append(token)
                  segment_ids.append(1)
              tokens.append("[SEP]")
              segment_ids.append(1)
  
          input_ids = tokenizer.convert_tokens_to_ids(tokens)
  
          # The mask has 1 for real tokens and 0 for padding tokens. Only real
          # tokens are attended to.
          input_mask = [1] * len(input_ids)
  
          # Zero-pad up to the sequence length.
          while len(input_ids) < max_seq_length:
              input_ids.append(0)
              input_mask.append(0)
              segment_ids.append(0)
  
          assert len(input_ids) == max_seq_length
          assert len(input_mask) == max_seq_length
          assert len(segment_ids) == max_seq_length
  
          if label_list is not None:
            label_id = label_map[example.label]
          else:
            label_id = float(example.label)
          #if ex_index < 5:
          #    logger.info("*** Example ***")
          #    logger.info("guid: %s" % (example.guid))
          #    logger.info("tokens: %s" % " ".join(
          #            [str(x) for x in tokens]))
          #    logger.info("input_ids: %s" % " ".join([str(x) for x in input_ids]))
          #    logger.info("input_mask: %s" % " ".join([str(x) for x in input_mask]))
          #    logger.info(
          #            "segment_ids: %s" % " ".join([str(x) for x in segment_ids]))
          #    logger.info("label: %s (id = %d)" % (example.label, label_id))
  
          features.append(
                  InputFeatures(input_ids=input_ids,
                                input_mask=input_mask,
                                segment_ids=segment_ids,
                                label_id=label_id))
      return features
  
  
  def _truncate_seq_pair(tokens_a, tokens_b, max_length):
      """Truncates a sequence pair in place to the maximum length."""
  
      # This is a simple heuristic which will always truncate the longer sequence
      # one token at a time. This makes more sense than truncating an equal percent
      # of tokens from each, since if one sequence is very short then each token
      # that's truncated likely contains more information than a longer sequence.
      while True:
          total_length = len(tokens_a) + len(tokens_b)
          if total_length <= max_length:
              break
          if len(tokens_a) > len(tokens_b):
              tokens_a.pop()
          else:
              tokens_b.pop()
  
  def accuracy(out, labels):
      outputs = np.argmax(out, axis=1)
      return np.sum(outputs == labels)
  
  def copy_optimizer_params_to_model(named_params_model, named_params_optimizer):
      """ Utility function for optimize_on_cpu and 16-bits training.
          Copy the parameters optimized on CPU/RAM back to the model on GPU
      """
      for (name_opti, param_opti), (name_model, param_model) in zip(named_params_optimizer, named_params_model):
          if name_opti != name_model:
              logger.error("name_opti != name_model: {} {}".format(name_opti, name_model))
              raise ValueError
          param_model.data.copy_(param_opti.data)
  
  def set_optimizer_params_grad(named_params_optimizer, named_params_model, test_nan=False):
      """ Utility function for optimize_on_cpu and 16-bits training.
          Copy the gradient of the GPU parameters to the CPU/RAMM copy of the model
      """
      is_nan = False
      for (name_opti, param_opti), (name_model, param_model) in zip(named_params_optimizer, named_params_model):
          if name_opti != name_model:
              logger.error("name_opti != name_model: {} {}".format(name_opti, name_model))
              raise ValueError
          if param_model.grad is not None:
              if test_nan and torch.isnan(param_model.grad).sum() > 0:
                  is_nan = True
              if param_opti.grad is None:
                  param_opti.grad = torch.nn.Parameter(param_opti.data.new().resize_(*param_opti.data.size()))
              param_opti.grad.data.copy_(param_model.grad.data)
          else:
              param_opti.grad = None
      return is_nan
  
  
  def main():
      parser = argparse.ArgumentParser()
  
      ## Required parameters
      parser.add_argument("--model", default="bert", type=str, required=True,
                          help="The model used for pretraining. Currently support bert or electra")
      parser.add_argument("--config_file", "--cf",
                                  help="pointer to the configuration file of the experiment", type=str, required=True)
      parser.add_argument("--config_file_path", default=None, type=str, required=True,
                                      help="The blob storage directory where config file is located.")
      parser.add_argument("--data_dir",
                          default=None,
                          type=str,
                          required=True,
                          help="The input data dir. Should contain the .tsv files (or other data files) for the task.")
      parser.add_argument("--task_name",
                          default=None,
                          type=str,
                          required=True,
                          help="The name of the task to train.")
      parser.add_argument("--output_dir", default=None, type=str, required=True,
                          help="The output directory where the model checkpoints will be written.")
                          
      ## Other parameters
      parser.add_argument("--jobid",
                          default=None,
                          type=str,
                          help="The optional job id suffix.")
      parser.add_argument("--checkpoint_file",
                          default=None,
                          type=str,
                          help="The path to checkpoint file which will be used to initializ the model parameters.")
      parser.add_argument("--checkpoint_from_finetune", 
                          default=False,
                          action='store_true',
                          help="whether the checkpoint model is saved from fune-tuning on a auxiliary task."
                         )
      parser.add_argument("--max_seq_length",
                          default=128,
                          type=int,
                          help="The maximum total input sequence length after WordPiece tokenization. \n"
                               "Sequences longer than this will be truncated, and sequences shorter \n"
                               "than this will be padded.")
      parser.add_argument("--do_train",
                          default=False,
                          action='store_true',
                          help="Whether to run training.")
      parser.add_argument("--do_eval",
                          default=False,
                          action='store_true',
                          help="Whether to run eval on the dev set.")
      parser.add_argument("--do_lower_case",
                          default=False,
                          action='store_true',
                          help="Set this flag if you are using an uncased model.")
      parser.add_argument("--train_batch_size",
                          default=32,
                          type=int,
                          help="Total batch size for training.")
      parser.add_argument("--eval_batch_size",
                          default=8,
                          type=int,
                          help="Total batch size for eval.")
      parser.add_argument("--learning_rate",
                          default=5e-5,
                          type=float,
                          help="The initial learning rate for Adam.")
      parser.add_argument("--num_train_epochs",
                          default=3.0,
                          type=float,
                          help="Total number of training epochs to perform.")
      parser.add_argument("--warmup_proportion",
                          default=0.1,
                          type=float,
                          help="Proportion of training to perform linear learning rate warmup for. "
                               "E.g., 0.1 = 10%% of training.")
      parser.add_argument("--no_cuda",
                          default=False,
                          action='store_true',
                          help="Whether not to use CUDA when available")
      parser.add_argument("--local_rank",
                          type=int,
                          default=-1,
                          help="local_rank for distributed training on gpus")
      parser.add_argument('--seed', 
                          type=int, 
                          default=42,
                          help="random seed for initialization")
      parser.add_argument('--gradient_accumulation_steps',
                          type=int,
                          default=1,
                          help="Number of updates steps to accumulate before performing a backward/update pass.")                       
      parser.add_argument('--optimize_on_cpu',
                          default=False,
                          action='store_true',
                          help="Whether to perform optimization and keep the optimizer averages on CPU")
      parser.add_argument('--fp16',
                          default=False,
                          action='store_true',
                          help="Whether to use 16-bit float precision instead of 32-bit")
      parser.add_argument('--loss_scale',
                          type=float, default=128,
                          help='Loss scaling, positive power of 2 values can improve fp16 convergence.')
      parser.add_argument('--step_per_log',
                          type=int, default=5,
                          help='Number of updates steps to log metrics.')
      parser.add_argument("--process_count_per_node", default=1, type=int,
                          help="Total number of process count to launch per node.")   
  
      args = parser.parse_args()
  
      #run = Run.get_context()
      
      processors = {
          "cola": ColaProcessor,
          "mnli": MnliProcessor,
          "mrpc": MrpcProcessor,
          "qqp": QQPProcessor,
          "qnli": QNLIProcessor,
          "sst2": SST2Processor,
          "stsb": STSBProcessor,
          "rte": RTEProcessor,
      }
  
  
      #comm = DistributedCommunicator(accumulation_step=args.gradient_accumulation_steps)
      #rank = comm.rank
      #local_rank = comm.local_rank
      #world_size = comm.world_size
      #is_master = rank == 0
      local_rank = 0
      world_size = 1
      
      # Prepare logger
      job_id = rutils.get_current_time()
      if args.jobid is not None:
          job_id = job_id + "_" + str(args.jobid)
      logger = rutils.FileLogging('%s_bert_fine_tune'%(job_id))
      logger.info("job id: %s"%job_id)
      logger.info(rutils.parser_args_to_dict(args))
  
      
      
      #logger.info("world size: {}, local rank: {}, global rank: {}, fp16: {}".format(world_size, local_rank, rank, args.fp16))
      logger.info("fp16: {}".format(args.fp16))
  
      #torch.cuda.set_device(local_rank)
      #device = torch.device("cuda", local_rank)
      device = torch.device("cuda")
      hostname = socket.gethostname()
      n_gpu = torch.cuda.device_count()
      logger.info("host: {}, device: {}, n_gpu: {}".format(hostname, device, n_gpu))
  
      # extract config
      job_config = BertJobConfiguration(config_file_path=os.path.join(args.config_file_path, args.config_file))
      
      #if os.path.exists(args.output_dir) and os.listdir(args.output_dir):
      #    raise ValueError("Output directory () already exists and is not empty.")
      #os.makedirs(args.output_dir, exist_ok=True)
      output_model_file = os.path.join(args.output_dir, job_id+ "_pytorch_model_fine_tune.bin")
  
      if args.gradient_accumulation_steps < 1:
          raise ValueError("Invalid gradient_accumulation_steps parameter: {}, should be >= 1".format(
                              args.gradient_accumulation_steps))
  
      #if local_rank == -1:
      #    args.train_batch_size = int(args.train_batch_size / args.gradient_accumulation_steps)
  
      random.seed(args.seed)
      np.random.seed(args.seed)
      torch.manual_seed(args.seed)
  
      if not args.do_train and not args.do_eval:
          raise ValueError("At least one of `do_train` or `do_eval` must be True.")
  
      task_name = args.task_name.lower()
  
      #is_master = (local_rank == -1 or rank == 0)
      is_master = True
      if task_name not in processors:
          raise ValueError("Task not found: %s" % (task_name))
  
      processor = processors[task_name]()
      label_list = processor.get_labels()
  
      tokenizer = BertTokenizer.from_pretrained(job_config.get_token_file_type(), do_lower_case=args.do_lower_case)
  
      train_examples = None
      num_train_steps = None
      if args.do_train:
          train_examples = processor.get_train_examples(args.data_dir)
          num_train_steps = int(
              len(train_examples) / args.train_batch_size / args.gradient_accumulation_steps * args.num_train_epochs)
  
      # Prepare model
      model_name = args.model
      model_config = job_config.get_model_config()
      if model_name == 'bert':
          config = BertConfig(**model_config)
          config.vocab_size = len(tokenizer.vocab)
          if task_name == "stsb":
              model = BertForSequenceRegression(config)
          else:
              num_labels = len(processor.get_labels())
              model = BertForSequenceClassification(config, num_labels=num_labels)
      elif model_name == 'electra':
          config = ElectraConfig(**model_config)
          config.vocab_size = len(tokenizer.vocab)
          if task_name == "stsb":
              model = ElectraForSequenceRegression(config)
          else:
              num_labels = len(processor.get_labels())
              model = ElectraForSequenceClassification(config, num_labels=num_labels)
  
  
      #model = BertForSequenceClassification.from_pretrained(args.bert_model, 
      #            cache_dir=PYTORCH_PRETRAINED_BERT_CACHE / 'distributed_{}'.format(local_rank), num_labels=num_labels)
  
      # Load checkpoint if specified
      if os.path.exists(str(args.checkpoint_file)):
          state_dict = torch.load(args.checkpoint_file)
          if args.checkpoint_from_finetune is True:
              state_dict = {'.'.join(key.split('.')[2:]): val for (key, val) in state_dict.items() if model_name in key}
              logger.info("Extract the model component in state_dict from a fine-tuned checkpoint.")
          if model_name == 'bert':
              model.bert.load_state_dict(state_dict)
          elif model_name == 'electra':
              model.electra.load_state_dict(state_dict)
          logger.info("Set the model parameter from the checkpoint %s"%args.checkpoint_file) 
  
  
      #comm.register_model(model, args.fp16)
      if args.fp16:
          model.half()
      model.to(device)
      if n_gpu > 1:
          model = torch.nn.DataParallel(model)
  
      if args.do_train:
   
          param_optimizer = list(model.named_parameters())
  
          # hack to remove pooler, which is not used
          # thus it produce None grad that break apex
          param_optimizer = [n for n in param_optimizer if 'pooler' not in n[0]]
  
          no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
          optimizer_grouped_parameters = [
              {'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
              {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
              ]
          t_total = num_train_steps // world_size
  
          if args.fp16:
              try:
                  from apex.optimizers import FP16_Optimizer
                  from apex.optimizers import FusedAdam
              except ImportError:
                  raise ImportError("Please install apex from https://www.github.com/nvidia/apex to run this.")
  
              optimizer = FusedAdam(optimizer_grouped_parameters,
                                  lr=args.learning_rate,
                                  bias_correction=False,
                                  max_grad_norm=1.0)
              if args.loss_scale == 0:
                  optimizer = FP16_Optimizer(optimizer, dynamic_loss_scale=True)
              else:
                  optimizer = FP16_Optimizer(optimizer, static_loss_scale=args.loss_scale)
          else:
              optimizer = BertAdam(optimizer_grouped_parameters,
                              lr=args.learning_rate,
                              warmup=args.warmup_proportion,
                              t_total=t_total)
          #if is_master:
          logger.info('lr: {}'.format(np.float(args.learning_rate)))
   
          train_features = convert_examples_to_features(
              train_examples, label_list, args.max_seq_length, tokenizer, logger)
          logger.info("***** Running training *****")
          logger.info("  Num examples = %d"%(len(train_examples)))
          logger.info("  Batch size = %d"%(args.train_batch_size))
          logger.info("  Num steps = %d"%(num_train_steps))
          all_input_ids = torch.tensor([f.input_ids for f in train_features], dtype=torch.long)
          all_input_mask = torch.tensor([f.input_mask for f in train_features], dtype=torch.long)
          all_segment_ids = torch.tensor([f.segment_ids for f in train_features], dtype=torch.long)
          if task_name == "stsb":
              all_label_ids = torch.tensor([f.label_id for f in train_features], dtype=torch.float)
          else:
              all_label_ids = torch.tensor([f.label_id for f in train_features], dtype=torch.long)
          train_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_label_ids)
          if local_rank != -1 and world_size > 1:
              train_sampler = DistributedSampler(train_data)
          else:
              train_sampler = RandomSampler(train_data)
          train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=args.train_batch_size)
  
          global_step, tr_loss = 0, 0
          model.train()
          for _ in trange(int(args.num_train_epochs), desc="Epoch"):
              for _, batch in enumerate(tqdm(train_dataloader, desc="Iteration")):
                  batch = tuple(t.to(device) for t in batch)
                  input_ids, input_mask, segment_ids, label_ids = batch
                  loss = model(input_ids, segment_ids, input_mask, label_ids)
  
                  if n_gpu > 1:
                      # average loss for multi-gpu
                      loss = loss.mean()
  
                  loss = loss / args.gradient_accumulation_steps
                  loss.backward()
                  global_step += 1
                  tr_loss += loss.item()
                  #if comm.synchronize():
                  lr_this_step = args.learning_rate * warmup_linear(global_step/t_total, args.warmup_proportion)
                  for param_group in optimizer.param_groups:
                          param_group['lr'] = lr_this_step
                  optimizer.step()
                  model.zero_grad()
                  if is_master and (global_step + 1) % args.step_per_log == 0:
                      logger.info('train_loss: {}'.format( np.float(tr_loss / args.step_per_log)))
                      tr_loss = 0
          if is_master:
              # Save a trained model
              torch.save(model.state_dict(), output_model_file)
              logger.info('model checkpoint saved at %s'%output_model_file)
  
      if args.do_eval and is_master:
          eval_examples = processor.get_dev_examples(args.data_dir)
          eval_features = convert_examples_to_features(
              eval_examples, label_list, args.max_seq_length, tokenizer, logger)
          logger.info("***** Running evaluation *****")
          logger.info("  Num examples = %d"%len(eval_examples))
          logger.info("  Batch size = %d"%args.eval_batch_size)
          all_input_ids = torch.tensor([f.input_ids for f in eval_features], dtype=torch.long)
          all_input_mask = torch.tensor([f.input_mask for f in eval_features], dtype=torch.long)
          all_segment_ids = torch.tensor([f.segment_ids for f in eval_features], dtype=torch.long)
          if task_name == "stsb":
              all_label_ids = torch.tensor([f.label_id for f in eval_features], dtype=torch.float)
          else:
              all_label_ids = torch.tensor([f.label_id for f in eval_features], dtype=torch.long)
          eval_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_label_ids)
          # Run prediction for full data
          eval_sampler = SequentialSampler(eval_data)
          eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=args.eval_batch_size)
          model.eval()
          eval_loss, eval_accuracy = 0, 0
          nb_eval_steps, nb_eval_examples = 0, 0
          logits_list = []
          pred_list = []
          label_list = []
          for input_ids, input_mask, segment_ids, label_ids in eval_dataloader:
              input_ids = input_ids.to(device)
              input_mask = input_mask.to(device)
              segment_ids = segment_ids.to(device)
              label_ids = label_ids.to(device)
              with torch.no_grad():
                  tmp_eval_loss = model(input_ids, segment_ids, input_mask, label_ids)
                  logits = model(input_ids, segment_ids, input_mask)
              logits = logits.detach().cpu().numpy()
              label_ids = label_ids.to('cpu').numpy()
              if task_name == "stsb":
                  logits_list += list(logits)
                  label_list += list(label_ids)
              if task_name == "cola":
                  preds = np.argmax(logits, axis=1)
                  pred_list += list(preds)
                  label_list += list(label_ids)
              tmp_eval_accuracy = accuracy(logits, label_ids)
              eval_loss += tmp_eval_loss.mean().item()
              eval_accuracy += tmp_eval_accuracy
              nb_eval_examples += input_ids.size(0)
              nb_eval_steps += 1
  
          eval_loss = eval_loss / nb_eval_steps
          eval_accuracy = eval_accuracy / nb_eval_examples
          if task_name == "stsb":
              # use spearman correlation as eval metric for STS-B to be consistent with Electra paper
              from scipy.stats import spearmanr
              eval_accuracy = spearmanr(label_list, logits_list)[0]
          if task_name == "cola":
              from sklearn.metrics import matthews_corrcoef
              eval_mcc = matthews_corrcoef(label_list, pred_list)
          result = {'eval_loss': eval_loss,
                    'eval_accuracy': eval_accuracy}
          if task_name == "cola":
              result['matthews_corrcoef'] = eval_mcc
          logger.info("***** Eval results *****")
          for key in sorted(result.keys()):
              logger.info("  %s = %s"%(key, str(result[key])))
              #logger.info(key, str(result[key]))
  
  
  if __name__ == "__main__":
      main()
  