Skip to content

Commit

Permalink
Ellipsis Serialization and tests (#19564)
Browse files Browse the repository at this point in the history
* Serialization and tests

* Serialization and tests

* Serialization and tests
  • Loading branch information
LukeWood authored Apr 20, 2024
1 parent 6e42834 commit ee8b2f8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions keras/src/saving/serialization_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ def serialize_keras_object(obj):
"step": serialize_keras_object(obj.step),
},
}
# Ellipsis is an instance, and ellipsis class is not in global scope.
# checking equality also fails elsewhere in the library, so we have
# to dynamically get the type.
if isinstance(obj, type(Ellipsis)):
return {"class_name": "__ellipsis__", "config": {}}
if isinstance(obj, backend.KerasTensor):
history = getattr(obj, "_keras_history", None)
if history:
Expand Down Expand Up @@ -613,6 +618,8 @@ class ModifiedMeanSquaredError(keras.losses.MeanSquaredError):
return np.array(inner_config["value"], dtype=inner_config["dtype"])
if config["class_name"] == "__bytes__":
return inner_config["value"].encode("utf-8")
if config["class_name"] == "__ellipsis__":
return Ellipsis
if config["class_name"] == "__slice__":
return slice(
deserialize_keras_object(
Expand Down
4 changes: 4 additions & 0 deletions keras/src/saving/serialization_lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def tuples_to_lists_str(x):
reserialized_str = tuples_to_lists_str(reserialized)
self.assertEqual(serialized_str, reserialized_str)

def test_serialize_ellipsis(self):
_, deserialized, _ = self.roundtrip(Ellipsis)
self.assertEqual(..., deserialized)

def test_tensors_and_shapes(self):
x = ops.random.normal((2, 2), dtype="float64")
obj = {"x": x}
Expand Down

0 comments on commit ee8b2f8

Please sign in to comment.