-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
[WIP] Safe bpe dropout for LM + joiner disjoin dropout #2009
Open
funboarder13920
wants to merge
5
commits into
OpenNMT:master
Choose a base branch
from
funboarder13920:bpe_dropout_
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
from onmt.utils.logging import logger | ||
from onmt.transforms import register_transform | ||
from .transform import Transform | ||
from onmt.constants import ModelTask | ||
|
||
|
||
class TokenizerTransform(Transform): | ||
|
@@ -90,6 +91,7 @@ def _parse_opts(self): | |
self.tgt_subword_vocab = self.opts.tgt_subword_vocab | ||
self.src_vocab_threshold = self.opts.src_vocab_threshold | ||
self.tgt_vocab_threshold = self.opts.tgt_vocab_threshold | ||
self.model_task = getattr(self.opts, "model_task", None) | ||
|
||
def _repr_args(self): | ||
"""Return str represent key arguments for TokenizerTransform.""" | ||
|
@@ -169,7 +171,10 @@ def _tokenize(self, tokens, side='src', is_train=False): | |
def apply(self, example, is_train=False, stats=None, **kwargs): | ||
"""Apply sentencepiece subword encode to src & tgt.""" | ||
src_out = self._tokenize(example['src'], 'src', is_train) | ||
tgt_out = self._tokenize(example['tgt'], 'tgt', is_train) | ||
if self.model_task == ModelTask.LANGUAGE_MODEL: | ||
tgt_out = src_out | ||
else: | ||
tgt_out = self._tokenize(example['tgt'], 'tgt', is_train) | ||
if stats is not None: | ||
n_words = len(example['src']) + len(example['tgt']) | ||
n_subwords = len(src_out) + len(tgt_out) | ||
|
@@ -243,7 +248,10 @@ def _tokenize(self, tokens, side='src', is_train=False): | |
def apply(self, example, is_train=False, stats=None, **kwargs): | ||
"""Apply bpe subword encode to src & tgt.""" | ||
src_out = self._tokenize(example['src'], 'src', is_train) | ||
tgt_out = self._tokenize(example['tgt'], 'tgt', is_train) | ||
if self.model_task == ModelTask.LANGUAGE_MODEL: | ||
tgt_out = src_out | ||
else: | ||
tgt_out = self._tokenize(example['tgt'], 'tgt', is_train) | ||
if stats is not None: | ||
n_words = len(example['src']) + len(example['tgt']) | ||
n_subwords = len(src_out) + len(tgt_out) | ||
|
@@ -327,7 +335,7 @@ def get_specials(cls, opts): | |
tgt_specials.update(_case_specials) | ||
return (set(), set()) | ||
|
||
def _get_subword_kwargs(self, side='src'): | ||
def _get_subword_kwargs(self, side='src', is_train=False): | ||
"""Return a dict containing kwargs relate to `side` subwords.""" | ||
subword_type = self.tgt_subword_type if side == 'tgt' \ | ||
else self.src_subword_type | ||
|
@@ -338,6 +346,10 @@ def _get_subword_kwargs(self, side='src'): | |
subword_alpha = self.tgt_subword_alpha if side == 'tgt' \ | ||
else self.src_subword_alpha | ||
kwopts = dict() | ||
if not is_train: | ||
# disable random aspects during validation | ||
subword_alpha = 0 | ||
subword_nbest = 1 | ||
if subword_type == 'bpe': | ||
kwopts['bpe_model_path'] = subword_model | ||
kwopts['bpe_dropout'] = subword_alpha | ||
|
@@ -360,42 +372,65 @@ def warm_up(self, vocabs=None): | |
"""Initialize Tokenizer models.""" | ||
super().warm_up(None) | ||
import pyonmttok | ||
src_subword_kwargs = self._get_subword_kwargs(side='src') | ||
|
||
src_subword_kwargs = self._get_subword_kwargs( | ||
side="src", is_train=True | ||
) | ||
valid_src_subword_kwargs = self._get_subword_kwargs( | ||
side="src", is_train=False | ||
) | ||
src_tokenizer = pyonmttok.Tokenizer( | ||
**src_subword_kwargs, **self.src_other_kwargs | ||
) | ||
tgt_subword_kwargs = self._get_subword_kwargs(side='tgt') | ||
_diff_vocab = ( | ||
src_subword_kwargs.get('vocabulary_path', '') != | ||
tgt_subword_kwargs.get('vocabulary_path', '') or | ||
src_subword_kwargs.get('vocabulary_threshold', 0) != | ||
tgt_subword_kwargs.get('vocabulary_threshold', 0)) | ||
Comment on lines
-368
to
-372
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally prefer current one which seems more readable |
||
valid_src_tokenizer = pyonmttok.Tokenizer( | ||
**valid_src_subword_kwargs, **self.src_other_kwargs | ||
) | ||
tgt_subword_kwargs = self._get_subword_kwargs( | ||
side="tgt", is_train=True | ||
) | ||
_diff_vocab = src_subword_kwargs.get( | ||
"vocabulary_path", "" | ||
) != tgt_subword_kwargs.get( | ||
"vocabulary_path", "" | ||
) or src_subword_kwargs.get( | ||
"vocabulary_threshold", 0 | ||
) != tgt_subword_kwargs.get( | ||
"vocabulary_threshold", 0 | ||
) | ||
if self.share_vocab and not _diff_vocab: | ||
self.load_models = { | ||
'src': src_tokenizer, | ||
'tgt': src_tokenizer | ||
"src": {"train": src_tokenizer, "valid": valid_src_tokenizer}, | ||
"tgt": {"train": src_tokenizer, "valid": valid_src_tokenizer}, | ||
} | ||
else: | ||
tgt_subword_kwargs = self._get_subword_kwargs(side='tgt') | ||
tgt_tokenizer = pyonmttok.Tokenizer( | ||
**tgt_subword_kwargs, **self.tgt_other_kwargs | ||
) | ||
valid_tgt_subword_kwargs = self._get_subword_kwargs( | ||
side="tgt", is_train=False | ||
) | ||
valid_tgt_tokenizer = pyonmttok.Tokenizer( | ||
**valid_tgt_subword_kwargs, **self.tgt_other_kwargs | ||
) | ||
self.load_models = { | ||
'src': src_tokenizer, | ||
'tgt': tgt_tokenizer | ||
"src": {"train": src_tokenizer, "valid": valid_src_tokenizer}, | ||
"tgt": {"train": tgt_tokenizer, "valid": valid_tgt_tokenizer}, | ||
} | ||
|
||
def _tokenize(self, tokens, side='src', is_train=False): | ||
"""Do OpenNMT Tokenizer's tokenize.""" | ||
tokenizer = self.load_models[side] | ||
tokenizer = self.load_models[side]['train' if is_train else 'valid'] | ||
sentence = ' '.join(tokens) | ||
segmented, _ = tokenizer.tokenize(sentence) | ||
return segmented | ||
|
||
def apply(self, example, is_train=False, stats=None, **kwargs): | ||
"""Apply OpenNMT Tokenizer to src & tgt.""" | ||
src_out = self._tokenize(example['src'], 'src') | ||
tgt_out = self._tokenize(example['tgt'], 'tgt') | ||
src_out = self._tokenize(example['src'], 'src', is_train) | ||
if self.model_task == ModelTask.LANGUAGE_MODEL: | ||
tgt_out = src_out | ||
else: | ||
tgt_out = self._tokenize(example['tgt'], 'tgt', is_train) | ||
if stats is not None: | ||
n_words = len(example['src']) + len(example['tgt']) | ||
n_subwords = len(src_out) + len(tgt_out) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the necessity of doing right and left token sides
It might create a difficulty to retrieve the initial token when detokenizing.
I can use a special token do distinguish left and right or remove the left side disjoin which doesn't occur much (mainly in punctuation)