-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathmodel_config_utils.cc
2514 lines (2276 loc) · 85.6 KB
/
model_config_utils.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 2018-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 "model_config_utils.h"
#include <google/protobuf/util/json_util.h>
#include <google/protobuf/util/message_differencer.h>
#include <deque>
#include <mutex>
#include <set>
#include "constants.h"
#include "cuda_utils.h"
#include "filesystem/api.h"
#include "triton/common/logging.h"
#define TRITONJSON_STATUSTYPE triton::core::Status
#define TRITONJSON_STATUSRETURN(M) \
return triton::core::Status(triton::core::Status::Code::INTERNAL, (M))
#define TRITONJSON_STATUSSUCCESS triton::core::Status::Success
#include "triton/common/triton_json.h"
#ifdef TRITON_ENABLE_GPU
#include <cuda_runtime_api.h>
#endif // TRITON_ENABLE_GPU
namespace triton { namespace core {
namespace {
#ifdef TRITON_ENABLE_ENSEMBLE
struct EnsembleTensor {
EnsembleTensor(const std::string& name, bool isOutput)
: name{name}, isOutput(isOutput)
{
}
const std::string name;
bool isOutput{false};
bool ready{false};
std::vector<EnsembleTensor*> prev_nodes;
std::vector<EnsembleTensor*> next_nodes;
};
/// Build a graph that represents the data flow in the ensemble specified in
/// given model config. the node (ensemble tensor) in the graph can be looked
/// up using its name as key.
/// \param ensemble_config The model configuration that specifies
/// ensemble_scheduling field.
/// \param keyed_ensemble_graph Returned the ensemble graph.
/// \return The error status. A non-OK status indicates the build fails because
/// the ensemble configuration is not valid.
Status
BuildEnsembleGraph(
const inference::ModelConfig& config,
std::unordered_map<std::string, EnsembleTensor>& keyed_ensemble_graph)
{
keyed_ensemble_graph.clear();
size_t step_idx = 0;
for (const auto& element : config.ensemble_scheduling().step()) {
if (element.model_name().empty()) {
return Status(
Status::Code::INVALID_ARG,
"must specify 'model_name' in step " + std::to_string(step_idx) +
" of ensemble '" + config.name() + "'");
}
if (element.input_map().size() == 0) {
return Status(
Status::Code::INVALID_ARG,
"must specify 'input_map' in step " + std::to_string(step_idx) +
" of ensemble '" + config.name() + "'");
}
if (element.output_map().size() == 0) {
return Status(
Status::Code::INVALID_ARG,
"must specify 'output_map' in step " + std::to_string(step_idx) +
" of ensemble '" + config.name() + "'");
}
// Link ensemble tensors
std::vector<EnsembleTensor*> tensor_as_output;
for (const auto& output_map : element.output_map()) {
auto it = keyed_ensemble_graph.find(output_map.second);
if (it != keyed_ensemble_graph.end()) {
if (it->second.isOutput) {
return Status(
Status::Code::INVALID_ARG,
"ensemble tensor '" + it->first +
"' can appear in an output map only once for ensemble '" +
config.name() + "' step " + std::to_string(step_idx));
} else {
it->second.isOutput = true;
}
} else {
it =
keyed_ensemble_graph
.emplace(std::make_pair(
output_map.second, EnsembleTensor(output_map.second, true)))
.first;
}
tensor_as_output.push_back(&(it->second));
}
std::set<std::string> model_inputs;
for (const auto& input_map : element.input_map()) {
if (model_inputs.find(input_map.first) != model_inputs.end()) {
return Status(
Status::Code::INVALID_ARG,
"input '" + input_map.first + "' in model '" +
element.model_name() +
"' is mapped to multiple ensemble tensors for ensemble '" +
config.name() + "' step " + std::to_string(step_idx));
} else {
model_inputs.emplace(input_map.first);
}
auto it = keyed_ensemble_graph.find(input_map.second);
if (it == keyed_ensemble_graph.end()) {
it = keyed_ensemble_graph
.emplace(std::make_pair(
input_map.second, EnsembleTensor(input_map.second, false)))
.first;
}
for (auto output : tensor_as_output) {
output->prev_nodes.push_back(&(it->second));
it->second.next_nodes.push_back(output);
}
}
step_idx++;
}
return Status::Success;
}
Status
ValidateEnsembleSchedulingConfig(const inference::ModelConfig& config)
{
if (config.platform() != kEnsemblePlatform) {
return Status(
Status::Code::INVALID_ARG,
"ensemble scheduling cannot be set for model '" + config.name() +
"' whose platform is not " + kEnsemblePlatform);
}
if (config.instance_group().size() != 0) {
return Status(
Status::Code::INVALID_ARG,
"instance group should not be specified for ensemble '" +
config.name() + "'");
}
if (config.has_optimization()) {
return Status(
Status::Code::INVALID_ARG,
"optimization should not be specified for ensemble '" + config.name() +
"'");
}
if (config.model_warmup_size() != 0) {
return Status(
Status::Code::INVALID_ARG,
"model_warmup can not be specified for ensemble '" + config.name() +
"'");
}
// Make sure step is not empty and all fields are set
if (config.ensemble_scheduling().step_size() == 0) {
return Status(
Status::Code::INVALID_ARG,
"must specify 'step' for ensemble '" + config.name() + "'");
}
std::unordered_map<std::string, EnsembleTensor> tensors;
RETURN_IF_ERROR(BuildEnsembleGraph(config, tensors));
// check data flow
std::deque<EnsembleTensor*> ready_queue;
for (const auto& input : config.input()) {
auto it = tensors.find(input.name());
if (it == tensors.end()) {
return Status(
Status::Code::INVALID_ARG, "ensemble input '" + input.name() +
"' for ensemble " + config.name() +
"' is not used");
}
it->second.ready = true;
ready_queue.push_back(&(it->second));
}
while (!ready_queue.empty()) {
auto& ready_node = ready_queue.front();
for (auto& next_node : ready_node->next_nodes) {
if (next_node->ready) {
continue;
}
bool next_node_ready = true;
for (auto& prev_node : next_node->prev_nodes) {
if (!prev_node->ready) {
next_node_ready = false;
break;
}
}
next_node->ready = next_node_ready;
if (next_node_ready) {
ready_queue.push_back(next_node);
}
}
ready_queue.pop_front();
}
std::set<std::string> outputs;
for (const auto& output : config.output()) {
auto it = tensors.find(output.name());
if (it == tensors.end()) {
return Status(
Status::Code::INVALID_ARG, "ensemble output '" + output.name() +
"' for ensemble " + config.name() +
"' is not used");
}
if (!it->second.ready) {
std::string error_message = "output '" + output.name() +
"' for ensemble '" + config.name() +
"' is not written";
// recurrsively check 'prev_nodes' for the source of not-ready state
std::vector<EnsembleTensor*>* prev_nodes = &it->second.prev_nodes;
auto last_not_ready_node = &it->second;
// there can be circular dependency so remember seen names to break it
std::set<std::string> seen_names;
while ((prev_nodes != nullptr) && (!prev_nodes->empty())) {
const auto& nodes = *prev_nodes;
// make sure while loop will terminate if no not-ready source is seen
prev_nodes = nullptr;
for (const auto& node : nodes) {
if ((!node->ready) &&
(seen_names.find(node->name) == seen_names.end())) {
seen_names.emplace(node->name);
last_not_ready_node = node;
prev_nodes = &node->prev_nodes;
break;
}
}
}
// there is not-ready source
if (last_not_ready_node->name != it->second.name) {
error_message += ": at least one of its depending tensors, '" +
last_not_ready_node->name + "', is not connected";
}
return Status(Status::Code::INVALID_ARG, error_message);
} else {
outputs.insert(it->first);
}
}
// Check redundant ensemble tensors
for (const auto& tensor : tensors) {
// skip ensemble outputs as they have been checked and can have no
// next nodes
if (outputs.find(tensor.first) != outputs.end()) {
continue;
}
if (!tensor.second.ready || (tensor.second.next_nodes.size() == 0)) {
return Status(
Status::Code::INVALID_ARG, "ensemble tensor '" + tensor.first +
"' is unused in ensemble '" +
config.name() + "'");
}
}
return Status::Success;
}
#endif // TRITON_ENABLE_ENSEMBLE
template <class ModelIO>
Status
ValidateIOShape(
const ModelIO& io, int32_t max_batch_size,
const std::string& message_prefix = "")
{
if (io.name().empty()) {
return Status(
Status::Code::INVALID_ARG, message_prefix + "must specify 'name'");
}
std::string message_prefix_with_name =
message_prefix + std::string("'" + io.name() + "' ");
if (io.data_type() == inference::DataType::TYPE_INVALID) {
return Status(
Status::Code::INVALID_ARG,
message_prefix_with_name + "must specify 'data_type'");
}
if (io.dims_size() == 0) {
return Status(
Status::Code::INVALID_ARG,
message_prefix_with_name + "must specify 'dims'");
}
// If the configuration is non-batching, then no input or output
// reshape can be empty as that would mean that input or output was
// always empty (no data).
if (io.has_reshape() && (io.reshape().shape_size() == 0) &&
(max_batch_size == 0)) {
return Status(
Status::Code::INVALID_ARG,
message_prefix_with_name +
"cannot have empty reshape for non-batching model as scalar "
"tensors are not supported");
}
for (auto dim : io.dims()) {
// Dimension cannot be 0.
if ((dim < 1) && (dim != triton::common::WILDCARD_DIM)) {
return Status(
Status::Code::INVALID_ARG,
message_prefix_with_name + "dimension must be integer >= 1, or " +
std::to_string(triton::common::WILDCARD_DIM) +
" to indicate a variable-size dimension");
}
}
if (io.has_reshape()) {
// Zeros are not allowed in reshape.
for (auto dim : io.reshape().shape()) {
if ((dim < 1) && (dim != triton::common::WILDCARD_DIM)) {
return Status(
Status::Code::INVALID_ARG,
message_prefix_with_name +
"reshape dimensions must be integer >= 1, or " +
std::to_string(triton::common::WILDCARD_DIM) +
" to indicate a variable-size dimension");
}
}
const int64_t dims_size = triton::common::GetElementCount(io.dims());
const int64_t reshape_size =
triton::common::GetElementCount(io.reshape().shape());
// dims and reshape must both have same element count
// or both have variable-size dimension.
// Special case for empty reshape... expect dims to have element
// count of 1.
if ((dims_size != reshape_size) &&
((reshape_size != 0) || (dims_size != 1))) {
return Status(
Status::Code::INVALID_ARG,
message_prefix_with_name + "has different size for dims and reshape");
}
// shape contains variable-size dimension, in this case we compare if
// each pair of the trunks separated by variable-size dimension has
// the same element count. For instance, from [2, 4, -1, 6] to [8, -1, 1, 6]
// is valid reshape as 2 * 4 = 8 and 6 = 1 * 6.
if (dims_size == -1) {
std::vector<int64_t> dim_element_cnts;
std::vector<int64_t> reshape_element_cnts;
int64_t current_cnt = 1;
for (const auto& dim : io.dims()) {
if (dim != -1) {
current_cnt *= dim;
} else {
dim_element_cnts.push_back(current_cnt);
current_cnt = 1;
}
}
dim_element_cnts.push_back(current_cnt);
current_cnt = 1;
for (const auto& dim : io.reshape().shape()) {
if (dim != -1) {
current_cnt *= dim;
} else {
reshape_element_cnts.push_back(current_cnt);
current_cnt = 1;
}
}
reshape_element_cnts.push_back(current_cnt);
if (dim_element_cnts.size() != reshape_element_cnts.size()) {
return Status(
Status::Code::INVALID_ARG,
message_prefix_with_name +
"has different number of variable-size dimensions for dims "
"and reshape");
}
for (size_t idx = 0; idx < dim_element_cnts.size(); idx++) {
if (dim_element_cnts[idx] != reshape_element_cnts[idx]) {
return Status(
Status::Code::INVALID_ARG,
message_prefix_with_name +
"has different size for dims and reshape");
}
}
}
}
return Status::Success;
}
/// Validate that Non-linear format inputs or outputs are specified correctly
/// in a model configuration.
template <class ModelIO>
Status
ValidateNonLinearFormatIO(
const ModelIO& io, const std::string& platform, bool is_input)
{
if (!io.is_non_linear_format_io()) {
// Nothing to validate as the tensor is not non-linear format.
return Status::Success;
}
if (platform != kTensorRTPlanPlatform) {
return Status(
Status::Code::INVALID_ARG,
"Non-linear IO format is only supported for the TensorRT platform");
}
if (io.dims_size() != 3) {
std::string io_type = is_input ? "input" : "output";
return Status(
Status::Code::INVALID_ARG,
"Non-linear IO format " + io_type + " requires 3 dims");
}
return Status::Success;
}
#ifdef TRITON_ENABLE_METRICS
// Helper function to validate that model_metrics contains all required data.
Status
ValidateModelMetrics(const inference::ModelMetrics& model_metrics)
{
for (const auto& metric_control : model_metrics.metric_control()) {
if (!metric_control.has_metric_identifier()) {
return Status(
Status::Code::INVALID_ARG,
"metric control must specify 'metric_identifier'");
}
if (metric_control.metric_identifier().family().empty()) {
return Status(
Status::Code::INVALID_ARG,
"metric identifier must specify non-empty 'family'");
}
if (!metric_control.has_histogram_options()) {
return Status(
Status::Code::INVALID_ARG,
"metric control must specify 'histogram_options'");
}
if (metric_control.histogram_options().buckets_size() == 0) {
return Status(
Status::Code::INVALID_ARG,
"histogram options must specify non-empty 'buckets'");
}
}
return Status::Success;
}
#endif // TRITON_ENABLE_METRICS
} // namespace
Status
GetModelVersionFromPath(const std::string& path, int64_t* version)
{
auto version_dir = BaseName(path);
// Determine the version from the last segment of 'path'
try {
*version = std::atoll(version_dir.c_str());
}
catch (...) {
return Status(
Status::Code::INTERNAL,
"unable to determine model version from " + path);
}
return Status::Success;
}
Status
GetBooleanSequenceControlProperties(
const inference::ModelSequenceBatching& batcher,
const std::string& model_name,
const inference::ModelSequenceBatching::Control::Kind control_kind,
const bool required, std::string* tensor_name,
inference::DataType* tensor_datatype, float* fp32_false_value,
float* fp32_true_value, int32_t* int32_false_value,
int32_t* int32_true_value, bool* bool_false_value, bool* bool_true_value)
{
// Make sure same tensor is not configured for multiple controls
std::set<std::string> seen_tensors;
// Make sure the control kind is not mentioned multiple times.
bool seen_control = false;
for (const auto& control_input : batcher.control_input()) {
if (control_input.name().empty()) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching control tensor must have a name for " +
model_name);
}
if (seen_tensors.find(control_input.name()) != seen_tensors.end()) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching control tensor '" + control_input.name() +
"' is specified for multiple control kinds for " + model_name);
}
seen_tensors.insert(control_input.name());
for (const auto& c : control_input.control()) {
if (c.kind() == control_kind) {
if (seen_control) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching specifies multiple " +
inference::ModelSequenceBatching_Control_Kind_Name(
control_kind) +
" tensors for " + model_name);
}
*tensor_name = control_input.name();
seen_control = true;
// Make sure only one of int, float, or bool type is specified.
if (!((c.int32_false_true_size() != 0) ||
(c.fp32_false_true_size() != 0) ||
(c.bool_false_true_size() != 0))) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching must specify either 'int32_false_true', "
"'fp32_false_true' or 'bool_false_true' for " +
inference::ModelSequenceBatching_Control_Kind_Name(
control_kind) +
" for " + model_name);
} else if (
((c.int32_false_true_size() != 0) &&
(c.fp32_false_true_size() != 0)) ||
((c.int32_false_true_size() != 0) &&
(c.bool_false_true_size() != 0)) ||
((c.fp32_false_true_size() != 0) &&
(c.bool_false_true_size() != 0))) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching specifies more than one from "
"'int32_false_true', 'fp32_false_true' and 'bool_false_true' "
"for " +
inference::ModelSequenceBatching_Control_Kind_Name(
control_kind) +
" for " + model_name);
}
if (c.int32_false_true_size() > 0) {
if (c.int32_false_true_size() != 2) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching control 'int32_false_true' must have "
"exactly 2 entries for " +
inference::ModelSequenceBatching_Control_Kind_Name(
control_kind) +
" for " + model_name);
}
if (tensor_datatype != nullptr) {
*tensor_datatype = inference::DataType::TYPE_INT32;
}
if (int32_false_value != nullptr) {
*int32_false_value = c.int32_false_true(0);
}
if (int32_true_value != nullptr) {
*int32_true_value = c.int32_false_true(1);
}
} else if (c.fp32_false_true_size() > 0) {
if (c.fp32_false_true_size() != 2) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching control 'fp32_false_true' must have exactly "
"2 entries for " +
inference::ModelSequenceBatching_Control_Kind_Name(
control_kind) +
" for " + model_name);
}
if (tensor_datatype != nullptr) {
*tensor_datatype = inference::DataType::TYPE_FP32;
}
if (fp32_false_value != nullptr) {
*fp32_false_value = c.fp32_false_true(0);
}
if (fp32_true_value != nullptr) {
*fp32_true_value = c.fp32_false_true(1);
}
} else {
if (c.bool_false_true_size() != 2) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching control 'bool_false_true' must have exactly "
"2 entries for " +
inference::ModelSequenceBatching_Control_Kind_Name(
control_kind) +
" for " + model_name);
}
if (tensor_datatype != nullptr) {
*tensor_datatype = inference::DataType::TYPE_BOOL;
}
if (bool_false_value != nullptr) {
*bool_false_value = c.bool_false_true(0);
}
if (bool_true_value != nullptr) {
*bool_true_value = c.bool_false_true(1);
}
}
}
}
}
if (!seen_control) {
if (required) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching control tensor must specify a " +
inference::ModelSequenceBatching_Control_Kind_Name(control_kind) +
" value for " + model_name);
}
tensor_name->clear();
}
return Status::Success;
}
Status
GetTypedSequenceControlProperties(
const inference::ModelSequenceBatching& batcher,
const std::string& model_name,
const inference::ModelSequenceBatching::Control::Kind control_kind,
const bool required, std::string* tensor_name,
inference::DataType* tensor_datatype)
{
// Make sure same tensor is not configured for multiple controls
std::set<std::string> seen_tensors;
// Make sure the control kind is not mentioned multiple times.
bool seen_control = false;
for (const auto& control_input : batcher.control_input()) {
if (control_input.name().empty()) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching control tensor must have a name for " +
model_name);
}
if (seen_tensors.find(control_input.name()) != seen_tensors.end()) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching control tensor '" + control_input.name() +
"' is specified for multiple control kinds for " + model_name);
}
seen_tensors.insert(control_input.name());
for (const auto& c : control_input.control()) {
if (c.kind() == control_kind) {
if (seen_control) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching specifies multiple " +
inference::ModelSequenceBatching_Control_Kind_Name(
control_kind) +
" tensors for " + model_name);
}
*tensor_name = control_input.name();
if (tensor_datatype != nullptr) {
*tensor_datatype = c.data_type();
}
seen_control = true;
if ((c.int32_false_true_size() > 0) || (c.fp32_false_true_size() > 0) ||
(c.bool_false_true_size() > 0)) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching must not specify either 'int32_false_true', "
"'fp32_false_true' or 'bool_false_true' for " +
inference::ModelSequenceBatching_Control_Kind_Name(
control_kind) +
" for " + model_name);
}
}
}
}
if (!seen_control) {
if (required) {
return Status(
Status::Code::INVALID_ARG,
"sequence batching control tensor must specify a " +
inference::ModelSequenceBatching_Control_Kind_Name(control_kind) +
" value for " + model_name);
}
tensor_name->clear();
}
return Status::Success;
}
Status
GetNormalizedModelConfig(
const std::string& model_name, const std::string& path,
const double min_compute_capability, inference::ModelConfig* config)
{
// Server-side autofill only sets certain backend fields for the models that
// belong to limited backends for backwards-compatibility. See TensorRT
// backend, ONNX Runtime backend, OpenVINO backend, TensorFLow backend, and
// PyTorch backend.
// Extracting detailed information is delegated to the backend implementation
// to auto-complete.
RETURN_IF_ERROR(
AutoCompleteBackendFields(model_name, std::string(path), config));
LOG_PROTOBUF_VERBOSE(1, "Server side auto-completed config: ", (*config));
RETURN_IF_ERROR(NormalizeModelConfig(min_compute_capability, config));
return Status::Success;
}
Status
NormalizeModelConfig(
const double min_compute_capability, inference::ModelConfig* config)
{
// If version_policy is not specified, default to Latest 1 version.
if (!config->has_version_policy()) {
inference::ModelVersionPolicy::Latest latest;
latest.set_num_versions(1);
config->mutable_version_policy()->mutable_latest()->CopyFrom(latest);
}
// If dynamic batching is specified...
if (config->has_dynamic_batching()) {
// If preferred batch size is not specified set it to
// max-batch-size.
if (config->dynamic_batching().preferred_batch_size().size() == 0) {
auto mutable_preferred_batch_size =
config->mutable_dynamic_batching()->mutable_preferred_batch_size();
if (config->max_batch_size() > 0) {
mutable_preferred_batch_size->Add(config->max_batch_size());
}
}
}
// If sequence batching is specified...
if (config->has_sequence_batching()) {
// Set default idle is not specified.
if (config->sequence_batching().max_sequence_idle_microseconds() == 0) {
config->mutable_sequence_batching()->set_max_sequence_idle_microseconds(
SEQUENCE_IDLE_DEFAULT_MICROSECONDS);
}
if (config->sequence_batching().has_oldest()) {
// If preferred batch size is not specified set it to
// max-batch-size.
if (config->sequence_batching().oldest().preferred_batch_size().size() ==
0) {
auto mutable_preferred_batch_size =
config->mutable_sequence_batching()
->mutable_oldest()
->mutable_preferred_batch_size();
if (config->max_batch_size() > 0) {
mutable_preferred_batch_size->Add(config->max_batch_size());
}
}
}
}
// If model ensembling is specified, don't attempt to normalize instance_group
// as it is not allowed in ensemble scheduling
if (!config->has_ensemble_scheduling()) {
auto optimization = config->mutable_optimization();
if (!optimization->has_input_pinned_memory()) {
optimization->mutable_input_pinned_memory()->set_enable(true);
}
if (!optimization->has_output_pinned_memory()) {
optimization->mutable_output_pinned_memory()->set_enable(true);
}
}
return Status::Success;
}
Status
NormalizeInstanceGroup(
const double min_compute_capability,
const std::vector<inference::ModelInstanceGroup>& preferred_groups,
inference::ModelConfig* config)
{
// Instance group setting doesn't apply to ensemble
if (config->has_ensemble_scheduling()) {
return Status::Success;
}
// Creates a set of supported GPU device ids
std::set<int> supported_gpus;
#ifdef TRITON_ENABLE_GPU
// Get the total number of GPUs from the runtime library.
Status status = GetSupportedGPUs(&supported_gpus, min_compute_capability);
if (!status.IsOk()) {
return status;
}
#endif // TRITON_ENABLE_GPU
// Make sure there is at least one instance_group.
if (config->instance_group().empty()) {
inference::ModelInstanceGroup* group = config->add_instance_group();
group->set_name(config->name());
for (const auto& pg : preferred_groups) {
// handle preferred GPU setting differently based on kind
if (pg.kind() == inference::ModelInstanceGroup::KIND_GPU) {
// Don't use preferred group with KIND_GPU if there is no GPU.
if (supported_gpus.empty()) {
continue;
}
// If preferred group sets GPUs, limit deployment onto those that
// are also listed in supported gpus
if (!pg.gpus().empty()) {
for (const int32_t gid : pg.gpus()) {
if (supported_gpus.find(gid) != supported_gpus.end()) {
group->add_gpus(gid);
}
}
}
} else if (pg.kind() == inference::ModelInstanceGroup::KIND_AUTO) {
// if AUTO, then set preferred GPU as is, to align with KIND_AUTO
// deduction specified below
for (const int32_t gid : pg.gpus()) {
group->add_gpus(gid);
}
}
group->set_kind(pg.kind());
group->set_count(pg.count());
// Found a valid preferred group.
break;
}
}
// Assign default name, kind and count to each instance group that
// doesn't give those values explicitly. For KIND_GPU, set GPUs to
// all available if not specified explicitly.
size_t cnt = 0;
for (auto& group : *config->mutable_instance_group()) {
// Name
if (group.name().empty()) {
group.set_name(config->name() + "_" + std::to_string(cnt));
}
cnt++;
// For KIND_AUTO... if there are no GPUs or if any of the listed
// 'gpu's are not present, then use KIND_CPU.
if (group.kind() == inference::ModelInstanceGroup::KIND_AUTO) {
if (supported_gpus.empty()) {
group.set_kind(inference::ModelInstanceGroup::KIND_CPU);
} else {
for (const int32_t gid : group.gpus()) {
if (supported_gpus.find(gid) == supported_gpus.end()) {
group.set_kind(inference::ModelInstanceGroup::KIND_CPU);
break;
}
}
}
if (group.kind() == inference::ModelInstanceGroup::KIND_AUTO) {
group.set_kind(inference::ModelInstanceGroup::KIND_GPU);
}
}
// KIND is resolved at this point
for (const auto& pg : preferred_groups) {
if (group.kind() != pg.kind()) {
continue;
}
// Limit the GPU setting within what is specified in the preferred group,
// if no available GPU then skip to next preferred group
if ((group.kind() == inference::ModelInstanceGroup::KIND_GPU) &&
group.gpus().empty() && !pg.gpus().empty()) {
for (const int32_t gid : pg.gpus()) {
if (supported_gpus.find(gid) != supported_gpus.end()) {
group.add_gpus(gid);
}
}
if (group.gpus().empty()) {
continue;
}
}
if ((group.count() < 1) && (pg.count() > 0)) {
group.set_count(pg.count());
}
}
// Set Triton default if the fields are not set from preferred group
// Count
if (group.count() < 1) {
RETURN_IF_ERROR(SetDefaultInstanceCount(&group, config->backend()));
}
// GPUs
if ((group.kind() == inference::ModelInstanceGroup::KIND_GPU) &&
(group.gpus().size() == 0)) {
for (auto d : supported_gpus) {
group.add_gpus(d);
}
}
}
return Status::Success;
}
Status
LocalizePythonBackendExecutionEnvironmentPath(
const std::string& model_path, inference::ModelConfig* config,
std::shared_ptr<LocalizedPath>* localized_model_dir)
{
if (config->backend() == kPythonBackend) {
if (config->parameters().contains("EXECUTION_ENV_PATH")) {
// Read EXECUTION_ENV_PATH
std::string exec_env_path =
config->parameters().at("EXECUTION_ENV_PATH").string_value();
// Replace model directory variable with model_path
std::string model_dir_var = "$$TRITON_MODEL_DIRECTORY";
if (exec_env_path.substr(0, model_dir_var.size()) == model_dir_var) {
exec_env_path.replace(0, model_dir_var.size(), model_path);
}
// Collapse any .. in the path
std::string abs_exec_env_path;
std::size_t prev_pos = exec_env_path.size();
std::size_t pos = exec_env_path.find_last_of('/', prev_pos - 1);
int skip = 0;
while (pos != std::string::npos && prev_pos > 0) {
if (!skip) {
abs_exec_env_path =
exec_env_path.substr(pos, prev_pos - pos) + abs_exec_env_path;
}
skip = skip > 0 ? skip - 1 : skip;
if (pos >= 3 && exec_env_path.substr(pos - 3, 3) == "/..") {
skip += 2;
}
prev_pos = pos;
pos = exec_env_path.find_last_of('/', prev_pos - 1);
}
abs_exec_env_path = exec_env_path.substr(0, prev_pos) + abs_exec_env_path;
// Localize iff abs_exec_env_path is outside the model directory
std::string model_path_slash =
model_path.back() == '/' ? model_path : model_path + "/";
if (abs_exec_env_path.substr(0, model_path_slash.size()) !=
model_path_slash) {
// Localize the file
std::shared_ptr<LocalizedPath> localized_exec_env_path;
RETURN_IF_ERROR(
LocalizePath(abs_exec_env_path, &localized_exec_env_path));
// Persist the localized temporary path
(*localized_model_dir)
->other_localized_path.push_back(localized_exec_env_path);
// Rewrite EXECUTION_ENV_PATH
config->mutable_parameters()
->at("EXECUTION_ENV_PATH")