From 970e63383f7d02b7355435d7384e81d2c7c98ead Mon Sep 17 00:00:00 2001 From: Rafal Skolasinski Date: Mon, 21 Feb 2022 11:24:46 +0000 Subject: [PATCH 1/2] cast float/int 64 to 32 in alibi-detect-server --- components/alibi-detect-server/adserver/ad_model.py | 3 ++- components/alibi-detect-server/adserver/od_model.py | 3 ++- .../alibi-detect-server/adserver/protocols/util.py | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/components/alibi-detect-server/adserver/ad_model.py b/components/alibi-detect-server/adserver/ad_model.py index 0a92370f65..53d3cc8019 100644 --- a/components/alibi-detect-server/adserver/ad_model.py +++ b/components/alibi-detect-server/adserver/ad_model.py @@ -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 @@ -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) diff --git a/components/alibi-detect-server/adserver/od_model.py b/components/alibi-detect-server/adserver/od_model.py index 56e66262c0..3c1b09f6f5 100644 --- a/components/alibi-detect-server/adserver/od_model.py +++ b/components/alibi-detect-server/adserver/od_model.py @@ -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 @@ -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) diff --git a/components/alibi-detect-server/adserver/protocols/util.py b/components/alibi-detect-server/adserver/protocols/util.py index 22c89923b1..66a37119d0 100644 --- a/components/alibi-detect-server/adserver/protocols/util.py +++ b/components/alibi-detect-server/adserver/protocols/util.py @@ -35,3 +35,13 @@ 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): + 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 From 4782a7a0b179aad0724bc74813439781ebee14e4 Mon Sep 17 00:00:00 2001 From: Rafal Skolasinski Date: Mon, 21 Feb 2022 12:32:12 +0000 Subject: [PATCH 2/2] add type hinting + docstring --- components/alibi-detect-server/adserver/protocols/util.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/components/alibi-detect-server/adserver/protocols/util.py b/components/alibi-detect-server/adserver/protocols/util.py index 66a37119d0..4e5fa5421e 100644 --- a/components/alibi-detect-server/adserver/protocols/util.py +++ b/components/alibi-detect-server/adserver/protocols/util.py @@ -1,5 +1,5 @@ import json - +from typing import List, Dict, Union import numpy as np @@ -37,7 +37,11 @@ def default(self, obj): # pylint: disable=arguments-differ,method-hidden return json.JSONEncoder.default(self, obj) -def read_inputs_as_numpy(inputs): +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)