#!/usr/bin/env python3 # coding=utf-8 # Copyright 2018 Google AI, Google Brain and Carnegie Mellon University Authors and the HuggingFace Inc. team. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. # # 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. """ Conditional text generation with the auto-regressive models of the library (GPT/GPT-2/Transformer-XL/XLNet) """ from __future__ import absolute_import, division, print_function, unicode_literals import argparse import glob import logging import os import pickle import random cwd = os.getcwd() print(f"Current working dir is {cwd}") import sys sys.path.append('./') pt_path = os.path.join( cwd, 'pytorch_transformers') sys.path.append(pt_path) print(f"Pytorch Transformer {pt_path}") import torch import torch.nn.functional as F import numpy as np from torch.utils.data import DataLoader, Dataset, SequentialSampler, RandomSampler, TensorDataset from torch.utils.data.distributed import DistributedSampler from tqdm import tqdm, trange from pytorch_transformers import GPT2Config, OpenAIGPTConfig, XLNetConfig, TransfoXLConfig, BertConfig from pytorch_transformers import GPT2LMHeadModel, GPT2Tokenizer, GPT2ForLatentConnector from pytorch_transformers import OpenAIGPTLMHeadModel, OpenAIGPTTokenizer from pytorch_transformers import XLNetLMHeadModel, XLNetTokenizer from pytorch_transformers import TransfoXLLMHeadModel, TransfoXLTokenizer from pytorch_transformers import BertForLatentConnector, BertTokenizer import pytorch_transformers from collections import defaultdict from modules import VAE from utils import (TextDataset_Split, TextDataset_2Tokenizers, BucketingDataLoader) from metrics import Bleu, SelfBleu import pdb 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__) MAX_LENGTH = int(10000) # Hardcoded max length to avoid infinite loop ALL_MODELS = sum((tuple(conf.pretrained_config_archive_map.keys()) for conf in (GPT2Config, OpenAIGPTConfig, XLNetConfig, TransfoXLConfig)), ()) MODEL_CLASSES = { 'gpt2': (GPT2Config, GPT2LMHeadModel, GPT2Tokenizer), 'bert': (BertConfig, BertForLatentConnector, BertTokenizer) } # Padding text to help Transformer-XL and XLNet with short prompts as proposed by Aman Rusia # in https://github.com/rusiaaman/XLNet-gen#methodology # and https://medium.com/@amanrusia/xlnet-speaks-comparison-to-gpt-2-ea1a4e9ba39e PADDING_TEXT = """ In 1991, the remains of Russian Tsar Nicholas II and his family (except for Alexei and Maria) are discovered. The voice of Nicholas's young son, Tsarevich Alexei Nikolaevich, narrates the remainder of the story. 1883 Western Siberia, a young Grigori Rasputin is asked by his father and a group of men to perform magic. Rasputin has a vision and denounces one of the men as a horse thief. Although his father initially slaps him for making such an accusation, Rasputin watches as the man is chased outside and beaten. Twenty years later, Rasputin sees a vision of the Virgin Mary, prompting him to become a priest. Rasputin quickly becomes famous, with people, even a bishop, begging for his blessing. <eod> </s> <eos>""" def set_seed(args): np.random.seed(args.seed) torch.manual_seed(args.seed) if args.n_gpu > 0: torch.cuda.manual_seed_all(args.seed) def load_and_cache_examples(args, tokenizer, evaluate=False): if isinstance(tokenizer, list): dataset = TextDataset_2Tokenizers(tokenizer, args, file_path=args.eval_data_file if evaluate else args.train_data_file, block_size=args.block_size) else: dataset = TextDataset_Split(tokenizer, args, file_path=args.eval_data_file if evaluate else args.train_data_file, block_size=args.block_size) return dataset def build_dataload_and_cache_examples(args, tokenizer, evaluate=False): if isinstance(tokenizer, list): if not evaluate: args.batch_size = args.per_gpu_train_batch_size * max(1, args.n_gpu) file_path=args.train_data_file else: args.batch_size = args.per_gpu_eval_batch_size * max(1, args.n_gpu) file_path=args.eval_data_file dataloader = BucketingDataLoader(file_path, args.batch_size, args.max_seq_length, tokenizer, args, bucket=100, shuffle=False) else: pass return dataloader def top_k_top_p_filtering(logits, top_k=0, top_p=0.0, filter_value=-float('Inf')): """ Filter a distribution of logits using top-k and/or nucleus (top-p) filtering Args: logits: logits distribution shape (vocabulary size) top_k > 0: keep only top k tokens with highest probability (top-k filtering). top_p > 0.0: keep the top tokens with cumulative probability >= top_p (nucleus filtering). Nucleus filtering is described in Holtzman et al. (http://arxiv.org/abs/1904.09751) From: https://gist.github.com/thomwolf/1a5a29f6962089e871b94cbd09daf317 """ assert logits.dim() == 1 # batch size 1 for now - could be updated for more but the code would be less clear # top-k top_k = min(top_k, logits.size(-1)) # Safety check if top_k > 0: # Remove all tokens with a probability less than the last token of the top-k indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None] logits[indices_to_remove] = filter_value # top-p if top_p > 0.0: sorted_logits, sorted_indices = torch.sort(logits, descending=True) cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1) # Remove tokens with cumulative probability above the threshold sorted_indices_to_remove = cumulative_probs > top_p # Shift the indices to the right to keep also the first token above the threshold sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone() sorted_indices_to_remove[..., 0] = 0 indices_to_remove = sorted_indices[sorted_indices_to_remove] logits[indices_to_remove] = filter_value return logits def sample_sequence(model, length, context, num_samples=1, temperature=1, top_k=0, top_p=0.0, is_xlnet=False, device='cpu', decoder_tokenizer=None, max_seq_length=-1): context = torch.tensor(context, dtype=torch.long, device=device) context = context.unsqueeze(0).repeat(num_samples, 1) generated = context gen_seq_length = 0 with torch.no_grad(): while True: inputs = {'input_ids': generated} outputs = model(**inputs) # Note: we could also use 'past' with GPT-2/Transfo-XL/XLNet (cached hidden-states) next_token_logits = outputs[0][0, -1, :] / temperature filtered_logits = top_k_top_p_filtering(next_token_logits, top_k=top_k, top_p=top_p) next_token = torch.multinomial(F.softmax(filtered_logits, dim=-1), num_samples=1) generated = torch.cat((generated, next_token.unsqueeze(0)), dim=1) gen_seq_length += 1 if next_token.unsqueeze(0)[0,0].item() == decoder_tokenizer.encode('<EOS>')[0]: break if max_seq_length>0 and gen_seq_length>max_seq_length: break return generated def sample_sequence_conditional(model, length, context, past=None, num_samples=1, temperature=1, top_k=0, top_p=0.0, device='cpu', decoder_tokenizer=None, max_seq_length=-1): context = torch.tensor(context, dtype=torch.long, device=device) context = context.unsqueeze(0).repeat(num_samples, 1) generated = context gen_seq_length = 0 with torch.no_grad(): while True: inputs = {'input_ids': generated, 'past': past} outputs = model(**inputs) # Note: we could also use 'past' with GPT-2/Transfo-XL/XLNet (cached hidden-states) next_token_logits = outputs[0][0, -1, :] / temperature filtered_logits = top_k_top_p_filtering(next_token_logits, top_k=top_k, top_p=top_p) next_token = torch.multinomial(F.softmax(filtered_logits, dim=-1), num_samples=1) generated = torch.cat((generated, next_token.unsqueeze(0)), dim=1) gen_seq_length += 1 # pdb.set_trace() if next_token.unsqueeze(0)[0,0].item() == decoder_tokenizer.encode('<EOS>')[0]: break if max_seq_length>0 and gen_seq_length>max_seq_length: break return generated def evaluate_generation_from_gpt2(model, decoder_tokenizer, args, ns=1): loc = torch.zeros([args.nz]).to(args.device) scale = torch.ones([args.nz]).to(args.device) prior = torch.distributions.normal.Normal(loc, scale) context_tokens = decoder_tokenizer.encode('<BOS>') count = 0 result = defaultdict(str) for i in tqdm(range(args.num_sents)): with torch.no_grad(): out = sample_sequence( model=model, context=context_tokens, length=args.max_seq_length, # Chunyuan: Fix length; or use <EOS> to complete a sentence temperature=args.temperature, top_k=args.top_k, top_p=args.top_p, device=args.device, decoder_tokenizer = decoder_tokenizer, max_seq_length = args.max_seq_length ) text_x1 = decoder_tokenizer.decode(out[0,:].tolist(), clean_up_tokenization_spaces=True) text_x1 = text_x1.split()[1:-1] text_x1 = ' '.join(text_x1) + '\n' result[i] = text_x1 if args.use_philly: print("PROGRESS: {}%".format( round(100 * i /args.num_sents , 4))) with open(args.output_generation_file, "w") as writer: logger.info("***** SHOW generated sentences from prior *****") for key in sorted(result.keys()): # logger.info(" %s \n %s", key, str(result[key])) # writer.write("%s \n %s\n" % (key, str(result[key]))) writer.write("%s" % str(result[key])) return result # bleu = evaluate_bleu(results, args) def main(): parser = argparse.ArgumentParser() parser.add_argument("--train_data_file", default=None, type=str, required=True, help="The input training data file (a text file).") parser.add_argument("--eval_data_file", default=None, type=str, help="An input evaluation data file to evaluate the perplexity on (a text file).") parser.add_argument("--checkpoint_dir", default=None, type=str, required=True, help="The directory where checkpoints are saved.") parser.add_argument("--output_dir", default=None, type=str, required=True, help="The output directory where the model predictions and checkpoints will be written.") parser.add_argument("--dataset", default='Snli', type=str, help="The dataset.") ## Variational auto-encoder parser.add_argument("--latent_size", default=32, type=int, help="Latent space dimension.") parser.add_argument("--total_sents", default=10, type=int, help="Total sentences to test recontruction.") parser.add_argument("--num_sents", default=10, type=int, help="Total sentences to generate.") ## Encoder options parser.add_argument("--encoder_model_type", default="bert", type=str, help="The encoder model architecture to be fine-tuned.") parser.add_argument("--encoder_model_name_or_path", default="bert-base-cased", type=str, help="The encoder model checkpoint for weights initialization.") parser.add_argument("--encoder_config_name", default="", type=str, help="Optional pretrained config name or path if not the same as model_name_or_path") parser.add_argument("--encoder_tokenizer_name", default="", type=str, help="Optional pretrained tokenizer name or path if not the same as model_name_or_path") ## Decoder options parser.add_argument("--decoder_model_type", default="gpt2", type=str, help="The decoder model architecture to be fine-tuned.") parser.add_argument("--decoder_model_name_or_path", default="bert-base-cased", type=str, help="The decoder model checkpoint for weights initialization.") parser.add_argument("--decoder_config_name", default="", type=str, help="Optional pretrained config name or path if not the same as model_name_or_path") parser.add_argument("--decoder_tokenizer_name", default="", type=str, help="Optional pretrained tokenizer name or path if not the same as model_name_or_path") parser.add_argument("--per_gpu_train_batch_size", default=1, type=int, help="Batch size per GPU/CPU for training.") parser.add_argument("--per_gpu_eval_batch_size", default=1, type=int, help="Batch size per GPU/CPU for evaluation.") parser.add_argument('--gloabl_step_eval', type=int, default=661, help="Evaluate the results at the given global step") parser.add_argument("--max_seq_length", default=512, type=int, help="Optional input sequence length before tokenization. The sequence will be dropped if it is longer the max_seq_length") ## Variational auto-encoder parser.add_argument("--nz", default=32, type=int, help="Latent space dimension.") parser.add_argument("--prompt", type=str, default="") parser.add_argument("--padding_text", type=str, default="") parser.add_argument("--length", type=int, default=20) parser.add_argument("--temperature", type=float, default=1.0) parser.add_argument("--top_k", type=int, default=0) parser.add_argument("--top_p", type=float, default=0.9) parser.add_argument("--no_cuda", action='store_true', help="Avoid using CUDA when available") parser.add_argument('--seed', type=int, default=42, help="random seed for initialization") parser.add_argument("--block_size", default=-1, type=int, help="Optional input sequence length after tokenization." "The training dataset will be truncated in block of this size for training." "Default to the model max input length for single sentence inputs (take into account special tokens).") parser.add_argument("--do_lower_case", action='store_true', help="Set this flag if you are using an uncased model.") parser.add_argument("--use_philly", action='store_true', help="Use Philly for computing.") args = parser.parse_args() args.device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu") args.n_gpu = torch.cuda.device_count() set_seed(args) args.decoder_model_type = args.decoder_model_type.lower() global_step = args.gloabl_step_eval output_decoder_dir = os.path.join(args.checkpoint_dir, 'checkpoint-{}'.format(global_step)) checkpoints = [ output_decoder_dir ] logger.info("Evaluate the following checkpoints: %s", checkpoints) # Load a trained Decoder model and vocabulary that you have fine-tuned decoder_config_class, decoder_model_class, decoder_tokenizer_class = MODEL_CLASSES[args.decoder_model_type] model_decoder = decoder_model_class.from_pretrained(output_decoder_dir) tokenizer_decoder = decoder_tokenizer_class.from_pretrained(args.decoder_tokenizer_name if args.decoder_tokenizer_name else args.decoder_model_name_or_path, do_lower_case=args.do_lower_case) model_decoder.to(args.device) if args.block_size <= 0: args.block_size = tokenizer_decoder.max_len_single_sentence # Our input block size will be the max possible for the model args.block_size = min(args.block_size, tokenizer_decoder.max_len_single_sentence) # pdb.set_trace() # Chunyuan: Add Padding token to GPT2 special_tokens_dict = {'pad_token': '<PAD>', 'bos_token': '<BOS>', 'eos_token': '<EOS>'} num_added_toks = tokenizer_decoder.add_special_tokens(special_tokens_dict) print('We have added', num_added_toks, 'tokens to GPT2') model_decoder.resize_token_embeddings(len(tokenizer_decoder)) # Notice: resize_token_embeddings expect to receive the full size of the new vocabulary, i.e. the length of the tokenizer. assert tokenizer_decoder.pad_token == '<PAD>' # Evaluation if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) args.output_generation_file = os.path.join(args.output_dir, f"generation_from_gpt2_t{args.temperature}_p{args.top_p}.txt") # args.output_generation_file = args.train_data_file result = evaluate_generation_from_gpt2(model_decoder, tokenizer_decoder, args) bleu5 = Bleu(test_text= args.output_generation_file, real_text=args.eval_data_file, num_real_sentences=args.num_sents, num_fake_sentences=args.num_sents, gram=5).get_score() logger.info(f'The bleu score is {bleu5}') sbleu5 = SelfBleu(test_text= args.output_generation_file, num_sentences=args.num_sents, gram=5).get_score() logger.info(f'The self-bleu score is {sbleu5}') args.eval_results_file = os.path.join(args.output_dir, f"eval_results_t{args.temperature}_p{args.top_p}.txt") eval_results = {'bleu5':bleu5 , 'sbleu5':sbleu5} with open(args.eval_results_file, "w") as writer: logger.info("***** SHOW the quantative evalution results *****") for key in sorted(eval_results.keys()): writer.write("%s %s" % (key, str(eval_results[key])) ) if __name__ == '__main__': main()