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

Expose token_embedding as a Backbone Property #676

Merged
merged 11 commits into from
Jan 24, 2023

Conversation

abheesht17
Copy link
Collaborator

@abheesht17 abheesht17 commented Jan 18, 2023

  • Removed self.token_embedding = token_embedding_layer.
  • Rearranged all self references to follow the same order as __init__ args.

Edit:

  • Exposed token_embedding as a property.

@abheesht17 abheesht17 requested a review from jbischof January 18, 2023 06:19
@abheesht17 abheesht17 changed the title Remove self.token_embedding, Rearrange self. references Remove self.token_embedding, Rearrange self references Jan 18, 2023
@abheesht17 abheesht17 changed the title Remove self.token_embedding, Rearrange self references Remove self.token_embedding, Rearrange self References Jan 18, 2023
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.

one comment!

@abheesht17 abheesht17 changed the title Remove self.token_embedding, Rearrange self References Expose token_embedding as a Backbone Property Jan 20, 2023
@mattdangerw
Copy link
Member

mattdangerw commented Jan 20, 2023

These test failures are interesting, changing the layers we set on self changes our checkpoints structure. We may need to dig into this more, let's talk tomorrow!

@abheesht17
Copy link
Collaborator Author

abheesht17 commented Jan 20, 2023

These test failures are interesting, changing the layers we set on self changes our checkpoints structure. We may need to dig into this more, let's talk tomorrow!

Very weird. I haven't changed the names of the layers!

And it is happening only for those for which I have broken the call into two:

        # Embed tokens and positions.
        token_and_position_embedding_layer = TokenAndPositionEmbedding(
            vocabulary_size=vocabulary_size,
            sequence_length=max_sequence_length,
            embedding_dim=hidden_dim,
            embeddings_initializer=roberta_kernel_initializer(),
            name="embeddings",
        )
        embedding = token_and_position_embedding_layer(token_id_input)

instead of

        embedding = TokenAndPositionEmbedding(
            vocabulary_size=vocabulary_size,
            sequence_length=max_sequence_length,
            embedding_dim=hidden_dim,
            embeddings_initializer=roberta_kernel_initializer(),
            name="embeddings",
        )(token_id_input)

@NusretOzates
Copy link
Contributor

Just a guess with a very limited log (only saw the error message or Roberta), it could be because of the self._token_embedding. Even when you don't use it, Keras tries to load its weights.

from tensorflow import keras
import tensorflow as tf


class LayerA(keras.layers.Layer):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.layer_b = keras.layers.Dense(1)

    def call(self, inputs):
        return self.layer_b(inputs)


class ModelA(keras.Model):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.layer_a = LayerA()

    def call(self, inputs):
        return self.layer_a(inputs)


a = ModelA()
a(tf.ones((1, 1)))
a.save_weights("test.h5")

after running this code, I added a new layer with self, say self.layer_b = LayerA(). But I will not use it. Even when I use the layer_b inside layer_a it will not work

from tensorflow import keras
import tensorflow as tf


class LayerA(keras.layers.Layer):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.layer_b = keras.layers.Dense(1)

    def call(self, inputs):
        return self.layer_b(inputs)


class ModelA(keras.Model):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.layer_a = LayerA()
        self.layer_b = self.layer_a.layer_b

    def call(self, inputs):
        return self.layer_a(inputs)


a = ModelA()
a(tf.ones((1, 1)))
a.load_weights("test.h5")

image

For now, the only solution that comes into my mind is using get_layer method of the model like this:

image

But with this approach, all token embeddings must have the name 'token_embedding'. I'm definitely not a fan of this approach 😄

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.

Looking great @abheesht17!

Could you update the Bert pretraining example (link) and file an Issue to update our "Getting Started" guide (link)?

@@ -271,6 +271,10 @@ def get_config(self):
)
return config

@property
Copy link
Contributor

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_embedding approach seems cleaner than relying on a specific layer name. We already have several other similar class variables.

Copy link
Collaborator Author

@abheesht17 abheesht17 Jan 22, 2023

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 TokenAndPositionEmbedding layer. Keras considers self._token_embedding as a separate embedding layer, and errors out when we try to load preset checkpoints. Hence, this elaborate-ish solution.

Copy link
Member

@mattdangerw mattdangerw Jan 24, 2023

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 setattr tracking 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-L3229

The solution laid our here seems like a nice way to avoid the setattr tracking entirely. This LGTM.

The alternate I see would be to add a line self._auto_track_sub_layers = False to 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!

Copy link
Member

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_model has no issues. But Roberta for example will have the token embedding as a nested layer. self.some_property = nested_layer_of_model will change our checkpoint structure! This is what @NusretOzates was mentioning above.

Also thanks @NusretOzates for that writeup! Very helpful!

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.

Just the one comment re switching returning the tf.Variable to the keras.layers.Embedding

@@ -271,6 +271,10 @@ def get_config(self):
)
return config

@property
def token_embedding(self):
return self.get_layer("token_embedding").embeddings
Copy link
Member

Choose a reason for hiding this comment

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

I think we should just return the embedding layer here, that is what you are documenting on the base class below! So leave off the .embeddings part, that can be done by calling code as required.

Expose the layer seems the more general case here. It doesn't preclude getting the weights out, but also gives you a callable layer if you want it!

@@ -271,6 +271,10 @@ def get_config(self):
)
return config

@property
Copy link
Member

@mattdangerw mattdangerw Jan 24, 2023

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 setattr tracking 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-L3229

The solution laid our here seems like a nice way to avoid the setattr tracking entirely. This LGTM.

The alternate I see would be to add a line self._auto_track_sub_layers = False to 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!

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.

Thank you!

This little "cleanup PR" turned into quite a complex issue, but I def think it was good to work through!

@mattdangerw mattdangerw merged commit f3ee3ec into keras-team:master Jan 24, 2023
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.

4 participants