Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the encode function in BART hub_interface: to add an extra option for not always adding OOV tokens into vocabulary #3905

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions fairseq/models/bart/hub_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ def __init__(self, cfg, task, model):
self.model = self.models[0]

def encode(
self, sentence: str, *addl_sentences, no_separator=True
self,
sentence: str,
*addl_sentences,
no_separator=True,
add_if_not_exist=True
) -> torch.LongTensor:
"""
BPE-encode a sentence (or multiple sentences).
Expand Down Expand Up @@ -59,7 +63,9 @@ def encode(
for s in addl_sentences:
bpe_sentence += " </s>" if not no_separator else ""
bpe_sentence += " " + self.bpe.encode(s) + " </s>"
tokens = self.task.source_dictionary.encode_line(bpe_sentence, append_eos=False)
tokens = self.task.source_dictionary.encode_line(
bpe_sentence, append_eos=False, add_if_not_exist=add_if_not_exist
)
return tokens.long()

def decode(self, tokens: torch.LongTensor):
Expand Down