def get_config(self): print(self.args.source_file) source_path = self.args.source_file.split('/')[:-1] source_path = '/'.join(source_path) + '/' print(source_path, 'source path', realpath, 'real path') if '774M' in source_path: print('774M', 'model specific configs') #self.use_common = False self.args.temperature = 1e-4 self.args.top_k = 100 if os.path.isfile(realpath + '/' + source_path + '/config.json'): print(realpath + '/' + source_path, 'config.json') with open(realpath + '/' + source_path + '/config.json', 'r') as f: hp_config = json.load(f) print(hp_config, 'before') self.config = GPT2Config( vocab_size_or_config_json_file=hp_config['vocab_size'], n_embd=hp_config['n_embd'], n_layer=hp_config['n_layer'], n_head=hp_config['n_head'], # intermediate_size=self.intermediate_size, # hidden_act=self.hidden_act, # hidden_dropout_prob=self.hidden_dropout_prob, # attention_probs_dropout_prob=self.attention_probs_dropout_prob, n_positions=hp_config['n_positions'], n_ctx=hp_config['n_ctx'] # type_vocab_size=self.type_vocab_size, # initializer_range=self.initializer_range ) else: self.config = GPT2Config() print(self.config)
def load_model(self): if self.args.quiet is False or True: print(self.args, 'args') if self.args.batch_size == -1: self.args.batch_size = 1 assert self.args.nsamples % self.args.batch_size == 0 seed = random.randint(0, 2147483647) np.random.seed(seed) torch.random.manual_seed(seed) torch.cuda.manual_seed(seed) self.device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") self.get_config() #self.get_args() # Load Model self.enc = self.get_encoder() if self.config is None: print('change config') self.config = GPT2Config() self.model = GPT2LMHeadModel(self.config) self.model = load_weight(self.model, self.state_dict) self.model.to(self.device) self.model.eval() print(self.config, 'config')
def main(): param_config = config() gpt_config = GPT2Config(vocab_size_or_config_json_file=param_config.input_dim, n_positions=param_config.sequence_length, n_ctx=param_config.sequence_length) model = GPT2LMHeadModel(gpt_config) # Load Data # Load Data if param_config.input == 'bytes': # Load Data for bytes _, validation_data = get_wili_data_bytes(param_config) else: # Load Data _, validation_data = get_wili_data(param_config) validation_loader = DataLoader(validation_data, batch_size=1, shuffle=False, drop_last=False) if param_config.model_checkpoint is not None: with open(param_config.model_checkpoint, 'rb') as f: state_dict = torch.load(f) model.load_state_dict(state_dict) print("Model Loaded From: {}".format(param_config.model_checkpoint)) model = model.to(device) predict(model, validation_loader, validation_data, param_config)
def text_generator(state_dict, given_starting_letter): seed = random.randint(0, 2147483647) np.random.seed(seed) torch.random.manual_seed(seed) torch.cuda.manual_seed(seed) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") enc = get_encoder() config = GPT2Config() model = GPT2LMHeadModel(config) model = load_weight(model, state_dict) model.to(device) model.eval() context_tokens = enc.encode(EXAMPLE_LETTER) generated = 0 out = sample_sequence( model=model, length=config.n_ctx // 2, context=context_tokens, start_token=None, batch_size=1, temperature=0.7, top_k=40, device=device, ) out = out[:, len(context_tokens):].tolist() text = enc.decode(out[0]) print(text) return text
def text_generator(state_dict): # parser = argparse.ArgumentParser() # parser.add_argument("--text", type=str, required=True) # parser.add_argument("--quiet", type=bool, default=False) # parser.add_argument("--nsamples", type=int, default=1) # parser.add_argument('--unconditional', action='store_true', help='If true, unconditional generation.') # parser.add_argument("--batch_size", type=int, default=-1) # parser.add_argument("--length", type=int, default=-1) # parser.add_argument("--temperature", type=float, default=0.7) # parser.add_argument("--top_k", type=int, default=40) # args = parser.parse_args() if args_quiet is False: print(args) # if args_batch_size == -1: args_batch_size = 1 assert args_nsamples % args_batch_size == 0 seed = random.randint(0, 2147483647) np.random.seed(seed) torch.random.manual_seed(seed) torch.cuda.manual_seed(seed) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Load Model enc = get_encoder() config = GPT2Config() model = GPT2LMHeadModel(config) model = load_weight(model, state_dict) model.to(device) model.eval() #if args_length == -1: args_length = config.n_ctx // 2 # elif args_length > config.n_ctx: # raise ValueError("Can't get samples longer than window size: %s" % config.n_ctx) # print(args.text) context_tokens = enc.encode(GPT2_seed_text) generated = 0 for _ in range(args_nsamples // args_batch_size): out = sample_sequence( model=model, length=args_length, context=context_tokens if not args_unconditional else None, start_token=enc.encoder['<|endoftext|>'] if args_unconditional else None, batch_size=args_batch_size, temperature=args_temperature, top_k=args_top_k, device=device ) out = out[:, len(context_tokens):].tolist() for i in range(args_batch_size): generated += 1 text = enc.decode(out[i]) if args_quiet is False: print("=" * 40 + " SAMPLE " + str(generated) + " " + "=" * 40) global GPT2_output GPT2_output = text print(text)
def text_generator(state_dict, param_prompt, param_nsamples, param_batch_size, param_length, param_temperature, param_top_k): #param_prompt = "Peter was a man" param_quiet = False #param_nsamples = 1 param_unconditional = None #param_batch_size = 1 #param_length = 5 #param_temperature = 0.95 #param_top_k = 100 if param_batch_size == -1: param_batch_size = 1 assert param_nsamples % param_batch_size == 0 seed = random.randint(0, 2147483647) np.random.seed(seed) torch.random.manual_seed(seed) torch.cuda.manual_seed(seed) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Load Model enc = get_encoder() config = GPT2Config() model = GPT2LMHeadModel(config) model = load_weight(model, state_dict) model.to(device) model.eval() if param_length == -1: param_length = config.n_ctx // 2 elif param_length > config.n_ctx: raise ValueError("Can't get samples longer than window size: %s" % config.n_ctx) response = param_prompt #print(param_prompt) context_tokens = enc.encode(param_prompt) generated = 0 for _ in range(param_nsamples // param_batch_size): out = sample_sequence( model=model, length=param_length, context=context_tokens if not param_unconditional else None, start_token=enc.encoder['<|endoftext|>'] if param_unconditional else None, batch_size=param_batch_size, temperature=param_temperature, top_k=param_top_k, device=device ) out = out[:, len(context_tokens):].tolist() for i in range(param_batch_size): generated += 1 text = enc.decode(out[i]) if param_quiet is False: response = "=" * 40 + " SAMPLE " + str(generated) + " " + "=" * 40 #return("=" * 40 + " SAMPLE " + str(generated) + " " + "=" * 40) response = param_prompt + text #return(text) return response
def text_generator(state_dict, args): if args.quiet is False: print(args) if args.batch_size == -1: args.batch_size = 1 assert args.nsamples % args.batch_size == 0 seed = random.randint(0, 2147483647) np.random.seed(seed) torch.random.manual_seed(seed) torch.cuda.manual_seed(seed) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Load Model enc = get_encoder() config = GPT2Config() model = GPT2LMHeadModel(config) model = load_weight(model, state_dict) model.to(device) model.eval() if args.length == -1: args.length = config.n_ctx // 2 elif args.length > config.n_ctx: raise ValueError("Can't get samples longer than window size: %s" % config.n_ctx) print(args.text) context_tokens = enc.encode(args.text) generated = 0 for _ in range(args.nsamples // args.batch_size): out = sample_sequence( model=model, length=args.length, context=context_tokens if not args.unconditional else None, start_token=enc.encoder['<|endoftext|>'] if args.unconditional else None, batch_size=args.batch_size, temperature=args.temperature, top_k=args.top_k, device=device) out = out[:, len(context_tokens):].tolist() for i in range(args.batch_size): generated += 1 text = enc.decode(out[i]) if args.quiet is False: print("=" * 40 + " SAMPLE " + str(generated) + " " + "=" * 40) print(text)
def main(): param_config = config() gpt_config = GPT2Config( vocab_size_or_config_json_file=param_config.input_dim, n_positions=param_config.sequence_length, n_ctx=param_config.sequence_length) model = GPT2LMHeadModel(gpt_config) #with open("./models/gpt/gpt2-pytorch_model.bin", 'rb') as f: # state_dict = torch.load(f, map_location='cpu' if not torch.cuda.is_available() else None) # print("GPT-2 Model Loaded.") # model = load_weight(model, state_dict) if param_config.model_checkpoint is not None: with open(param_config.model_checkpoint, 'rb') as f: state_dict = torch.load(f) model.load_state_dict(state_dict) print("Model Loaded From: {}".format( param_config.model_checkpoint)) model.to(device) # Load Data if param_config.input == 'bytes': # Load Data for bytes training_data, validation_data = get_wili_data_bytes(param_config) else: # Load Data training_data, validation_data = get_wili_data(param_config) training_loader = DataLoader(training_data, batch_size=param_config.batch_size, shuffle=True, drop_last=False) validation_loader = DataLoader(validation_data, batch_size=1, shuffle=True, drop_last=False) train(model, training_loader, validation_loader, validation_data, param_config)
def load_small_model(device): #print ("find free enouph device") print("text_generator_for_out", path) if os.path.exists(path + '/' + 'gpt2-pytorch_model.bin'): print("exist1") state_dict = torch.load( path + '/' + 'gpt2-pytorch_model.bin', map_location='cpu' if not torch.cuda.is_available() else None) print("exist2") config = GPT2Config() print("exist5") model = GPT2LMHeadModel(config) print("exist6") model = load_weight(model, state_dict) print("exist7") print(device) #torch.cuda.set_device(device) model.to(device) model.eval() return model else: raise RuntimeError("Can't load small gpt model")
def __init__(self): state_dict = torch.load( (path.join(path.dirname(path.abspath(__file__)), 'gpt-2-Pytorch', 'gpt2-pytorch_model.bin')), map_location='cpu' if not torch.cuda.is_available() else None) batch_size = 1 # assert nsamples % batch_size == 0 seed = random.randint(0, 2147483647) np.random.seed(seed) torch.random.manual_seed(seed) torch.cuda.manual_seed(seed) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Load Model enc = get_encoder() config = GPT2Config() model = GPT2LMHeadModel(config) model = load_weight(model, state_dict) model.to(device) model.eval() length = -1 if length == -1: length = config.n_ctx // 2 elif length > config.n_ctx: raise ValueError("Can't get samples longer than window size: %s" % config.n_ctx) self.enc = enc self.batch_size = batch_size self.model = model self.length = 20 self.device = device
def load_gpt2_model(): """Load in the pre-trained model""" # Load Model File state_dict = torch.load( '../models/gpt2-pytorch_model.bin', map_location='cpu' if not torch.cuda.is_available() else None) seed = random.randint(0, 2147483647) np.random.seed(seed) torch.random.manual_seed(seed) torch.cuda.manual_seed(seed) global device device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Load Model global config config = GPT2Config() global model model = GPT2LMHeadModel(config) model = load_weight(model, state_dict) model.to(device) model.eval()
import quotes import json from GPT2.model import GPT2LMHeadModel from GPT2.utils import load_weight from GPT2.config import GPT2Config from GPT2.sample import sample_sequence from GPT2.encoder import get_encoder # Load Model state_dict = torch.load( "gpt2-pytorch_model.bin", map_location="cpu" if not torch.cuda.is_available() else None, ) enc = get_encoder() config = GPT2Config() model = GPT2LMHeadModel(config) model = load_weight(model, state_dict) def text_generator(model, text): nsamples = 1 batch_size = -1 length = 200 temperature = .7 top_k = 40 unconditional = False if batch_size == -1: batch_size = 1 assert nsamples % batch_size == 0
) print("\nValidity Tensor Estimation\n") # -- Setting up PyTorch Information -- # seed = random.randint(0, 2147483647) np.random.seed(seed) torch.random.manual_seed(seed) torch.cuda.manual_seed(seed) # device = torch.device("cpu") device = torch.device("cuda" if ( torch.cuda.is_available() and device_choice == "gpu") else "cpu") known_configurations = { "s_ai": GPT2Config(), "xl_ai": GPT2Config( vocab_size_or_config_json_file=50257, n_positions=1024, n_ctx=1024, n_embd=1600, n_layer=48, n_head=25, layer_norm_epsilon=1e-5, initializer_range=0.02, ), } # -- Load Model -- # gpt2_config = known_configurations[config("MODEL_NAME")]
def text_generator(state_dict): parser = argparse.ArgumentParser() parser.add_argument("--text", type=str, required=True) parser.add_argument("--quiet", type=bool, default=False) parser.add_argument("--nsamples", type=int, default=1) parser.add_argument('--unconditional', action='store_true', help='If true, unconditional generation.') parser.add_argument("--batch_size", type=int, default=-1) parser.add_argument("--length", type=int, default=-1) parser.add_argument("--temperature", type=float, default=0.7) parser.add_argument("--top_k", type=int, default=40) args = parser.parse_args() # ================================================================================ if args.quiet is False: print(args) # ================================================================================ if args.batch_size == -1: args.batch_size = 1 # ================================================================================ assert args.nsamples % args.batch_size == 0 # ================================================================================ seed = random.randint(0, 2147483647) np.random.seed(seed) torch.random.manual_seed(seed) torch.cuda.manual_seed(seed) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # ================================================================================ # Load Model enc = get_encoder() config = GPT2Config() model = GPT2LMHeadModel(config) # ================================================================================ model = load_weight(model, state_dict) model.to(device) model.eval() # ================================================================================ if args.length == -1: args.length = config.n_ctx // 2 elif args.length > config.n_ctx: raise ValueError("Can't get samples longer than window size: %s" % config.n_ctx) # ================================================================================ print(args.text) # I use computer # ================================================================================ context_tokens = enc.encode(args.text) # afaf 2: context_tokens = enc.encode(args.text) # print("context_tokens",context_tokens) # [40, 779, 3644] # ================================================================================ # print("args.length",args.length) # 512 generated = 0 for _ in range(args.nsamples // args.batch_size): out = sample_sequence( model=model, length=args.length, context=context_tokens if not args.unconditional else None, start_token=enc.encoder['<|endoftext|>'] if args.unconditional else None, batch_size=args.batch_size, temperature=args.temperature, top_k=args.top_k, device=device) # afaf 5: out = sample_sequence( # print("out",out) # tensor([[ 40, 779, 3644, 1143, 3788, 284, 2198, 262, 2033, 286, # 1321, 287, 262, 2393, 11, 290, 788, 4866, 340, 284, # print("out",out.shape) # torch.Size([1, 515]) len_ctx_tokens = len(context_tokens) # print("len_ctx_tokens",len_ctx_tokens) # 3 out = out[:, len_ctx_tokens:].tolist() # ================================================================================ # print("args.batch_size",args.batch_size) # 1 for i in range(args.batch_size): generated += 1 # ================================================================================ # print("out",out) # [[3783, 11, 543, 318, 257, 1688, 636, 286, 616, 3047, 290, 318, 257, 845, # print("out",len(out)) # 1 # ================================================================================ indexed_out = out[i] # print("indexed_out",indexed_out) # [5479, 588, 9678, 290, 24134, 284, 16481, 1366, 287, 257, 30117, 13, 383, 1917, 318, 326, # print("indexed_out",len(indexed_out)) # 512 # ================================================================================ text = enc.decode(indexed_out) print("text", text) afaf # terminals with Ethernet cable to connect the computer to a computer system that has a computer terminal. # An additional feature # ================================================================================ if args.quiet is False: print("=" * 40 + " SAMPLE " + str(generated) + " " + "=" * 40) print(text)
def generator(text): # parser = argparse.ArgumentParser() # parser.add_argument("--text", type=str, required=True) # parser.add_argument("--quiet", type=bool, default=False) # parser.add_argument("--nsamples", type=int, default=1) # parser.add_argument('--unconditional', action='store_true', help='If true, unconditional generation.') # parser.add_argument("--batch_size", type=int, default=-1) # parser.add_argument("--length", type=int, default=-1) # parser.add_argument("--temperature", type=float, default=0.7) # parser.add_argument("--top_k", type=int, default=40) # args = parser.parse_args() state_dict = torch.load( 'gpt2-pytorch_model.bin', map_location='cpu' if not torch.cuda.is_available() else None) input = text quiet = False nsamples = 1 unconditional = False batch_size = -1 length = -1 temperature = 0.7 top_k = 40 if batch_size == -1: batch_size = 1 assert nsamples % batch_size == 0 seed = random.randint(0, 2147483647) np.random.seed(seed) torch.random.manual_seed(seed) torch.cuda.manual_seed(seed) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Load Model enc = get_encoder() config = GPT2Config() model = GPT2LMHeadModel(config) model = load_weight(model, state_dict) model.to(device) model.eval() if length == -1: length = config.n_ctx // 2 elif length > config.n_ctx: raise ValueError("Can't get samples longer than window size: %s" % config.n_ctx) print(text) context_tokens = enc.encode(text) generated = 0 for _ in range(nsamples // batch_size): out = sample_sequence( model=model, length=length, context=context_tokens if not unconditional else None, start_token=enc.encoder['<|endoftext|>'] if unconditional else None, batch_size=batch_size, temperature=temperature, top_k=top_k, device=device) out = out[:, len(context_tokens):].tolist() for i in range(batch_size): generated += 1 text = enc.decode(out[i]) if quiet is False: print("=" * 40 + " SAMPLE " + str(generated) + " " + "=" * 40) return text
def text_generator(state_dict): parser = argparse.ArgumentParser() #parser.add_argument("--text", type = file, required=True) parser.add_argument('filename') parser.add_argument("--quiet", type=bool, default=False) parser.add_argument("--nsamples", type=int, default=1) parser.add_argument('--unconditional', action='store_true', help='If true, unconditional generation.') parser.add_argument("--batch_size", type=int, default=-1) parser.add_argument("--length", type=int, default=40) parser.add_argument("--temperature", type=float, default=0.7) parser.add_argument("--top_k", type=int, default=40) args = parser.parse_args() open_bbc_page = requests.get(main_url).json() article = open_bbc_page["articles"] results = [] for ar in article: results.append(ar["title"]) print(results[1]) text1 = results[1] with open(args.filename) as file: #text1 = file.read() print(text1) if args.quiet is False: print(args) if args.batch_size == -1: args.batch_size = 1 assert args.nsamples % args.batch_size == 0 seed = random.randint(0, 2147483647) np.random.seed(seed) torch.random.manual_seed(seed) torch.cuda.manual_seed(seed) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Load Model enc = get_encoder() config = GPT2Config() model = GPT2LMHeadModel(config) model = load_weight(model, state_dict) model.to(device) model.eval() if args.length == -1: args.length = config.n_ctx // 2 elif args.length > config.n_ctx: raise ValueError("Can't get samples longer than window size: %s" % config.n_ctx) print(text1) context_tokens = enc.encode(text1) generated = 0 for _ in range(args.nsamples // args.batch_size): out = sample_sequence( model=model, length=args.length, context=context_tokens if not args.unconditional else None, start_token=enc.encoder['<|endoftext|>'] if args.unconditional else None, batch_size=args.batch_size, temperature=args.temperature, top_k=args.top_k, device=device) out = out[:, len(context_tokens):].tolist() for i in range(args.batch_size): generated += 1 text = enc.decode(out[i]) if args.quiet is False: print("=" * 40 + " SAMPLE " + str(generated) + " " + "=" * 40) print(text) text = text1 + text api.update_status(status=text)