/
numpy.py
339 lines (274 loc) · 11.9 KB
/
numpy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from __future__ import annotations
import json
import typing as t
import logging
from typing import TYPE_CHECKING
from starlette.requests import Request
from starlette.responses import Response
from .base import IODescriptor
from .json import MIME_TYPE_JSON
from ..types import LazyType
from ..utils import LazyLoader
from ..utils.http import set_cookies
from ...exceptions import BadInput
from ...exceptions import BentoMLException
from ...exceptions import InternalServerError
if TYPE_CHECKING:
import numpy as np
from bentoml.grpc.v1 import service_pb2
from .. import external_typing as ext
from ..context import InferenceApiContext as Context
from ..server.grpc.types import BentoServicerContext
else:
np = LazyLoader("np", globals(), "numpy")
logger = logging.getLogger(__name__)
def _is_matched_shape(
left: t.Optional[t.Tuple[int, ...]],
right: t.Optional[t.Tuple[int, ...]],
) -> bool: # pragma: no cover
if (left is None) or (right is None):
return False
if len(left) != len(right):
return False
for i, j in zip(left, right):
if i == -1 or j == -1:
continue
if i == j:
continue
return False
return True
class NumpyNdarray(IODescriptor["ext.NpNDArray"]):
"""
:code:`NumpyNdarray` defines API specification for the inputs/outputs of a Service, where
either inputs will be converted to or outputs will be converted from type
:code:`numpy.ndarray` as specified in your API function signature.
Sample implementation of a sklearn service:
.. code-block:: python
# sklearn_svc.py
import bentoml
from bentoml.io import NumpyNdarray
import bentoml.sklearn
runner = bentoml.sklearn.get("sklearn_model_clf").to_runner()
svc = bentoml.Service("iris-classifier", runners=[runner])
@svc.api(input=NumpyNdarray(), output=NumpyNdarray())
def predict(input_arr):
res = runner.run(input_arr)
return res
Users then can then serve this service with :code:`bentoml serve`:
.. code-block:: bash
% bentoml serve ./sklearn_svc.py:svc --auto-reload
(Press CTRL+C to quit)
[INFO] Starting BentoML API server in development mode with auto-reload enabled
[INFO] Serving BentoML Service "iris-classifier" defined in "sklearn_svc.py"
[INFO] API Server running on http://0.0.0.0:3000
Users can then send requests to the newly started services with any client:
.. tabs::
.. code-block:: bash
% curl -X POST -H "Content-Type: application/json" --data '[[5,4,3,2]]' http://0.0.0.0:3000/predict
[1]%
Args:
dtype (:code:`numpy.typings.DTypeLike`, `optional`, default to :code:`None`):
Data Type users wish to convert their inputs/outputs to. Refers to `arrays dtypes <https://numpy.org/doc/stable/reference/arrays.dtypes.html>`_ for more information.
enforce_dtype (:code:`bool`, `optional`, default to :code:`False`):
Whether to enforce a certain data type. if :code:`enforce_dtype=True` then :code:`dtype` must be specified.
shape (:code:`Tuple[int, ...]`, `optional`, default to :code:`None`):
Given shape that an array will be converted to. For example:
.. code-block:: python
from bentoml.io import NumpyNdarray
@svc.api(input=NumpyNdarray(shape=(2,2), enforce_shape=False), output=NumpyNdarray())
def predict(input_array: np.ndarray) -> np.ndarray:
# input_array will be reshaped to (2,2)
result = await runner.run(input_array)
When `enforce_shape=True` is provided, BentoML will raise an exception if
the input array received does not match the `shape` provided.
enforce_shape (:code:`bool`, `optional`, default to :code:`False`):
Whether to enforce a certain shape. If `enforce_shape=True` then `shape`
must be specified.
Returns:
:obj:`~bentoml._internal.io_descriptors.IODescriptor`: IO Descriptor that :code:`np.ndarray`.
"""
def __init__(
self,
dtype: t.Optional[t.Union[str, "np.dtype[t.Any]"]] = None,
enforce_dtype: bool = False,
shape: t.Optional[t.Tuple[int, ...]] = None,
enforce_shape: bool = False,
):
if dtype is not None and not isinstance(dtype, np.dtype):
# Convert from primitive type or type string, e.g.:
# np.dtype(float)
# np.dtype("float64")
try:
dtype = np.dtype(dtype)
except TypeError as e:
raise BentoMLException(f'NumpyNdarray: Invalid dtype "{dtype}": {e}')
self._dtype = dtype
self._shape = shape
self._enforce_dtype = enforce_dtype
self._enforce_shape = enforce_shape
def _infer_types(self) -> str: # pragma: no cover
if self._dtype is not None:
name = self._dtype.name
if name.startswith("int") or name.startswith("uint"):
var_type = "integer"
elif name.startswith("float") or name.startswith("complex"):
var_type = "number"
else:
var_type = "object"
else:
var_type = "object"
return var_type
def _items_schema(self) -> t.Dict[str, t.Any]:
if self._shape is not None:
if len(self._shape) > 1:
return {"type": "array", "items": {"type": self._infer_types()}}
return {"type": self._infer_types()}
return {}
def input_type(self) -> LazyType["ext.NpNDArray"]:
return LazyType("numpy", "ndarray")
def openapi_schema_type(self) -> t.Dict[str, t.Any]:
return {"type": "array", "items": self._items_schema()}
def openapi_request_schema(self) -> t.Dict[str, t.Any]:
"""Returns OpenAPI schema for incoming requests"""
return {MIME_TYPE_JSON: {"schema": self.openapi_schema_type()}}
def openapi_responses_schema(self) -> t.Dict[str, t.Any]:
"""Returns OpenAPI schema for outcoming responses"""
return {MIME_TYPE_JSON: {"schema": self.openapi_schema_type()}}
def _verify_ndarray(
self,
obj: "ext.NpNDArray",
exception_cls: t.Type[Exception] = BadInput,
) -> "ext.NpNDArray":
if self._dtype is not None and self._dtype != obj.dtype:
# ‘same_kind’ means only safe casts or casts within a kind, like float64
# to float32, are allowed.
if np.can_cast(obj.dtype, self._dtype, casting="same_kind"):
obj = obj.astype(self._dtype, casting="same_kind") # type: ignore
else:
msg = f'{self.__class__.__name__}: Expecting ndarray of dtype "{self._dtype}", but "{obj.dtype}" was received.'
if self._enforce_dtype:
raise exception_cls(msg)
else:
logger.debug(msg)
if self._shape is not None and not _is_matched_shape(self._shape, obj.shape):
msg = f'{self.__class__.__name__}: Expecting ndarray of shape "{self._shape}", but "{obj.shape}" was received.'
if self._enforce_shape:
raise exception_cls(msg)
try:
obj = obj.reshape(self._shape)
except ValueError as e:
logger.debug(f"{msg} Failed to reshape: {e}.")
return obj
async def from_http_request(self, request: Request) -> ext.NpNDArray:
"""
Process incoming requests and convert incoming
objects to `numpy.ndarray`
Args:
request (`starlette.requests.Requests`):
Incoming Requests
Returns:
a `numpy.ndarray` object. This can then be used
inside users defined logics.
"""
obj = await request.json()
res: "ext.NpNDArray"
try:
res = np.array(obj, dtype=self._dtype)
except ValueError:
res = np.array(obj)
res = self._verify_ndarray(res, BadInput)
return res
async def to_http_response(self, obj: ext.NpNDArray, ctx: Context | None = None):
"""
Process given objects and convert it to HTTP response.
Args:
obj (`np.ndarray`):
`np.ndarray` that will be serialized to JSON
Returns:
HTTP Response of type `starlette.responses.Response`. This can
be accessed via cURL or any external web traffic.
"""
obj = self._verify_ndarray(obj, InternalServerError)
if ctx is not None:
res = Response(
json.dumps(obj.tolist()),
media_type=MIME_TYPE_JSON,
headers=ctx.response.metadata, # type: ignore (bad starlette types)
status_code=ctx.response.status_code,
)
set_cookies(res, ctx.response.cookies)
return res
else:
return Response(json.dumps(obj.tolist()), media_type=MIME_TYPE_JSON)
async def from_grpc_request(
self, request: service_pb2.InferenceRequest, context: BentoServicerContext
) -> ext.NpNDArray:
"""
Process incoming protobuf request and convert it to `numpy.ndarray`
Args:
request: Incoming Requests
Returns:
a `numpy.ndarray` object. This can then be used
inside users defined logics.
"""
from ..utils.grpc import proto_to_dict
# from bentoml.grpc.v1 import struct_pb2
# logger.info([f for f in struct_pb2.ContentsProto.DESCRIPTOR.fields])
print(request.contents)
contents = proto_to_dict(request.contents)
print(contents)
raise RuntimeError
return np.frombuffer(contents)
async def to_grpc_response(
self, obj: ext.NpNDArray, context: BentoServicerContext
) -> service_pb2.InferenceResponse:
"""
Process given objects and convert it to grpc protobuf response.
Args:
obj: `np.ndarray` that will be serialized to protobuf
context: grpc.aio.ServicerContext from grpc.aio.Server
Returns:
`io_descriptor_pb2.Array`:
Protobuf representation of given `np.ndarray`
"""
from bentoml.grpc.v1 import service_pb2
return service_pb2.InferenceResponse()
def generate_protobuf(self):
pass
@classmethod
def from_sample(
cls,
sample_input: "ext.NpNDArray",
enforce_dtype: bool = True,
enforce_shape: bool = True,
) -> "NumpyNdarray":
"""
Create a NumpyNdarray IO Descriptor from given inputs.
Args:
sample_input (:code:`np.ndarray`): Given sample np.ndarray data
enforce_dtype (;code:`bool`, `optional`, default to :code:`True`):
Enforce a certain data type. :code:`dtype` must be specified at function
signature. If you don't want to enforce a specific dtype then change
:code:`enforce_dtype=False`.
enforce_shape (:code:`bool`, `optional`, default to :code:`False`):
Enforce a certain shape. :code:`shape` must be specified at function
signature. If you don't want to enforce a specific shape then change
:code:`enforce_shape=False`.
Returns:
:obj:`~bentoml._internal.io_descriptors.NumpyNdarray`: :code:`NumpyNdarray` IODescriptor from given users inputs.
Example:
.. code-block:: python
import numpy as np
from bentoml.io import NumpyNdarray
arr = [[1,2,3]]
inp = NumpyNdarray.from_sample(arr)
...
@svc.api(input=inp, output=NumpyNdarray())
def predict() -> np.ndarray:...
"""
return cls(
dtype=sample_input.dtype,
shape=sample_input.shape,
enforce_dtype=enforce_dtype,
enforce_shape=enforce_shape,
)