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

Sherif akoush/mlsv 42/add alibi explain runtime #320

Merged
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
1e592ce
initial project skeleton for alibi-explain runtime
sakoush Sep 13, 2021
9c10981
POC commit
sakoush Sep 14, 2021
43b1db6
POC prepare testing
sakoush Sep 15, 2021
e472624
disable testing
sakoush Sep 15, 2021
af1810e
WIP
sakoush Sep 17, 2021
36acbc5
wiring up predict fn
sakoush Sep 20, 2021
56d4552
anchor image with local model
sakoush Sep 22, 2021
d439648
add a hacky test for remote_predict
sakoush Sep 22, 2021
7bd1bb9
allow numpy codec for v2 output
sakoush Sep 23, 2021
85eb8fb
MNIST working?
sakoush Sep 23, 2021
b469a3f
tidy up
sakoush Sep 23, 2021
d90e061
wire up explain parameters (POC)
sakoush Sep 23, 2021
2f48edb
add the ability to load an anchor from disk
sakoush Sep 23, 2021
a9ffe5b
push test artifacts
sakoush Sep 23, 2021
1657a2a
remove one TODO!
sakoush Sep 23, 2021
d82b0ee
pass kwargs instead
sakoush Sep 24, 2021
f519352
defaults are set in alibi
sakoush Sep 24, 2021
44698e9
POC integrated gradients
sakoush Sep 27, 2021
399bb4a
POC integrated gradients with infernece model as uri
sakoush Sep 30, 2021
0f420aa
abstract explainers into bb and wb
sakoush Sep 30, 2021
5ff6390
minor refactor to common utils
sakoush Sep 30, 2021
bfc4d2a
add integrated gradients via factory
sakoush Sep 30, 2021
a3eb52d
add a test to make sure we can import the strings that define the cla…
sakoush Sep 30, 2021
6cf0fe1
add support for anchor text
sakoush Oct 1, 2021
7b535db
tweaks to get things working
sakoush Oct 1, 2021
8c618a7
tidy up
sakoush Oct 4, 2021
b64e3ca
fix test to run automatically
sakoush Oct 5, 2021
6501fcc
tidy up
sakoush Oct 5, 2021
b3e8aa8
recover correct settings file
sakoush Oct 5, 2021
ebb2b1a
add some comments
sakoush Oct 6, 2021
5d68820
allow quantize from 64->32
sakoush Oct 6, 2021
b5f8550
quantize on mlserver side
sakoush Oct 6, 2021
99c065e
remove quantize on mlserver side
sakoush Oct 7, 2021
0d1c9b1
use tf mnist model in test
sakoush Oct 7, 2021
fe197c1
add a test for infer_impl
sakoush Oct 7, 2021
c324c20
test we get the same results with anchors
sakoush Oct 7, 2021
9f29391
some refactor
sakoush Oct 7, 2021
fb3701d
add a test for integrated gradients
sakoush Oct 7, 2021
0fe7fa9
refactor
sakoush Oct 7, 2021
5874335
add a test for the wrapper
sakoush Oct 8, 2021
4b4441a
add retry with tenacity for remote predict
sakoush Oct 8, 2021
77bcd5e
refactor
sakoush Oct 8, 2021
7e08766
refactor #2
sakoush Oct 8, 2021
6daf1aa
use StringCodec
sakoush Oct 11, 2021
c008320
remove tenacity and add a todo to consider retry
sakoush Oct 11, 2021
09547b3
fmt
sakoush Oct 11, 2021
d801eb0
lint
sakoush Oct 11, 2021
e16f5c5
mypy set#1
sakoush Oct 11, 2021
de42b34
mypy set#2
sakoush Oct 11, 2021
642c0e4
mypy set#3
sakoush Oct 11, 2021
cea0446
refactor
sakoush Oct 11, 2021
86e72dd
refactor #2
sakoush Oct 11, 2021
bc48764
remove dep on mlflow in alibi runtime tests
sakoush Oct 11, 2021
37332e3
refactor
sakoush Oct 11, 2021
b6d27af
pin fastapi to 0.68.2 as it will fail tests
sakoush Oct 12, 2021
2f98c9c
lets be safe and pin fastapi version for now.
sakoush Oct 12, 2021
f03a576
fix tests
sakoush Oct 12, 2021
82bbc04
lint
sakoush Oct 12, 2021
7055ea0
Add the support of List of string for black box explainers (AnchorText)
sakoush Oct 12, 2021
0384120
fix lint issue
sakoush Oct 12, 2021
1af15b6
fixes for anchor text working
sakoush Oct 13, 2021
1cc91c9
add a test for numpy codec
sakoush Oct 14, 2021
e82e1cf
tidy up numpy codec
sakoush Oct 14, 2021
588f178
add tests for str codec
sakoush Oct 14, 2021
e396277
refactor and add a test for black box v2 inference request encoding
sakoush Oct 14, 2021
fc1c255
add more tests
sakoush Oct 14, 2021
402df44
use assert_array_almost_equal
sakoush Oct 14, 2021
acfb093
refactor to fix tests
sakoush Oct 14, 2021
012f620
fmt
sakoush Oct 14, 2021
5d0b7a4
add a test for explain parameters pass through
sakoush Oct 14, 2021
7eba4a0
check also data is wired through properly
sakoush Oct 14, 2021
ed5f2a3
add a test for explain parameters pass through
sakoush Oct 15, 2021
602cd1f
set default alibi detect settings
sakoush Oct 15, 2021
6925b0a
use mlserver errors
sakoush Oct 15, 2021
81a296c
make alibi dependency reference mode readable
sakoush Oct 15, 2021
3720a26
add exceptions for black box explainers
sakoush Oct 15, 2021
e525b56
and for whitebox
sakoush Oct 15, 2021
b992a30
share common code for loading explainers from disk
sakoush Oct 15, 2021
66c0080
revert param rename
sakoush Oct 15, 2021
5b433d9
check inference uri in init
sakoush Oct 15, 2021
9afd2aa
tidy up exception handling
sakoush Oct 15, 2021
6d272be
PR comments
sakoush Oct 15, 2021
03ad825
add anchor tabular
sakoush Oct 15, 2021
8f449ff
PR comments
sakoush Oct 15, 2021
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
14 changes: 14 additions & 0 deletions docs/examples/alibi-explain/model-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "anchor-image-explain-model",
"implementation": "mlserver_alibi_explain.AlibiExplainRuntime",
"parallel_workers": 0,
"parameters": {
"uri": "./data/mnist_anchor_image",
"version": "v0.1.0",
"extra": {
"explainer_type": "anchor_image",
"infer_uri": "http://localhost:42315/v2/models/test-pytorch-mnist/infer"
}
}
}

