-
Notifications
You must be signed in to change notification settings - Fork 251
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
Move from_preset
to base tokenizer classes
#673
Conversation
@jbischof Please review. |
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.
Thanks you!!
The actual runnable code changes looks good, just one minor comment.
We will likely need to do something fancier for docstrings though. Will think through this a bit and post more here.
@@ -536,3 +537,57 @@ def _bpe_merge_and_update_cache(self, tokens): | |||
tokenized_words, axis=1, separator=" " | |||
) | |||
self.cache.insert(tokens, tokenized_words) | |||
|
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.
each of these classes should probably have a preset property defined now, that is empty. E.g.
https://github.com/keras-team/keras-nlp/blob/master/keras_nlp/models/backbone.py#L33-L35
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.
Thanks mostly documentation changes!
tokenizer.detokenize([5, 6, 7, 8, 9]) | ||
``` | ||
""" | ||
|
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.
After adding the preset
property, check if empty as we do for backbone (link). Same for the other two tokenizers.
preset, | ||
**kwargs, | ||
): | ||
"""Instantiate a GPT-2 tokenizer from preset vocabulary and merge rules. |
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.
Please remove GPT-2 language. This should be a generic docstring for BPE. Same for the other two
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.
We can actually switch this to a templatized version like this... https://github.com/keras-team/keras-nlp/blob/c9e5040bf7646da471bf9cec2177be2398162568/keras_nlp/models/backbone.py#L45-L65
Make sure to not copy that verbatim, we should keep the language from this docstring, but update this to use the format variables {{model_name}}
{{preset_names}}
and {{example_preset_name}}
.
To get that working, you will also need to copy the __init_subclass__
method we use for our Backbone
and Task
classes, but you should be able to copy that almost exactly (just update Backbone -> BytePairTokenizer). https://github.com/keras-team/keras-nlp/blob/c9e5040bf7646da471bf9cec2177be2398162568/keras_nlp/models/backbone.py#L94-L114
We should make similar changes to the other tokenizer base classes.
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.
Left some comments re how we can handle the docstrings here.
Also, we are a bit of a moving target here (lots of changes this week!), but you can also mirror these changes for the albert and f_net models. Thank you!
preset, | ||
**kwargs, | ||
): | ||
"""Instantiate a GPT-2 tokenizer from preset vocabulary and merge rules. |
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.
We can actually switch this to a templatized version like this... https://github.com/keras-team/keras-nlp/blob/c9e5040bf7646da471bf9cec2177be2398162568/keras_nlp/models/backbone.py#L45-L65
Make sure to not copy that verbatim, we should keep the language from this docstring, but update this to use the format variables {{model_name}}
{{preset_names}}
and {{example_preset_name}}
.
To get that working, you will also need to copy the __init_subclass__
method we use for our Backbone
and Task
classes, but you should be able to copy that almost exactly (just update Backbone -> BytePairTokenizer). https://github.com/keras-team/keras-nlp/blob/c9e5040bf7646da471bf9cec2177be2398162568/keras_nlp/models/backbone.py#L94-L114
We should make similar changes to the other tokenizer base classes.
@@ -113,51 +111,9 @@ def __init__( | |||
def presets(cls): | |||
return copy.deepcopy({**backbone_presets, **classifier_presets}) | |||
|
|||
@classmethod | |||
@format_docstring(names=PRESET_NAMES) | |||
def from_preset( |
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.
After following the changes below re docstrings and __init__subclass__
you should be able to remove the from_preset
method here and elsewhere entirely!
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.
@shivance, we don't need from_preset
in subclasses anymore! See, for example, BertPreprocessor
Looks like you also have some formatting issues on this PR, checkout the |
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.
Good progress! Let us know if you get stuck. You need to run the format.sh
script before every commit, so let us know if you're having trouble there.
@@ -113,51 +111,9 @@ def __init__( | |||
def presets(cls): | |||
return copy.deepcopy({**backbone_presets, **classifier_presets}) | |||
|
|||
@classmethod | |||
@format_docstring(names=PRESET_NAMES) | |||
def from_preset( |
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.
@shivance, we don't need from_preset
in subclasses anymore! See, for example, BertPreprocessor
) | ||
|
||
return cls.from_config({**config, **kwargs}) | ||
return super().from_preset(cls, preset, **kwargs) |
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.
You need a newline at the end of each file. Are you still having issues with the format.sh
script?
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.
Hi @jbischof , I'm still addressing the comments. So work is pending.
I tend to run formatting scripts upon finishing changes for every round of review.
Should I continue with this or run it every time before commit?
Thanks.
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.
Sorry, didn't understand this was still WIP! Up to you on how you organize your commits 😄
@jbischof It's ready for review now 😄 |
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.
LGTM! Thank you!
Just found a few small nits that need fixing.
@@ -89,52 +87,3 @@ def __init__(self, proto, **kwargs): | |||
@classproperty |
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.
We will need some changes to the class level docstrings for our model specific tokenizers, we should document the from preset usage front and center in our code examples above. But I think that would best be done as a follow up anyway, just opened #688
"""Instantiate {{model_name}} tokenizer from preset vocabulary. | ||
|
||
Args: | ||
preset: string. Must be one of {{preset_names}}. |
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.
We actually need {{preset_names}}
surrounded by quotes for the docstring to render correctly. See https://github.com/keras-team/keras-nlp/blob/3cfdeb6bb1eeacd755a880f1674bf8b9d765aa43/keras_nlp/models/backbone.py#L58
"""Instantiate {{model_name}} tokenizer from preset vocabulary. | ||
|
||
Args: | ||
preset: string. Must be one of {{preset_names}}. |
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.
Surround with quotes.
"""Instantiate {{model_name}} tokenizer from preset vocabulary. | ||
|
||
Args: | ||
preset: string. Must be one of {{preset_names}}. |
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.
Surround with quotes.
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.
Looks good in general, but please follow @mattdangerw's suggestions and fix the formatting
Actually, since my comments add up to just a couple lines changes, I can just make these as merge this. Thanks very much for contribution! |
Closes #648