forked from facebookresearch/ParlAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_teachers.py
495 lines (425 loc) · 17.4 KB
/
test_teachers.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
"""
Test Teachers.
A module for testing various teacher types in ParlAI
"""
import os
import unittest
from parlai.utils import testing as testing_utils
from parlai.core.teachers import DialogTeacher
from parlai.core.metrics import SumMetric
import regex as re
from parlai.core.message import Message
from parlai.core.opt import Opt
import parlai.utils.logging as logging
from parlai.utils.io import PathManager
from parlai.core.loader import register_agent
from collections import defaultdict
from parlai.agents.repeat_label.repeat_label import RepeatLabelAgent
class TestAbstractImageTeacher(unittest.TestCase):
"""
Test AbstractImageTeacher.
"""
def _test_display_output(self, image_mode):
"""
Test display data output with given image_mode.
"""
with testing_utils.tempdir() as tmpdir:
data_path = tmpdir
PathManager.mkdirs(os.path.join(data_path, 'ImageTeacher'))
opt = {
'task': 'integration_tests:ImageTeacher',
'datapath': data_path,
'image_mode': image_mode,
'verbose': True,
}
output = testing_utils.display_data(opt)
train_labels = re.findall(r"\[labels\].*\n", output[0])
valid_labels = re.findall(r"\[eval_labels\].*\n", output[1])
test_labels = re.findall(r"\[eval_labels\].*\n", output[2])
for i, lbls in enumerate([train_labels, valid_labels, test_labels]):
self.assertGreater(len(lbls), 0, 'DisplayData failed')
self.assertEqual(len(lbls), len(set(lbls)), output[i])
def test_display_data_no_image(self):
"""
Test that, with no images loaded, all examples are different.
"""
self._test_display_output('no_image_model')
@testing_utils.skipUnlessVision
@testing_utils.skipUnlessGPU
def test_display_data_resnet(self):
"""
Test that, with pre-loaded image features, all examples are different.
"""
self._test_display_output('resnet152')
class TestParlAIDialogTeacher(unittest.TestCase):
def test_good_fileformat(self):
"""
Checks that we fail to load a dataset where the use specified eval_labels.
"""
with testing_utils.tempdir() as tmpdir:
fp = os.path.join(tmpdir, "goodfile.txt")
with PathManager.open(fp, "w") as f:
f.write('id:test_file\ttext:input\tlabels:good label\n\n')
opt = {'task': 'fromfile', 'fromfile_datapath': fp, 'verbose': True}
testing_utils.display_data(opt)
def test_bad_fileformat(self):
"""
Checks that we fail to load a dataset where the use specified eval_labels.
"""
with testing_utils.tempdir() as tmpdir:
fp = os.path.join(tmpdir, "badfile.txt")
with PathManager.open(fp, "w") as f:
f.write('id:test_file\ttext:input\teval_labels:bad label\n\n')
opt = {'task': 'fromfile', 'fromfile_datapath': fp, 'verbose': True}
with self.assertRaises(ValueError):
testing_utils.display_data(opt)
def test_no_text(self):
with testing_utils.tempdir() as tmpdir:
fp = os.path.join(tmpdir, "badfile.txt")
with PathManager.open(fp, "w") as f:
f.write('id:test_file\tlabels:bad label\n\n')
opt = {'task': 'fromfile', 'fromfile_datapath': fp, 'verbose': True}
with self.assertRaises(ValueError):
testing_utils.display_data(opt)
def test_no_labels(self):
with testing_utils.tempdir() as tmpdir:
fp = os.path.join(tmpdir, "badfile.txt")
with PathManager.open(fp, "w") as f:
f.write('id:test_file\ttext:bad text\n\n')
opt = {'task': 'fromfile', 'fromfile_datapath': fp, 'verbose': True}
with self.assertRaises(ValueError):
testing_utils.display_data(opt)
def test_one_episode(self):
with testing_utils.tempdir() as tmpdir:
fp = os.path.join(tmpdir, "badfile.txt")
with PathManager.open(fp, "w") as f:
for _ in range(1000):
f.write('id:test_file\ttext:placeholder\tlabels:placeholder\n\n')
opt = {'task': 'fromfile', 'fromfile_datapath': fp, 'verbose': True}
with self.assertLogs(logger=logging.logger, level='DEBUG') as cm:
testing_utils.display_data(opt)
print("\n".join(cm.output))
assert any('long episode' in l for l in cm.output)
# invert the logic of the assertion
with self.assertRaises(self.failureException):
fp = os.path.join(tmpdir, "goodfile.txt")
with PathManager.open(fp, "w") as f:
for _ in range(1000):
f.write(
'id:test_file\ttext:placeholder\tlabels:placeholder\tepisode_done:True\n\n'
)
opt = {'task': 'fromfile', 'fromfile_datapath': fp, 'verbose': True}
with self.assertLogs(logger=logging.logger, level='DEBUG') as cm:
testing_utils.display_data(opt)
assert any('long episode' in l for l in cm.output)
class TestConversationTeacher(unittest.TestCase):
def test_good_fileformat(self):
"""
Checks that we succeed in loading a well formatted jsonl file.
"""
with testing_utils.tempdir() as tmpdir:
fp = os.path.join(tmpdir, "goodfile.jsonl")
with PathManager.open(fp, "w") as f:
f.write(
'{"dialog": [[{"text": "Hi.", "id": "speaker1"}, {"text": "Hello.", "id": "speaker2"}]]}\n'
)
opt = {'task': 'jsonfile', 'jsonfile_datapath': fp, 'verbose': True}
testing_utils.display_data(opt)
def test_no_text(self):
with testing_utils.tempdir() as tmpdir:
fp = os.path.join(tmpdir, "badfile.jsonl")
with PathManager.open(fp, "w") as f:
f.write(
'{"dialog": [[{"id": "speaker1"}, {"text": "Hello.", "id": "speaker2"}]]}\n'
)
opt = {'task': 'jsonfile', 'jsonfile_datapath': fp, 'verbose': True}
with self.assertRaises(AttributeError):
testing_utils.display_data(opt)
def test_firstspeaker_label(self):
with testing_utils.tempdir() as tmpdir:
fp = os.path.join(tmpdir, "goodfile.jsonl")
with PathManager.open(fp, "w") as f:
f.write(
'{"dialog": [[{"text": "Hi.", "id": "speaker1"}, {"text": "Hello.", "id": "speaker2"}]]}\n'
)
opt = {
'task': 'jsonfile',
'jsonfile_datapath': fp,
'verbose': True,
'label_turns': 'firstspeaker',
}
train_out, valid_out, test_out = testing_utils.display_data(opt)
texts = [
l.split(':', 1)[-1].strip()
for l in train_out.split('\n')
if l in train_out
if 'text' in l
]
labels = [
l.split(':', 1)[-1].strip()
for l in train_out.split('\n')
if l in train_out
if 'labels' in l
]
self.assertEqual(texts[0], '__SILENCE__')
self.assertEqual(labels[0], 'Hi.')
def test_secondspeaker_label(self):
with testing_utils.tempdir() as tmpdir:
fp = os.path.join(tmpdir, "goodfile.jsonl")
with PathManager.open(fp, "w") as f:
f.write(
'{"dialog": [[{"text": "Hi.", "id": "speaker1"}, {"text": "Hello.", "id": "speaker2"}]]}\n'
)
opt = {
'task': 'jsonfile',
'jsonfile_datapath': fp,
'verbose': True,
'label_turns': 'secondspeaker',
}
train_out, valid_out, test_out = testing_utils.display_data(opt)
texts = [
l.split(':', 1)[-1].strip()
for l in train_out.split('\n')
if l in train_out
if 'text' in l
]
labels = [
l.split(':', 1)[-1].strip()
for l in train_out.split('\n')
if l in train_out
if 'labels' in l
]
self.assertEqual(texts[0], 'Hi.')
self.assertEqual(labels[0], 'Hello.')
def test_both_label(self):
with testing_utils.tempdir() as tmpdir:
fp = os.path.join(tmpdir, "goodfile.jsonl")
with PathManager.open(fp, "w") as f:
f.write(
'{"dialog": [[{"text": "Hi.", "id": "speaker1"}, {"text": "Hello.", "id": "speaker2"}]]}\n'
)
opt = {
'task': 'jsonfile',
'jsonfile_datapath': fp,
'verbose': True,
'label_turns': 'both',
}
train_out, valid_out, test_out = testing_utils.display_data(opt)
texts = [
l.split(':', 1)[-1].strip()
for l in train_out.split('\n')
if l in train_out
if 'text' in l
]
labels = [
l.split(':', 1)[-1].strip()
for l in train_out.split('\n')
if l in train_out
if 'labels' in l
]
num_episodes = train_out.count("END OF EPISODE")
self.assertEqual(texts[0], '__SILENCE__')
self.assertEqual(labels[0], 'Hi.')
self.assertEqual(texts[1], 'Hi.')
self.assertEqual(labels[1], 'Hello.')
self.assertEqual(num_episodes, 2)
@register_agent("unique_examples")
class UniqueExamplesAgent(RepeatLabelAgent):
"""
Simple agent which asserts that it has only seen unique examples.
Useful for debugging. Inherits from RepeatLabelAgent.
"""
def __init__(self, opt, shared=None):
super().__init__(opt)
self.unique_examples = defaultdict(int)
def reset(self):
super().reset()
self.unique_examples = defaultdict(int)
def act(self):
obs = self.observation
text = obs.get('text')
if text in self.unique_examples:
raise RuntimeError(f'Already saw example: {text}')
else:
self.unique_examples[text] += 1
return super().act()
class TestChunkTeacher(unittest.TestCase):
"""
Test chunked teacher.
"""
def test_no_batched(self):
valid, test = testing_utils.eval_model(
dict(task='integration_tests:chunky', model='repeat_label'),
valid_datatype='valid:stream',
test_datatype='test:stream',
)
assert valid['exs'] == 100
assert test['exs'] == 100
def test_batched(self):
valid, test = testing_utils.eval_model(
dict(
task='integration_tests:chunky',
model='parlai.agents.test_agents.test_agents:MockTorchAgent',
batchsize=32,
),
valid_datatype='valid:stream',
test_datatype='test:stream',
)
assert valid['exs'] == 100
assert test['exs'] == 100
def test_dynamic_batched(self):
valid, test = testing_utils.eval_model(
dict(
task='integration_tests:chunky',
model='parlai.agents.test_agents.test_agents:MockTorchAgent',
datatype='valid:stream',
batchsize=32,
truncate=16,
dynamic_batching='full',
),
valid_datatype='valid:stream',
test_datatype='test:stream',
)
assert valid['exs'] == 100
assert test['exs'] == 100
def test_stream_only(self):
with self.assertRaises(ValueError):
valid, test = testing_utils.eval_model(
dict(
task='integration_tests:chunky',
model='parlai.agents.test_agents.test_agents:MockTorchAgent',
batchsize=32,
),
valid_datatype='valid',
)
with self.assertRaises(ValueError):
valid, test = testing_utils.eval_model(
dict(
task='integration_tests:chunky',
model='parlai.agents.test_agents.test_agents:MockTorchAgent',
batchsize=32,
),
valid_datatype='valid:stream',
test_datatype='test',
)
def test_slow_loading(self):
"""
Test that a slow loading teacher sees the right examples during validation.
"""
with testing_utils.tempdir() as tmpdir:
model_file = os.path.join(tmpdir, 'model')
valid, test = testing_utils.train_model(
dict(
task='integration_tests:chunky_unique_slow',
model='unique_examples',
model_file=model_file,
datatype='train:stream',
num_epochs=0.5,
validation_every_n_epochs=0.1,
batchsize=1,
dynamic_batching='full',
dict_maxexs=0,
)
)
class CustomEvaluationTeacher(DialogTeacher):
def __init__(self, opt, shared=None):
opt['datafile'] = 'mock'
super().__init__(opt, shared)
def custom_evaluation(self, teacher_action, label, model_response):
self.metrics.add('contains1', SumMetric(int('1' in model_response['text'])))
def setup_data(self, fold):
yield ('1 2', '1 2'), True
yield ('3 4', '3 4'), True
class TestCustomEvaluation(unittest.TestCase):
def test_custom_eval(self):
opt = {'task': 'custom', 'datatype': 'valid'}
teacher = CustomEvaluationTeacher(opt)
teacher.act()
teacher.observe({'text': 'a b'})
teacher.act()
teacher.observe({'text': '1 2'})
report = teacher.report()
assert 'contains1' in report
assert report['contains1'] == 1
assert report['exs'] == 2
class _MockTeacher(DialogTeacher):
def __init__(self, opt, shared=None):
opt['datafile'] = 'mock'
super().__init__(opt)
class TupleTeacher(_MockTeacher):
def setup_data(self, datafile):
for _ in range(3):
for j in range(1, 4):
yield (str(j), str(j * 2)), j == 1
class DictTeacher(_MockTeacher):
def setup_data(self, datafile):
for _ in range(3):
for j in range(1, 4):
yield {'text': str(j), 'label': str(j * 2)}, j == 1
class MessageTeacher(_MockTeacher):
def setup_data(self, datafile):
for _ in range(3):
for j in range(1, 4):
yield Message({'text': str(j), 'label': str(j * 2)}), j == 1
class NoDatafileTeacher(DialogTeacher):
def setup_data(self, datafile):
yield Message({'text': datafile, 'label': datafile}), True
class ViolationTeacher(_MockTeacher):
def setup_data(self, datafile):
yield {'text': 'foo', 'episode_done': True}, True
class TestDialogTeacher(unittest.TestCase):
def test_nodatafile(self):
for dt in [
'train:ordered',
'train:stream:ordered',
'valid',
'test',
'valid:stream',
'test:stream',
]:
opt = Opt({'datatype': dt, 'datapath': '/tmp', 'task': 'test'})
with self.assertRaises(KeyError):
NoDatafileTeacher(opt)
def _verify_act(self, act, goal_text, goal_label, episode_done):
assert 'eval_labels' in act or 'labels' in act
labels = act.get('labels', act.get('eval_labels'))
assert isinstance(labels, tuple)
assert len(labels) == 1
assert act['text'] == str(goal_text)
assert labels[0] == str(goal_label)
def _test_iterate(self, teacher_class):
for dt in [
'train:ordered',
'train:stream:ordered',
'valid',
'test',
'valid:stream',
'test:stream',
]:
opt = Opt({'datatype': dt, 'datapath': '/tmp', 'task': 'test'})
teacher = teacher_class(opt)
self._verify_act(teacher.act(), 1, 2, False)
self._verify_act(teacher.act(), 2, 4, False)
self._verify_act(teacher.act(), 3, 6, True)
self._verify_act(teacher.act(), 1, 2, False)
self._verify_act(teacher.act(), 2, 4, False)
self._verify_act(teacher.act(), 3, 6, True)
self._verify_act(teacher.act(), 1, 2, False)
self._verify_act(teacher.act(), 2, 4, False)
self._verify_act(teacher.act(), 3, 6, True)
assert teacher.epoch_done()
def test_tuple_teacher(self):
self._test_iterate(TupleTeacher)
def test_dict_teacher(self):
self._test_iterate(DictTeacher)
def test_message_teacher(self):
self._test_iterate(MessageTeacher)
def test_violation_teacher(self):
with self.assertRaises(KeyError):
self._test_iterate(ViolationTeacher)
if __name__ == '__main__':
unittest.main()