3 changes: 3 additions & 0 deletions docs/examples/alibi-explain/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"debug": "true"
}
44 changes: 31 additions & 13 deletions mlserver/codecs/numpy.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import numpy as np

from typing import Any
from typing import Any, Union

from ..types import RequestInput, ResponseOutput
from ..types import RequestInput, ResponseOutput, Parameters

from .base import InputCodec, register_input_codec, register_request_codec
from .utils import FirstInputRequestCodec
Expand Down Expand Up @@ -30,13 +30,13 @@
_NumpyToDatatype["S"] = "BYTES"


def _to_dtype(request_input: RequestInput) -> "np.dtype":
dtype = _DatatypeToNumpy[request_input.datatype]
def _to_dtype(v2_data: Union[RequestInput, ResponseOutput]) -> "np.dtype":
dtype = _DatatypeToNumpy[v2_data.datatype]

if request_input.datatype == "BYTES":
if v2_data.datatype == "BYTES":
# bytes have variable size, so need to specify as part of type
# TODO: Make elem size variable (and not just the last dimension)
elem_size = request_input.shape[-1]
elem_size = v2_data.shape[-1]
return np.dtype((dtype, elem_size))

return np.dtype(dtype)
Expand All @@ -54,11 +54,11 @@ def to_datatype(dtype: np.dtype) -> str:
return datatype


def _to_ndarray(request_input: RequestInput) -> np.ndarray:
data = getattr(request_input.data, "__root__", request_input.data)
dtype = _to_dtype(request_input)
def _to_ndarray(v2_data: Union[RequestInput, ResponseOutput]) -> np.ndarray:
data = getattr(v2_data.data, "__root__", v2_data.data)
dtype = _to_dtype(v2_data)

if request_input.datatype == "BYTES":
if v2_data.datatype == "BYTES":
return np.frombuffer(data, dtype)

return np.array(data, dtype)
Expand Down Expand Up @@ -93,11 +93,29 @@ def encode(cls, name: str, payload: np.ndarray) -> ResponseOutput:
)

@classmethod
def decode(cls, request_input: RequestInput) -> np.ndarray:
model_data = _to_ndarray(request_input)
def decode(cls, v2_data: RequestInput) -> np.ndarray:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is there any reason for the name change? I'm wondering whether request_input may be more explicit than v2_data?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cant remember of any reason why I have renamed this variable, will revert.

model_data = _to_ndarray(v2_data)

# TODO: Check if reshape not valid
return model_data.reshape(request_input.shape)
return model_data.reshape(v2_data.shape)

