forked from triton-inference-server/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.cc
928 lines (823 loc) · 31.1 KB
/
simple.cc
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
// Copyright (c) 2020, NVIDIA CORPORATION. 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.
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <unistd.h>
#include <chrono>
#include <cstring>
#include <future>
#include <iostream>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
#include "src/servers/common.h"
#include "triton/core/tritonserver.h"
#ifdef TRITON_ENABLE_GPU
#include <cuda_runtime_api.h>
#endif // TRITON_ENABLE_GPU
namespace ni = nvidia::inferenceserver;
namespace {
bool enforce_memory_type = false;
TRITONSERVER_MemoryType requested_memory_type;
#ifdef TRITON_ENABLE_GPU
static auto cuda_data_deleter = [](void* data) {
if (data != nullptr) {
cudaPointerAttributes attr;
auto cuerr = cudaPointerGetAttributes(&attr, data);
if (cuerr != cudaSuccess) {
std::cerr << "error: failed to get CUDA pointer attribute of " << data
<< ": " << cudaGetErrorString(cuerr) << std::endl;
}
if (attr.type == cudaMemoryTypeDevice) {
cuerr = cudaFree(data);
} else if (attr.type == cudaMemoryTypeHost) {
cuerr = cudaFreeHost(data);
}
if (cuerr != cudaSuccess) {
std::cerr << "error: failed to release CUDA pointer " << data << ": "
<< cudaGetErrorString(cuerr) << std::endl;
}
}
};
#endif // TRITON_ENABLE_GPU
void
Usage(char** argv, const std::string& msg = std::string())
{
if (!msg.empty()) {
std::cerr << msg << std::endl;
}
std::cerr << "Usage: " << argv[0] << " [options]" << std::endl;
std::cerr << "\t-m <\"system\"|\"pinned\"|gpu>"
<< " Enforce the memory type for input and output tensors."
<< " If not specified, inputs will be in system memory and outputs"
<< " will be based on the model's preferred type." << std::endl;
std::cerr << "\t-v Enable verbose logging" << std::endl;
std::cerr << "\t-r [model repository absolute path]" << std::endl;
exit(1);
}
TRITONSERVER_Error*
ResponseAlloc(
TRITONSERVER_ResponseAllocator* allocator, const char* tensor_name,
size_t byte_size, TRITONSERVER_MemoryType preferred_memory_type,
int64_t preferred_memory_type_id, void* userp, void** buffer,
void** buffer_userp, TRITONSERVER_MemoryType* actual_memory_type,
int64_t* actual_memory_type_id)
{
// Initially attempt to make the actual memory type and id that we
// allocate be the same as preferred memory type
*actual_memory_type = preferred_memory_type;
*actual_memory_type_id = preferred_memory_type_id;
// If 'byte_size' is zero just return 'buffer' == nullptr, we don't
// need to do any other book-keeping.
if (byte_size == 0) {
*buffer = nullptr;
*buffer_userp = nullptr;
std::cout << "allocated " << byte_size << " bytes for result tensor "
<< tensor_name << std::endl;
} else {
void* allocated_ptr = nullptr;
if (enforce_memory_type) {
*actual_memory_type = requested_memory_type;
}
switch (*actual_memory_type) {
#ifdef TRITON_ENABLE_GPU
case TRITONSERVER_MEMORY_CPU_PINNED: {
auto err = cudaSetDevice(*actual_memory_type_id);
if ((err != cudaSuccess) && (err != cudaErrorNoDevice) &&
(err != cudaErrorInsufficientDriver)) {
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_INTERNAL,
std::string(
"unable to recover current CUDA device: " +
std::string(cudaGetErrorString(err)))
.c_str());
}
err = cudaHostAlloc(&allocated_ptr, byte_size, cudaHostAllocPortable);
if (err != cudaSuccess) {
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_INTERNAL,
std::string(
"cudaHostAlloc failed: " +
std::string(cudaGetErrorString(err)))
.c_str());
}
break;
}
case TRITONSERVER_MEMORY_GPU: {
auto err = cudaSetDevice(*actual_memory_type_id);
if ((err != cudaSuccess) && (err != cudaErrorNoDevice) &&
(err != cudaErrorInsufficientDriver)) {
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_INTERNAL,
std::string(
"unable to recover current CUDA device: " +
std::string(cudaGetErrorString(err)))
.c_str());
}
err = cudaMalloc(&allocated_ptr, byte_size);
if (err != cudaSuccess) {
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_INTERNAL,
std::string(
"cudaMalloc failed: " + std::string(cudaGetErrorString(err)))
.c_str());
}
break;
}
#endif // TRITON_ENABLE_GPU
// Use CPU memory if the requested memory type is unknown
// (default case).
case TRITONSERVER_MEMORY_CPU:
default: {
*actual_memory_type = TRITONSERVER_MEMORY_CPU;
allocated_ptr = malloc(byte_size);
break;
}
}
// Pass the tensor name with buffer_userp so we can show it when
// releasing the buffer.
if (allocated_ptr != nullptr) {
*buffer = allocated_ptr;
*buffer_userp = new std::string(tensor_name);
std::cout << "allocated " << byte_size << " bytes in "
<< TRITONSERVER_MemoryTypeString(*actual_memory_type)
<< " for result tensor " << tensor_name << std::endl;
}
}
return nullptr; // Success
}
TRITONSERVER_Error*
ResponseRelease(
TRITONSERVER_ResponseAllocator* allocator, void* buffer, void* buffer_userp,
size_t byte_size, TRITONSERVER_MemoryType memory_type,
int64_t memory_type_id)
{
std::string* name = nullptr;
if (buffer_userp != nullptr) {
name = reinterpret_cast<std::string*>(buffer_userp);
} else {
name = new std::string("<unknown>");
}
std::cout << "Releasing buffer " << buffer << " of size " << byte_size
<< " in " << TRITONSERVER_MemoryTypeString(memory_type)
<< " for result '" << *name << "'" << std::endl;
switch (memory_type) {
case TRITONSERVER_MEMORY_CPU:
free(buffer);
break;
#ifdef TRITON_ENABLE_GPU
case TRITONSERVER_MEMORY_CPU_PINNED: {
auto err = cudaSetDevice(memory_type_id);
if (err == cudaSuccess) {
err = cudaFreeHost(buffer);
}
if (err != cudaSuccess) {
std::cerr << "error: failed to cudaFree " << buffer << ": "
<< cudaGetErrorString(err) << std::endl;
}
break;
}
case TRITONSERVER_MEMORY_GPU: {
auto err = cudaSetDevice(memory_type_id);
if (err == cudaSuccess) {
err = cudaFree(buffer);
}
if (err != cudaSuccess) {
std::cerr << "error: failed to cudaFree " << buffer << ": "
<< cudaGetErrorString(err) << std::endl;
}
break;
}
#endif // TRITON_ENABLE_GPU
default:
std::cerr << "error: unexpected buffer allocated in CUDA managed memory"
<< std::endl;
break;
}
delete name;
return nullptr; // Success
}
void
InferRequestComplete(
TRITONSERVER_InferenceRequest* request, const uint32_t flags, void* userp)
{
// We reuse the request so we don't delete it here.
}
void
InferResponseComplete(
TRITONSERVER_InferenceResponse* response, const uint32_t flags, void* userp)
{
if (response != nullptr) {
// Send 'response' to the future.
std::promise<TRITONSERVER_InferenceResponse*>* p =
reinterpret_cast<std::promise<TRITONSERVER_InferenceResponse*>*>(userp);
p->set_value(response);
delete p;
}
}
TRITONSERVER_Error*
ParseModelMetadata(
const rapidjson::Document& model_metadata, bool* is_int,
bool* is_torch_model)
{
std::string seen_data_type;
for (const auto& input : model_metadata["inputs"].GetArray()) {
if (strcmp(input["datatype"].GetString(), "INT32") &&
strcmp(input["datatype"].GetString(), "FP32")) {
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_UNSUPPORTED,
"simple lib example only supports model with data type INT32 or "
"FP32");
}
if (seen_data_type.empty()) {
seen_data_type = input["datatype"].GetString();
} else if (strcmp(seen_data_type.c_str(), input["datatype"].GetString())) {
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_INVALID_ARG,
"the inputs and outputs of 'simple' model must have the data type");
}
}
for (const auto& output : model_metadata["outputs"].GetArray()) {
if (strcmp(output["datatype"].GetString(), "INT32") &&
strcmp(output["datatype"].GetString(), "FP32")) {
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_UNSUPPORTED,
"simple lib example only supports model with data type INT32 or "
"FP32");
} else if (strcmp(seen_data_type.c_str(), output["datatype"].GetString())) {
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_INVALID_ARG,
"the inputs and outputs of 'simple' model must have the data type");
}
}
*is_int = (strcmp(seen_data_type.c_str(), "INT32") == 0);
*is_torch_model =
(strcmp(model_metadata["platform"].GetString(), "pytorch_libtorch") == 0);
return nullptr;
}
template <typename T>
void
GenerateInputData(
std::vector<char>* input0_data, std::vector<char>* input1_data)
{
input0_data->resize(16 * sizeof(T));
input1_data->resize(16 * sizeof(T));
for (size_t i = 0; i < 16; ++i) {
((T*)input0_data->data())[i] = i;
((T*)input1_data->data())[i] = 1;
}
}
template <typename T>
void
CompareResult(
const std::string& output0_name, const std::string& output1_name,
const void* input0, const void* input1, const char* output0,
const char* output1)
{
for (size_t i = 0; i < 16; ++i) {
std::cout << ((T*)input0)[i] << " + " << ((T*)input1)[i] << " = "
<< ((T*)output0)[i] << std::endl;
std::cout << ((T*)input0)[i] << " - " << ((T*)input1)[i] << " = "
<< ((T*)output1)[i] << std::endl;
if ((((T*)input0)[i] + ((T*)input1)[i]) != ((T*)output0)[i]) {
FAIL("incorrect sum in " + output0_name);
}
if ((((T*)input0)[i] - ((T*)input1)[i]) != ((T*)output1)[i]) {
FAIL("incorrect difference in " + output1_name);
}
}
}
void
Check(
TRITONSERVER_InferenceResponse* response,
const std::vector<char>& input0_data, const std::vector<char>& input1_data,
const std::string& output0, const std::string& output1,
const size_t expected_byte_size,
const TRITONSERVER_DataType expected_datatype, const bool is_int)
{
std::unordered_map<std::string, std::vector<char>> output_data;
uint32_t output_count;
FAIL_IF_ERR(
TRITONSERVER_InferenceResponseOutputCount(response, &output_count),
"getting number of response outputs");
if (output_count != 2) {
FAIL("expecting 2 response outputs, got " + std::to_string(output_count));
}
for (uint32_t idx = 0; idx < output_count; ++idx) {
const char* cname;
TRITONSERVER_DataType datatype;
const int64_t* shape;
uint64_t dim_count;
const void* base;
size_t byte_size;
TRITONSERVER_MemoryType memory_type;
int64_t memory_type_id;
void* userp;
FAIL_IF_ERR(
TRITONSERVER_InferenceResponseOutput(
response, idx, &cname, &datatype, &shape, &dim_count, &base,
&byte_size, &memory_type, &memory_type_id, &userp),
"getting output info");
if (cname == nullptr) {
FAIL("unable to get output name");
}
std::string name(cname);
if ((name != output0) && (name != output1)) {
FAIL("unexpected output '" + name + "'");
}
if ((dim_count != 2) || (shape[0] != 1) || (shape[1] != 16)) {
FAIL("unexpected shape for '" + name + "'");
}
if (datatype != expected_datatype) {
FAIL(
"unexpected datatype '" +
std::string(TRITONSERVER_DataTypeString(datatype)) + "' for '" +
name + "'");
}
if (byte_size != expected_byte_size) {
FAIL(
"unexpected byte-size, expected " +
std::to_string(expected_byte_size) + ", got " +
std::to_string(byte_size) + " for " + name);
}
if (enforce_memory_type && (memory_type != requested_memory_type)) {
FAIL(
"unexpected memory type, expected to be allocated in " +
std::string(TRITONSERVER_MemoryTypeString(requested_memory_type)) +
", got " + std::string(TRITONSERVER_MemoryTypeString(memory_type)) +
", id " + std::to_string(memory_type_id) + " for " + name);
}
// We make a copy of the data here... which we could avoid for
// performance reasons but ok for this simple example.
std::vector<char>& odata = output_data[name];
switch (memory_type) {
case TRITONSERVER_MEMORY_CPU: {
std::cout << name << " is stored in system memory" << std::endl;
const char* cbase = reinterpret_cast<const char*>(base);
odata.assign(cbase, cbase + byte_size);
break;
}
case TRITONSERVER_MEMORY_CPU_PINNED: {
std::cout << name << " is stored in pinned memory" << std::endl;
const char* cbase = reinterpret_cast<const char*>(base);
odata.assign(cbase, cbase + byte_size);
break;
}
#ifdef TRITON_ENABLE_GPU
case TRITONSERVER_MEMORY_GPU: {
std::cout << name << " is stored in GPU memory" << std::endl;
odata.reserve(byte_size);
FAIL_IF_CUDA_ERR(
cudaMemcpy(&odata[0], base, byte_size, cudaMemcpyDeviceToHost),
"getting " + name + " data from GPU memory");
break;
}
#endif
default:
FAIL("unexpected memory type");
}
}
if (is_int) {
CompareResult<int32_t>(
output0, output1, &input0_data[0], &input1_data[0],
output_data[output0].data(), output_data[output1].data());
} else {
CompareResult<float>(
output0, output1, &input0_data[0], &input1_data[0],
output_data[output0].data(), output_data[output1].data());
}
}
} // namespace
int
main(int argc, char** argv)
{
std::string model_repository_path;
int verbose_level = 0;
// Parse commandline...
int opt;
while ((opt = getopt(argc, argv, "vm:r:")) != -1) {
switch (opt) {
case 'm': {
enforce_memory_type = true;
if (!strcmp(optarg, "system")) {
requested_memory_type = TRITONSERVER_MEMORY_CPU;
} else if (!strcmp(optarg, "pinned")) {
requested_memory_type = TRITONSERVER_MEMORY_CPU_PINNED;
} else if (!strcmp(optarg, "gpu")) {
requested_memory_type = TRITONSERVER_MEMORY_GPU;
} else {
Usage(
argv,
"-m must be used to specify one of the following types:"
" <\"system\"|\"pinned\"|gpu>");
}
break;
}
case 'r':
model_repository_path = optarg;
break;
case 'v':
verbose_level = 1;
break;
case '?':
Usage(argv);
break;
}
}
if (model_repository_path.empty()) {
Usage(argv, "-r must be used to specify model repository path");
}
#ifndef TRITON_ENABLE_GPU
if (enforce_memory_type && requested_memory_type != TRITONSERVER_MEMORY_CPU) {
Usage(argv, "-m can only be set to \"system\" without enabling GPU");
}
#endif // TRITON_ENABLE_GPU
// Check API version.
uint32_t api_version_major, api_version_minor;
FAIL_IF_ERR(
TRITONSERVER_ApiVersion(&api_version_major, &api_version_minor),
"getting Triton API version");
if ((TRITONSERVER_API_VERSION_MAJOR != api_version_major) ||
(TRITONSERVER_API_VERSION_MINOR > api_version_minor)) {
FAIL("triton server API version mismatch");
}
// Create the server...
TRITONSERVER_ServerOptions* server_options = nullptr;
FAIL_IF_ERR(
TRITONSERVER_ServerOptionsNew(&server_options),
"creating server options");
FAIL_IF_ERR(
TRITONSERVER_ServerOptionsSetModelRepositoryPath(
server_options, model_repository_path.c_str()),
"setting model repository path");
FAIL_IF_ERR(
TRITONSERVER_ServerOptionsSetLogVerbose(server_options, verbose_level),
"setting verbose logging level");
FAIL_IF_ERR(
TRITONSERVER_ServerOptionsSetBackendDirectory(
server_options, "/opt/tritonserver/backends"),
"setting backend directory");
FAIL_IF_ERR(
TRITONSERVER_ServerOptionsSetRepoAgentDirectory(
server_options, "/opt/tritonserver/repoagents"),
"setting repository agent directory");
FAIL_IF_ERR(
TRITONSERVER_ServerOptionsSetStrictModelConfig(server_options, true),
"setting strict model configuration");
#ifdef TRITON_ENABLE_GPU
double min_compute_capability = TRITON_MIN_COMPUTE_CAPABILITY;
#else
double min_compute_capability = 0;
#endif // TRITON_ENABLE_GPU
FAIL_IF_ERR(
TRITONSERVER_ServerOptionsSetMinSupportedComputeCapability(
server_options, min_compute_capability),
"setting minimum supported CUDA compute capability");
TRITONSERVER_Server* server_ptr = nullptr;
FAIL_IF_ERR(
TRITONSERVER_ServerNew(&server_ptr, server_options), "creating server");
FAIL_IF_ERR(
TRITONSERVER_ServerOptionsDelete(server_options),
"deleting server options");
std::shared_ptr<TRITONSERVER_Server> server(
server_ptr, TRITONSERVER_ServerDelete);
// Wait until the server is both live and ready.
size_t health_iters = 0;
while (true) {
bool live, ready;
FAIL_IF_ERR(
TRITONSERVER_ServerIsLive(server.get(), &live),
"unable to get server liveness");
FAIL_IF_ERR(
TRITONSERVER_ServerIsReady(server.get(), &ready),
"unable to get server readiness");
std::cout << "Server Health: live " << live << ", ready " << ready
<< std::endl;
if (live && ready) {
break;
}
if (++health_iters >= 10) {
FAIL("failed to find healthy inference server");
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
// Print status of the server.
{
TRITONSERVER_Message* server_metadata_message;
FAIL_IF_ERR(
TRITONSERVER_ServerMetadata(server.get(), &server_metadata_message),
"unable to get server metadata message");
const char* buffer;
size_t byte_size;
FAIL_IF_ERR(
TRITONSERVER_MessageSerializeToJson(
server_metadata_message, &buffer, &byte_size),
"unable to serialize server metadata message");
std::cout << "Server Status:" << std::endl;
std::cout << std::string(buffer, byte_size) << std::endl;
FAIL_IF_ERR(
TRITONSERVER_MessageDelete(server_metadata_message),
"deleting status metadata");
}
const std::string model_name("simple");
// Wait for the model to become available.
bool is_torch_model = false;
bool is_int = true;
bool is_ready = false;
health_iters = 0;
while (!is_ready) {
FAIL_IF_ERR(
TRITONSERVER_ServerModelIsReady(
server.get(), model_name.c_str(), 1, &is_ready),
"unable to get model readiness");
if (!is_ready) {
if (++health_iters >= 10) {
FAIL("model failed to be ready in 10 iterations");
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
continue;
}
TRITONSERVER_Message* model_metadata_message;
FAIL_IF_ERR(
TRITONSERVER_ServerModelMetadata(
server.get(), model_name.c_str(), 1, &model_metadata_message),
"unable to get model metadata message");
const char* buffer;
size_t byte_size;
FAIL_IF_ERR(
TRITONSERVER_MessageSerializeToJson(
model_metadata_message, &buffer, &byte_size),
"unable to serialize model status protobuf");
rapidjson::Document model_metadata;
model_metadata.Parse(buffer, byte_size);
if (model_metadata.HasParseError()) {
FAIL(
"error: failed to parse model metadata from JSON: " +
std::string(GetParseError_En(model_metadata.GetParseError())) +
" at " + std::to_string(model_metadata.GetErrorOffset()));
}
FAIL_IF_ERR(
TRITONSERVER_MessageDelete(model_metadata_message),
"deleting status protobuf");
if (strcmp(model_metadata["name"].GetString(), model_name.c_str())) {
FAIL("unable to find metadata for model");
}
bool found_version = false;
if (model_metadata.HasMember("versions")) {
for (const auto& version : model_metadata["versions"].GetArray()) {
if (strcmp(version.GetString(), "1") == 0) {
found_version = true;
break;
}
}
}
if (!found_version) {
FAIL("unable to find version 1 status for model");
}
FAIL_IF_ERR(
ParseModelMetadata(model_metadata, &is_int, &is_torch_model),
"parsing model metadata");
}
// Create the allocator that will be used to allocate buffers for
// the result tensors.
TRITONSERVER_ResponseAllocator* allocator = nullptr;
FAIL_IF_ERR(
TRITONSERVER_ResponseAllocatorNew(
&allocator, ResponseAlloc, ResponseRelease, nullptr /* start_fn */),
"creating response allocator");
// Inference
TRITONSERVER_InferenceRequest* irequest = nullptr;
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestNew(
&irequest, server.get(), model_name.c_str(), -1 /* model_version */),
"creating inference request");
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestSetId(irequest, "my_request_id"),
"setting ID for the request");
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestSetReleaseCallback(
irequest, InferRequestComplete, nullptr /* request_release_userp */),
"setting request release callback");
// Inputs
auto input0 = is_torch_model ? "INPUT__0" : "INPUT0";
auto input1 = is_torch_model ? "INPUT__1" : "INPUT1";
std::vector<int64_t> input0_shape({1, 16});
std::vector<int64_t> input1_shape({1, 16});
const TRITONSERVER_DataType datatype =
(is_int) ? TRITONSERVER_TYPE_INT32 : TRITONSERVER_TYPE_FP32;
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestAddInput(
irequest, input0, datatype, &input0_shape[0], input0_shape.size()),
"setting input 0 meta-data for the request");
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestAddInput(
irequest, input1, datatype, &input1_shape[0], input1_shape.size()),
"setting input 1 meta-data for the request");
auto output0 = is_torch_model ? "OUTPUT__0" : "OUTPUT0";
auto output1 = is_torch_model ? "OUTPUT__1" : "OUTPUT1";
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestAddRequestedOutput(irequest, output0),
"requesting output 0 for the request");
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestAddRequestedOutput(irequest, output1),
"requesting output 1 for the request");
// Create the data for the two input tensors. Initialize the first
// to unique values and the second to all ones.
std::vector<char> input0_data;
std::vector<char> input1_data;
if (is_int) {
GenerateInputData<int32_t>(&input0_data, &input1_data);
} else {
GenerateInputData<float>(&input0_data, &input1_data);
}
size_t input0_size = input0_data.size();
size_t input1_size = input1_data.size();
const void* input0_base = &input0_data[0];
const void* input1_base = &input1_data[0];
#ifdef TRITON_ENABLE_GPU
std::unique_ptr<void, decltype(cuda_data_deleter)> input0_gpu(
nullptr, cuda_data_deleter);
std::unique_ptr<void, decltype(cuda_data_deleter)> input1_gpu(
nullptr, cuda_data_deleter);
bool use_cuda_memory =
(enforce_memory_type &&
(requested_memory_type != TRITONSERVER_MEMORY_CPU));
if (use_cuda_memory) {
FAIL_IF_CUDA_ERR(cudaSetDevice(0), "setting CUDA device to device 0");
if (requested_memory_type != TRITONSERVER_MEMORY_CPU_PINNED) {
void* dst;
FAIL_IF_CUDA_ERR(
cudaMalloc(&dst, input0_size),
"allocating GPU memory for INPUT0 data");
input0_gpu.reset(dst);
FAIL_IF_CUDA_ERR(
cudaMemcpy(dst, &input0_data[0], input0_size, cudaMemcpyHostToDevice),
"setting INPUT0 data in GPU memory");
FAIL_IF_CUDA_ERR(
cudaMalloc(&dst, input1_size),
"allocating GPU memory for INPUT1 data");
input1_gpu.reset(dst);
FAIL_IF_CUDA_ERR(
cudaMemcpy(dst, &input1_data[0], input1_size, cudaMemcpyHostToDevice),
"setting INPUT1 data in GPU memory");
} else {
void* dst;
FAIL_IF_CUDA_ERR(
cudaHostAlloc(&dst, input0_size, cudaHostAllocPortable),
"allocating pinned memory for INPUT0 data");
input0_gpu.reset(dst);
FAIL_IF_CUDA_ERR(
cudaMemcpy(dst, &input0_data[0], input0_size, cudaMemcpyHostToHost),
"setting INPUT0 data in pinned memory");
FAIL_IF_CUDA_ERR(
cudaHostAlloc(&dst, input1_size, cudaHostAllocPortable),
"allocating pinned memory for INPUT1 data");
input1_gpu.reset(dst);
FAIL_IF_CUDA_ERR(
cudaMemcpy(dst, &input1_data[0], input1_size, cudaMemcpyHostToHost),
"setting INPUT1 data in pinned memory");
}
}
input0_base = use_cuda_memory ? input0_gpu.get() : &input0_data[0];
input1_base = use_cuda_memory ? input1_gpu.get() : &input1_data[0];
#endif // TRITON_ENABLE_GPU
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestAppendInputData(
irequest, input0, input0_base, input0_size, requested_memory_type,
0 /* memory_type_id */),
"assigning INPUT0 data");
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestAppendInputData(
irequest, input1, input1_base, input1_size, requested_memory_type,
0 /* memory_type_id */),
"assigning INPUT1 data");
// Perform inference...
{
auto p = new std::promise<TRITONSERVER_InferenceResponse*>();
std::future<TRITONSERVER_InferenceResponse*> completed = p->get_future();
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestSetResponseCallback(
irequest, allocator, nullptr /* response_allocator_userp */,
InferResponseComplete, reinterpret_cast<void*>(p)),
"setting response callback");
FAIL_IF_ERR(
TRITONSERVER_ServerInferAsync(
server.get(), irequest, nullptr /* trace */),
"running inference");
// Wait for the inference to complete.
TRITONSERVER_InferenceResponse* completed_response = completed.get();
FAIL_IF_ERR(
TRITONSERVER_InferenceResponseError(completed_response),
"response status");
Check(
completed_response, input0_data, input1_data, output0, output1,
input0_size, datatype, is_int);
FAIL_IF_ERR(
TRITONSERVER_InferenceResponseDelete(completed_response),
"deleting inference response");
}
// Modify some input data in place and then reuse the request
// object. For simplicity we only do this when the input tensors are
// in non-pinned system memory.
if (!enforce_memory_type ||
(requested_memory_type == TRITONSERVER_MEMORY_CPU)) {
if (is_int) {
int32_t* input0_base = reinterpret_cast<int32_t*>(&input0_data[0]);
input0_base[0] = 27;
} else {
float* input0_base = reinterpret_cast<float*>(&input0_data[0]);
input0_base[0] = 27.0;
}
auto p = new std::promise<TRITONSERVER_InferenceResponse*>();
std::future<TRITONSERVER_InferenceResponse*> completed = p->get_future();
// Using a new promise so have to re-register the callback to set
// the promise as the userp.
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestSetResponseCallback(
irequest, allocator, nullptr /* response_allocator_userp */,
InferResponseComplete, reinterpret_cast<void*>(p)),
"setting response callback");
FAIL_IF_ERR(
TRITONSERVER_ServerInferAsync(
server.get(), irequest, nullptr /* trace */),
"running inference");
// Wait for the inference to complete.
TRITONSERVER_InferenceResponse* completed_response = completed.get();
FAIL_IF_ERR(
TRITONSERVER_InferenceResponseError(completed_response),
"response status");
Check(
completed_response, input0_data, input1_data, output0, output1,
input0_size, datatype, is_int);
FAIL_IF_ERR(
TRITONSERVER_InferenceResponseDelete(completed_response),
"deleting inference response");
}
// Remove input data and then add back different data.
{
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestRemoveAllInputData(irequest, input0),
"removing INPUT0 data");
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestAppendInputData(
irequest, input0, input1_base, input1_size, requested_memory_type,
0 /* memory_type_id */),
"assigning INPUT1 data to INPUT0");
auto p = new std::promise<TRITONSERVER_InferenceResponse*>();
std::future<TRITONSERVER_InferenceResponse*> completed = p->get_future();
// Using a new promise so have to re-register the callback to set
// the promise as the userp.
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestSetResponseCallback(
irequest, allocator, nullptr /* response_allocator_userp */,
InferResponseComplete, reinterpret_cast<void*>(p)),
"setting response callback");
FAIL_IF_ERR(
TRITONSERVER_ServerInferAsync(
server.get(), irequest, nullptr /* trace */),
"running inference");
// Wait for the inference to complete.
TRITONSERVER_InferenceResponse* completed_response = completed.get();
FAIL_IF_ERR(
TRITONSERVER_InferenceResponseError(completed_response),
"response status");
// Both inputs are using input1_data...
Check(
completed_response, input1_data, input1_data, output0, output1,
input0_size, datatype, is_int);
FAIL_IF_ERR(
TRITONSERVER_InferenceResponseDelete(completed_response),
"deleting inference response");
}
FAIL_IF_ERR(
TRITONSERVER_InferenceRequestDelete(irequest),
"deleting inference request");
FAIL_IF_ERR(
TRITONSERVER_ResponseAllocatorDelete(allocator),
"deleting response allocator");
return 0;
}