-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_clients.py
283 lines (227 loc) · 10.8 KB
/
grpc_clients.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
# Copyright 2016-2019 The Van Valen Lab at the California Institute of
# Technology (Caltech), with support from the Paul Allen Family Foundation,
# Google, & National Institutes of Health (NIH) under Grant U24CA224309-01.
# All rights reserved.
#
# Licensed under a modified Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.github.com/vanvalenlab/kiosk-redis-consumer/LICENSE
#
# The Work provided may be used for non-commercial academic purposes only.
# For any other use of the Work, including commercial use, please contact:
# vanvalenlab@gmail.com
#
# Neither the name of Caltech nor the names of its contributors may be used
# to endorse or promote products derived from this software without specific
# prior written permission.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""GRPC Clients inspired by
https://github.com/epigramai/tfserving-python-predict-client
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import logging
import timeit
import grpc
from grpc import RpcError
import grpc.beta.implementations
from grpc._cython import cygrpc
import numpy as np
from redis_consumer.pbs.prediction_service_pb2_grpc import PredictionServiceStub
from redis_consumer.pbs.processing_service_pb2_grpc import ProcessingServiceStub
from redis_consumer.pbs.predict_pb2 import PredictRequest
from redis_consumer.pbs.process_pb2 import ProcessRequest
from redis_consumer.pbs.process_pb2 import ChunkedProcessRequest
from redis_consumer.utils import grpc_response_to_dict
from redis_consumer.utils import make_tensor_proto
class GrpcClient(object): # pylint: disable=useless-object-inheritance
"""Abstract class for all gRPC clients.
Arguments:
host: string, the hostname and port of the server (`localhost:8080`)
"""
def __init__(self, host):
self.logger = logging.getLogger(self.__class__.__name__)
self.host = host
def insecure_channel(self):
"""Create an insecure channel with max message length.
Returns:
channel: grpc.insecure channel object
"""
t = timeit.default_timer()
channel = grpc.insecure_channel(
target=self.host,
options=[(cygrpc.ChannelArgKey.max_send_message_length, -1),
(cygrpc.ChannelArgKey.max_receive_message_length, -1)])
self.logger.debug('Establishing insecure channel took: %s',
timeit.default_timer() - t)
return channel
class PredictClient(GrpcClient):
"""gRPC Client for tensorflow-serving API.
Arguments:
host: string, the hostname and port of the server (`localhost:8080`)
model_name: string, name of model served by tensorflow-serving
model_version: integer, version of the named model
"""
def __init__(self, host, model_name, model_version):
super(PredictClient, self).__init__(host)
self.model_name = model_name
self.model_version = model_version
def predict(self, request_data, request_timeout=10):
self.logger.info('Sending request to %s model %s:%s.',
self.host, self.model_name, self.model_version)
channel = self.insecure_channel()
t = timeit.default_timer()
stub = PredictionServiceStub(channel)
self.logger.debug('Created TensorFlowServingServiceStub in %s seconds.',
timeit.default_timer() - t)
t = timeit.default_timer()
request = PredictRequest()
self.logger.debug('Created TensorFlowServingRequest object in %s '
'seconds.', timeit.default_timer() - t)
request.model_spec.name = self.model_name # pylint: disable=E1101
if self.model_version > 0:
# pylint: disable=E1101
request.model_spec.version.value = self.model_version
t = timeit.default_timer()
for d in request_data:
tensor_proto = make_tensor_proto(d['data'], d['in_tensor_dtype'])
# pylint: disable=E1101
request.inputs[d['in_tensor_name']].CopyFrom(tensor_proto)
self.logger.debug('Made tensor protos in %s seconds.',
timeit.default_timer() - t)
try:
t = timeit.default_timer()
predict_response = stub.Predict(request, timeout=request_timeout)
self.logger.debug('gRPC TensorFlowServingRequest finished in %s '
'seconds.', timeit.default_timer() - t)
t = timeit.default_timer()
predict_response_dict = grpc_response_to_dict(predict_response)
self.logger.debug('gRPC TensorFlowServingProtobufConversion took '
'%s seconds.', timeit.default_timer() - t)
keys = [k for k in predict_response_dict]
self.logger.info('Got TensorFlowServingResponse with keys: %s ',
keys)
return predict_response_dict
except RpcError as err:
self.logger.error(err)
self.logger.error('Prediction failed!')
raise err
return {}
class ProcessClient(GrpcClient):
"""gRPC Client for data-processing API.
Arguments:
host: string, the hostname and port of the server (`localhost:8080`)
process_type: string, pre or post processing
function_name: string, name of processing function
"""
def __init__(self, host, process_type, function_name):
super(ProcessClient, self).__init__(host)
self.process_type = process_type
self.function_name = function_name
def process(self, request_data, request_timeout=10):
self.logger.info('Sending request to %s %s-process data with the '
'data-processing API at %s.', self.function_name,
self.process_type, self.host)
# Create gRPC client and request
channel = self.insecure_channel()
t = timeit.default_timer()
stub = ProcessingServiceStub(channel)
self.logger.debug('Created DataProcessingProcessingServiceStub in %s '
'seconds.', timeit.default_timer() - t)
t = timeit.default_timer()
request = ProcessRequest()
self.logger.debug('Created DataProcessingRequest object in %s seconds.',
timeit.default_timer() - t)
# pylint: disable=E1101
request.function_spec.name = self.function_name
request.function_spec.type = self.process_type
# pylint: enable=E1101
t = timeit.default_timer()
for d in request_data:
tensor_proto = make_tensor_proto(d['data'], d['in_tensor_dtype'])
# pylint: disable=E1101
request.inputs[d['in_tensor_name']].CopyFrom(tensor_proto)
self.logger.debug('Made tensor protos in %s seconds.',
timeit.default_timer() - t)
try:
t = timeit.default_timer()
response = stub.Process(request, timeout=request_timeout)
self.logger.debug('gRPC DataProcessingRequest finished in %s '
'seconds.', timeit.default_timer() - t)
t = timeit.default_timer()
response_dict = grpc_response_to_dict(response)
self.logger.debug('gRPC DataProcessingProtobufConversion took %s '
'seconds.', timeit.default_timer() - t)
keys = [k for k in response_dict]
self.logger.debug('Got processing_response with keys: %s', keys)
return response_dict
except RpcError as err:
self.logger.error(err)
self.logger.error('Processing failed!')
raise err
return {}
def stream_process(self, request_data, request_timeout=10):
self.logger.info('Sending request to %s %s-process data with the '
'data-processing API at %s.', self.function_name,
self.process_type, self.host)
# Create gRPC client and request
channel = self.insecure_channel()
t = timeit.default_timer()
stub = ProcessingServiceStub(channel)
self.logger.debug('Created stub in %s seconds.',
timeit.default_timer() - t)
chunk_size = 64 * 1024 # 64 kB is recommended payload size
def request_iterator(image):
dtype = str(image.dtype)
shape = list(image.shape)
bytearr = image.tobytes()
self.logger.info('Streaming %s bytes in %s requests',
len(bytearr), chunk_size % len(bytearr))
for i in range(0, len(bytearr), chunk_size):
request = ChunkedProcessRequest()
# pylint: disable=E1101
request.function_spec.name = self.function_name
request.function_spec.type = self.process_type
request.shape[:] = shape
request.dtype = dtype
request.inputs['data'] = bytearr[i: i + chunk_size]
# pylint: enable=E1101
yield request
try:
t = timeit.default_timer()
req_iter = request_iterator(request_data[0]['data'])
res_iter = stub.StreamProcess(req_iter, timeout=request_timeout)
shape = None
dtype = None
processed_bytes = []
for response in res_iter:
shape = tuple(response.shape)
dtype = str(response.dtype)
processed_bytes.append(response.outputs['data'])
npbytes = b''.join(processed_bytes)
# Got response stream of %s bytes in %s seconds.
self.logger.info('gRPC DataProcessingStreamRequest of %s bytes '
'finished in %s seconds.', len(npbytes),
timeit.default_timer() - t)
t = timeit.default_timer()
processed_image = np.frombuffer(npbytes, dtype=dtype)
results = processed_image.reshape(shape)
self.logger.info('gRPC DataProcessingStreamConversion from %s bytes'
' to a numpy array of shape %s in %s seconds.',
len(npbytes), results.shape,
timeit.default_timer() - t)
return {'results': results}
except RpcError as err:
self.logger.error(err)
self.logger.error('Processing failed!')
raise err
return {}