@classmethod
def decode_response_output(cls, v2_data: ResponseOutput) -> np.ndarray:
# TODO: merge this logic with `decode`
return cls.decode(v2_data) # type: ignore

@classmethod
def encode_request_input(cls, name: str, payload: np.ndarray) -> RequestInput:
# TODO: merge this logic with `encode`
data = cls.encode(name=name, payload=payload)

return RequestInput(
name=data.name,
datatype=data.datatype,
shape=data.shape,
data=data.data,
parameters=Parameters(content_type=cls.ContentType),
)


@register_request_codec
Expand Down
14 changes: 13 additions & 1 deletion mlserver/codecs/string.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from ..types import RequestInput, ResponseOutput
from ..types import RequestInput, ResponseOutput, Parameters

from .utils import FirstInputRequestCodec
from .base import InputCodec, register_input_codec, register_request_codec
Expand Down Expand Up @@ -51,6 +51,18 @@ def decode(cls, request_input: RequestInput) -> List[str]:
unpacked = map(_decode_str, unpack(packed, shape))
return list(unpacked)

@classmethod
def encode_request_input(cls, name: str, payload: List[str]) -> RequestInput:
# TODO: merge this logic with `encode`
data = cls.encode(name=name, payload=payload)
return RequestInput(
name=data.name,
datatype=data.datatype,
shape=data.shape,
data=data.data.__root__.decode("ascii"), # to allow json serialisation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this break serialisation into protobufs? Although we should probably leave that point outside of this PR TBH, as it's not clear how to make something compatible for both.

Besides that, should we decode it as utf8 instead of ascii?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not removed and we will just pass the List[str]
As discussed this will only work with json / REST

parameters=Parameters(content_type=cls.ContentType),
)


@register_request_codec
class StringRequestCodec(FirstInputRequestCodec):
Expand Down
2 changes: 1 addition & 1 deletion mlserver/handlers/dataplane.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async def infer(
model = await self._model_registry.get_model(name, version)

# Run middlewares
inference_middlewares(payload, model._settings)
inference_middlewares(payload, model.settings)

# TODO: Make await optional for sync methods
prediction = await model.predict(payload)
Expand Down
18 changes: 10 additions & 8 deletions mlserver/model.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
from typing import Any, Dict, Optional, List

from .codecs import (
decode_request_input,
decode_inference_request,
InputCodec,
has_decoded,
get_decoded,
)
from .settings import ModelSettings
from .types import (
InferenceRequest,
InferenceResponse,
RequestInput,
Parameters,
MetadataModelResponse,
MetadataTensor,
)
from .settings import ModelSettings
from .codecs import (
decode_request_input,
decode_inference_request,
InputCodec,
has_decoded,
get_decoded,
from .types import (
Parameters,
)


Expand Down
2 changes: 1 addition & 1 deletion runtimes/alibi-detect/mlserver_alibi_detect/cd/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def load(self) -> bool:

parameters = self.alibi_detect_settings.init_parameters

self._model = drift_detector(**detector_data, **parameters)
self._model = drift_detector(**detector_data, **parameters) # type: ignore

self.ready = True
return self.ready
7 changes: 4 additions & 3 deletions runtimes/alibi-detect/mlserver_alibi_detect/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Config:
env_prefix = ENV_PREFIX_ALIBI_DETECT_SETTINGS

init_detector: bool = False
detector_type: PyObject = ""
detector_type: PyObject = "" # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we set it to None, would we also need the type: ignore comment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because we need also to set it as Optional. this has been used elsewhere as well.

protocol: Optional[str] = "seldon.http"
init_parameters: Optional[dict] = {}
predict_parameters: Optional[dict] = {}
Expand All @@ -47,7 +47,8 @@ class AlibiDetectRuntime(MLModel):

def __init__(self, settings: ModelSettings):

self.alibi_detect_settings = AlibiDetectSettings(**settings.parameters.extra)
extra = settings.parameters.extra # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that type: ignore required? Is it because parameters may be None? If that's the case, we could check here whether it's set, and if not just default to an empty AlibiDetectSettings() object (which could still fetch values from the environment).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point.

self.alibi_detect_settings = AlibiDetectSettings(**extra) # type: ignore
super().__init__(settings)

@custom_handler(rest_path="/")
Expand Down Expand Up @@ -93,7 +94,7 @@ async def predict(self, payload: types.InferenceRequest) -> types.InferenceRespo

async def predict_fn(self, input_data: Any) -> dict:
parameters = self.alibi_detect_settings.predict_parameters
return self._model.predict(input_data, **parameters)
return self._model.predict(input_data, **parameters) # type: ignore

def _check_request(self, payload: types.InferenceRequest) -> types.InferenceRequest:
if len(payload.inputs) != 1:
Expand Down
Loading