-
Notifications
You must be signed in to change notification settings - Fork 103
/
tritonserver.cc
3566 lines (3157 loc) · 116 KB
/
tritonserver.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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019-2024, 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.
#include <string>
#include <vector>
#include "buffer_attributes.h"
#include "cuda_utils.h"
#include "infer_parameter.h"
#include "infer_request.h"
#include "infer_response.h"
#include "infer_stats.h"
#include "metric_family.h"
#include "metrics.h"
#include "model.h"
#include "model_config_utils.h"
#include "model_repository_manager/model_repository_manager.h"
#include "rate_limiter.h"
#include "response_allocator.h"
#include "server.h"
#include "server_message.h"
#include "status.h"
#include "triton/common/logging.h"
#include "triton/common/model_config.h"
#include "triton/common/nvtx.h"
#include "triton/common/table_printer.h"
#include "triton/common/triton_json.h"
#include "tritonserver_apis.h"
// For unknown reason, windows will not export some functions declared
// with dllexport in tritonrepoagent.h and tritonbackend.h. To get
// those functions exported it is (also?) necessary to mark the
// definitions in this file with dllexport as well. The TRITONSERVER_*
// functions are getting exported but for consistency adding the
// declspec to these definitions as well.
#if defined(_MSC_VER)
#define TRITONAPI_DECLSPEC __declspec(dllexport)
#elif defined(__GNUC__)
#define TRITONAPI_DECLSPEC __attribute__((__visibility__("default")))
#else
#define TRITONAPI_DECLSPEC
#endif
namespace tc = triton::core;
namespace {
std::string
ResourceString(const std::string& name, const int count, const int device_id)
{
return std::string(
"{\"name\":\"" + name + "\", \"count\":" + std::to_string(count) +
" \"device\":" + std::to_string(device_id) + "}");
}
std::string
RateLimitModeToString(const tc::RateLimitMode rate_limit_mode)
{
std::string rl_mode_str("<unknown>");
switch (rate_limit_mode) {
case tc::RateLimitMode::RL_EXEC_COUNT: {
rl_mode_str = "EXEC_COUNT";
break;
}
case tc::RateLimitMode::RL_OFF: {
rl_mode_str = "OFF";
break;
}
}
return rl_mode_str;
}
//
// TritonServerError
//
// Implementation for TRITONSERVER_Error.
//
class TritonServerError {
public:
static TRITONSERVER_Error* Create(
TRITONSERVER_Error_Code code, const char* msg);
static TRITONSERVER_Error* Create(
TRITONSERVER_Error_Code code, const std::string& msg);
static TRITONSERVER_Error* Create(const tc::Status& status);
TRITONSERVER_Error_Code Code() const { return code_; }
const std::string& Message() const { return msg_; }
private:
TritonServerError(TRITONSERVER_Error_Code code, const std::string& msg)
: code_(code), msg_(msg)
{
}
TritonServerError(TRITONSERVER_Error_Code code, const char* msg)
: code_(code), msg_(msg)
{
}
TRITONSERVER_Error_Code code_;
const std::string msg_;
};
TRITONSERVER_Error*
TritonServerError::Create(TRITONSERVER_Error_Code code, const char* msg)
{
return reinterpret_cast<TRITONSERVER_Error*>(
new TritonServerError(code, msg));
}
TRITONSERVER_Error*
TritonServerError::Create(TRITONSERVER_Error_Code code, const std::string& msg)
{
return reinterpret_cast<TRITONSERVER_Error*>(
new TritonServerError(code, msg));
}
TRITONSERVER_Error*
TritonServerError::Create(const tc::Status& status)
{
// If 'status' is success then return nullptr as that indicates
// success
if (status.IsOk()) {
return nullptr;
}
return Create(
tc::StatusCodeToTritonCode(status.StatusCode()), status.Message());
}
#define RETURN_IF_STATUS_ERROR(S) \
do { \
const tc::Status& status__ = (S); \
if (!status__.IsOk()) { \
return TritonServerError::Create(status__); \
} \
} while (false)
//
// TritonServerMetrics
//
// Implementation for TRITONSERVER_Metrics.
//
class TritonServerMetrics {
public:
TritonServerMetrics() = default;
TRITONSERVER_Error* Serialize(const char** base, size_t* byte_size);
private:
std::string serialized_;
};
TRITONSERVER_Error*
TritonServerMetrics::Serialize(const char** base, size_t* byte_size)
{
#ifdef TRITON_ENABLE_METRICS
serialized_ = tc::Metrics::SerializedMetrics();
*base = serialized_.c_str();
*byte_size = serialized_.size();
return nullptr; // Success
#else
*base = nullptr;
*byte_size = 0;
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_UNSUPPORTED, "metrics not supported");
#endif // TRITON_ENABLE_METRICS
}
//
// TritonServerOptions
//
// Implementation for TRITONSERVER_ServerOptions.
//
class TritonServerOptions {
public:
TritonServerOptions();
const std::string& ServerId() const { return server_id_; }
void SetServerId(const char* id) { server_id_ = id; }
const std::set<std::string>& ModelRepositoryPaths() const
{
return repo_paths_;
}
void SetModelRepositoryPath(const char* p) { repo_paths_.insert(p); }
tc::ModelControlMode ModelControlMode() const { return model_control_mode_; }
void SetModelControlMode(tc::ModelControlMode m) { model_control_mode_ = m; }
const std::set<std::string>& StartupModels() const { return models_; }
void SetStartupModel(const char* m) { models_.insert(m); }
const std::string& ModelConfigName() const { return model_config_name_; }
void SetModelConfigName(const std::string& name)
{
model_config_name_ = name;
}
bool ExitOnError() const { return exit_on_error_; }
void SetExitOnError(bool b) { exit_on_error_ = b; }
bool StrictModelConfig() const { return strict_model_config_; }
void SetStrictModelConfig(bool b) { strict_model_config_ = b; }
tc::RateLimitMode RateLimiterMode() const { return rate_limit_mode_; }
void SetRateLimiterMode(tc::RateLimitMode m) { rate_limit_mode_ = m; }
TRITONSERVER_Error* AddRateLimiterResource(
const std::string& resource, const size_t count, const int device);
// The resource map is the map from device id to the map of
// of resources with their respective counts for that device.
const tc::RateLimiter::ResourceMap& RateLimiterResources() const
{
return rate_limit_resource_map_;
}
uint64_t PinnedMemoryPoolByteSize() const { return pinned_memory_pool_size_; }
void SetPinnedMemoryPoolByteSize(uint64_t s) { pinned_memory_pool_size_ = s; }
const std::map<int, uint64_t>& CudaMemoryPoolByteSize() const
{
return cuda_memory_pool_size_;
}
void SetCudaMemoryPoolByteSize(int id, uint64_t s)
{
cuda_memory_pool_size_[id] = s;
}
double MinSupportedComputeCapability() const
{
return min_compute_capability_;
}
void SetMinSupportedComputeCapability(double c)
{
min_compute_capability_ = c;
}
const std::map<int, size_t>& CudaVirtualAddressSpaceSize() const
{
return cuda_virtual_address_space_size_;
}
void SetCudaVirtualAddressSpaceSize(int id, size_t virtual_address_space_size)
{
cuda_virtual_address_space_size_[id] = virtual_address_space_size;
}
bool StrictReadiness() const { return strict_readiness_; }
void SetStrictReadiness(bool b) { strict_readiness_ = b; }
unsigned int ExitTimeout() const { return exit_timeout_; }
void SetExitTimeout(unsigned int t) { exit_timeout_ = t; }
unsigned int BufferManagerThreadCount() const
{
return buffer_manager_thread_count_;
}
void SetBufferManagerThreadCount(unsigned int c)
{
buffer_manager_thread_count_ = c;
}
unsigned int ModelLoadThreadCount() const { return model_load_thread_count_; }
void SetModelLoadThreadCount(unsigned int c) { model_load_thread_count_ = c; }
unsigned int ModelLoadRetryCount() const { return model_load_retry_count_; }
void SetModelLoadRetryCount(unsigned int c) { model_load_retry_count_ = c; }
bool ModelNamespacingEnabled() { return enable_model_namespacing_; }
bool PeerAccessEnabled() { return enable_peer_access_; }
void SetModelNamespacingEnabled(const bool e)
{
enable_model_namespacing_ = e;
}
void SetEnablePeerAccess(const bool e) { enable_peer_access_ = e; }
bool Metrics() const { return metrics_; }
void SetMetrics(bool b) { metrics_ = b; }
bool GpuMetrics() const { return gpu_metrics_; }
void SetGpuMetrics(bool b) { gpu_metrics_ = b; }
bool CpuMetrics() const { return cpu_metrics_; }
void SetCpuMetrics(bool b) { cpu_metrics_ = b; }
uint64_t MetricsInterval() const { return metrics_interval_; }
void SetMetricsInterval(uint64_t m) { metrics_interval_ = m; }
#ifdef TRITON_ENABLE_METRICS
const tc::MetricsConfigMap& MetricsConfigMap() { return metrics_config_map_; }
TRITONSERVER_Error* AddMetricsConfig(
const std::string& name, const std::string& setting,
const std::string& value);
#endif // TRITON_ENABLE_METRICS
const std::string& BackendDir() const { return backend_dir_; }
void SetBackendDir(const std::string& bd) { backend_dir_ = bd; }
const std::string& RepoAgentDir() const { return repoagent_dir_; }
void SetRepoAgentDir(const std::string& rad) { repoagent_dir_ = rad; }
// The backend config map is a map from backend name to the
// setting=value pairs for that backend. The empty backend name ("")
// is used to communicate configuration information that is used
// internally.
const triton::common::BackendCmdlineConfigMap& BackendCmdlineConfigMap() const
{
return backend_cmdline_config_map_;
}
TRITONSERVER_Error* AddBackendConfig(
const std::string& backend_name, const std::string& setting,
const std::string& value);
TRITONSERVER_Error* SetHostPolicy(
const std::string& policy_name, const std::string& setting,
const std::string& value);
const triton::common::HostPolicyCmdlineConfigMap& HostPolicyCmdlineConfigMap()
const
{
return host_policy_map_;
}
const tc::CacheConfigMap& CacheConfig() { return cache_config_map_; }
TRITONSERVER_Error* AddCacheConfig(
const std::string& cache_name, const std::string& config_json);
const std::string& CacheDir() const { return cache_dir_; }
void SetCacheDir(const std::string& dir) { cache_dir_ = dir; }
private:
std::string server_id_;
std::set<std::string> repo_paths_;
tc::ModelControlMode model_control_mode_;
std::set<std::string> models_;
bool exit_on_error_;
bool strict_model_config_;
std::string model_config_name_;
bool strict_readiness_;
tc::RateLimitMode rate_limit_mode_;
tc::RateLimiter::ResourceMap rate_limit_resource_map_;
bool metrics_;
bool gpu_metrics_;
bool cpu_metrics_;
uint64_t metrics_interval_;
unsigned int exit_timeout_;
uint64_t pinned_memory_pool_size_;
unsigned int buffer_manager_thread_count_;
unsigned int model_load_thread_count_;
unsigned int model_load_retry_count_;
bool enable_model_namespacing_;
bool enable_peer_access_;
std::map<int, uint64_t> cuda_memory_pool_size_;
double min_compute_capability_;
std::string backend_dir_;
std::string repoagent_dir_;
std::string cache_dir_;
tc::CacheConfigMap cache_config_map_;
triton::common::BackendCmdlineConfigMap backend_cmdline_config_map_;
triton::common::HostPolicyCmdlineConfigMap host_policy_map_;
std::map<int, size_t> cuda_virtual_address_space_size_;
#ifdef TRITON_ENABLE_METRICS
tc::MetricsConfigMap metrics_config_map_;
#endif // TRITON_ENABLE_METRICS
};
TritonServerOptions::TritonServerOptions()
: server_id_("triton"),
model_control_mode_(tc::ModelControlMode::MODE_POLL),
exit_on_error_(true), strict_model_config_(true), model_config_name_(""),
strict_readiness_(true), rate_limit_mode_(tc::RateLimitMode::RL_OFF),
metrics_(true), gpu_metrics_(true), cpu_metrics_(true),
metrics_interval_(2000), exit_timeout_(30),
pinned_memory_pool_size_(1 << 28), buffer_manager_thread_count_(0),
model_load_thread_count_(4), enable_model_namespacing_(false),
#ifdef TRITON_ENABLE_GPU
min_compute_capability_(TRITON_MIN_COMPUTE_CAPABILITY),
#else
min_compute_capability_(0),
#endif // TRITON_ENABLE_GPU
backend_dir_("/opt/tritonserver/backends"),
repoagent_dir_("/opt/tritonserver/repoagents"),
cache_dir_("/opt/tritonserver/caches")
{
#ifndef TRITON_ENABLE_METRICS
metrics_ = false;
gpu_metrics_ = false;
cpu_metrics_ = false;
#endif // TRITON_ENABLE_METRICS
#ifndef TRITON_ENABLE_METRICS_GPU
gpu_metrics_ = false;
#endif // TRITON_ENABLE_METRICS_GPU
#ifndef TRITON_ENABLE_METRICS_CPU
cpu_metrics_ = false;
#endif // TRITON_ENABLE_METRICS_CPU
}
TRITONSERVER_Error*
TritonServerOptions::AddRateLimiterResource(
const std::string& name, const size_t count, const int device)
{
auto ditr = rate_limit_resource_map_.find(device);
if (ditr == rate_limit_resource_map_.end()) {
ditr = rate_limit_resource_map_
.emplace(device, std::map<std::string, size_t>())
.first;
}
auto ritr = ditr->second.find(name);
if (ritr == ditr->second.end()) {
ditr->second.emplace(name, count).first;
} else {
// If already present then store the minimum of the two.
if (ritr->second > count) {
ritr->second = count;
}
}
return nullptr; // success
}
TRITONSERVER_Error*
TritonServerOptions::AddBackendConfig(
const std::string& backend_name, const std::string& setting,
const std::string& value)
{
triton::common::BackendCmdlineConfig& cc =
backend_cmdline_config_map_[backend_name];
cc.push_back(std::make_pair(setting, value));
return nullptr; // success
}
TRITONSERVER_Error*
TritonServerOptions::AddCacheConfig(
const std::string& cache_name, const std::string& config_json)
{
cache_config_map_[cache_name] = config_json;
return nullptr; // success
}
#ifdef TRITON_ENABLE_METRICS
TRITONSERVER_Error*
TritonServerOptions::AddMetricsConfig(
const std::string& name, const std::string& setting,
const std::string& value)
{
tc::MetricsConfig& mc = metrics_config_map_[name];
mc.push_back(std::make_pair(setting, value));
return nullptr; // success
}
#endif // TRITON_ENABLE_METRICS
TRITONSERVER_Error*
TritonServerOptions::SetHostPolicy(
const std::string& policy_name, const std::string& setting,
const std::string& value)
{
// Check if supported setting is passed
if ((setting != "numa-node") && (setting != "cpu-cores")) {
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_UNSUPPORTED,
std::string(
"Unsupported host policy setting '" + setting +
"' is specified, supported settings are 'numa-node', 'cpu-cores'")
.c_str());
}
triton::common::HostPolicyCmdlineConfig& hp = host_policy_map_[policy_name];
hp[setting] = value;
return nullptr; // success
}
#define SetDurationStat(DOC, PARENT, STAT_NAME, COUNT, NS) \
do { \
triton::common::TritonJson::Value dstat( \
DOC, triton::common::TritonJson::ValueType::OBJECT); \
dstat.AddUInt("count", (COUNT)); \
dstat.AddUInt("ns", (NS)); \
PARENT.Add(STAT_NAME, std::move(dstat)); \
} while (false)
} // namespace
extern "C" {
//
// TRITONSERVER API Version
//
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_ApiVersion(uint32_t* major, uint32_t* minor)
{
*major = TRITONSERVER_API_VERSION_MAJOR;
*minor = TRITONSERVER_API_VERSION_MINOR;
return nullptr; // success
}
//
// TRITONSERVER_DataType
//
TRITONAPI_DECLSPEC const char*
TRITONSERVER_DataTypeString(TRITONSERVER_DataType datatype)
{
switch (datatype) {
case TRITONSERVER_TYPE_BOOL:
return "BOOL";
case TRITONSERVER_TYPE_UINT8:
return "UINT8";
case TRITONSERVER_TYPE_UINT16:
return "UINT16";
case TRITONSERVER_TYPE_UINT32:
return "UINT32";
case TRITONSERVER_TYPE_UINT64:
return "UINT64";
case TRITONSERVER_TYPE_INT8:
return "INT8";
case TRITONSERVER_TYPE_INT16:
return "INT16";
case TRITONSERVER_TYPE_INT32:
return "INT32";
case TRITONSERVER_TYPE_INT64:
return "INT64";
case TRITONSERVER_TYPE_FP16:
return "FP16";
case TRITONSERVER_TYPE_FP32:
return "FP32";
case TRITONSERVER_TYPE_FP64:
return "FP64";
case TRITONSERVER_TYPE_BYTES:
return "BYTES";
case TRITONSERVER_TYPE_BF16:
return "BF16";
default:
break;
}
return "<invalid>";
}
TRITONAPI_DECLSPEC TRITONSERVER_DataType
TRITONSERVER_StringToDataType(const char* dtype)
{
const size_t len = strlen(dtype);
return tc::DataTypeToTriton(
triton::common::ProtocolStringToDataType(dtype, len));
}
TRITONAPI_DECLSPEC uint32_t
TRITONSERVER_DataTypeByteSize(TRITONSERVER_DataType datatype)
{
switch (datatype) {
case TRITONSERVER_TYPE_BOOL:
case TRITONSERVER_TYPE_INT8:
case TRITONSERVER_TYPE_UINT8:
return 1;
case TRITONSERVER_TYPE_INT16:
case TRITONSERVER_TYPE_UINT16:
case TRITONSERVER_TYPE_FP16:
case TRITONSERVER_TYPE_BF16:
return 2;
case TRITONSERVER_TYPE_INT32:
case TRITONSERVER_TYPE_UINT32:
case TRITONSERVER_TYPE_FP32:
return 4;
case TRITONSERVER_TYPE_INT64:
case TRITONSERVER_TYPE_UINT64:
case TRITONSERVER_TYPE_FP64:
return 8;
case TRITONSERVER_TYPE_BYTES:
return 0;
default:
break;
}
return 0;
}
//
// TRITONSERVER_MemoryType
//
TRITONAPI_DECLSPEC const char*
TRITONSERVER_MemoryTypeString(TRITONSERVER_MemoryType memtype)
{
switch (memtype) {
case TRITONSERVER_MEMORY_CPU:
return "CPU";
case TRITONSERVER_MEMORY_CPU_PINNED:
return "CPU_PINNED";
case TRITONSERVER_MEMORY_GPU:
return "GPU";
default:
break;
}
return "<invalid>";
}
//
// TRITONSERVER_Parameter
//
TRITONAPI_DECLSPEC const char*
TRITONSERVER_ParameterTypeString(TRITONSERVER_ParameterType paramtype)
{
switch (paramtype) {
case TRITONSERVER_PARAMETER_STRING:
return "STRING";
case TRITONSERVER_PARAMETER_INT:
return "INT";
case TRITONSERVER_PARAMETER_BOOL:
return "BOOL";
case TRITONSERVER_PARAMETER_DOUBLE:
return "DOUBLE";
case TRITONSERVER_PARAMETER_BYTES:
return "BYTES";
default:
break;
}
return "<invalid>";
}
TRITONAPI_DECLSPEC TRITONSERVER_Parameter*
TRITONSERVER_ParameterNew(
const char* name, const TRITONSERVER_ParameterType type, const void* value)
{
std::unique_ptr<tc::InferenceParameter> lparam;
switch (type) {
case TRITONSERVER_PARAMETER_STRING:
lparam.reset(new tc::InferenceParameter(
name, reinterpret_cast<const char*>(value)));
break;
case TRITONSERVER_PARAMETER_INT:
lparam.reset(new tc::InferenceParameter(
name, *reinterpret_cast<const int64_t*>(value)));
break;
case TRITONSERVER_PARAMETER_DOUBLE:
lparam.reset(new tc::InferenceParameter(
name, *reinterpret_cast<const double*>(value)));
break;
case TRITONSERVER_PARAMETER_BOOL:
lparam.reset(new tc::InferenceParameter(
name, *reinterpret_cast<const bool*>(value)));
break;
default:
break;
}
return reinterpret_cast<TRITONSERVER_Parameter*>(lparam.release());
}
TRITONAPI_DECLSPEC TRITONSERVER_Parameter*
TRITONSERVER_ParameterBytesNew(
const char* name, const void* byte_ptr, const uint64_t size)
{
std::unique_ptr<tc::InferenceParameter> lparam(
new tc::InferenceParameter(name, byte_ptr, size));
return reinterpret_cast<TRITONSERVER_Parameter*>(lparam.release());
}
TRITONAPI_DECLSPEC void
TRITONSERVER_ParameterDelete(TRITONSERVER_Parameter* parameter)
{
delete reinterpret_cast<tc::InferenceParameter*>(parameter);
}
//
// TRITONSERVER_InstanceGroupKind
//
TRITONAPI_DECLSPEC const char*
TRITONSERVER_InstanceGroupKindString(TRITONSERVER_InstanceGroupKind kind)
{
switch (kind) {
case TRITONSERVER_INSTANCEGROUPKIND_AUTO:
return "AUTO";
case TRITONSERVER_INSTANCEGROUPKIND_CPU:
return "CPU";
case TRITONSERVER_INSTANCEGROUPKIND_GPU:
return "GPU";
case TRITONSERVER_INSTANCEGROUPKIND_MODEL:
return "MODEL";
default:
break;
}
return "<invalid>";
}
//
// TRITONSERVER_Log
//
TRITONAPI_DECLSPEC bool
TRITONSERVER_LogIsEnabled(TRITONSERVER_LogLevel level)
{
switch (level) {
case TRITONSERVER_LOG_INFO:
return LOG_INFO_IS_ON;
case TRITONSERVER_LOG_WARN:
return LOG_WARNING_IS_ON;
case TRITONSERVER_LOG_ERROR:
return LOG_ERROR_IS_ON;
case TRITONSERVER_LOG_VERBOSE:
return LOG_VERBOSE_IS_ON(1);
}
return false;
}
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_LogMessage(
TRITONSERVER_LogLevel level, const char* filename, const int line,
const char* msg)
{
switch (level) {
case TRITONSERVER_LOG_INFO:
LOG_INFO_FL(filename, line) << msg;
return nullptr;
case TRITONSERVER_LOG_WARN:
LOG_WARNING_FL(filename, line) << msg;
return nullptr;
case TRITONSERVER_LOG_ERROR:
LOG_ERROR_FL(filename, line) << msg;
return nullptr;
case TRITONSERVER_LOG_VERBOSE:
LOG_VERBOSE_FL(1, filename, line) << msg;
return nullptr;
default:
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_INVALID_ARG,
std::string("unknown logging level '" + std::to_string(level) + "'")
.c_str());
}
}
//
// TRITONSERVER_Error
//
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_ErrorNew(TRITONSERVER_Error_Code code, const char* msg)
{
return reinterpret_cast<TRITONSERVER_Error*>(
TritonServerError::Create(code, msg));
}
TRITONAPI_DECLSPEC void
TRITONSERVER_ErrorDelete(TRITONSERVER_Error* error)
{
TritonServerError* lerror = reinterpret_cast<TritonServerError*>(error);
delete lerror;
}
TRITONSERVER_Error_Code
TRITONSERVER_ErrorCode(TRITONSERVER_Error* error)
{
TritonServerError* lerror = reinterpret_cast<TritonServerError*>(error);
return lerror->Code();
}
TRITONAPI_DECLSPEC const char*
TRITONSERVER_ErrorCodeString(TRITONSERVER_Error* error)
{
TritonServerError* lerror = reinterpret_cast<TritonServerError*>(error);
return tc::Status::CodeString(tc::TritonCodeToStatusCode(lerror->Code()));
}
TRITONAPI_DECLSPEC const char*
TRITONSERVER_ErrorMessage(TRITONSERVER_Error* error)
{
TritonServerError* lerror = reinterpret_cast<TritonServerError*>(error);
return lerror->Message().c_str();
}
//
// TRITONSERVER_ResponseAllocator
//
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_ResponseAllocatorNew(
TRITONSERVER_ResponseAllocator** allocator,
TRITONSERVER_ResponseAllocatorAllocFn_t alloc_fn,
TRITONSERVER_ResponseAllocatorReleaseFn_t release_fn,
TRITONSERVER_ResponseAllocatorStartFn_t start_fn)
{
*allocator = reinterpret_cast<TRITONSERVER_ResponseAllocator*>(
new tc::ResponseAllocator(alloc_fn, release_fn, start_fn));
return nullptr; // Success
}
TRITONSERVER_Error*
TRITONSERVER_ResponseAllocatorSetQueryFunction(
TRITONSERVER_ResponseAllocator* allocator,
TRITONSERVER_ResponseAllocatorQueryFn_t query_fn)
{
reinterpret_cast<tc::ResponseAllocator*>(allocator)->SetQueryFunction(
query_fn);
return nullptr; // success
}
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_ResponseAllocatorSetBufferAttributesFunction(
TRITONSERVER_ResponseAllocator* allocator,
TRITONSERVER_ResponseAllocatorBufferAttributesFn_t buffer_attributes_fn)
{
reinterpret_cast<tc::ResponseAllocator*>(allocator)
->SetBufferAttributesFunction(buffer_attributes_fn);
return nullptr; // success
}
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_ResponseAllocatorDelete(TRITONSERVER_ResponseAllocator* allocator)
{
tc::ResponseAllocator* lalloc =
reinterpret_cast<tc::ResponseAllocator*>(allocator);
delete lalloc;
return nullptr; // Success
}
//
// TRITONSERVER_Message
//
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_MessageNewFromSerializedJson(
TRITONSERVER_Message** message, const char* base, size_t byte_size)
{
*message = reinterpret_cast<TRITONSERVER_Message*>(
new tc::TritonServerMessage({base, byte_size}));
return nullptr;
}
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_MessageDelete(TRITONSERVER_Message* message)
{
tc::TritonServerMessage* lmessage =
reinterpret_cast<tc::TritonServerMessage*>(message);
delete lmessage;
return nullptr; // Success
}
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_MessageSerializeToJson(
TRITONSERVER_Message* message, const char** base, size_t* byte_size)
{
tc::TritonServerMessage* lmessage =
reinterpret_cast<tc::TritonServerMessage*>(message);
lmessage->Serialize(base, byte_size);
return nullptr; // Success
}
//
// TRITONSERVER_Metrics
//
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_MetricsDelete(TRITONSERVER_Metrics* metrics)
{
TritonServerMetrics* lmetrics =
reinterpret_cast<TritonServerMetrics*>(metrics);
delete lmetrics;
return nullptr; // Success
}
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_MetricsFormatted(
TRITONSERVER_Metrics* metrics, TRITONSERVER_MetricFormat format,
const char** base, size_t* byte_size)
{
TritonServerMetrics* lmetrics =
reinterpret_cast<TritonServerMetrics*>(metrics);
switch (format) {
case TRITONSERVER_METRIC_PROMETHEUS: {
return lmetrics->Serialize(base, byte_size);
}
default:
break;
}
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_INVALID_ARG,
std::string("unknown metrics format '" + std::to_string(format) + "'")
.c_str());
}
//
// TRITONSERVER_InferenceTrace
//
TRITONAPI_DECLSPEC const char*
TRITONSERVER_InferenceTraceLevelString(TRITONSERVER_InferenceTraceLevel level)
{
switch (level) {
case TRITONSERVER_TRACE_LEVEL_DISABLED:
return "DISABLED";
case TRITONSERVER_TRACE_LEVEL_MIN:
return "MIN";
case TRITONSERVER_TRACE_LEVEL_MAX:
return "MAX";
case TRITONSERVER_TRACE_LEVEL_TIMESTAMPS:
return "TIMESTAMPS";
case TRITONSERVER_TRACE_LEVEL_TENSORS:
return "TENSORS";
}
return "<unknown>";
}
TRITONAPI_DECLSPEC const char*
TRITONSERVER_InferenceTraceActivityString(
TRITONSERVER_InferenceTraceActivity activity)
{
switch (activity) {
case TRITONSERVER_TRACE_REQUEST_START:
return "REQUEST_START";
case TRITONSERVER_TRACE_QUEUE_START:
return "QUEUE_START";
case TRITONSERVER_TRACE_COMPUTE_START:
return "COMPUTE_START";
case TRITONSERVER_TRACE_COMPUTE_INPUT_END:
return "COMPUTE_INPUT_END";
case TRITONSERVER_TRACE_COMPUTE_OUTPUT_START:
return "COMPUTE_OUTPUT_START";
case TRITONSERVER_TRACE_COMPUTE_END:
return "COMPUTE_END";
case TRITONSERVER_TRACE_REQUEST_END:
return "REQUEST_END";
case TRITONSERVER_TRACE_TENSOR_QUEUE_INPUT:
return "TENSOR_QUEUE_INPUT";
case TRITONSERVER_TRACE_TENSOR_BACKEND_INPUT:
return "TENSOR_BACKEND_INPUT";
case TRITONSERVER_TRACE_TENSOR_BACKEND_OUTPUT:
return "TENSOR_BACKEND_OUTPUT";
case TRITONSERVER_TRACE_CUSTOM_ACTIVITY:
return "CUSTOM_ACTIVITY";
}
return "<unknown>";
}
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_InferenceTraceNew(
TRITONSERVER_InferenceTrace** trace, TRITONSERVER_InferenceTraceLevel level,
uint64_t parent_id, TRITONSERVER_InferenceTraceActivityFn_t activity_fn,
TRITONSERVER_InferenceTraceReleaseFn_t release_fn, void* trace_userp)
{
#ifdef TRITON_ENABLE_TRACING
if ((level & TRITONSERVER_TRACE_LEVEL_MIN) > 0) {
level = static_cast<TRITONSERVER_InferenceTraceLevel>(
(level ^ TRITONSERVER_TRACE_LEVEL_MIN) |
TRITONSERVER_TRACE_LEVEL_TIMESTAMPS);
}
if ((level & TRITONSERVER_TRACE_LEVEL_MAX) > 0) {
level = static_cast<TRITONSERVER_InferenceTraceLevel>(
(level ^ TRITONSERVER_TRACE_LEVEL_MAX) |
TRITONSERVER_TRACE_LEVEL_TIMESTAMPS);
}
tc::InferenceTrace* ltrace = new tc::InferenceTrace(
level, parent_id, activity_fn, nullptr, release_fn, trace_userp);
*trace = reinterpret_cast<TRITONSERVER_InferenceTrace*>(ltrace);
return nullptr; // Success
#else
*trace = nullptr;
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_UNSUPPORTED, "inference tracing not supported");
#endif // TRITON_ENABLE_TRACING
}
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_InferenceTraceTensorNew(
TRITONSERVER_InferenceTrace** trace, TRITONSERVER_InferenceTraceLevel level,
uint64_t parent_id, TRITONSERVER_InferenceTraceActivityFn_t activity_fn,
TRITONSERVER_InferenceTraceTensorActivityFn_t tensor_activity_fn,
TRITONSERVER_InferenceTraceReleaseFn_t release_fn, void* trace_userp)
{
#ifdef TRITON_ENABLE_TRACING
if ((level & TRITONSERVER_TRACE_LEVEL_MIN) > 0) {
level = static_cast<TRITONSERVER_InferenceTraceLevel>(
(level ^ TRITONSERVER_TRACE_LEVEL_MIN) |
TRITONSERVER_TRACE_LEVEL_TIMESTAMPS);
}