Skip to content

Replace Backbone with keras.Model in CLIPTextEncoder and T5XXLTextEncoder #1802

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
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions keras_nlp/src/models/stable_diffusion_v3/clip_text_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import keras
from keras import layers
from keras import ops

from keras_nlp.src.layers.modeling.token_and_position_embedding import (
TokenAndPositionEmbedding,
)
from keras_nlp.src.models.backbone import Backbone
from keras_nlp.src.models.stable_diffusion_v3.clip_encoder_block import (
CLIPEncoderBlock,
)


class CLIPTextEncoder(Backbone):
class CLIPTextEncoder(keras.Model):
def __init__(
self,
embedding_dim,
Expand Down Expand Up @@ -108,7 +108,6 @@ def __init__(
super().__init__(
inputs={"encoder_token_ids": encoder_token_ids},
outputs=outputs,
dtype=dtype,
**kwargs,
)

Expand All @@ -123,6 +122,15 @@ def __init__(
self.vocabulary_size = vocabulary_size
self.sequence_length = sequence_length

if dtype is not None:
try:
self.dtype_policy = keras.dtype_policies.get(dtype)
# Before Keras 3.2, there is no `keras.dtype_policies.get`.
except AttributeError:
if isinstance(dtype, keras.DTypePolicy):
dtype = dtype.name
self.dtype_policy = keras.DTypePolicy(dtype)

def get_config(self):
config = super().get_config()
config.update(
Expand Down
13 changes: 10 additions & 3 deletions keras_nlp/src/models/stable_diffusion_v3/t5_xxl_text_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
from keras_nlp.src.layers.modeling.reversible_embedding import (
ReversibleEmbedding,
)
from keras_nlp.src.models.backbone import Backbone
from keras_nlp.src.models.t5.t5_layer_norm import T5LayerNorm
from keras_nlp.src.models.t5.t5_transformer_layer import T5TransformerLayer


class T5XXLTextEncoder(Backbone):
class T5XXLTextEncoder(keras.Model):
def __init__(
self,
vocabulary_size,
Expand Down Expand Up @@ -111,7 +110,6 @@ def __init__(
"encoder_padding_mask": encoder_padding_mask_input,
},
outputs=encoder_output,
dtype=dtype,
**kwargs,
)

Expand All @@ -128,6 +126,15 @@ def __init__(
self.layer_norm_epsilon = layer_norm_epsilon
self.tie_embedding_weights = tie_embedding_weights

if dtype is not None:
try:
self.dtype_policy = keras.dtype_policies.get(dtype)
# Before Keras 3.2, there is no `keras.dtype_policies.get`.
except AttributeError:
if isinstance(dtype, keras.DTypePolicy):
dtype = dtype.name
self.dtype_policy = keras.DTypePolicy(dtype)

def get_config(self):
config = super().get_config()
config.update(
Expand Down
Loading