def read_args(default_config="confs/base.json", **parser_kwargs): parser = argparse.ArgumentParser(**parser_kwargs) parser.add_argument("--config", "-c", type=str, default=default_config) args, _ = parser.parse_known_args() options = argconf.options_from_json("confs/options.json") config = argconf.config_from_json(args.config) return edict(argconf.parse_args(options, config))
def main(): description = "Trains or evaluates a model." epilog = "Usage:\npython -m deeplm.utils.run -c confs/default.json --action train" parser = argparse.ArgumentParser(description=description, epilog=epilog) parser.add_argument("-c", "--config", dest="config", type=str, default="confs/default.json") parser.add_argument("--action", choices=["train", "eval"], type=str, default="train") args = parser.parse_args() conf = argconf.config_from_json(args.config) if args.action == "train": train(conf)
def main(): description = "Trains a Magpie model." epilog = "Usage:\npython -m magpie.utils.train --config confs/cae_model_config.json "\ "--options confs/options.json > cae_train_log" parser = argparse.ArgumentParser(description=description, epilog=epilog) parser.add_argument("--config", type=str, default="confs/cae_model_config.json") parser.add_argument("--options", type=str, default="confs/options.json") args, _ = parser.parse_known_args() option_dict = argconf.options_from_json(args.options) config = argconf.config_from_json(args.config) config = argconf.parse_args(option_dict, config=config) trainer_cls = model.find_trainer(config["trainer_type"]) trainer = trainer_cls(config) trainer.train()
def main(): parser = argparse.ArgumentParser(description="Runs the server for tokenization as a service.", epilog="Usage:\npython -m trident.utils.run_server -c confs/default.json") parser.add_argument("--config", "-c", type=str, default="confs/default.json") parser.add_argument("--port", "-p", type=int, default=8080) args = parser.parse_args() config = argconf.config_from_json(args.config) vocab, _ = deeplm.data.Seq2SeqDataset.iters(config) model = deeplm.model.InterstitialModel(vocab, config) sd = torch.load(config["resume"]) model.load_state_dict(sd["state"]) model.avg_param = sd["ema"] model.steps_ema = sd["steps_ema"] model.cuda() model.eval() model.load_ema_params() cherrypy.config.update({"global": {"engine.autoreload.on": False}}) cherrypy.quickstart(TokenizationServer(vocab, model), "/", {"/": {"request.dispatch": cherrypy.dispatch.MethodDispatcher()}})
def main(): local_rank = -1 parser = argparse.ArgumentParser() parser.add_argument("--config", "-c", type=str, required=True) args, _ = parser.parse_known_args() options = argconf.options_from_json("confs/options.json") config = argconf.config_from_json(args.config) args = edict(argconf.parse_args(options, config)) args.local_rank = local_rank args.on_memory = True if args.local_rank == -1 or args.no_cuda: device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu") n_gpu = torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank) device = torch.device("cuda", args.local_rank) n_gpu = 1 # Initializes the distributed backend which will take care of sychronizing nodes/GPUs torch.distributed.init_process_group(backend='nccl') logger.info( "device: {} n_gpu: {}, distributed training: {}, 16-bits training: {}". format(device, n_gpu, bool(args.local_rank != -1), args.fp16)) if args.gradient_accumulation_steps < 1: raise ValueError( "Invalid gradient_accumulation_steps parameter: {}, should be >= 1" .format(args.gradient_accumulation_steps)) args.train_batch_size = args.train_batch_size // args.gradient_accumulation_steps random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) if n_gpu > 0: torch.cuda.manual_seed_all(args.seed) if not args.do_train: raise ValueError( "Training is currently the only implemented execution option. Please set `do_train`." ) if os.path.exists(args.workspace) and os.listdir(args.workspace): raise ValueError( "Output directory ({}) already exists and is not empty.".format( args.workspace)) if not os.path.exists(args.workspace): os.makedirs(args.workspace) tokenizer = BertTokenizer.from_pretrained(args.model_file, do_lower_case=True) #train_examples = None num_train_optimization_steps = None if args.do_train: print("Loading Train Dataset", args.train_file) train_dataset = BERTDataset(args.train_file, tokenizer, seq_len=args.max_seq_length, corpus_lines=None, on_memory=args.on_memory) num_train_optimization_steps = int( len(train_dataset) / args.train_batch_size / args.gradient_accumulation_steps) * args.num_train_epochs if args.local_rank != -1: num_train_optimization_steps = num_train_optimization_steps // torch.distributed.get_world_size( ) # Prepare model model = BertForPreTraining.from_pretrained(args.model_file) if args.fp16: model.half() model.to(device) if args.local_rank != -1: try: from apex.parallel import DistributedDataParallel as DDP except ImportError: raise ImportError( "Please install apex from https://www.github.com/nvidia/apex to use distributed and fp16 training." ) model = DDP(model) elif n_gpu > 1: model = torch.nn.DataParallel(model) # Prepare optimizer param_optimizer = list(model.named_parameters()) 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 }] 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 use distributed and fp16 training." ) 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=num_train_optimization_steps) global_step = 0 if args.do_train: logger.info("***** Running training *****") logger.info(" Num examples = %d", len(train_dataset)) logger.info(" Batch size = %d", args.train_batch_size) logger.info(" Num steps = %d", num_train_optimization_steps) if args.local_rank == -1: train_sampler = RandomSampler(train_dataset) else: #TODO: check if this works with current data generator from disk that relies on next(file) # (it doesn't return item back by index) train_sampler = DistributedSampler(train_dataset) train_dataloader = DataLoader(train_dataset, sampler=train_sampler, batch_size=args.train_batch_size) model.train() loss_fct = nn.CrossEntropyLoss(ignore_index=-1) for _ in trange(int(args.num_train_epochs), desc="Epoch"): tr_loss = 0 nb_tr_examples, nb_tr_steps = 0, 0 for step, batch in enumerate( tqdm(train_dataloader, desc="Iteration")): batch = tuple(t.to(device) for t in batch) input_ids, input_mask, segment_ids, lm_label_ids = batch prediction_scores, _ = model(input_ids, segment_ids, input_mask, lm_label_ids) loss = loss_fct( prediction_scores.view(-1, model.module.config.vocab_size), lm_label_ids.view(-1)).mean() if n_gpu > 1: loss = loss.mean() # mean() to average on multi-gpu. if args.gradient_accumulation_steps > 1: loss = loss / args.gradient_accumulation_steps if args.fp16: optimizer.backward(loss) else: loss.backward() tr_loss += loss.item() nb_tr_examples += input_ids.size(0) nb_tr_steps += 1 if (step + 1) % args.gradient_accumulation_steps == 0: if args.fp16: # modify learning rate with special warm up BERT uses # if args.fp16 is False, BertAdam is used that handles this automatically lr_this_step = args.learning_rate * warmup_linear( global_step / num_train_optimization_steps, args.warmup_proportion) for param_group in optimizer.param_groups: param_group['lr'] = lr_this_step optimizer.step() optimizer.zero_grad() global_step += 1 # Save a trained model logger.info("** ** * Saving fine - tuned model ** ** * ") model_to_save = model.module if hasattr( model, 'module') else model # Only save the model it-self output_model_file = os.path.join(args.workspace, "pytorch_model.bin") if args.do_train: torch.save(model_to_save.state_dict(), output_model_file)
def main(): def evaluate(dataloader, export=None): eval_loss, eval_accuracy = 0, 0 nb_eval_steps, nb_eval_examples = 0, 0 logits_list = [] iter_idx = 0 corr_x = [] corr_y = [] for input_ids, input_mask, segment_ids, label_ids in tqdm( dataloader, desc="Evaluating"): 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, mse=is_float) logits = model(input_ids, segment_ids, input_mask, mse=is_float) logits = logits.detach().cpu().numpy() if export is not None: logits_list.append(logits) label_ids = label_ids.to('cpu').numpy() if is_float: corr_x.extend(logits.flatten()) corr_y.extend(label_ids.flatten()) 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 # if (iter_idx + 1) % 1000 == 0 and export is not None: # torch.save((iter_idx, logits_list), export) iter_idx += 1 if export is not None: torch.save(logits_list, export) eval_loss = eval_loss / nb_eval_steps eval_accuracy = eval_accuracy / nb_eval_examples loss = tr_loss / nb_tr_steps if args.do_train else None if is_float: print(pearsonr(corr_x, corr_y)) print(spearmanr(corr_x, corr_y)) result = { 'eval_loss': eval_loss, 'eval_accuracy': eval_accuracy, 'global_step': global_step, 'loss': loss } return result local_rank = -1 parser = argparse.ArgumentParser() parser.add_argument("--config", "-c", type=str, required=True) args, _ = parser.parse_known_args() options = argconf.options_from_json("confs/options.json") config = argconf.config_from_json(args.config) args = edict(argconf.parse_args(options, config)) print(f"Using config: {args}") bv_utils.set_seed(args.seed) args.do_train = args.do_train and not args.do_test_only processors = { "cola": ColaProcessor, "mnli": MnliProcessor, "mrpc": MrpcProcessor, "sst2": SST2Processor, 'qnli': QnliProcessor, 'rte': RteProcessor, "imdb": IMDBSentenceProcessor, "qqp": QuoraProcessor, "sts": STSProcessor, "raw_sts_pair": RawSTSPairProcessor, "raw_pair": RawPairProcessor } device = torch.device("cuda" if torch.cuda.is_available() else "cpu") n_gpu = torch.cuda.device_count() logger.info( "device: {} n_gpu: {}, distributed training: {}, 16-bits training: {}". format(device, n_gpu, bool(local_rank != -1), args.fp16)) if args.gradient_accumulation_steps < 1: raise ValueError( "Invalid gradient_accumulation_steps parameter: {}, should be >= 1" .format(args.gradient_accumulation_steps)) args.train_batch_size = args.train_batch_size // args.gradient_accumulation_steps if not args.do_train and not args.do_eval: raise ValueError( "At least one of `do_train` or `do_eval` must be True.") # if os.path.exists(args.workspace) and os.listdir(args.workspace) and args.do_train: # raise ValueError("Output directory ({}) already exists and is not empty.".format(args.workspace)) if not os.path.exists(args.workspace): os.makedirs(args.workspace) task_name = args.task_name.lower() if task_name not in processors: raise ValueError("Task not found: %s" % (task_name)) processor = processors[task_name]() num_labels = args.n_labels label_list = processor.get_labels() tokenizer = BertTokenizer.from_pretrained(args.model_file, do_lower_case=args.uncased) num_train_optimization_steps = None train_examples = processor.get_train_examples(args.data_dir) num_train_optimization_steps = int( len(train_examples) / args.train_batch_size / args.gradient_accumulation_steps) * args.num_train_epochs if local_rank != -1: num_train_optimization_steps = num_train_optimization_steps // torch.distributed.get_world_size( ) # Prepare model cache_dir = os.path.join(PYTORCH_PRETRAINED_BERT_CACHE, 'distributed_{}'.format(local_rank)) model = BertForSequenceClassification.from_pretrained( args.model_file, cache_dir=cache_dir, num_labels=num_labels) if args.fp16: model.half() model.to(device) # sd = torch.load('qqp.pt') # sd = torch.load('sts.pt') # del sd['classifier.weight'] # del sd['classifier.bias'] # model.load_state_dict(sd, strict=False) if local_rank != -1: try: from apex.parallel import DistributedDataParallel as DDP except ImportError: raise ImportError( "Please install apex from https://www.github.com/nvidia/apex to use distributed and fp16 training." ) model = DDP(model) elif n_gpu > 1: model = torch.nn.DataParallel(model) # Prepare optimizer param_optimizer = list(model.named_parameters()) param_optimizer = list( filter( lambda x: x[0] in ("module.classifier.weight", "module.classifier.bias"), param_optimizer)) print(len(param_optimizer)) 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 }] 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 use distributed and fp16 training." ) 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=num_train_optimization_steps) global_step = 0 nb_tr_steps = 0 tr_loss = 0 train_features = convert_examples_to_features(train_examples, label_list, args.max_seq_length, tokenizer) is_float = isinstance(train_features[0].label_id, float) 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) all_label_ids = torch.tensor([f.label_id for f in train_features], dtype=torch.float if is_float else torch.long) train_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_label_ids) if local_rank == -1: train_sampler = RandomSampler(train_data) else: train_sampler = DistributedSampler(train_data) train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=args.train_batch_size) # BEGIN SST-2 -> QQP experiments # END SST-2 -> QQP experiments if args.do_train: 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_optimization_steps) model.train() for _ in trange(int(args.num_train_epochs), desc="Epoch"): tr_loss = 0 nb_tr_examples, nb_tr_steps = 0, 0 for step, 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, mse=is_float) if n_gpu > 1: loss = loss.mean() # mean() to average on multi-gpu. if args.gradient_accumulation_steps > 1: loss = loss / args.gradient_accumulation_steps if args.fp16: optimizer.backward(loss) else: loss.backward() tr_loss += loss.item() nb_tr_examples += input_ids.size(0) nb_tr_steps += 1 if (step + 1) % args.gradient_accumulation_steps == 0: if args.fp16: # modify learning rate with special warm up BERT uses # if args.fp16 is False, BertAdam is used that handles this automatically lr_this_step = args.learning_rate * warmup_linear( global_step / num_train_optimization_steps, args.warmup_proportion) for param_group in optimizer.param_groups: param_group['lr'] = lr_this_step optimizer.step() optimizer.zero_grad() global_step += 1 output_model_file = os.path.join(args.workspace, WEIGHTS_NAME) if args.do_train: # Save a trained model and the associated configuration model_to_save = model.module if hasattr( model, 'module') else model # Only save the model it-self torch.save(model_to_save.state_dict(), output_model_file) output_config_file = os.path.join(args.workspace, CONFIG_NAME) with open(output_config_file, 'w') as f: f.write(model_to_save.config.to_json_string()) # Load a trained model and config that you have fine-tuned config = BertConfig(output_config_file) model = BertForSequenceClassification(config, num_labels=num_labels) model.load_state_dict(torch.load(output_model_file)) elif args.do_test_only: convert = bv_utils.convert_single_to_dp if isinstance( model, torch.nn.DataParallel) else bv_utils.convert_dp_to_single model.load_state_dict(convert(torch.load(output_model_file))) else: # pass model = BertForSequenceClassification.from_pretrained( args.model_file, num_labels=num_labels) model.to(device) if args.export: model.eval() train_dataloader = DataLoader(train_data, batch_size=args.eval_batch_size, shuffle=False) with torch.no_grad(): evaluate(train_dataloader, export=args.export) return if args.visualize: model.eval() train_dataloader = DataLoader(train_data, batch_size=args.eval_batch_size, shuffle=False) with open(os.path.join(args.workspace, "viz_results.csv"), "w") as f: writer = None dir_a = bv_viz.choose_random_dir(list(model.parameters())) dir_b = bv_viz.choose_random_dir(list(model.parameters())) torch.save(dir_a, os.path.join(args.workspace, "viz_dir_a.pt")) torch.save(dir_b, os.path.join(args.workspace, "viz_dir_b.pt")) for a, b in bv_viz.contour_2d(model, dir_a, dir_b): result = evaluate(train_dataloader) result["a"] = a result["b"] = b if writer is None: writer = csv.DictWriter(f, fieldnames=result.keys()) writer.writeheader() for key in sorted(result.keys()): logger.info(" %s = %s", key, str(result[key])) writer.writerow(result) if args.do_eval and (local_rank == -1 or torch.distributed.get_rank() == 0): eval_examples = processor.get_test_examples( args.data_dir ) if args.do_test_only else processor.get_dev_examples(args.data_dir) eval_features = convert_examples_to_features(eval_examples, label_list, args.max_seq_length, tokenizer) 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) all_label_ids = torch.tensor( [f.label_id for f in eval_features], dtype=torch.long if isinstance(eval_features[0].label_id, int) else torch.float) 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() result = evaluate(eval_dataloader) output_eval_file = os.path.join(args.workspace, "eval_results.txt") with open(output_eval_file, "w") as writer: logger.info("***** Eval results *****") for key in sorted(result.keys()): logger.info(" %s = %s", key, str(result[key])) writer.write("%s = %s\n" % (key, str(result[key])))