-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3248 from cliveseldon/3247_v2_protocol_explain
Add kfserving protocol to alibi explainer server
- Loading branch information
Showing
13 changed files
with
1,553 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import numpy as np | ||
from typing import Dict, List, Union | ||
|
||
_v2tymap: Dict[str, np.dtype] = { | ||
"BOOL": np.dtype("bool"), | ||
"UINT8": np.dtype("uint8"), | ||
"UINT16": np.dtype("uint16"), | ||
"UINT32": np.dtype("uint32"), | ||
"UINT64": np.dtype("uint64"), | ||
"INT8": np.dtype("int8"), | ||
"INT16": np.dtype("int16"), | ||
"INT32": np.dtype("int32"), | ||
"INT64": np.dtype("int64"), | ||
"FP16": np.dtype("float32"), | ||
"FP32": np.dtype("float32"), | ||
"FP64": np.dtype("float64"), | ||
} | ||
|
||
_nptymap = dict([(value, key) for key, value in _v2tymap.items()]) | ||
_nptymap[np.dtype("float32")] = "FP32" # Ensure correct mapping for ambiguous type | ||
|
||
def _create_np_from_v2(data: list, ty: str, shape: list) -> np.array: | ||
npty = _v2tymap[ty] | ||
arr = np.array(data, dtype=npty) | ||
arr.shape = tuple(shape) | ||
return arr | ||
|
||
def _create_v2_from_np(arr: np.ndarray, name: str, ty: str) -> Dict: | ||
if arr.dtype in _nptymap: | ||
return { | ||
"name": name, | ||
"datatype": ty, | ||
"data": arr.flatten().tolist(), | ||
"shape": list(arr.shape), | ||
} | ||
else: | ||
raise ValueError(f"Unknown numpy type {arr.dtype}") | ||
|
||
# Only handle single input/output payloads | ||
class KFServingV2RequestHandler(): | ||
|
||
def extract_request(self, request: Dict) -> List: | ||
inputs = request["inputs"][0] | ||
data_type = inputs["datatype"] | ||
shape = inputs["shape"] | ||
data = inputs["data"] | ||
arr = _create_np_from_v2(data, data_type, shape) | ||
return arr.tolist() | ||
|
||
def extract_response(self, request: Dict) -> List: | ||
inputs = request["outputs"][0] | ||
data_type = inputs["datatype"] | ||
shape = inputs["shape"] | ||
data = inputs["data"] | ||
arr = _create_np_from_v2(data, data_type, shape) | ||
return arr | ||
|
||
def create_request(self, arr: np.ndarray, name: str, ty: str) -> Dict: | ||
req = {} | ||
data = _create_v2_from_np(arr, name, ty) | ||
req["inputs"] = [data] | ||
return req | ||
|
||
def extract_name(self, request: Dict) -> str: | ||
return request["inputs"][0]["name"] | ||
|
||
def extract_type(self, request: Dict) -> str: | ||
return request["inputs"][0]["datatype"] |
26 changes: 26 additions & 0 deletions
26
components/alibi-explain-server/tests/data/truck-v2-response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"model_name": "cifar10", | ||
"model_version": "1", | ||
"outputs": [ | ||
{ | ||
"data": [ | ||
0.0000012644828757402138, | ||
4.881440140991344e-9, | ||
1.5153264198985994e-9, | ||
8.490542491301767e-9, | ||
5.513066114737342e-10, | ||
1.1617126149943147e-9, | ||
5.772862743391727e-10, | ||
2.8839471610808687e-7, | ||
0.000614893389865756, | ||
0.9993835687637329 | ||
], | ||
"datatype": "FP32", | ||
"name": "fc10", | ||
"shape": [ | ||
1, | ||
10 | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.