-
Notifications
You must be signed in to change notification settings - Fork 310
Expose token_embedding as a Backbone Property
#676
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
Merged
mattdangerw
merged 11 commits into
keras-team:master
from
abheesht17:bert-backbone-mem-vars
Jan 24, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fe2d084
Remove member variable
abheesht17 5c15110
Try get_layer() approach
abheesht17 f981468
Merge branch 'master' into bert-backbone-mem-vars
abheesht17 fc0ae8c
Format
abheesht17 07d370b
Merge branch 'keras-team:master' into bert-backbone-mem-vars
abheesht17 eb9712f
Expose token_embedding as a backbone property
abheesht17 a7442f3
RoBERTa fix test
abheesht17 d8f0eae
Fix attempt
abheesht17 91dc841
Small edit
abheesht17 14ec2f9
Reduce code delta
abheesht17 89c5f65
Remove .embedding
abheesht17 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
I agree with @NusretOzates, the
self._token_embeddingapproach seems cleaner than relying on a specific layer name. We already have several other similar class variables.Uh oh!
There was an error while loading. Please reload this page.
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.
@jbischof - it doesn't work for models with
TokenAndPositionEmbeddinglayer. Keras considersself._token_embeddingas a separate embedding layer, and errors out when we try to load preset checkpoints. Hence, this elaborate-ish solution.Uh oh!
There was an error while loading. Please reload this page.
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.
Yeah, I think what we are facing here is
setattrtracking on all keras layers. Basically anytime you are setting a layer attribute on self, if gets added to a list of resources used for serialization. It looks like this can affect our checkpoint compatibility! Which is not good, we don't want to be affecting our checkpoints just to expose something like this. Relevant code -> https://github.com/keras-team/keras/blob/2727df09aa284a94ce8234ad1279d9659cdf2064/keras/engine/base_layer.py#L3215-L3229The solution laid our here seems like a nice way to avoid the
setattrtracking entirely. This LGTM.The alternate I see would be to add a line
self._auto_track_sub_layers = Falseto the backbone base class. But this could run us into hot water if we ever had non-functional Backbones (not everything can be a functional model -> https://keras.io/guides/functional_api/#functional-api-weakness). So the solution here seem most robust!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.
I know I am dumping too much context, but for those interested in going deeper...
The
__setattr__tracking is deduped, so for Bert, where the token embedding is already a sublayer of the model directly, there is no issue here.self.some_properly = direct_layer_of_modelhas no issues. But Roberta for example will have the token embedding as a nested layer.self.some_property = nested_layer_of_modelwill change our checkpoint structure! This is what @NusretOzates was mentioning above.Also thanks @NusretOzates for that writeup! Very helpful!