def load_model(model_type='gpt2', model_name_or_path='output_yt/m', no_cuda = True): device = torch.device("cuda" if torch.cuda.is_available() and not no_cuda else "cpu") config_class, model_class, tokenizer_class = MODEL_CLASSES[model_type] config = config_class.from_pretrained(model_name_or_path) # if tokenizer_class: tokenizer_class = globals()[tokenizer_class] #print(tokenizer_class, type(tokenizer_class)) tokenizer = YTEncoder.from_pretrained(model_name_or_path) #if args.block_size <= 0: # args.block_size = tokenizer.max_len_single_sentence # Our input block size will be the max possible for the model #args.block_size = min(args.block_size, tokenizer.max_len_single_sentence) model = model_class.from_pretrained(model_name_or_path, from_tf=bool('.ckpt' in model_name_or_path), config=config) model.to(device) return model, tokenizer
def _load_tokenizer(path: str): return YTEncoder.from_pretrained(path)
flavor_id = device + environ.get('INSTANCE', ':0') from tendo import singleton me = singleton.SingleInstance(flavor_id=flavor_id) import logging for handler in logging.root.handlers[:]: logging.root.removeHandler(handler) logging.basicConfig(filename=f"logs/{hash(flavor_id)}.log", level=logging.INFO) logger = logging.getLogger(__name__) model_path = 'gpt2/medium/pelevin' tokenizer = YTEncoder.from_pretrained(model_path) model = GPT2LMHeadModel.from_pretrained(model_path) model.to(device) model.eval() if torch.cuda.is_available(): from apex import amp model = amp.initialize(model, opt_level='O2') def get_sample(model, prompt, length: int, num_samples: int, allow_linebreak: bool): logger.info(prompt) filter_n = tokenizer.encode('\n')[-1:]
flavor_id = device + environ.get('INSTANCE', ':0') from tendo import singleton me = singleton.SingleInstance(flavor_id=flavor_id) import logging for handler in logging.root.handlers[:]: logging.root.removeHandler(handler) logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%d.%m.%Y %H:%M:%S', level=logging.INFO) logger = logging.getLogger(__name__) tokenizer = YTEncoder.from_pretrained('/pelevin') model = GPT2LMHeadModel.from_pretrained('/pelevin') model.to(device) model.eval() vocab = tokenizer.vocab() all_matching_words = [] strings_to_filter = ['.', '!', '?', ';', '"', '«', '»', ':', '–', '…', '—'] for string_to_filter in strings_to_filter: matching_word_array = [s for s in vocab if string_to_filter in s] all_matching_words += matching_word_array true_filter = tokenizer.encode_list(list(set(all_matching_words)))
with torch.no_grad(): for i in range(text_len): outputs = model(cur_ids, labels=cur_ids) loss, logits = outputs[:2] softmax_logits = torch.softmax( logits[0, -1], dim=0 ) #Take the first(only one) batch and the last predicted embedding next_token_id = choose_from_top( softmax_logits.to('cpu').numpy(), n=10 ) #Randomly(from the given probability distribution) choose the next word from the top n words cur_ids = torch.cat([ cur_ids, torch.ones((1, 1)).long().to(device) * next_token_id ], dim=1) # Add the last word output_list = list(cur_ids.squeeze().to('cpu').numpy()) output_text = tokenizer.decode([output_list]) print(output_text) if __name__ == '__main__': device = 'cuda' if torch.cuda.is_available() else 'cpu' tokenizer = YTEncoder.from_pretrained(str(PATH_TO_MODELS / "yt.model")) model = GPT2LMHeadModel.from_pretrained(str(PATH_TO_MODELS / "m_gpt_2/")).to(device) generate_some_text(" хм...фестиваль в сочи...киберсборная сельца ")
def main(): parser = argparse.ArgumentParser() parser.add_argument("--model_type", default=None, type=str, required=True, help="Model type selected in the list: " + ", ".join(MODEL_CLASSES.keys())) parser.add_argument( "--model_name_or_path", default=None, type=str, required=True, help="Path to pre-trained model or shortcut name selected in the list: " + ", ".join(ALL_MODELS)) parser.add_argument("--prompt", type=str, default="") parser.add_argument( "--config_name", default="", type=str, help= "Optional pretrained config name or path if not the same as model_name_or_path" ) 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( "--tokenizer_name", default="", type=str, help= "Optional pretrained tokenizer name or path if not the same as model_name_or_path" ) parser.add_argument("--tokenizer_class", default="", type=str, help="Optional pretrained tokenizer clas") 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") 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.model_type = args.model_type.lower() print(args.model_type) config_class, model_class, tokenizer_class = MODEL_CLASSES[args.model_type] #tokenizer_class = [args.tokenizer_class] config = config_class.from_pretrained(args.config_name if args.config_name else args.model_name_or_path) if args.tokenizer_class: tokenizer_class = globals()[args.tokenizer_class] tokenizer = tokenizer_class.from_pretrained(args.tokenizer_name if args.tokenizer_name else args.model_name_or_path, do_lower_case=args.do_lower_case) model = model_class.from_pretrained(args.model_name_or_path, from_tf=bool('.ckpt' in args.model_name_or_path), config=config) model.to(args.device) model.eval() if args.length < 0 and model.config.max_position_embeddings > 0: args.length = model.config.max_position_embeddings elif 0 < model.config.max_position_embeddings < args.length: args.length = model.config.max_position_embeddings # No generation bigger than model size elif args.length < 0: args.length = MAX_LENGTH # avoid infinite loop ''' config_class, model_class, tokenizer_class = MODEL_CLASSES[args.model_type] config = config_class.from_pretrained( args.config_name if args.config_name else args.model_name_or_path) if args.tokenizer_class: tokenizer_class = globals()[args.tokenizer_class] print(tokenizer_class, type(tokenizer_class)) tokenizer = YTEncoder.from_pretrained(args.model_name_or_path) #if args.block_size <= 0: # args.block_size = tokenizer.max_len_single_sentence # Our input block size will be the max possible for the model #args.block_size = min(args.block_size, tokenizer.max_len_single_sentence) model = model_class.from_pretrained( args.model_name_or_path, from_tf=bool('.ckpt' in args.model_name_or_path), config=config) model.to(args.device) print(args) while True: raw_text = args.prompt if args.prompt else input("Model prompt >>> ") if args.model_type in ["transfo-xl", "xlnet"]: # Models with memory likes to have a long prompt for short inputs. raw_text = (args.padding_text if args.padding_text else PADDING_TEXT) + raw_text context_tokens = tokenizer.encode(raw_text) out = sample_sequence( model=model, context=context_tokens, length=args.length, temperature=args.temperature, top_k=args.top_k, top_p=args.top_p, device=args.device, is_xlnet=bool(args.model_type == "xlnet"), ) out = out[0, len(context_tokens):].tolist() text = tokenizer.decode(out) #, clean_up_tokenization_spaces=True) print(text) if args.prompt: break return text
def load_tokenizer(model_path: str): tokenizer = YTEncoder.from_pretrained(model_path) return tokenizer
flavor_id = device + environ.get('INSTANCE', ':0') from tendo import singleton me = singleton.SingleInstance(flavor_id=flavor_id) import logging for handler in logging.root.handlers[:]: logging.root.removeHandler(handler) logging.basicConfig(filename=f"logs/{hash(flavor_id)}.log", level=logging.INFO) logger = logging.getLogger(__name__) BASE_PATH = '../../content/drive/My Drive/Colab Notebooks/ru_trfs_inference/ckpts/' model_paths = ['medium', 'gradual_unfreeze', 'all_unfreeze', 'noy'] tokenizer = YTEncoder.from_pretrained(BASE_PATH + model_paths[0]) models = [ GPT2LMHeadModel.from_pretrained(BASE_PATH + m_path).to(device).eval() for m_path in model_paths ] from apex import amp models = amp.initialize(models, opt_level='O2') print(f'{len(models)} models loaded for inference. URLs: {model_paths}') def get_sample(model, prompt, length: int, num_samples: int, allow_linebreak: bool, temperature: float, top_p: float, top_k: int): logger.info(prompt)