Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

[Mutators] Ensure mutations do not persist across epochs #3649

Merged
merged 2 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions parlai/core/teachers.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,10 @@ def next_example(self):
break
buffer_entry_idx += 1
# apply mutators
for mutator in self.mutators:
episode_buffer = mutator(episode_buffer)
if self.mutators:
episode_buffer = [m.copy() for m in episode_buffer]
for mutator in self.mutators:
episode_buffer = mutator(episode_buffer)
self.episode_buffer = list(episode_buffer)

if not self.episode_buffer:
Expand Down Expand Up @@ -766,8 +768,10 @@ def next_example(self):
self._saw_epoch_done = epoch_done
break
# perform any mutations there are
for mutator in self.mutators:
episode_buffer = mutator(episode_buffer)
if self.mutators:
episode_buffer = [m.copy() for m in episode_buffer]
for mutator in self.mutators:
episode_buffer = mutator(episode_buffer)
# make sure mutations are fully realized (not generators)
self.episode_buffer = list(episode_buffer)
# The recursive call has dual purpose:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_mutators.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,27 @@ def test_word_shuffle(self):
assert set(ex2['text'].split()) == set(EXAMPLE2['text'].split())
assert set(ex3['text'].split()) == set(EXAMPLE3['text'].split())
assert set(ex4['text'].split()) == set(EXAMPLE4['text'].split())


class TestMutatorStickiness(unittest.TestCase):
"""
Test that mutations DO NOT stick with episode.
"""

def test_not_sticky(self):
pp = ParlaiParser(True, False)
opt = pp.parse_kwargs(
task='integration_tests:multiturn',
mutators='flatten',
datatype='train:ordered',
)
teacher = create_task_agent_from_taskname(opt)[0]
first_epoch = []
second_epoch = []
for _ in range(teacher.num_examples()):
first_epoch.append(teacher.act())
teacher.reset()
for _ in range(teacher.num_examples()):
second_epoch.append(teacher.act())

assert all(f == s for f, s in zip(first_epoch, second_epoch))