forked from triton-inference-server/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
executable file
·810 lines (749 loc) · 31.7 KB
/
__init__.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
#!/usr/bin/env python3
# Copyright 2022-2023, 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 base64
import sys
import grpc
import rapidjson as json
from google.protobuf.json_format import MessageToJson
from tritonclient.grpc import *
from tritonclient.grpc import model_config_pb2, service_pb2, service_pb2_grpc
from ... import _auth as auth
from ..._plugin import InferenceServerClientPlugin
from ..._request import Request
from .._client import MAX_GRPC_MESSAGE_SIZE, InferenceServerClientBase, KeepAliveOptions
from .._utils import (
_get_inference_request,
_grpc_compression_type,
raise_error,
raise_error_grpc,
)
class InferenceServerClient(InferenceServerClientBase):
"""This feature is currently in beta and may be subject to change.
An analogy of the :py:class:`tritonclient.grpc.InferenceServerClient` to enable
calling via asyncio syntax. The object is intended to be used by a single
thread and simultaneously calling methods with different threads is not
supported and can cause undefined behavior.
"""
def __init__(
self,
url,
verbose=False,
ssl=False,
root_certificates=None,
private_key=None,
certificate_chain=None,
creds=None,
keepalive_options=None,
channel_args=None,
):
super().__init__()
# Explicitly check "is not None" here to support passing an empty
# list to specify setting no channel arguments.
if channel_args is not None:
channel_opt = channel_args
else:
# Use GRPC KeepAlive client defaults if unspecified
if not keepalive_options:
keepalive_options = KeepAliveOptions()
# To specify custom channel_opt, see the channel_args parameter.
channel_opt = [
("grpc.max_send_message_length", MAX_GRPC_MESSAGE_SIZE),
("grpc.max_receive_message_length", MAX_GRPC_MESSAGE_SIZE),
("grpc.keepalive_time_ms", keepalive_options.keepalive_time_ms),
("grpc.keepalive_timeout_ms", keepalive_options.keepalive_timeout_ms),
(
"grpc.keepalive_permit_without_calls",
keepalive_options.keepalive_permit_without_calls,
),
(
"grpc.http2.max_pings_without_data",
keepalive_options.http2_max_pings_without_data,
),
]
if creds:
self._channel = grpc.aio.secure_channel(url, creds, options=channel_opt)
elif ssl:
rc_bytes = pk_bytes = cc_bytes = None
if root_certificates is not None:
with open(root_certificates, "rb") as rc_fs:
rc_bytes = rc_fs.read()
if private_key is not None:
with open(private_key, "rb") as pk_fs:
pk_bytes = pk_fs.read()
if certificate_chain is not None:
with open(certificate_chain, "rb") as cc_fs:
cc_bytes = cc_fs.read()
creds = grpc.ssl_channel_credentials(
root_certificates=rc_bytes,
private_key=pk_bytes,
certificate_chain=cc_bytes,
)
self._channel = grpc.aio.secure_channel(url, creds, options=channel_opt)
else:
self._channel = grpc.aio.insecure_channel(url, options=channel_opt)
self._client_stub = service_pb2_grpc.GRPCInferenceServiceStub(self._channel)
self._verbose = verbose
def _return_response(self, response, as_json):
if as_json:
return json.loads(MessageToJson(response, preserving_proto_field_name=True))
else:
return response
async def __aenter__(self):
return self
async def __aexit__(self, type, value, traceback):
await self.close()
async def close(self):
"""Close the client. Any future calls to server
will result in an Error.
"""
await self._channel.close()
def _get_metadata(self, headers):
request = Request(headers)
self._call_plugin(request)
request_metadata = (
request.headers.items() if request.headers is not None else ()
)
return request_metadata
async def is_server_live(self, headers=None, client_timeout=None):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.is_server_live`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.ServerLiveRequest()
if self._verbose:
print("is_server_live, metadata {}\n{}".format(metadata, request))
response = await self._client_stub.ServerLive(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return response.live
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def is_server_ready(self, headers=None, client_timeout=None):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.is_server_ready`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.ServerReadyRequest()
if self._verbose:
print("is_server_ready, metadata {}\n{}".format(metadata, request))
response = await self._client_stub.ServerReady(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return response.ready
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def is_model_ready(
self, model_name, model_version="", headers=None, client_timeout=None
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.is_model_ready`"""
metadata = self._get_metadata(headers)
try:
if type(model_version) != str:
raise_error("model version must be a string")
request = service_pb2.ModelReadyRequest(
name=model_name, version=model_version
)
if self._verbose:
print("is_model_ready, metadata {}\n{}".format(metadata, request))
response = await self._client_stub.ModelReady(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return response.ready
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def get_server_metadata(
self, headers=None, as_json=False, client_timeout=None
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.get_server_metadata`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.ServerMetadataRequest()
if self._verbose:
print("get_server_metadata, metadata {}\n{}".format(metadata, request))
response = await self._client_stub.ServerMetadata(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return self._return_response(response, as_json)
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def get_model_metadata(
self,
model_name,
model_version="",
headers=None,
as_json=False,
client_timeout=None,
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.get_model_metadata`"""
metadata = self._get_metadata(headers)
try:
if type(model_version) != str:
raise_error("model version must be a string")
request = service_pb2.ModelMetadataRequest(
name=model_name, version=model_version
)
if self._verbose:
print("get_model_metadata, metadata {}\n{}".format(metadata, request))
response = await self._client_stub.ModelMetadata(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return self._return_response(response, as_json)
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def get_model_config(
self,
model_name,
model_version="",
headers=None,
as_json=False,
client_timeout=None,
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.get_model_config`"""
metadata = self._get_metadata(headers)
try:
if type(model_version) != str:
raise_error("model version must be a string")
request = service_pb2.ModelConfigRequest(
name=model_name, version=model_version
)
if self._verbose:
print("get_model_config, metadata {}\n{}".format(metadata, request))
response = await self._client_stub.ModelConfig(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return self._return_response(response, as_json)
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def get_model_repository_index(
self, headers=None, as_json=False, client_timeout=None
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.get_model_repository_index`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.RepositoryIndexRequest()
if self._verbose:
print(
"get_model_repository_index, metadata {}\n{}".format(
metadata, request
)
)
response = await self._client_stub.RepositoryIndex(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return self._return_response(response, as_json)
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def load_model(
self,
model_name,
headers=None,
config=None,
files=None,
client_timeout=None,
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.load_model`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.RepositoryModelLoadRequest(model_name=model_name)
if config is not None:
request.parameters["config"].string_param = config
if self._verbose:
# Don't print file content which can be large
print(
"load_model, metadata {}\noverride files omitted:\n{}".format(
metadata, request
)
)
if files is not None:
for path, content in files.items():
request.parameters[path].bytes_param = content
await self._client_stub.RepositoryModelLoad(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print("Loaded model '{}'".format(model_name))
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def unload_model(
self,
model_name,
headers=None,
unload_dependents=False,
client_timeout=None,
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.unload_model`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.RepositoryModelUnloadRequest(model_name=model_name)
request.parameters["unload_dependents"].bool_param = unload_dependents
if self._verbose:
print("unload_model, metadata {}\n{}".format(metadata, request))
await self._client_stub.RepositoryModelUnload(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print("Unloaded model '{}'".format(model_name))
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def get_inference_statistics(
self,
model_name="",
model_version="",
headers=None,
as_json=False,
client_timeout=None,
):
"""Refer to ::py:meth:`tritonclient.grpc.InferenceServerClient.get_inference_statistics`"""
metadata = self._get_metadata(headers)
try:
if type(model_version) != str:
raise_error("model version must be a string")
request = service_pb2.ModelStatisticsRequest(
name=model_name, version=model_version
)
if self._verbose:
print(
"get_inference_statistics, metadata {}\n{}".format(
metadata, request
)
)
response = await self._client_stub.ModelStatistics(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return self._return_response(response, as_json)
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def update_trace_settings(
self,
model_name=None,
settings={},
headers=None,
as_json=False,
client_timeout=None,
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.update_trace_settings`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.TraceSettingRequest()
if (model_name is not None) and (model_name != ""):
request.model_name = model_name
for key, value in settings.items():
if value is None:
request.settings[key]
else:
request.settings[key].value.extend(
value if isinstance(value, list) else [value]
)
if self._verbose:
print(
"update_trace_settings, metadata {}\n{}".format(metadata, request)
)
response = await self._client_stub.TraceSetting(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return self._return_response(response, as_json)
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def get_trace_settings(
self, model_name=None, headers=None, as_json=False, client_timeout=None
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.get_trace_settings`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.TraceSettingRequest()
if (model_name is not None) and (model_name != ""):
request.model_name = model_name
if self._verbose:
print("get_trace_settings, metadata {}\n{}".format(metadata, request))
response = await self._client_stub.TraceSetting(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return self._return_response(response, as_json)
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def update_log_settings(
self, settings, headers=None, as_json=False, client_timeout=None
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.update_log_settings`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.LogSettingsRequest()
for key, value in settings.items():
if value is None:
request.settings[key]
else:
if key == "log_file" or key == "log_format":
request.settings[key].string_param = value
elif key == "log_verbose_level":
request.settings[key].uint32_param = value
else:
request.settings[key].bool_param = value
if self._verbose:
print("update_log_settings, metadata {}\n{}".format(metadata, request))
response = await self._client_stub.LogSettings(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return self._return_response(response, as_json)
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def get_log_settings(self, headers=None, as_json=False, client_timeout=None):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.get_log_settings`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.LogSettingsRequest()
if self._verbose:
print("get_log_settings, metadata {}\n{}".format(metadata, request))
response = await self._client_stub.LogSettings(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return self._return_response(response, as_json)
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def get_system_shared_memory_status(
self, region_name="", headers=None, as_json=False, client_timeout=None
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.get_system_shared_memory_status`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.SystemSharedMemoryStatusRequest(name=region_name)
if self._verbose:
print(
"get_system_shared_memory_status, metadata {}\n{}".format(
metadata, request
)
)
response = await self._client_stub.SystemSharedMemoryStatus(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return self._return_response(response, as_json)
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def register_system_shared_memory(
self, name, key, byte_size, offset=0, headers=None, client_timeout=None
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.register_system_shared_memory`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.SystemSharedMemoryRegisterRequest(
name=name, key=key, offset=offset, byte_size=byte_size
)
if self._verbose:
print(
"register_system_shared_memory, metadata {}\n{}".format(
metadata, request
)
)
await self._client_stub.SystemSharedMemoryRegister(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print("Registered system shared memory with name '{}'".format(name))
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def unregister_system_shared_memory(
self, name="", headers=None, client_timeout=None
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.unregister_system_shared_memory`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.SystemSharedMemoryUnregisterRequest(name=name)
if self._verbose:
print(
"unregister_system_shared_memory, metadata {}\n{}".format(
metadata, request
)
)
await self._client_stub.SystemSharedMemoryUnregister(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
if name != "":
print(
"Unregistered system shared memory with name '{}'".format(name)
)
else:
print("Unregistered all system shared memory regions")
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def get_cuda_shared_memory_status(
self, region_name="", headers=None, as_json=False, client_timeout=None
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.get_cuda_shared_memory_status`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.CudaSharedMemoryStatusRequest(name=region_name)
if self._verbose:
print(
"get_cuda_shared_memory_status, metadata {}\n{}".format(
metadata, request
)
)
response = await self._client_stub.CudaSharedMemoryStatus(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print(response)
return self._return_response(response, as_json)
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def register_cuda_shared_memory(
self,
name,
raw_handle,
device_id,
byte_size,
headers=None,
client_timeout=None,
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.register_cuda_shared_memory`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.CudaSharedMemoryRegisterRequest(
name=name,
raw_handle=base64.b64decode(raw_handle),
device_id=device_id,
byte_size=byte_size,
)
if self._verbose:
print(
"register_cuda_shared_memory, metadata {}\n{}".format(
metadata, request
)
)
await self._client_stub.CudaSharedMemoryRegister(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
print("Registered cuda shared memory with name '{}'".format(name))
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def unregister_cuda_shared_memory(
self, name="", headers=None, client_timeout=None
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.unregister_cuda_shared_memory`"""
metadata = self._get_metadata(headers)
try:
request = service_pb2.CudaSharedMemoryUnregisterRequest(name=name)
if self._verbose:
print(
"unregister_cuda_shared_memory, metadata {}\n{}".format(
metadata, request
)
)
await self._client_stub.CudaSharedMemoryUnregister(
request=request, metadata=metadata, timeout=client_timeout
)
if self._verbose:
if name != "":
print("Unregistered cuda shared memory with name '{}'".format(name))
else:
print("Unregistered all cuda shared memory regions")
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
async def infer(
self,
model_name,
inputs,
model_version="",
outputs=None,
request_id="",
sequence_id=0,
sequence_start=False,
sequence_end=False,
priority=0,
timeout=None,
client_timeout=None,
headers=None,
compression_algorithm=None,
parameters=None,
):
"""Refer to :py:meth:`tritonclient.grpc.InferenceServerClient.infer`"""
metadata = self._get_metadata(headers)
if type(model_version) != str:
raise_error("model version must be a string")
request = _get_inference_request(
model_name=model_name,
inputs=inputs,
model_version=model_version,
request_id=request_id,
outputs=outputs,
sequence_id=sequence_id,
sequence_start=sequence_start,
sequence_end=sequence_end,
priority=priority,
timeout=timeout,
parameters=parameters,
)
if self._verbose:
print("infer, metadata {}\n{}".format(metadata, request))
try:
response = await self._client_stub.ModelInfer(
request=request,
metadata=metadata,
timeout=client_timeout,
compression=_grpc_compression_type(compression_algorithm),
)
if self._verbose:
print(response)
result = InferResult(response)
return result
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)
def stream_infer(
self,
inputs_iterator,
stream_timeout=None,
headers=None,
compression_algorithm=None,
):
"""Runs an asynchronous inference over gRPC bi-directional streaming
API.
Parameters
----------
inputs_iterator : asynchronous iterator
Async iterator that yields a dict(s) consists of the input
parameters to the :py:meth:`tritonclient.grpc.InferenceServerClient.async_stream_infer` function defined in
:py:class:`tritonclient.grpc.InferenceServerClient`.
stream_timeout : float
Optional stream timeout. The stream will be closed once the
specified timeout expires.
headers: dict
Optional dictionary specifying additional HTTP headers to include
in the request.
compression_algorithm : str
Optional grpc compression algorithm to be used on client side.
Currently supports "deflate", "gzip" and None. By default, no
compression is used.
Returns
-------
asynchronous iterator
Yield tuple holding (:py:class:`tritonclient.grpc.InferResult`, :py:class:`tritonclient.grpc.InferenceServerException`) objects.
Note
----
This object can be used to cancel the inference request like below:
>>> it = stream_infer(...)
>>> ret = it.cancel()
Raises
------
:py:class:`tritonclient.grpc.InferenceServerException`
If inputs_iterator does not yield the correct input.
"""
metadata = self._get_metadata(headers)
async def _request_iterator(inputs_iterator):
# Internal iterator for converting into grpc request
async for inputs in inputs_iterator:
if type(inputs) != dict:
raise_error("inputs_iterator is not yielding a dict")
if "model_name" not in inputs or "inputs" not in inputs:
raise_error(
"model_name and/or inputs is missing from inputs_iterator's yielded dict"
)
if "model_version" not in inputs:
inputs["model_version"] = ""
if type(inputs["model_version"]) != str:
raise_error("model_version must be a string")
if "outputs" not in inputs:
inputs["outputs"] = None
if "request_id" not in inputs:
inputs["request_id"] = ""
if "sequence_id" not in inputs:
inputs["sequence_id"] = 0
if "sequence_start" not in inputs:
inputs["sequence_start"] = False
if "sequence_end" not in inputs:
inputs["sequence_end"] = False
if "priority" not in inputs:
inputs["priority"] = 0
if "timeout" not in inputs:
inputs["timeout"] = None
if "parameters" not in inputs:
inputs["parameters"] = None
yield _get_inference_request(
model_name=inputs["model_name"],
inputs=inputs["inputs"],
model_version=inputs["model_version"],
request_id=inputs["request_id"],
outputs=inputs["outputs"],
sequence_id=inputs["sequence_id"],
sequence_start=inputs["sequence_start"],
sequence_end=inputs["sequence_end"],
priority=inputs["priority"],
timeout=inputs["timeout"],
parameters=inputs["parameters"],
)
class _ResponseIterator:
def __init__(self, grpc_call, verbose):
self._grpc_call = grpc_call
self._verbose = verbose
def __aiter__(self):
return self
async def __anext__(self):
response = await self._grpc_call.__aiter__().__anext__()
if self._verbose:
print(response)
result = error = None
if response.error_message != "":
error = InferenceServerException(msg=response.error_message)
else:
result = InferResult(response.infer_response)
return result, error
def cancel(self):
return self._grpc_call.cancel()
try:
grpc_call = self._client_stub.ModelStreamInfer(
_request_iterator(inputs_iterator),
metadata=metadata,
timeout=stream_timeout,
compression=_grpc_compression_type(compression_algorithm),
)
return _ResponseIterator(grpc_call, self._verbose)
except grpc.RpcError as rpc_error:
raise_error_grpc(rpc_error)