-
Notifications
You must be signed in to change notification settings - Fork 879
/
Copy pathservice.py
197 lines (168 loc) · 6.04 KB
/
service.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
"""
CustomService class definitions
"""
import logging
import os
import time
from builtins import str
import ts
from ts.context import Context, RequestProcessor
from ts.protocol.otf_torch_message_handler import create_predict_response
from ts.utils.util import PredictionException, get_yaml_config
PREDICTION_METRIC = "PredictionTime"
logger = logging.getLogger(__name__)
class Service(object):
"""
Wrapper for custom entry_point
"""
def __init__(
self,
model_name,
model_dir,
manifest,
entry_point,
gpu,
batch_size,
limit_max_image_pixels=True,
metrics_cache=None,
):
model_yaml_config = {}
if manifest is not None and "model" in manifest:
model = manifest["model"]
if "configFile" in model:
model_yaml_config_file = model["configFile"]
model_yaml_config = get_yaml_config(
os.path.join(model_dir, model_yaml_config_file)
)
self._context = Context(
model_name,
model_dir,
manifest,
batch_size,
gpu,
ts.__version__,
limit_max_image_pixels,
metrics_cache,
model_yaml_config,
)
self._entry_point = entry_point
@property
def context(self):
return self._context
@staticmethod
def retrieve_data_for_inference(batch):
"""
REQUEST_INPUT = {
"requestId" : "111-222-3333",
"parameters" : [ PARAMETER ]
}
PARAMETER = {
"name" : parameter name
"contentType": "http-content-types",
"value": "val1"
}
:param batch:
:return:
"""
if batch is None:
raise ValueError("Received invalid inputs")
req_to_id_map = {}
headers = []
input_batch = []
for batch_idx, request_batch in enumerate(batch):
req_id = request_batch.get("requestId").decode("utf-8")
parameters = request_batch["parameters"]
model_in_headers = {}
model_in = {}
# Parameter level headers are updated here. multipart/form-data can have multiple headers.
for parameter in parameters:
model_in.update({parameter["name"]: parameter["value"]})
model_in_headers.update(
{parameter["name"]: {"content-type": parameter["contentType"]}}
)
# Request level headers are populated here
if request_batch.get("headers") is not None:
for h in request_batch.get("headers"):
model_in_headers.update(
{h["name"].decode("utf-8"): h["value"].decode("utf-8")}
)
headers.append(RequestProcessor(model_in_headers))
input_batch.append(model_in)
req_to_id_map[batch_idx] = req_id
return headers, input_batch, req_to_id_map
def set_cl_socket(self, cl_socket):
self.cl_socket = cl_socket
def predict(self, batch):
"""
PREDICT COMMAND = {
"command": "predict",
"batch": [ REQUEST_INPUT ]
}
:param batch: list of request
:return:
"""
headers, input_batch, req_id_map = Service.retrieve_data_for_inference(batch)
self.context.request_ids = req_id_map
self.context.request_processor = headers
metrics = self.context.metrics
metrics.request_ids = req_id_map
self.context.cl_socket = self.cl_socket
start_time = time.time()
# noinspection PyBroadException
try:
ret = self._entry_point(input_batch, self.context)
except MemoryError:
logger.error("System out of memory", exc_info=True)
return create_predict_response(None, req_id_map, "Out of resources", 507)
except PredictionException as e:
logger.error("Prediction error", exc_info=True)
return create_predict_response(None, req_id_map, e.message, e.error_code)
except Exception as ex: # pylint: disable=broad-except
if "CUDA" in str(ex):
# Handles Case A: CUDA error: CUBLAS_STATUS_NOT_INITIALIZED (Close to OOM) &
# Case B: CUDA out of memory (OOM)
logger.error("CUDA out of memory", exc_info=True)
return create_predict_response(
None, req_id_map, "Out of resources", 507
)
else:
logger.warning("Invoking custom service failed.", exc_info=True)
return create_predict_response(
None, req_id_map, "Prediction failed", 503
)
if not isinstance(ret, list):
logger.warning(
"model: %s, Invalid return type: %s.",
self.context.model_name,
type(ret),
)
return create_predict_response(
None, req_id_map, "Invalid model predict output", 503
)
if len(ret) != len(input_batch):
logger.warning(
"model: %s, number of batch response mismatched, expect: %d, got: %d.",
self.context.model_name,
len(input_batch),
len(ret),
)
return create_predict_response(
None, req_id_map, "number of batch response mismatched", 503
)
duration = round((time.time() - start_time) * 1000, 2)
metrics.add_time(PREDICTION_METRIC, duration)
return create_predict_response(
ret, req_id_map, "Prediction success", 200, context=self.context
)
def emit_metrics(metrics):
"""
Emit the metrics in the provided Dictionary
Parameters
----------
metrics: Dictionary
A dictionary of all metrics, when key is metric_name
value is a metric object
"""
if metrics:
for met in metrics:
logger.info("[METRICS]%s", str(met))