Skip to content
Merged
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
21 changes: 12 additions & 9 deletions keras_nlp/src/models/backbone.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ def __init__(self, *args, dtype=None, **kwargs):
id(layer) for layer in self._flatten_layers()
)
self._initialized = True
# Before Keras 3.2, there is no `keras.dtype_policies.get`.
if hasattr(keras.dtype_policies, "get"):
self.dtype_policy = keras.dtype_policies.get(dtype)
else:
if isinstance(dtype, keras.dtype_policies.DTypePolicy):
dtype = dtype.name
dtype = dtype or keras.config.dtype_policy().name
self.dtype_policy = keras.dtype_policies.DTypePolicy(dtype)
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 __setattr__(self, name, value):
# Work around setattr issues for Keras 2 and Keras 3 torch backend.
Expand Down Expand Up @@ -121,7 +121,7 @@ def get_config(self):
}

# Add quantization support by utilizing `DTypePolicyMap`
if hasattr(keras.dtype_policies, "DTypePolicyMap"):
try:
if isinstance(
self.dtype_policy, keras.dtype_policies.DTypePolicyMap
):
Expand All @@ -133,6 +133,9 @@ def get_config(self):
policy_map[layer.path] = layer.dtype_policy
if len(policy_map) > 0:
config.update({"dtype": policy_map})
# Before Keras 3.2, there is no `keras.dtype_policies.get`.
except AttributeError:
pass
Copy link
Member

Choose a reason for hiding this comment

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

I was wondering why we don't handle this case like the first one using keras.DTypePolicy(dtype).

Copy link
Member

Choose a reason for hiding this comment

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

nvm, I just noticed they are completely different cases!

return config

@classmethod
Expand Down