-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgpt_inference.py
177 lines (159 loc) · 7.33 KB
/
gpt_inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import argparse
import json
import os, sys
import numpy as np
import tensorflow as tf
sys.path.append(os.curdir+'/src')
import model
import sample
import refine_punc
import tokenization
import utils
class GPT:
def __init__(self,
checkpoint_path,
model_name,
device,
seed=None,
nsamples=1,
batch_size=1,
tok_length=256,
sent_length=3,
top_k=0,
top_p=0.0):
"""
Interactively run the model
:model_name=345K : String, which model to use
:seed=None : Integer seed for random number generators, fix seed to reproduce
results
:nsamples=1 : Number of samples to return total
:batch_size=1 : Number of batches (only affects speed/memory). Must divide nsamples.
:tok_length=None : Number of tokens in generated text, if None (default), is
determined by model hyperparameters
:sent_length=None : Number of sentence in generated text, if None (default) ignored
:top_k=0 : Integer value controlling diversity. 1 means only 1 word is
considered for each step (token), resulting in deterministic completions,
while 40 means 40 words are considered at each step. 0 (default) is a
special setting meaning no restrictions. 40 generally is a good value.
:top_p=0.0 : Float value controlling diversity. Implements nucleus sampling,
overriding top_k if set to a value > 0. A good setting is 0.9.
"""
if device is not None:
os.environ['CUDA_VISIBLE_DEVICES'] = str(device)
self.conditional = True
if batch_size is None:
batch_size = 1
assert nsamples % batch_size == 0
self.batch_size = batch_size
self.nsamples = nsamples
self.tokenizer = tokenization.FullTokenizer(
vocab_file='./models/'+model_name+'/vocab.txt',
do_lower_case=False)
self.en = False
print('Korean GPT loaded!')
hparams = model.default_hparams()
with open(os.path.join('./models/'+model_name, 'hparams.json')) as f:
hparams.override_from_dict(json.load(f))
if tok_length is None:
tok_length = hparams.n_ctx // 2
elif tok_length > hparams.n_ctx:
raise ValueError("Can't get samples longer than window size: %s" % hparams.n_ctx)
self.context = tf.placeholder(tf.int32, [batch_size, None])
start_token = None
np.random.seed(seed)
tf.set_random_seed(seed)
self.output = sample.sample_sequence(
hparams=hparams, length=tok_length,
context=self.context,
start_token=start_token,
batch_size=batch_size,
temperature=1, top_k=top_k, top_p=top_p)
self.sent_length = sent_length
saver = tf.train.Saver()
ckpt = tf.train.latest_checkpoint(checkpoint_path)
self.sess = tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True)))
self.sess.run(tf.global_variables_initializer())
saver.restore(self.sess, ckpt)
def infer(self, raw_text):
output = []
if self.conditional:
# while True:
# raw_text = input("Model prompt >>> ")
# while not raw_text:
# print('Prompt should not be empty!')
# raw_text = input("Model prompt >>> ")
if self.en:
context_tokens = self.enc.encode(raw_text)
else:
context_tokens = self.tokenizer.convert_tokens_to_ids(self.tokenizer.tokenize(raw_text))
for sample_id in range(self.nsamples // self.batch_size):
out = self.sess.run(self.output, feed_dict={
self.context: [context_tokens for _ in range(1)]})[:, len(context_tokens):]
for batch_id in range(self.batch_size):
# if self.en:
# text = self.enc.decode(out[batch_id])
# else:
# text = self.tokenizer.convert_ids_to_tokens(out[batch_id])
# text = refine_punc.refine_punc(text)
# text = text.split('.')[:self.sent_length]
# if text[-1] != '':
# text = text + ['']
# text = '. '.join([i.strip() for i in text]).strip()
# output.append(text)
if self.en:
text = self.enc.decode(out[batch_id])
text = text.split('.')[:self.sent_length]
if text[-1] != '':
text = text + ['']
text = '. '.join([i.strip() for i in text]).strip()
output.append(text)
else:
text = self.tokenizer.convert_ids_to_tokens(out[batch_id])
text = refine_punc.refine_punc(text)
text = ' '.join(utils.rm_sp(utils.convert_text(text).split('.. '))[:self.sent_length])
output.append(text)
else:
generated = 0
while self.nsamples == 0 or generated < self.nsamples:
out = self.sess.run(self.output)
for batch_id in range(self.batch_size):
generated += self.batch_size
if self.en:
text = self.enc.decode(out[batch_id])
else:
text = self.tokenizer.convert_ids_to_tokens(out[batch_id])
text = refine_punc.refine_punc(text)
text = text.split('.')[:self.sent_length]
if text[-1] != '':
text = text + ['']
text = '. '.join([i.strip() for i in text]).strip()
output.append(text)
return output
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-ckpt', '--checkpoint_path', type=str, required=True,
help='trained checkpoint path')
parser.add_argument('--model_name', type=str, default='345K',
help='model (architecture) name')
parser.add_argument("--device", type=int, default=1)
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--nsamples", type=int, default=2)
parser.add_argument("--batch_size", type=int, default=1)
parser.add_argument("--tok_length", type=int, default=128)
parser.add_argument("--sent_length", type=int, default=3)
parser.add_argument("--top_k", type=int, default=0)
parser.add_argument("--top_p", type=float, default=.0)
parser.add_argument("--context", type=str, default="")
args = parser.parse_args()
model = GPT(args.checkpoint_path,
args.model_name,
args.device,
args.seed,
args.nsamples,
args.batch_size,
args.tok_length,
args.sent_length,
args.top_k,
args.top_p)
out = model.infer(args.context)
print(out)