From 2c45d4a18316c4bf800d8670808609baed5cda44 Mon Sep 17 00:00:00 2001 From: Stephen Roller Date: Sun, 12 Sep 2021 11:17:20 -0400 Subject: [PATCH 1/5] [mutators] Prevent name collisions in mutators. --- parlai/core/mutators.py | 5 +++++ tests/test_mutators.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/parlai/core/mutators.py b/parlai/core/mutators.py index 56efced4c44..bc907b6eda8 100644 --- a/parlai/core/mutators.py +++ b/parlai/core/mutators.py @@ -58,6 +58,11 @@ def register_mutator(name: str) -> Callable[[Type], Type]: def _inner(cls_: Type) -> Type: global MUTATOR_REGISTRY + if name in MUTATOR_REGISTRY and cls_ is not MUTATOR_REGISTRY[name]: + raise NameError( + "Mutators must be uniquely named, but detected two mutators with " + f"the name '{name}'." + ) MUTATOR_REGISTRY[name] = cls_ return cls_ diff --git a/tests/test_mutators.py b/tests/test_mutators.py index c26a2712987..33a2789d35b 100644 --- a/tests/test_mutators.py +++ b/tests/test_mutators.py @@ -250,3 +250,22 @@ def test_not_sticky(self): second_epoch.append(teacher.act()) assert all(f == s for f, s in zip(first_epoch, second_epoch)) + + +class TestUniqueness(unittest.TestCase): + """ + Test that mutators cannot have duplicate names. + """ + + def test_uniqueness(self): + from parlai.core.mutators import register_mutator, Mutator + + @register_mutator("test_unique_mutator") + class Mutator1(Mutator): + pass + + with self.assertRaises(NameError): + + @register_mutator("test_unique_mutator") + class Mutator2(Mutator): + pass From e895e70965b047a94cd5c48b1ae1d08fe5ed986f Mon Sep 17 00:00:00 2001 From: Stephen Roller Date: Sun, 12 Sep 2021 11:19:09 -0400 Subject: [PATCH 2/5] Enhance test --- tests/test_mutators.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_mutators.py b/tests/test_mutators.py index 33a2789d35b..a1a80864a03 100644 --- a/tests/test_mutators.py +++ b/tests/test_mutators.py @@ -264,6 +264,10 @@ def test_uniqueness(self): class Mutator1(Mutator): pass + # don't freak out if we accidentally register the exact same class twice + register_mutator("test_unique_mutator")(Mutator1) + + # but do demand uniqueness with self.assertRaises(NameError): @register_mutator("test_unique_mutator") From 9c53943f7578fd21417dbf093c9c43648c4e9b81 Mon Sep 17 00:00:00 2001 From: Stephen Roller Date: Thu, 16 Sep 2021 08:35:25 -0400 Subject: [PATCH 3/5] Fix mutator collision in wizard tasks --- parlai/tasks/wizard_of_internet/agents.py | 4 ++-- parlai/tasks/wizard_of_wikipedia/agents.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/parlai/tasks/wizard_of_internet/agents.py b/parlai/tasks/wizard_of_internet/agents.py index e5c9302baf1..0de39112662 100644 --- a/parlai/tasks/wizard_of_internet/agents.py +++ b/parlai/tasks/wizard_of_internet/agents.py @@ -581,13 +581,13 @@ def _knowledge_piece(self): return CONST.SELECTED_DOCS_TITLES -@register_mutator("add_checked_sentence_to_input") +@register_mutator("add_checked_sentence_to_input_woi") class AddCheckedSentence(AddCheckedSentenceWizWiki): """ Adds the checked sentence to the end of the text. E.g. run with: parlai display_data -t wizard_of_internet -n 100 -dt valid --mutators - flatten,add_checked_sentence_to_input + flatten,add_checked_sentence_to_input_woi """ @property diff --git a/parlai/tasks/wizard_of_wikipedia/agents.py b/parlai/tasks/wizard_of_wikipedia/agents.py index a0d85490296..75dc6ad305b 100644 --- a/parlai/tasks/wizard_of_wikipedia/agents.py +++ b/parlai/tasks/wizard_of_wikipedia/agents.py @@ -1279,7 +1279,7 @@ class SelfchatTeacher(BasicBothDialogTeacher): pass -@register_mutator("add_checked_sentence_to_input") +@register_mutator("add_checked_sentence_to_input_wow") class AddCheckedSentence(MessageMutator): """ Adds the checked sentence to the end of the text. From 5d54bf3d1dbc8928d7e7179c84f090b6642daeb0 Mon Sep 17 00:00:00 2001 From: Stephen Roller Date: Thu, 16 Sep 2021 10:20:44 -0400 Subject: [PATCH 4/5] More conflicts. --- parlai/tasks/wizard_of_internet/agents.py | 14 +++++++------- parlai/tasks/wizard_of_wikipedia/agents.py | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/parlai/tasks/wizard_of_internet/agents.py b/parlai/tasks/wizard_of_internet/agents.py index 0de39112662..70894ef046f 100644 --- a/parlai/tasks/wizard_of_internet/agents.py +++ b/parlai/tasks/wizard_of_internet/agents.py @@ -595,13 +595,13 @@ def checked_sentence_kword(self): return CONST.SELECTED_SENTENCES -@register_mutator("checked_sentence_as_label") +@register_mutator("checked_sentence_as_label_woi") class CheckedSentenceAsLabel(CheckedSentenceAsLabelWizWiki): """ Uses the checked sentence (knowledge) as label. E.g. run with: parlai display_data -t wizard_of_internet -n 100 -dt valid --mutators - flatten,checked_sentence_as_label + flatten,checked_sentence_as_label_woi """ @property @@ -609,19 +609,19 @@ def checked_sentence_kword(self): return CONST.SELECTED_SENTENCES -@register_mutator("add_label_to_input") +@register_mutator("add_label_to_input_woi") class AddLabel(AddLabelWizWiki): """ Adds the dialogue sentence to the input. E.g. run with: parlai display_data -t wizard_of_internet -n 100 -dt valid --mutators - flatten,checked_sentence_as_label,add_label_to_input + flatten,checked_sentence_as_label_woi,add_label_to_input """ pass -@register_mutator("add_label_to_input_lm") +@register_mutator("add_label_to_input_lm_woi") class AddLabelLM(AddLabelLMWizWiki): """ Adds the dialogue sentence to the input (language modeling version). @@ -630,11 +630,11 @@ class AddLabelLM(AddLabelLMWizWiki): the input. The rest is placed inside special tokens. E.g. run with: parlai display_data -t wizard_of_internet -n 100 -dt valid --mutators - flatten,add_label_to_input_lm + flatten,add_label_to_input_lm_woi To add the checked sentence as the label, use: parlai display_data -t wizard_of_internet -n 100 -dt valid --mutators - flatten,add_label_to_input_lm,checked_sentence_as_label + flatten,add_label_to_input_lm_woi,checked_sentence_as_label """ pass diff --git a/parlai/tasks/wizard_of_wikipedia/agents.py b/parlai/tasks/wizard_of_wikipedia/agents.py index 75dc6ad305b..6de79b974f5 100644 --- a/parlai/tasks/wizard_of_wikipedia/agents.py +++ b/parlai/tasks/wizard_of_wikipedia/agents.py @@ -1306,7 +1306,7 @@ def message_mutation(self, message: Message) -> Message: return new_message -@register_mutator("checked_sentence_as_label") +@register_mutator("checked_sentence_as_label_wow") class CheckedSentenceAsLabel(MessageMutator): """ Uses the checked sentence (knowledge) as label. @@ -1330,7 +1330,7 @@ def message_mutation(self, message: Message) -> Message: return new_message -@register_mutator("add_label_to_input") +@register_mutator("add_label_to_input_wow") class AddLabel(MessageMutator): """ Adds the dialogue sentence to the input. @@ -1356,7 +1356,7 @@ def message_mutation(self, message: Message) -> Message: return new_message -@register_mutator("add_label_to_input_lm") +@register_mutator("add_label_to_input_lm_wow") class AddLabelLM(MessageMutator): """ Adds the dialogue sentence to the input (language modeling version). @@ -1365,11 +1365,11 @@ class AddLabelLM(MessageMutator): the input. The rest is placed inside special tokens. E.g. run with: parlai display_data -t wizard_of_wikipedia -n 100 -dt valid --mutators - flatten,add_label_to_input_lm + flatten,add_label_to_input_lm_wow To add the checked sentence as the label, use: parlai display_data -t wizard_of_wikipedia -n 100 -dt valid --mutators - flatten,add_label_to_input_lm,checked_sentence_as_label + flatten,add_label_to_input_lm_wow,checked_sentence_as_label """ def message_mutation(self, message: Message) -> Message: From 3a55f26bf84c8526ae886e4baf47ff7b8e5a92ad Mon Sep 17 00:00:00 2001 From: Stephen Roller Date: Thu, 16 Sep 2021 10:41:07 -0400 Subject: [PATCH 5/5] More doc updates. --- parlai/tasks/wizard_of_internet/agents.py | 4 ++-- parlai/tasks/wizard_of_wikipedia/agents.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/parlai/tasks/wizard_of_internet/agents.py b/parlai/tasks/wizard_of_internet/agents.py index 70894ef046f..bab275bcdec 100644 --- a/parlai/tasks/wizard_of_internet/agents.py +++ b/parlai/tasks/wizard_of_internet/agents.py @@ -615,7 +615,7 @@ class AddLabel(AddLabelWizWiki): Adds the dialogue sentence to the input. E.g. run with: parlai display_data -t wizard_of_internet -n 100 -dt valid --mutators - flatten,checked_sentence_as_label_woi,add_label_to_input + flatten,checked_sentence_as_label_woi,add_label_to_input_woi """ pass @@ -634,7 +634,7 @@ class AddLabelLM(AddLabelLMWizWiki): To add the checked sentence as the label, use: parlai display_data -t wizard_of_internet -n 100 -dt valid --mutators - flatten,add_label_to_input_lm_woi,checked_sentence_as_label + flatten,add_label_to_input_lm_woi,checked_sentence_as_label_woi """ pass diff --git a/parlai/tasks/wizard_of_wikipedia/agents.py b/parlai/tasks/wizard_of_wikipedia/agents.py index 6de79b974f5..726ca955b29 100644 --- a/parlai/tasks/wizard_of_wikipedia/agents.py +++ b/parlai/tasks/wizard_of_wikipedia/agents.py @@ -1369,7 +1369,7 @@ class AddLabelLM(MessageMutator): To add the checked sentence as the label, use: parlai display_data -t wizard_of_wikipedia -n 100 -dt valid --mutators - flatten,add_label_to_input_lm_wow,checked_sentence_as_label + flatten,add_label_to_input_lm_wow,checked_sentence_as_label_wow """ def message_mutation(self, message: Message) -> Message: