Skip to content

Commit

Permalink
[Serve] Support serializing numpy scaler (#24512)
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-mo authored May 5, 2022
1 parent 70d3bfc commit a424e91
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
23 changes: 9 additions & 14 deletions python/ray/serve/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,15 @@ def test_numpy_encoding():
floats = np.array(data).astype(np.float32)
ints = floats.astype(np.int32)
uints = floats.astype(np.uint32)

assert (
json.loads(json.dumps(jsonable_encoder(floats, custom_encoder=serve_encoders)))
== data
)
assert (
json.loads(json.dumps(jsonable_encoder(ints, custom_encoder=serve_encoders)))
== data
)
assert (
json.loads(json.dumps(jsonable_encoder(uints, custom_encoder=serve_encoders)))
== data
)

list_of_uints = [np.int64(1), np.int64(2)]

for np_data in [floats, ints, uints, list_of_uints]:
assert (
json.loads(
json.dumps(jsonable_encoder(np_data, custom_encoder=serve_encoders))
)
== data
)
nested = {"a": np.array([1, 2])}
assert json.loads(
json.dumps(jsonable_encoder(nested, custom_encoder=serve_encoders))
Expand Down
6 changes: 6 additions & 0 deletions python/ray/serve/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def encode_np_array(obj):
obj = obj.astype(int)
return obj.tolist()

@staticmethod
def encode_np_scaler(obj):
assert isinstance(obj, np.generic)
return obj.item()

@staticmethod
def encode_exception(obj):
assert isinstance(obj, Exception)
Expand All @@ -66,6 +71,7 @@ def encode_exception(obj):

serve_encoders = {
np.ndarray: _ServeCustomEncoders.encode_np_array,
np.generic: _ServeCustomEncoders.encode_np_scaler,
Exception: _ServeCustomEncoders.encode_exception,
}

Expand Down

0 comments on commit a424e91

Please sign in to comment.