run_gpt2_generate_unconditional_samples.py 3.32 KB
Newer Older
thomwolf's avatar
thomwolf committed
1
2
3
4
5
6
#!/usr/bin/env python3

import argparse
import logging

import torch
thomwolf's avatar
thomwolf committed
7
import torch.nn.functional as F
thomwolf's avatar
thomwolf committed
8
import numpy as np
thomwolf's avatar
thomwolf committed
9
from tqdm import trange
thomwolf's avatar
thomwolf committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

from pytorch_pretrained_bert import GPT2LMHeadModel, GPT2Tokenizer

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 top_k_logits(logits, k):
    if k == 0:
        return logits
    values, _ = torch.topk(logits, k)
    min_values = values[:, -1]
    return torch.where(logits < min_values, torch.ones_like(logits, dtype=logits.dtype) * -1e10, logits)

def sample_sequence(model, length, start_token=None, batch_size=None, context=None, temperature=1, top_k=0, device='cuda'):
    if start_token is None:
        assert context is not None, 'Specify exactly one of start_token and context!'
thomwolf's avatar
thomwolf committed
28
        context = torch.tensor(context, device=device, dtype=torch.long)
thomwolf's avatar
thomwolf committed
29
30
    else:
        assert context is None, 'Specify exactly one of start_token and context!'
thomwolf's avatar
thomwolf committed
31
        context = torch.full((batch_size, 1), start_token, device=device, dtype=torch.long)
thomwolf's avatar
thomwolf committed
32
33
    prev = context
    output = context
thomwolf's avatar
thomwolf committed
34
    past = None
thomwolf's avatar
thomwolf committed
35
    with torch.no_grad():
thomwolf's avatar
thomwolf committed
36
        for i in trange(length):
thomwolf's avatar
thomwolf committed
37
38
39
            logits, past = model(prev, past=past)
            logits = logits[:, -1, :] / temperature
            logits = top_k_logits(logits, k=top_k)
thomwolf's avatar
thomwolf committed
40
41
            log_probs = F.softmax(logits, dim=-1)
            prev = torch.multinomial(log_probs, num_samples=1)
thomwolf's avatar
thomwolf committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
            output = torch.cat((output, prev), dim=1)
    return output

def sample_model():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model_name_or_path', type=str, default='gpt2', help='pretrained model name or path to local checkpoint')
    parser.add_argument("--seed", type=int, default=0)
    parser.add_argument("--nsamples", type=int, default=0)
    parser.add_argument("--batch_size", type=int, default=1)
    parser.add_argument("--length", type=int, default=-1)
    parser.add_argument("--temperature", type=int, default=1)
    parser.add_argument("--top_k", type=int, default=0)
    args = parser.parse_args()
    print(args)

    np.random.seed(args.seed)
    torch.random.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    enc = GPT2Tokenizer.from_pretrained(args.model_name_or_path)
    model = GPT2LMHeadModel.from_pretrained(args.model_name_or_path)
thomwolf's avatar
thomwolf committed
64
65
    model.to(device)
    model.eval()
thomwolf's avatar
thomwolf committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79

    if args.length == -1:
        args.length = model.config.n_ctx
    elif args.length > model.config.n_ctx:
        raise ValueError("Can't get samples longer than window size: %s" % model.config.n_ctx)

    generated = 0
    while args.nsamples == 0 or generated < args.nsamples:
        out = sample_sequence(
            model=model, length=args.length,
            start_token=enc.encoder['<|endoftext|>'],
            batch_size=args.batch_size,
            temperature=args.temperature, top_k=args.top_k, device=device
        )
thomwolf's avatar
thomwolf committed
80
        out = out.tolist()
thomwolf's avatar
thomwolf committed
81
82
83
84
85
86
87
88
        for i in range(args.batch_size):
            generated += args.batch_size
            text = enc.decode(out[i])
            print("=" * 40 + " SAMPLE " + str(generated) + " " + "=" * 40)
            print(text)

if __name__ == '__main__':
    sample_model()