Skip to content
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

cast float/int 64 to 32 in alibi-detect-server #3958

Merged
merged 2 commits into from
Feb 21, 2022
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
3 changes: 2 additions & 1 deletion components/alibi-detect-server/adserver/ad_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
from adserver.constants import HEADER_RETURN_INSTANCE_SCORE
from .numpy_encoder import NumpyEncoder
from adserver.protocols.util import read_inputs_as_numpy
from alibi_detect.utils.saving import load_detector, Data
from adserver.base import CEModel, ModelResponse
from adserver.base.storage import download_model
Expand Down Expand Up @@ -60,7 +61,7 @@ def process_event(self, inputs: Union[List, Dict], headers: Dict) -> ModelRespon
logging.info(str(headers))
logging.info("----")
try:
X = np.array(inputs)
X = read_inputs_as_numpy(inputs)
except Exception as e:
raise Exception(
"Failed to initialize NumPy array from inputs: %s, %s" % (e, inputs)
Expand Down
3 changes: 2 additions & 1 deletion components/alibi-detect-server/adserver/od_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import numpy as np
from .numpy_encoder import NumpyEncoder
from adserver.protocols.util import read_inputs_as_numpy
from adserver.base import CEModel, ModelResponse
from alibi_detect.utils.saving import load_detector, Data
from adserver.base.storage import download_model
Expand Down Expand Up @@ -85,7 +86,7 @@ def process_event(self, inputs: Union[List, Dict], headers: Dict) -> Optional[Mo
logging.info(str(headers))
logging.info("----")
try:
X = np.array(inputs)
X = read_inputs_as_numpy(inputs)
except Exception as e:
raise Exception(
"Failed to initialize NumPy array from inputs: %s, %s" % (e, inputs)
Expand Down
16 changes: 15 additions & 1 deletion components/alibi-detect-server/adserver/protocols/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json

from typing import List, Dict, Union
import numpy as np


Expand Down Expand Up @@ -35,3 +35,17 @@ def default(self, obj): # pylint: disable=arguments-differ,method-hidden
elif isinstance(obj, (np.ndarray,)):
return obj.tolist()
return json.JSONEncoder.default(self, obj)


def read_inputs_as_numpy(inputs: Union[List, Dict]) -> np.ndarray:
"""
Read payload inputs as np.ndarray enforcing float32/int32 dtypes.
See: https://github.com/SeldonIO/seldon-core/issues/3940 for details.
"""
x = np.array(inputs)
if x.dtype == np.float64:
return x.astype(np.float32)
elif x.dtype == np.int64:
return x.astype(np.int32)
else:
return x