-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathtriton_python_backend_utils.py
503 lines (455 loc) · 18.1 KB
/
triton_python_backend_utils.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# Copyright 2020-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import numpy as np
import struct
import json
TRITON_STRING_TO_NUMPY = {
'TYPE_BOOL': bool,
'TYPE_UINT8': np.uint8,
'TYPE_UINT16': np.uint16,
'TYPE_UINT32': np.uint32,
'TYPE_UINT64': np.uint64,
'TYPE_INT8': np.int8,
'TYPE_INT16': np.int16,
'TYPE_INT32': np.int32,
'TYPE_INT64': np.int64,
'TYPE_FP16': np.float16,
'TYPE_FP32': np.float32,
'TYPE_FP64': np.float64,
'TYPE_STRING': np.object_
}
def serialize_byte_tensor(input_tensor):
"""
Serializes a bytes tensor into a flat numpy array of length prepended
bytes. The numpy array should use dtype of np.object_. For np.bytes_,
numpy will remove trailing zeros at the end of byte sequence and because
of this it should be avoided.
Parameters
----------
input_tensor : np.array
The bytes tensor to serialize.
Returns
-------
serialized_bytes_tensor : np.array
The 1-D numpy array of type uint8 containing the serialized bytes in 'C' order.
Raises
------
InferenceServerException
If unable to serialize the given tensor.
"""
if input_tensor.size == 0:
return ()
# If the input is a tensor of string/bytes objects, then must flatten those
# into a 1-dimensional array containing the 4-byte byte size followed by the
# actual element bytes. All elements are concatenated together in "C" order.
if (input_tensor.dtype == np.object_) or (input_tensor.dtype.type
== np.bytes_):
flattened_ls = []
for obj in np.nditer(input_tensor, flags=["refs_ok"], order='C'):
# If directly passing bytes to BYTES type,
# don't convert it to str as Python will encode the
# bytes which may distort the meaning
if input_tensor.dtype == np.object_:
if type(obj.item()) == bytes:
s = obj.item()
else:
s = str(obj.item()).encode('utf-8')
else:
s = obj.item()
flattened_ls.append(struct.pack("I", len(s)))
flattened_ls.append(s)
flattened = b''.join(flattened_ls)
return flattened
return None
def deserialize_bytes_tensor(encoded_tensor):
"""
Deserializes an encoded bytes tensor into an
numpy array of dtype of python objects
Parameters
----------
encoded_tensor : bytes
The encoded bytes tensor where each element
has its length in first 4 bytes followed by
the content
Returns
-------
string_tensor : np.array
The 1-D numpy array of type object containing the
deserialized bytes in 'C' order.
"""
strs = list()
offset = 0
val_buf = encoded_tensor
while offset < len(val_buf):
l = struct.unpack_from("I", val_buf, offset)[0]
offset += 4
sb = struct.unpack_from("{}s".format(l), val_buf, offset)[0]
offset += l
strs.append(sb)
return (np.array(strs, dtype=np.object_))
def get_input_tensor_by_name(inference_request, name):
"""Find an input Tensor in the inference_request that has the given
name
Parameters
----------
inference_request : InferenceRequest
InferenceRequest object
name : str
name of the input Tensor object
Returns
-------
Tensor
The input Tensor with the specified name, or None if no
input Tensor with this name exists
"""
input_tensors = inference_request.inputs()
for input_tensor in input_tensors:
if input_tensor.name() == name:
return input_tensor
return None
def get_output_tensor_by_name(inference_response, name):
"""Find an output Tensor in the inference_response that has the given
name
Parameters
----------
inference_response : InferenceResponse
InferenceResponse object
name : str
name of the output Tensor object
Returns
-------
Tensor
The output Tensor with the specified name, or None if no
output Tensor with this name exists
"""
output_tensors = inference_response.output_tensors()
for output_tensor in output_tensors:
if output_tensor.name() == name:
return output_tensor
return None
def get_input_config_by_name(model_config, name):
"""Get input properties corresponding to the input
with given `name`
Parameters
----------
model_config : dict
dictionary object containing the model configuration
name : str
name of the input object
Returns
-------
dict
A dictionary containing all the properties for a given input
name, or None if no input with this name exists
"""
if 'input' in model_config:
inputs = model_config['input']
for input_properties in inputs:
if input_properties['name'] == name:
return input_properties
return None
def get_output_config_by_name(model_config, name):
"""Get output properties corresponding to the output
with given `name`
Parameters
----------
model_config : dict
dictionary object containing the model configuration
name : str
name of the output object
Returns
-------
dict
A dictionary containing all the properties for a given output
name, or None if no output with this name exists
"""
if 'output' in model_config:
outputs = model_config['output']
for output_properties in outputs:
if output_properties['name'] == name:
return output_properties
return None
def using_decoupled_model_transaction_policy(model_config):
"""Whether or not the model is configured with decoupled
transaction policy.
Parameters
----------
model_config : dict
dictionary object containing the model configuration
Returns
-------
bool
True if the model is configured with decoupled transaction
policy.
"""
if 'model_transaction_policy' in model_config:
return model_config['model_transaction_policy']['decoupled']
return False
def triton_to_numpy_type(data_type):
if data_type == 1:
return np.bool_
elif data_type == 2:
return np.uint8
elif data_type == 3:
return np.uint16
elif data_type == 4:
return np.uint32
elif data_type == 5:
return np.uint64
elif data_type == 6:
return np.int8
elif data_type == 7:
return np.int16
elif data_type == 8:
return np.int32
elif data_type == 9:
return np.int64
elif data_type == 10:
return np.float16
elif data_type == 11:
return np.float32
elif data_type == 12:
return np.float64
elif data_type == 13:
return np.object_
def numpy_to_triton_type(data_type):
if data_type == np.bool_:
return 1
elif data_type == np.uint8:
return 2
elif data_type == np.uint16:
return 3
elif data_type == np.uint32:
return 4
elif data_type == np.uint64:
return 5
elif data_type == np.int8:
return 6
elif data_type == np.int16:
return 7
elif data_type == np.int32:
return 8
elif data_type == np.int64:
return 9
elif data_type == np.float16:
return 10
elif data_type == np.float32:
return 11
elif data_type == np.float64:
return 12
elif data_type == np.object_ or data_type == np.bytes_:
return 13
def triton_string_to_numpy(triton_type_string):
return TRITON_STRING_TO_NUMPY[triton_type_string]
class ModelConfig:
"""An object of ModelConfig class is used to describe
the model configuration for autocomplete.
Parameters
----------
model_config : ModelConfig Object
Object containing the model configuration. Only the max_batch_size, inputs
and outputs properties can be modified for auto-complete model configuration.
"""
def __init__(self, model_config):
self._model_config = json.loads(model_config)
def as_dict(self):
"""Provide the read-only access to the model configuration
Returns
-------
dict
dictionary type of the model configuration contained in
the ModelConfig object
"""
return self._model_config
def set_max_batch_size(self, max_batch_size):
"""Set the max batch size for the model.
Parameters
----------
max_batch_size : int
The max_batch_size to be set.
Raises
------
ValueError
If configuration has specified max_batch_size non-zero value which
is larger than the max_batch_size to be set for the model.
"""
if self._model_config["max_batch_size"] > max_batch_size:
raise ValueError(
"configuration specified max_batch_size " +
str(self._model_config["max_batch_size"]) +
", but in auto-complete-config function for model '" +
self._model_config["name"] + "' specified max_batch_size " +
str(max_batch_size))
else:
self._model_config["max_batch_size"] = max_batch_size
def set_dynamic_batching(self):
"""Set dynamic_batching as the scheduler for the model if no scheduler
is set. If dynamic_batching is set in the model configuration, then no
action is taken and return success.
Raises
------
ValueError
If the 'sequence_batching' or 'ensemble_scheduling' scheduler is
set for this model configuration.
"""
found_scheduler = None
if "sequence_batching" in self._model_config:
found_scheduler = "sequence_batching"
elif "ensemble_scheduling" in self._model_config:
found_scheduler = "ensemble_scheduling"
if found_scheduler != None:
raise ValueError(
"Configuration specified scheduling_choice as '" \
+ found_scheduler + "', but auto-complete-config " \
"function for model '" + self._model_config["name"]
+ "' tries to set scheduling_choice as 'dynamic_batching'")
if "dynamic_batching" not in self._model_config:
self._model_config["dynamic_batching"] = {}
def add_input(self, input):
"""Add the input for the model.
Parameters
----------
input : dict
The input to be added.
Raises
------
ValueError
If input contains property other than 'name', 'data_type'
and 'dims' or any of the properties are not set, or if an
input with the same name already exists in the configuration
but has different data_type or dims property
"""
valid_properties = ['name', 'data_type', 'dims']
for current_property in input:
if current_property not in valid_properties:
raise ValueError(
"input '" + input['name'] +
"' in auto-complete-config function for model '" +
self._model_config["name"] +
"' contains property other than 'name', 'data_type' and 'dims'."
)
if 'name' not in input:
raise ValueError(
"input in auto-complete-config function for model '" +
self._model_config["name"] + "' is missing 'name' property.")
elif 'data_type' not in input:
raise ValueError("input '" + input['name'] +
"' in auto-complete-config function for model '" +
self._model_config["name"] +
"' is missing 'data_type' property.")
elif 'dims' not in input:
raise ValueError("input '" + input['name'] +
"' in auto-complete-config function for model '" +
self._model_config["name"] +
"' is missing 'dims' property.")
for current_input in self._model_config["input"]:
if input['name'] == current_input['name']:
if current_input[
'data_type'] != "TYPE_INVALID" and current_input[
'data_type'] != input['data_type']:
raise ValueError("unable to load model '" +
self._model_config["name"] +
"', configuration expects datatype " +
current_input['data_type'] +
" for input '" + input['name'] +
"', model provides " + input['data_type'])
elif current_input[
'dims'] and current_input['dims'] != input['dims']:
raise ValueError(
"model '" + self._model_config["name"] + "', tensor '" +
input['name'] + "': the model expects dims " +
str(input['dims']) +
" but the model configuration specifies dims " +
str(current_input['dims']))
else:
current_input['data_type'] = input['data_type']
current_input['dims'] = input['dims']
return
self._model_config["input"].append(input)
def add_output(self, output):
"""Add the output for the model.
Parameters
----------
output : dict
The output to be added.
Raises
------
ValueError
If output contains property other than 'name', 'data_type'
and 'dims' or any of the properties are not set, or if an
output with the same name already exists in the configuration
but has different data_type or dims property
"""
valid_properties = ['name', 'data_type', 'dims']
for current_property in output:
if current_property not in valid_properties:
raise ValueError(
"output '" + output['name'] +
"' in auto-complete-config function for model '" +
self._model_config["name"] +
"' contains property other than 'name', 'data_type' and 'dims'."
)
if 'name' not in output:
raise ValueError(
"output in auto-complete-config function for model '" +
self._model_config["name"] + "' is missing 'name' property.")
elif 'data_type' not in output:
raise ValueError("output '" + output['name'] +
"' in auto-complete-config function for model '" +
self._model_config["name"] +
"' is missing 'data_type' property.")
elif 'dims' not in output:
raise ValueError("output '" + output['name'] +
"' in auto-complete-config function for model '" +
self._model_config["name"] +
"' is missing 'dims' property.")
for current_output in self._model_config["output"]:
if output['name'] == current_output['name']:
if current_output[
'data_type'] != "TYPE_INVALID" and current_output[
'data_type'] != output['data_type']:
raise ValueError("unable to load model '" +
self._model_config["name"] +
"', configuration expects datatype " +
current_output['data_type'] +
" for output '" + output['name'] +
"', model provides " + output['data_type'])
elif current_output[
'dims'] and current_output['dims'] != output['dims']:
raise ValueError(
"model '" + self._model_config["name"] + "', tensor '" +
output['name'] + "': the model expects dims " +
str(output['dims']) +
" but the model configuration specifies dims " +
str(current_output['dims']))
else:
current_output['data_type'] = output['data_type']
current_output['dims'] = output['dims']
return
self._model_config["output"].append(output)
TRITONSERVER_REQUEST_FLAG_SEQUENCE_START = 1
TRITONSERVER_REQUEST_FLAG_SEQUENCE_END = 2
TRITONSERVER_RESPONSE_COMPLETE_FINAL = 1