-
Notifications
You must be signed in to change notification settings - Fork 309
Move from_preset to base tokenizer classes
#673
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
Conversation
|
@jbischof Please review. |
mattdangerw
left a comment
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.
| 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.
jbischof
left a comment
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.
mattdangerw
left a comment
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.
|
|
||
| @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 |
jbischof
left a comment
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.
|
|
||
| @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 😄 |
mattdangerw
left a comment
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.
| self.sep_token_id = self.token_to_id(sep_token) | ||
| self.pad_token_id = self.token_to_id(pad_token) | ||
|
|
||
| @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