-
Notifications
You must be signed in to change notification settings - Fork 33
/
decoding.py
398 lines (346 loc) · 13.2 KB
/
decoding.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
r"""Decode from trained T2T models.
Mimic t2t-decoder binary.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import collections
import operator
import os
import re
import string
import sys
import time
import numpy as np
import six
from tensor2tensor.bin import t2t_trainer
from tensor2tensor.data_generators import problem # pylint: disable=unused-import
from tensor2tensor.utils import decoding
from tensor2tensor.utils import trainer_lib
from tensor2tensor.utils import usr_dir
import tensorflow as tf
import text_processing
flags = tf.flags
FLAGS = flags.FLAGS
def create_hp_and_estimator(
problem_name, data_dir, checkpoint_path, decode_to_file=None):
trainer_lib.set_random_seed(FLAGS.random_seed)
hp = trainer_lib.create_hparams(
FLAGS.hparams_set,
FLAGS.hparams,
data_dir=os.path.expanduser(data_dir),
problem_name=problem_name)
decode_hp = decoding.decode_hparams(FLAGS.decode_hparams)
decode_hp.shards = FLAGS.decode_shards
decode_hp.shard_id = FLAGS.worker_id
decode_in_memory = FLAGS.decode_in_memory or decode_hp.decode_in_memory
decode_hp.decode_in_memory = decode_in_memory
decode_hp.decode_to_file = decode_to_file
decode_hp.decode_reference = None
FLAGS.checkpoint_path = checkpoint_path
estimator = trainer_lib.create_estimator(
FLAGS.model,
hp,
t2t_trainer.create_run_config(hp),
decode_hparams=decode_hp,
use_tpu=FLAGS.use_tpu)
return hp, decode_hp, estimator
def backtranslate_interactively(
from_problem, to_problem,
from_data_dir, to_data_dir,
from_ckpt, to_ckpt):
from_hp, from_decode_hp, from_estimator = create_hp_and_estimator(
from_problem, from_data_dir, from_ckpt)
to_hp, to_decode_hp, to_estimator = create_hp_and_estimator(
to_problem, to_data_dir, to_ckpt)
class BackTranslateHelper(object):
def __init__(self):
self.current_input = None
self.translated = {
'medical': 'y tế',
'y tế': 'medical',
'software': 'phần mềm',
'phần mềm': 'software',
'law': 'luật',
'luật': 'law',
'book': 'sách',
'sách': 'book',
'movie': 'phim',
'phim': 'movie',
'TED': 'TED'
}
def interactive_text_input(self):
while True:
if sys.version_info >= (3, 0):
input_text = input('>>> ')
else:
input_text = raw_input('>>> ')
if input_text == 'q':
break
if '\\' in input_text and input_text.split('\\')[-1].strip() == 'TED talk':
input_text = input_text.split(' \\')[0] + ' \\ TED'
self.current_input = input_text
yield text_processing.fix_contents(input_text)
def intermediate_lang_processor(self, intermediate_lang):
for text in intermediate_lang:
text = text.replace(''', "'")
text = text.replace('"', "'")
text = text.replace('[', "(")
text = text.replace(']', ")")
if '\\' in text:
print('Translated :', text.split('\\')[0])
else:
print('Translated :', text)
# Fix the input to meet the translation model requirement:
# if '\\' in self.current_input and ' \\ ' not in self.current_input:
# input_text = self.current_input.split('\\')[0].strip()
# tag = self.current_input.split('\\')[1].strip()
# self.current_input = input_text + ' \\ ' + tag
if ' \\ ' in self.current_input and ' \\ ' not in text:
tag = self.current_input.split('\\')[1].strip()
if tag in self.translated:
text += ' \\ ' + self.translated[tag]
elif '\\' not in self.current_input and '\\' in text:
text = text.split('\\')[0]
yield text_processing.fix_contents(text)
helper = BackTranslateHelper()
print('Loading from {} ..'.format(from_ckpt))
intermediate_lang = decode_interactively(
from_estimator, helper.interactive_text_input(),
from_problem, from_hp, from_decode_hp, from_ckpt)
print('Loading from {} ..'.format(to_ckpt))
outputs = decode_interactively(
to_estimator, helper.intermediate_lang_processor(intermediate_lang),
to_problem, to_hp, to_decode_hp, to_ckpt)
for output in outputs:
if ' \\' in output:
output = output.split(' \\')[0]
print('Back-translated : {}'.format(
output.replace(''', "'")
.replace('"', "'")
.replace('[', "(")
.replace(']', ")")))
def decode_interactively(estimator,
input_generator,
problem_name,
hparams,
decode_hp,
checkpoint_path=None):
"""Compute predictions on entries in filename and write them out."""
decode_hp.batch_size = 1
tf.logging.info(
"decode_hp.batch_size not specified; default=%d" % decode_hp.batch_size)
# Inputs vocabulary is set to targets if there are no inputs in the problem,
# e.g., for language models where the inputs are just a prefix of targets.
p_hp = hparams.problem_hparams
has_input = "inputs" in p_hp.vocabulary
inputs_vocab_key = "inputs" if has_input else "targets"
inputs_vocab = p_hp.vocabulary[inputs_vocab_key]
targets_vocab = p_hp.vocabulary["targets"]
length = getattr(hparams, "length", 0) or hparams.max_length
def input_fn_gen():
for line in input_generator:
if has_input:
ids = inputs_vocab.encode(line.strip()) + [1]
else:
ids = targets_vocab.encode(line)
if len(ids) < length:
ids.extend([0] * (length - len(ids)))
else:
ids = ids[:length]
np_ids = np.array(ids, dtype=np.int32)
yield dict(
inputs=np_ids.reshape((length, 1, 1))
)
def input_fn(params):
return tf.data.Dataset.from_generator(
input_fn_gen,
output_types=dict(
inputs=tf.int32,
),
output_shapes=dict(
inputs=(length, 1, 1)
)
).batch(1)
result_iter = estimator.predict(input_fn, checkpoint_path=checkpoint_path)
for result in result_iter:
_, decoded_outputs, _ = decoding.log_decode_results(
result["inputs"],
result["outputs"],
problem_name,
None,
inputs_vocab,
targets_vocab,
log_results=False,
skip_eos_postprocess=decode_hp.skip_eos_postprocess)
yield decoded_outputs
def decode_from_file_fn(estimator,
filename,
hparams,
decode_hp,
decode_to_file=None,
checkpoint_path=None):
"""Compute predictions on entries in filename and write them out."""
if not decode_hp.batch_size:
decode_hp.batch_size = 32
tf.logging.info(
"decode_hp.batch_size not specified; default=%d" % decode_hp.batch_size)
# Inputs vocabulary is set to targets if there are no inputs in the problem,
# e.g., for language models where the inputs are just a prefix of targets.
p_hp = hparams.problem_hparams
has_input = "inputs" in p_hp.vocabulary
inputs_vocab_key = "inputs" if has_input else "targets"
inputs_vocab = p_hp.vocabulary[inputs_vocab_key]
targets_vocab = p_hp.vocabulary["targets"]
problem_name = FLAGS.problem
filename = decoding._add_shard_to_filename(filename, decode_hp)
tf.logging.info("Performing decoding from file (%s)." % filename)
if has_input:
sorted_inputs, sorted_keys = decoding._get_sorted_inputs(
filename, decode_hp.delimiter)
else:
sorted_inputs = decoding._get_language_modeling_inputs(
filename, decode_hp.delimiter, repeat=decode_hp.num_decodes)
sorted_keys = range(len(sorted_inputs))
num_sentences = len(sorted_inputs)
num_decode_batches = (num_sentences - 1) // decode_hp.batch_size + 1
if estimator.config.use_tpu:
length = getattr(hparams, "length", 0) or hparams.max_length
batch_ids = []
for line in sorted_inputs:
if has_input:
ids = inputs_vocab.encode(line.strip()) + [1]
else:
ids = targets_vocab.encode(line)
if len(ids) < length:
ids.extend([0] * (length - len(ids)))
else:
ids = ids[:length]
batch_ids.append(ids)
np_ids = np.array(batch_ids, dtype=np.int32)
def input_fn(params):
batch_size = params["batch_size"]
dataset = tf.data.Dataset.from_tensor_slices({"inputs": np_ids})
dataset = dataset.map(
lambda ex: {"inputs": tf.reshape(ex["inputs"], (length, 1, 1))})
dataset = dataset.batch(batch_size)
return dataset
else:
def input_fn():
input_gen = decoding._decode_batch_input_fn(
num_decode_batches, sorted_inputs,
inputs_vocab, decode_hp.batch_size,
decode_hp.max_input_size,
task_id=decode_hp.multiproblem_task_id, has_input=has_input)
gen_fn = decoding.make_input_fn_from_generator(input_gen)
example = gen_fn()
return decoding._decode_input_tensor_to_features_dict(example, hparams, decode_hp)
decodes = []
result_iter = estimator.predict(input_fn, checkpoint_path=checkpoint_path)
start_time = time.time()
total_time_per_step = 0
total_cnt = 0
def timer(gen):
while True:
try:
start_time = time.time()
item = next(gen)
elapsed_time = time.time() - start_time
yield elapsed_time, item
except StopIteration:
break
for elapsed_time, result in timer(result_iter):
if decode_hp.return_beams:
beam_decodes = []
beam_scores = []
output_beams = np.split(result["outputs"], decode_hp.beam_size, axis=0)
scores = None
if "scores" in result:
if np.isscalar(result["scores"]):
result["scores"] = result["scores"].reshape(1)
scores = np.split(result["scores"], decode_hp.beam_size, axis=0)
for k, beam in enumerate(output_beams):
tf.logging.info("BEAM %d:" % k)
score = scores and scores[k]
_, decoded_outputs, _ = decoding.log_decode_results(
result["inputs"],
beam,
problem_name,
None,
inputs_vocab,
targets_vocab,
log_results=decode_hp.log_results,
skip_eos_postprocess=decode_hp.skip_eos_postprocess)
beam_decodes.append(decoded_outputs)
if decode_hp.write_beam_scores:
beam_scores.append(score)
if decode_hp.write_beam_scores:
decodes.append("\t".join([
"\t".join([d, "%.2f" % s])
for d, s in zip(beam_decodes, beam_scores)
]))
else:
decodes.append("\t".join(beam_decodes))
else:
_, decoded_outputs, _ = decoding.log_decode_results(
result["inputs"],
result["outputs"],
problem_name,
None,
inputs_vocab,
targets_vocab,
log_results=decode_hp.log_results,
skip_eos_postprocess=decode_hp.skip_eos_postprocess)
decodes.append(decoded_outputs)
total_time_per_step += elapsed_time
total_cnt += result["outputs"].shape[-1]
duration = time.time() - start_time
tf.logging.info("Elapsed Time: %5.5f" % duration)
tf.logging.info("Averaged Single Token Generation Time: %5.7f "
"(time %5.7f count %d)" %
(total_time_per_step / total_cnt,
total_time_per_step, total_cnt))
if decode_hp.batch_size == 1:
tf.logging.info("Inference time %.4f seconds "
"(Latency = %.4f ms/setences)" %
(duration, 1000.0*duration/num_sentences))
else:
tf.logging.info("Inference time %.4f seconds "
"(Throughput = %.4f sentences/second)" %
(duration, num_sentences/duration))
# If decode_to_file was provided use it as the output filename without change
# (except for adding shard_id if using more shards for decoding).
# Otherwise, use the input filename plus model, hp, problem, beam, alpha.
decode_filename = decode_to_file if decode_to_file else filename
if not decode_to_file:
decode_filename = decoding._decode_filename(decode_filename, problem_name, decode_hp)
else:
decode_filename = decoding._add_shard_to_filename(decode_filename, decode_hp)
tf.logging.info("Writing decodes into %s" % decode_filename)
outfile = tf.gfile.Open(decode_filename, "w")
for index in range(len(sorted_inputs)):
try:
outfile.write("%s%s" % (decodes[sorted_keys[index]], decode_hp.delimiter))
except:
outfile.write("%s" % decode_hp.delimiter)
outfile.flush()
outfile.close()
# output_dir = os.path.join(estimator.model_dir, "decode")
# tf.gfile.MakeDirs(output_dir)
# run_postdecode_hooks(DecodeHookArgs(
# estimator=estimator,
# problem=hparams.problem,
# output_dirs=[output_dir],
# hparams=hparams,
# decode_hparams=decode_hp,
# predictions=list(result_iter)
# ), None)
def t2t_decoder(problem_name, data_dir,
decode_from_file, decode_to_file,
checkpoint_path):
hp, decode_hp, estimator = create_hp_and_estimator(
problem_name, data_dir, checkpoint_path, decode_to_file)
decode_from_file_fn(
estimator, decode_from_file,
hp, decode_hp, decode_to_file,
checkpoint_path=checkpoint_path)