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

Move from_preset to base tokenizer classes #673

Merged
merged 8 commits into from
Jan 24, 2023

Conversation

shivance
Copy link
Collaborator

Closes #648

@shivance
Copy link
Collaborator Author

@jbischof Please review.

@mattdangerw mattdangerw self-requested a review January 17, 2023 19:59
Copy link
Member

@mattdangerw mattdangerw left a 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)

Copy link
Member

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

Copy link
Contributor

@jbischof jbischof left a 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])
```
"""

Copy link
Contributor

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.
Copy link
Contributor

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

Copy link
Member

@mattdangerw mattdangerw Jan 19, 2023

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.

Copy link
Member

@mattdangerw mattdangerw left a 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.
Copy link
Member

@mattdangerw mattdangerw Jan 19, 2023

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(
Copy link
Member

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!

Copy link
Contributor

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

@mattdangerw
Copy link
Member

Looks like you also have some formatting issues on this PR, checkout the Tests / Check the code format (pull_request) above!

Copy link
Contributor

@jbischof jbischof left a 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(
Copy link
Contributor

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)
Copy link
Contributor

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?

Copy link
Collaborator Author

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.

Copy link
Contributor

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 😄

@shivance
Copy link
Collaborator Author

@jbischof It's ready for review now 😄

@shivance shivance requested a review from jbischof January 22, 2023 18:10
Copy link
Member

@mattdangerw mattdangerw left a 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
Copy link
Member

@mattdangerw mattdangerw Jan 23, 2023

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}}.
Copy link
Member

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}}.
Copy link
Member

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}}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surround with quotes.

Copy link
Contributor

@jbischof jbischof left a 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

@mattdangerw
Copy link
Member

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!

@mattdangerw mattdangerw merged commit 9d19bc5 into keras-team:master Jan 24, 2023
@shivance shivance deleted the issue648 branch February 13, 2023 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move from_preset to base tokenizer classes
3 participants