-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgpt2_generate_main.py
206 lines (170 loc) · 7.55 KB
/
gpt2_generate_main.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Copyright 2019 The Texar Authors. 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.
"""Example of building OpenAI GPT-2 language model for sample generation.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import importlib
import numpy as np
import tensorflow as tf
import texar as tx
from utils import model_utils, processor
# pylint: disable=invalid-name, too-many-locals, too-many-statements, no-member
# pylint: disable=too-many-branches
flags = tf.flags
FLAGS = flags.FLAGS
flags.DEFINE_string("checkpoint",
"gpt2_pretrained_models/model_117M/model.ckpt",
"Model checkpoint to load model weights from.")
flags.DEFINE_integer("seed", None, "Random seed.")
flags.DEFINE_integer("nsamples", 1, "The number of samples per input.")
flags.DEFINE_integer("batch_size", 1, "The batch size of input.")
flags.DEFINE_integer("max_decoding_length", 100,
"The maximun length of generated text.")
flags.DEFINE_float("temperature", 0.7,
"Softmax temperature for top-k sample decoding. Must be "
"strictly greater than 0. Defaults to 0.7.")
flags.DEFINE_integer("top_k", 40,
"The number of top most likely candidates from a vocab "
"distribution.")
flags.DEFINE_boolean("is_interactive", False, "Interactive mode or not.")
flags.DEFINE_string("config_type", "texar",
"The configuration file type. Set to 'json' if the GPT-2 "
"config file is in the same type of the official GPT-2 "
"config file. Set to 'texar' if GPT-2 config file is in "
"Texar type.")
flags.DEFINE_string("config_model", "configs.config_model",
"The model configuration file to configure the model. "
"The config file type is define by the 'config_type',"
"it be of texar type or json type."
"For '--config_type=json', set the json config file path"
"like: '--config_model gpt2_pretrained_models/model_117M/"
"hparams.json';"
"For '--config_type=texar', set the texar config file "
"like: '--config_model configs.config_model'.")
def main(_):
"""
Builds the model and runs
"""
np.random.seed(FLAGS.seed)
tf.set_random_seed(FLAGS.seed)
nsamples = FLAGS.nsamples
batch_size = FLAGS.batch_size
max_decoding_length = FLAGS.max_decoding_length
ckpt_path = FLAGS.checkpoint
# Load GPT-2 model configuration
if FLAGS.config_type == "json":
gpt2_config = model_utils.transform_gpt2_to_texar_config(
FLAGS.config_model)
elif FLAGS.config_type == 'texar':
gpt2_config = importlib.import_module(
FLAGS.config_model)
else:
raise ValueError('Unknown config_type.')
assert max_decoding_length <= gpt2_config.position_size, (
"max_decoding_length should not be greater than position size")
assert nsamples % batch_size == 0, (
"nsamples must be dividable by batch_size")
# Create a data pre-processor for, e.g., BPE encoding
proc = processor.get_encoder(
"gpt2_pretrained_models/model_117M")
context = tf.placeholder(tf.int32, [batch_size, None])
context_length = tf.placeholder(tf.int32, [batch_size])
end_token = proc.encoder['<|endoftext|>']
if FLAGS.is_interactive:
start_tokens = context[:, 0]
else:
start_tokens = tf.fill([batch_size], end_token)
# Build the GPT-2 model
word_embedder = tx.modules.WordEmbedder(
vocab_size=gpt2_config.vocab_size,
hparams=gpt2_config.embed)
pos_embedder = tx.modules.PositionEmbedder(
position_size=gpt2_config.position_size,
hparams=gpt2_config.pos_embed)
def _embedding_fn(x, y):
# `x` is token ids, `y` is time steps
return word_embedder(x) + pos_embedder(y)
helper = tx.modules.TopKSampleEmbeddingHelper(
embedding=_embedding_fn,
start_tokens=start_tokens,
end_token=end_token,
top_k=FLAGS.top_k,
softmax_temperature=FLAGS.temperature)
output_layer = tf.transpose(word_embedder.embedding, (1, 0))
decoder = tx.modules.TransformerDecoder(
vocab_size=gpt2_config.vocab_size,
output_layer=output_layer,
hparams=gpt2_config.decoder)
with tf.Session() as sess:
if FLAGS.is_interactive:
# Generate continuations of context
lm_output, _ = decoder(
context=context,
context_sequence_length=context_length,
max_decoding_length=max_decoding_length,
helper=helper,
mode=tf.estimator.ModeKeys.PREDICT)
# Load model checkpoint
model_utils.init_gpt2_checkpoint(sess, ckpt_path)
print("\nFinished loading\n")
# Enter interactive mode
while True:
raw_text = input("Model input >>> ")
while not raw_text:
print('Input should not be empty!')
raw_text = input("Model input >>> ")
context_tokens = proc.encode(raw_text)
feed_dict = {
context: [context_tokens for _ in range(batch_size)],
context_length:
[len(context_tokens) for _ in range(batch_size)],
tx.context.global_mode():tf.estimator.ModeKeys.PREDICT
}
generated = 0
for _ in range(nsamples // batch_size):
output = sess.run(lm_output, feed_dict=feed_dict)
sample_id = output.sample_id
for i in range(batch_size):
generated += 1
print("=" * 40 +
" SAMPLE " + str(generated) + " " + "=" * 40)
si = sample_id[i][len(context_tokens):]
print(proc.decode(si))
print("=" * 80)
else:
# Generate samples from scratch
lm_output, _ = decoder(
max_decoding_length=max_decoding_length,
helper=helper,
mode=tf.estimator.ModeKeys.PREDICT)
# Load model checkpoint
model_utils.init_gpt2_checkpoint(sess, ckpt_path)
print("\nFinished loading\n")
feed_dict = {
tx.context.global_mode(): tf.estimator.ModeKeys.PREDICT
}
generated = 0
while nsamples == 0 or generated < nsamples:
output = sess.run(lm_output, feed_dict=feed_dict)
sample_id = output.sample_id
for i in range(batch_size):
generated += batch_size
text = proc.decode(sample_id[i])
print("=" * 40 +
" SAMPLE " + str(generated) + " " + "=" * 40)
print(text)
if __name__ == '__main__':
tf.app.run()