-
Notifications
You must be signed in to change notification settings - Fork 1
/
translate.py
executable file
·380 lines (288 loc) · 9.87 KB
/
translate.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
import argparse
import torch
from datetime import datetime
from lark import Lark
from torch.nn.utils.rnn import pad_sequence
from model import build_model, model_settings
from model.statistics import Scorer
from util.log import Logger
from util.nlp import NLP, Vocab, parse_grammar
device = torch.device(
'cuda' if torch.cuda.is_available()
else 'cpu'
)
def validate_args(args):
# TODO: Validate args.
pass
def load_model_env(model_path):
"""
Loads a .pt model file.
:param model_path: path to model file.
:returns: model environment including
neural network, parser and utilites.
"""
model_data = torch.load(
model_path,
map_location=device
)
model_opt = model_data['model_opts']
lang = model_data['lang']
vocab = {
'src': Vocab(lang['vocab']['src']),
'tgt': Vocab(lang['vocab']['tgt']),
'stack': Vocab(lang['vocab']['stack']),
'operator': Vocab(lang['vocab']['operator'])
}
# Load model environment (Neural Net, Parser, Utilities).
grammar, operators = parse_grammar(lang['grammar'])
lark = Lark(
grammar,
keep_all_tokens=True,
parser='lalr',
start='start'
)
nlp = NLP(lark, operators)
nlp.collect_tokens(vocab)
nlp.vocab = vocab
# Load model from settings and set to eval mode.
settings = model_settings(vocab, model_opt)
model = build_model(vocab, settings)
model.load_state_dict(model_data['state_dict'])
model.to(model.device)
model.eval()
model_env = {
'model': model,
'model_opt': model_opt,
'vocab': vocab,
'nlp': nlp
}
return model_env
def translate(model_env, src):
"""
Translate a single natural language input string.
:param model_env: the model environment to execute the
source input string against.
:param model_opt: model and parser options.
:param src: source input string to translate.
:returns: model response program code.
"""
model = model_env['model']
vocab = model_env['vocab']
nlp = model_env['nlp']
src_i = []
# Preprocess source input string.
tokens = nlp.normalize(src, delimiters=True)
unk_token = nlp.mark.inp['UNK']
unk_index = vocab['src'].w2i(unk_token)
# Get translation specific options.
model_opt = model_env['model_opt']
opts = get_opts(model_opt)
for t in tokens:
try:
i = vocab['src'].w2i(t)
src_i.append(i)
except KeyError:
src_i.append(unk_index)
src_i = torch.LongTensor(src_i).to(model.device)
input_fields = {
'src': src,
'src_i': src_i
}
if True:
# Create a mini sample vocab for copying.
orig_tokens = nlp.normalize(src, lower=False)
sample_i2w = {i: t for i, t in enumerate(orig_tokens)}
sample_w2i = {t: i for i, t in enumerate(orig_tokens)}
sample_vocab = {'i2w': sample_i2w, 'w2i': sample_w2i}
input_fields.update({'sample_vocab': sample_vocab})
# Evaluate input.
top, candidates = model.evaluate(
nlp, input_fields,
num_parsers=opts['beam_width'],
beam_width=opts['beam_width']
)
return top
def get_opts(model_opt):
# Default options.
options = {
'beam_width': 1
}
if 'beam_width' in model_opt:
options['beam_width'] = model_opt['beam_width']
return options
def evaluate(args, env, dataset, logger):
"""
Evaluates the model in 'env' on 'dataset'.
:param env: the model environment to evaluate
the dataset against.
:param dataset: the dataset to be evaluated.
:param logger: logging information on console or
to file.
:returns: scorer results.
"""
model = env['model']
vocab = env['vocab']
nlp = env['nlp']
logger['log'].log(
f'[INFO {datetime.now()}] evaluating model '
f'\'{args.model}\' on dataset \'{args.eval}\''
)
count = 0
data_len = len(dataset)
now = datetime.now()
logger['line'].update(
f'[INFO {now}] {count:<4}/{data_len:>4} '
f'examples evaluated'
)
scorer = Scorer(nlp, vocab)
for example in dataset:
tgt = example['tgt_i'][1:]
if args.no_parser:
# Evaluate model without parser assistance.
# TODO: Gives wrong results when stack
# encodings were used.
alignment = example['alignment']
tgt_i = torch.LongTensor(example['tgt_i'])
src_i = torch.LongTensor(example['src_i'])
tgt_i = tgt_i.unsqueeze(1).to(model.device)
src_i = src_i.unsqueeze(1).to(model.device)
stacks = torch.LongTensor(example['value_stacks']).to(model.device)
stack_lens = torch.LongTensor(example['stack_lens'])
stacks = stacks.unsqueeze(0)
stack_lens = pad_sequence((stack_lens,), padding_value=1)
stack_lens = stack_lens.tolist()
with torch.no_grad():
output = model(
src_i, tgt_i,
[len(src_i)], [len(tgt_i)],
alignment, stacks,
stack_lens, 0.0
)
dec_outs = output['dec_outs']
dec_outs = dec_outs[1:, :, :]
dec_outs = dec_outs.squeeze()
predictions = dec_outs.argmax(1)
# No parse to abort.
aborted = False
# Resolve copy pointers.
if model.copy_attention:
predictions = __resolve_pointers(
nlp,
example,
predictions,
output['copy_weights'][1:]
)
else:
# Evaluate model with parser assistance.
top, _ = model.evaluate(
nlp, example,
num_parsers=args.beam_width,
beam_width=args.beam_width,
max_cycles=2
)
predictions = top['parser'].predictions
aborted = top['aborted']
results = {
'predictions': predictions,
'attn_used': False,
'copy_used': False,
'aborted': aborted
}
if model.attention:
results.update({
'attn_used': True
})
if model.copy_attention:
results.update({
'copy_used': True,
'alignment': example['alignment']
})
scorer.score(results, tgt)
count += 1
logger['line'].update(
f'[INFO {now}] {count:<4}/{data_len:>4} '
f'examples evaluated'
)
logger['line'].close()
return scorer.results()
def __resolve_pointers(nlp, example, predictions, copy_w):
"""
Resolves pointer operators in predicted sequence.
:param nlp: nl processing and parsing utils.
:param example: the example currently evaluated.
:param predictions: the predicted token sequence.
:param copy_w: copy weights for each predicted token.
:returns: the resolved token sequence, where copied
indices correspond to the indices in the
extended vocabulary.
"""
op_vocab = nlp.vocab['operator']
# Find operator indices in predicted sequence.
pred_indices = []
for i in op_vocab._i2w.keys():
pred_indices.extend(
(predictions == i).nonzero()
.reshape(-1).tolist()
)
# Resolve operators.
for i in pred_indices:
pred_op = predictions[i]
op = op_vocab.i2w(pred_op.item())
# TODO: Get operator.
copy_index, _ = op.apply(
(example, copy_w[i])
)
predictions[i] = copy_index
return predictions
def main(args, logger):
env = load_model_env(args.model)
if args.eval:
dataset = torch.load(args.eval)
scores = evaluate(args, env, dataset, logger)
logger['log'].log(
f'[INFO {datetime.now()}] {scores["accuracy"]*100:0>6.3f}% '
f'accuracy on dataset \'{args.eval}\''
)
logger['log'].log(
f'[INFO {datetime.now()}] {scores["gold_acc"]*100:0>6.3f}% '
f'gold accuracy on dataset \'{args.eval}\''
)
logger['log'].log(
f'[INFO {datetime.now()}] {scores["aborted"]} '
f'aborted parses'
)
logger['log'].close()
else:
# TODO: Run server.
logger['log'].log(
f'[INFO {datetime.now()}] translation service'
' successfully started.'
)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, required=True,
help='The model file to use for translation.')
parser.add_argument('--eval', type=str, default=None,
help='The test dataset to evaluate.')
parser.add_argument('--out', type=str, default=None,
help='The logging file.')
parser.add_argument('--beam_width', type=int, default=1,
help='The beam with for the parser decoder. Defaults'
'to greedy search.')
parser.add_argument('--no_parser', action='store_true',
help='Turns off parser-assisted decoding.')
# Server options.
parser.add_argument('--host', type=str, default='localhost',
help='Server host address.')
parser.add_argument('--port', type=int, default=4996,
help='Port on which to serve.')
args = parser.parse_args()
log = Logger(out_path=args.out)
line = log.add_text('')
log.start()
logger = {
'log': log,
'line': line
}
validate_args(args)
main(args, logger)