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

[mutators] Prevent name collisions in mutators. #4006

Merged
merged 5 commits into from
Sep 22, 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
5 changes: 5 additions & 0 deletions parlai/core/mutators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_

Expand Down
18 changes: 9 additions & 9 deletions parlai/tasks/wizard_of_internet/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,47 +581,47 @@ 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
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
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_woi
"""

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).
Expand All @@ -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_woi
"""

pass
12 changes: 6 additions & 6 deletions parlai/tasks/wizard_of_wikipedia/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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).
Expand All @@ -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_wow
"""

def message_mutation(self, message: Message) -> Message:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_mutators.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,26 @@ 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

# 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")
class Mutator2(Mutator):
pass