Skip to content
Open
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: 10 additions & 4 deletions keras/src/backend/common/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,15 +573,21 @@ def initialize_all_variables():
def standardize_dtype(dtype):
if dtype is None:
return config.floatx()

orig_dtype = dtype
dtype = dtypes.PYTHON_DTYPES_MAP.get(dtype, dtype)
if hasattr(dtype, "name"):
dtype = dtype.name
elif hasattr(dtype, "__name__"):
dtype = dtype.__name__
elif hasattr(dtype, "__str__") and (
"torch" in str(dtype) or "jax.numpy" in str(dtype)
):
dtype = str(dtype).split(".")[-1]
else:
# Only call str(dtype) once if needed
dtype_str = None
if hasattr(dtype, "__str__"):
dtype_str = str(dtype)
# Only check and parse if the str contains what we expect
if "torch" in dtype_str or "jax.numpy" in dtype_str:
dtype = dtype_str.split(".")[-1]

if dtype not in dtypes.ALLOWED_DTYPES:
raise ValueError(f"Invalid dtype: {dtype}")
Expand Down
7 changes: 2 additions & 5 deletions keras/src/dtype_policies/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from keras.src import backend
from keras.src.api_export import keras_export
from keras.src.dtype_policies import dtype_policy
from keras.src.dtype_policies.dtype_policy import QUANTIZATION_MODES
from keras.src.dtype_policies.dtype_policy import _get_quantized_dtype_policy_by_str, QUANTIZATION_MODES
from keras.src.dtype_policies.dtype_policy import DTypePolicy
from keras.src.dtype_policies.dtype_policy import FloatDTypePolicy
from keras.src.dtype_policies.dtype_policy import GPTQDTypePolicy
Expand Down Expand Up @@ -85,9 +85,6 @@ def get(identifier):
Returns:
A Keras `DTypePolicy` instance.
"""
from keras.src.dtype_policies.dtype_policy import (
_get_quantized_dtype_policy_by_str,
)

if identifier is None:
return dtype_policy.dtype_policy()
Expand All @@ -102,7 +99,7 @@ def get(identifier):
return DTypePolicy(identifier)
try:
return DTypePolicy(backend.standardize_dtype(identifier))
except:
except Exception:
raise ValueError(
"Cannot interpret `dtype` argument. Expected a string "
f"or an instance of DTypePolicy. Received: dtype={identifier}"
Expand Down