-
Notifications
You must be signed in to change notification settings - Fork 546
/
ModelImporter.cpp
982 lines (891 loc) · 37.2 KB
/
ModelImporter.cpp
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
/*
* SPDX-License-Identifier: Apache-2.0
*/
#include "ModelImporter.hpp"
#include "OnnxAttrs.hpp"
#include "Status.hpp"
#include "errorHelpers.hpp"
#include "importerUtils.hpp"
#include "onnxProtoUtils.hpp"
#include "toposort.hpp"
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <functional>
#include <limits>
#include <sys/stat.h>
#include <unordered_set>
namespace onnx2trt
{
// Helper class and object to shutdown protobuf library upon library unload.
class ProtobufShutter
{
public:
~ProtobufShutter()
{
google::protobuf::ShutdownProtobufLibrary();
}
};
static ProtobufShutter protobufShutter;
// Helper for deserializing INetwork
void setTensorLocations(
ImporterContext* ctx, std::vector<std::string> const& tensors, std::vector<std::string> const& locations)
{
ONNXTRT_CHECK((tensors.size() >= locations.size())
&& "The size of tensors misaligns with the size of the attribute trt_outputs_loc.",
nvonnxparser::ErrorCode::kINVALID_GRAPH);
for (size_t i = 0; i < locations.size(); ++i)
{
std::string tensor = tensors.at(i);
std::string location = locations.at(i);
nvinfer1::TensorLocation loc
= location == "device" ? nvinfer1::TensorLocation::kDEVICE : nvinfer1::TensorLocation::kHOST;
if (ctx->tensorLocations().count(tensor) > 0)
{
ONNXTRT_CHECK((ctx->tensorLocations()[tensor] == loc) && "The tensor location cannot be changed.",
nvonnxparser::ErrorCode::kINVALID_GRAPH);
}
else
{
ctx->tensorLocations()[tensor] = loc;
}
}
}
// Helper for deserializing INetwork
template <typename T>
void setStringMap(
ImporterContext* ctx, std::vector<std::string> const& tensors, std::vector<T> const& data, StringMap<T>& map)
{
ONNXTRT_CHECK((tensors.size() >= data.size())
&& "The size of tensors misaligns with the size of the attribute trt_outputs_range_min/max.",
nvonnxparser::ErrorCode::kINVALID_GRAPH);
for (size_t i = 0; i < data.size(); ++i)
{
std::string name = tensors.at(i);
T dataName = data.at(i);
if (map.count(name) > 0)
{
ONNXTRT_CHECK( (map[name] == dataName) && "The order of tensorRangeMin/Max in context misaligns with the order of the attribute trt_outputs_range_min/max.", nvonnxparser::ErrorCode::kINVALID_GRAPH);
}
else
{
map[name] = dataName;
}
}
}
//! Make error explanation from TensorRT error recorder.
static std::string makeErrorExplanation(ImporterContext* ctx, std::string const& nodeName)
{
std::ostringstream result;
result << "Invalid Node - " << nodeName;
if (auto* errorRecorder = ctx->getErrorRecorder())
{
// Append information that might help the user understand the error.
int32_t const nbErrors = errorRecorder->getNbErrors();
for (int32_t i = 0; i < nbErrors; ++i)
{
result << "\n" << errorRecorder->getErrorDesc(i);
}
}
return result.str();
}
//! Make error explanation from an exception.
static std::string makeErrorExplanation(std::exception const& e, std::string const& nodeName)
{
std::ostringstream result;
result << "Exception occurred in - " << nodeName << "\n" << e.what();
return result.str();
}
bool isNodeInPluginRegistry(ImporterContext* ctx, ::ONNX_NAMESPACE::NodeProto const& node)
{
OnnxAttrs attrs(node, ctx);
std::string const pluginVersion{attrs.get<std::string>("plugin_version", "1")};
std::string const pluginNamespace{attrs.get<std::string>("plugin_namespace", "")};
LOG_INFO("Checking if node can be treated as plugin: " << node.op_type() << ", plugin_version: " << pluginVersion
<< ", plugin_namespace: " << pluginNamespace);
nvinfer1::IPluginCreatorInterface* creator
= importPluginCreator(ctx, node.op_type(), pluginVersion, pluginNamespace);
return creator;
}
void parseNode(
ImporterContext* ctx, ::ONNX_NAMESPACE::NodeProto const& node, size_t const nodeIdx, bool deserializingINetwork)
{
// For nodes that contain subgraphs (Ifs, Loops, Scans, LocalFunctions), ensure that the recursion depth is
// limited to a set amount. Recursion depth is tracked by the size of ctx->mBaseNameScopeStack().
size_t const kMAX_NESTED_SUBGRAPHS = 24;
if (ctx->getNestedDepth() > kMAX_NESTED_SUBGRAPHS)
{
ONNXTRT_THROW(MAKE_ERROR("ONNX graph contains nested structures that exceed the maximum allowed by TensorRT!",
ErrorCode::kUNSUPPORTED_GRAPH));
}
StringMap<NodeImporter> const& opImporters = getBuiltinOpImporterMap();
std::string const& nodeName = getNodeName(node);
std::string const& nodeType = node.op_type();
LOG_VERBOSE("Parsing node: " << nodeName << " [" << nodeType << "]");
// Assemble node inputs. These may come from outside the subgraph.
std::vector<TensorOrWeights> nodeInputs;
std::ostringstream ssInputs{};
ssInputs << nodeName << " [" << nodeType << "] inputs: ";
for (auto const& inputName : node.input())
{
// Empty input names indicate optional inputs which have not been supplied.
if (inputName.empty())
{
// Push back null input as place holder.
nodeInputs.emplace_back(nullptr);
ssInputs << "[optional input, not set], ";
}
else
{
LOG_VERBOSE("Searching for input: " << inputName);
ONNXTRT_CHECK_NODE((ctx->tensors().count(inputName)), "Node input was not registered.", node, nodeIdx,
ErrorCode::kINVALID_GRAPH);
nodeInputs.push_back(ctx->tensors().at(inputName));
ssInputs << "[" << inputName << " -> " << nodeInputs.back().shape() << "[" << nodeInputs.back().getType()
<< "]"
<< "], ";
}
}
LOG_VERBOSE(ssInputs.str());
// Dispatch to appropriate converter.
NodeImporter const* importFunc{nullptr};
if (opImporters.count(nodeType))
{
importFunc = &opImporters.at(nodeType);
}
else if (ctx->localFunctions().count(nodeType))
{
// Let plugin take precedence over local function. So first check if this can be dispatched to a plugin.
if (isNodeInPluginRegistry(ctx, node))
{
LOG_INFO("Found registered plugin: " << nodeType << ". Importing local function as a plugin.");
importFunc = &opImporters.at("FallbackPluginImporter");
}
else
{
LOG_INFO("Found registered local function: " << nodeType << ". Importing as a local function.");
importFunc = &opImporters.at("LocalFunctionImporter");
}
}
else
{
LOG_INFO("No importer registered for op: " << nodeType << ". Attempting to import as plugin.");
importFunc = &opImporters.at("FallbackPluginImporter");
}
std::vector<TensorOrWeights> outputs;
try
{
outputs = (*importFunc)(ctx, node, nodeIdx, nodeInputs);
}
catch (OnnxTrtException& e)
{
throw e;
}
catch (std::exception& e)
{
ONNXTRT_THROW(MAKE_NODE_ERROR(makeErrorExplanation(ctx, nodeName), ErrorCode::kINVALID_NODE, node, nodeIdx));
}
ctx->addLayerOutputTensors(nodeName, outputs);
for (auto const& output : outputs)
{
if (output.is_tensor())
{
// check that we can resolve output dims
// in the future we may have a network/layer.validate() which will help with that as well
output.tensor().getDimensions();
// If output dimensions cannot be resolved the error will be captured by the ErrorRecorder.
if (ctx->hasError())
{
ONNXTRT_THROW(
MAKE_NODE_ERROR(makeErrorExplanation(ctx, nodeName), ErrorCode::kINVALID_NODE, node, nodeIdx));
}
}
}
if (deserializingINetwork)
{
OnnxAttrs attrs(node, ctx);
// Tensor locations, dynamic ranges and layer precisions will be set after parsing the network
std::vector<std::string> outputsLocation = attrs.get<std::vector<std::string>>("trt_outputs_loc", {});
std::vector<std::string> outputsVec(node.output().begin(), node.output().end());
std::vector<std::string> layerName{nodeName};
setTensorLocations(ctx, outputsVec, outputsLocation);
auto outputsRangeMin = attrs.get<std::vector<float>>("trt_outputs_range_min", {});
setStringMap<float>(ctx, outputsVec, outputsRangeMin, ctx->tensorRangeMins());
auto outputsRangeMax = attrs.get<std::vector<float>>("trt_outputs_range_max", {});
setStringMap<float>(ctx, outputsVec, outputsRangeMax, ctx->tensorRangeMaxes());
if (attrs.count("trt_layer_precision"))
{
std::vector<nvinfer1::DataType> layerPrecision{attrs.get<nvinfer1::DataType>("trt_layer_precision")};
setStringMap<nvinfer1::DataType>(ctx, layerName, layerPrecision, ctx->layerPrecisions());
}
}
int32_t nonEmptyOutputs
= std::count_if(node.output().begin(), node.output().end(), [](std::string const& str) { return !str.empty(); });
ONNXTRT_CHECK_NODE(nonEmptyOutputs == static_cast<int32_t>(outputs.size()),
"Node has more output tensors than TRT expected, expected output size is "
<< outputs.size() << ", actual output size is " << nonEmptyOutputs << ".",
node, nodeIdx, ErrorCode::kINVALID_GRAPH);
// Set output names and register outputs with the context.
std::ostringstream ssOutputs{};
ssOutputs << nodeName << " [" << node.op_type() << "] outputs: ";
for (int32_t i = 0, trtCnt = 0; i < node.output().size(); ++i)
{
auto const& outputName = node.output(i);
// Empty strings denote null-tensor outputs. Ignore these.
if (outputName.empty())
{
continue;
}
auto& output = outputs.at(trtCnt);
ssOutputs << "[" << outputName << " -> " << output.shape() << "[" << output.getType() << "]"
<< "], ";
// Note: This condition is to allow ONNX outputs to be ignored
// Always register output weights (even empty ones) as it may be mapped to an unused input
if ((output || output.is_weights()) && !outputName.empty())
{
ctx->registerTensor(std::move(output), outputName);
}
// UINT8 is only allowed as network inputs and outputs. Therefore any node that produces an UINT8-typed
// output that is not also a graph output is unsupported.
if (output.getType() == "UINT8")
{
bool legalUINT8 = false;
for (auto const& graphOutput : ctx->getGraphOutputNames())
{
if (graphOutput.name() == outputName)
{
legalUINT8 = true;
}
}
ONNXTRT_CHECK_NODE(legalUINT8, "TensorRT does not support UINT8 types for intermediate tensors!", node,
nodeIdx, ErrorCode::kUNSUPPORTED_NODE);
}
trtCnt++;
}
LOG_VERBOSE(ssOutputs.str());
}
void parseNodeStaticCheck(
ImporterContext* ctx, ::ONNX_NAMESPACE::NodeProto const& node, std::vector<Status>& errors, size_t const nodeIndex)
{
StringMap<OpStaticErrorChecker> const& opCheckers = getOpStaticErrorCheckerMap();
StringMap<NodeImporter> const& opImporters = getBuiltinOpImporterMap();
std::string const& nodeName = getNodeName(node);
std::string const& nodeType = node.op_type();
LOG_VERBOSE("Static check for parsing node: " << nodeName << " [" << nodeType << "]");
// Dispatch to appropriate static error checker.
OpStaticErrorChecker const* checkerFunc{nullptr};
if (opImporters.count(nodeType))
{
if (!opCheckers.count(nodeType))
{
std::string errorMsg = "No static checker was found for " + nodeType;
errors.push_back(MAKE_NODE_ERROR(errorMsg, ErrorCode::kINTERNAL_ERROR, node, nodeIndex));
return;
}
checkerFunc = &opCheckers.at(nodeType);
}
else if (opCheckers.count(nodeType))
{
checkerFunc = &opCheckers.at(nodeType);
}
else if (ctx->localFunctions().count(nodeType))
{
// Let plugin take precedence over local function. So first check if this can be dispatched to a plugin.
if (isNodeInPluginRegistry(ctx, node))
{
LOG_INFO("Found registered plugin: " << nodeType << ". Importing local function as a plugin.");
checkerFunc = &opCheckers.at("FallbackPluginImporter");
}
else
{
LOG_INFO("Found registered local function: " << nodeType << ". Importing as a local function.");
checkerFunc = &opCheckers.at("LocalFunctionImporter");
}
}
else
{
LOG_INFO("No checker registered for op: " << nodeType << ". Attempting to check as plugin.");
checkerFunc = &opCheckers.at("FallbackPluginImporter");
}
(*checkerFunc)(ctx, node, errors, nodeIndex);
}
void parseGraph(ImporterContext* ctx, ::ONNX_NAMESPACE::GraphProto const& graph, std::vector<Status>& errors,
bool deserializingINetwork, int* currentNode)
{
// Import initializers.
try
{
for (::ONNX_NAMESPACE::TensorProto const& initializer : graph.initializer())
{
LOG_VERBOSE("Importing initializer: " << initializer.name());
ShapedWeights weights;
ONNXTRT_CHECK(
ctx->getWeightsContext().convertOnnxWeights(initializer, &weights) && "Failed to import initializer.",
ErrorCode::kUNSUPPORTED_NODE);
ctx->registerTensor(TensorOrWeights{std::move(weights)}, initializer.name());
}
}
catch (const std::exception& e)
{
ONNXTRT_THROW(MAKE_ERROR("Failed to import initialzer", ErrorCode::kINVALID_GRAPH));
}
// Keep track of graph outputs in the context to validate UINT8 nodes
for (const auto& output : graph.output())
{
ctx->getGraphOutputNames().push_back(output);
}
std::vector<size_t> topoOrder;
ONNXTRT_CHECK(
toposort(graph.node(), &topoOrder) && "Failed to sort the model topologically.", ErrorCode::kINVALID_GRAPH);
for (auto const& nodeIndex : topoOrder)
{
if (currentNode)
{
*currentNode = nodeIndex;
}
parseNodeStaticCheck(ctx, graph.node(nodeIndex), errors, nodeIndex);
if (errors.size() == 0)
{
// At most one dynamic error will be returned.
parseNode(ctx, graph.node(nodeIndex), nodeIndex, deserializingINetwork);
}
}
// Static check still reports error through the error vector by design
if (errors.size() != 0)
{
auto result = errors.back();
errors.pop_back(); // this error will be added back to the list in ModelImporter::parseWithWeightDescriptors.
ONNXTRT_THROW(result);
}
}
// Still returns a vector<Status> since CHECK_INPUT doesn't immediately return
std::vector<Status> importInput(ImporterContext* ctx, ::ONNX_NAMESPACE::ValueInfoProto const& input,
nvinfer1::ITensor** tensor, std::vector<NamedDimension>& namedDims)
{
std::vector<Status> errorList{};
auto const& onnxDtype = input.type().tensor_type();
nvinfer1::DataType trtDtype{nvinfer1::DataType::kFLOAT};
CHECK_INPUT(
convertDtype(onnxDtype.elem_type(), &trtDtype) && "Failed to convert ONNX date type to TensorRT data type.",
ErrorCode::kUNSUPPORTED_NODE, input.name(), errorList);
nvinfer1::Dims trt_dims;
size_t const oldNbNamedDimensions = namedDims.size();
CHECK_INPUT(convertOnnxDims(onnxDtype.shape().dim(), trt_dims, namedDims)
&& "Failed to convert ONNX dimensions to TensorRT dimensions.",
ErrorCode::kUNSUPPORTED_GRAPH, input.name(), errorList);
LOG_VERBOSE(
"Adding network input: " << input.name() << " with dtype: " << trtDtype << ", dimensions: " << trt_dims);
if (errorList.empty())
{
*tensor = ctx->network()->addInput(input.name().c_str(), trtDtype, trt_dims);
CHECK_INPUT(
*tensor && "Failed to add input to the network.", ErrorCode::kUNSUPPORTED_NODE, input.name(), errorList);
}
// Fill in field `tensor` for any dimensions that had names in the ONNX.
for (auto i = oldNbNamedDimensions; i < namedDims.size(); ++i)
{
namedDims[i].tensor = *tensor;
}
return errorList;
}
static void setDimensionNames(ImporterContext* ctx, std::vector<NamedDimension>& namedDims)
{
for (auto const& namedDim : namedDims)
{
namedDim.tensor->setDimensionName(namedDim.index, namedDim.dimParam.c_str());
}
}
void importInputs(ImporterContext* ctx, ::ONNX_NAMESPACE::GraphProto const& graph, StringMap<TensorOrWeights>* tensors,
std::vector<Status>& errors)
{
// The weights come from the Initializer list in onnx graph
// Initializers are not really network inputs, so they need to be excluded.
std::unordered_set<std::string> initializers{};
for (::ONNX_NAMESPACE::TensorProto const& initializer : graph.initializer())
{
initializers.emplace(initializer.name());
}
std::vector<NamedDimension> namedDims;
std::vector<Status> statusList{};
for (::ONNX_NAMESPACE::ValueInfoProto const& input : graph.input())
{
TensorOrWeights tensor;
if (!initializers.count(input.name()))
{
nvinfer1::ITensor* tensor_ptr{nullptr};
std::vector<Status> status = importInput(ctx, input, &tensor_ptr, namedDims);
statusList.insert(statusList.end(), status.begin(), status.end());
tensor = tensor_ptr;
if (statusList.empty() && tensor_ptr->getType() == nvinfer1::DataType::kINT64)
{
LOG_WARNING("Make sure input " << input.name() << " has Int64 binding.");
}
}
ctx->registerTensor(std::move(tensor), input.name());
}
if (!statusList.empty())
{
errors.insert(errors.end(), statusList.begin(), statusList.end());
return;
}
setDimensionNames(ctx, namedDims);
}
void importLocalFunctions(ImporterContext* ctx, ::ONNX_NAMESPACE::ModelProto const& model)
{
for (auto const& localFunction : model.functions())
{
ctx->localFunctions().insert({localFunction.name(), localFunction});
}
}
std::pair<bool, ModelImporter::SubGraphSupportVector_t> ModelImporter::doSupportsModel(
void const* serialized_onnx_model, size_t serialized_onnx_model_size, char const* model_path)
{
::ONNX_NAMESPACE::ModelProto model;
deserializeOnnxModel(serialized_onnx_model, serialized_onnx_model_size, &model);
if (model_path)
{
mImporterCtx.setOnnxFileLocation(model_path);
}
bool allSupported{true};
// Parse the graph and see if we hit any parsing errors
allSupported = parse(serialized_onnx_model, serialized_onnx_model_size);
int32_t error_node = -1;
std::string input_node{};
if (!allSupported)
{
int32_t nerror = getNbErrors();
for (int32_t i = 0; i < nerror; ++i)
{
nvonnxparser::IParserError const* error = getError(i);
if (error->node() != -1)
{
error_node = error->node();
allSupported = false;
}
// The node that we failed on is one of the input nodes (-1). Get the name of the input node
// that we failed on and remove all nodes that spawn out of it.
else
{
// Node name is extracted through error->file as all errors thrown on input nodes are wrapped
// around MAKE_INPUT_ERROR.
input_node = error->file();
}
}
}
auto* ctx = &mImporterCtx;
auto checkForInput = [&input_node, &ctx](::ONNX_NAMESPACE::NodeProto const& node) {
for (auto input : node.input())
{
if (input_node == input || ctx->loopTensors()[input_node] == input)
{
return true;
}
}
return false;
};
bool newSubGraph(true);
// Sort and partition supported subgraphs
std::vector<size_t> topological_order;
if (!toposort(model.graph().node(), &topological_order))
{
LOG_VERBOSE("Failed to sort model topologically, exiting ...");
return std::make_pair<bool, SubGraphSupportVector_t>(false, {});
}
SubGraphSupportVector_t supportVector;
for (int32_t node_idx : topological_order)
{
::ONNX_NAMESPACE::NodeProto const& node = model.graph().node(node_idx);
// Add the node to the subgraph if:
// 1. It is not directly connected to an unsupported input
// 2. The importer function did not throw an assertion
bool unsupportedInput = (input_node.empty()) ? false : checkForInput(node);
bool unsuccessfulParse = node_idx == error_node;
if (!unsupportedInput && !unsuccessfulParse)
{
if (newSubGraph)
{
// If it is the beginning of a new subGraph, we start a new vector
supportVector.emplace_back();
// Mark all new graphs as "unknown"
supportVector.back().second = false;
newSubGraph = false;
}
// We add the new node to the last graph
supportVector.back().first.emplace_back(node_idx);
}
else
{
// This is not a supported node, reset newSubGraph
newSubGraph = true;
allSupported = false;
}
}
// Only mark the subgraph as supported if there is one supported subgraph.
if (allSupported)
{
supportVector.back().second = true;
}
return std::make_pair(allSupported, std::move(supportVector));
}
bool ModelImporter::supportsModel(void const* serialized_onnx_model, size_t serialized_onnx_model_size,
SubGraphCollection_t& sub_graph_collection, char const* model_path) noexcept
{
ONNXTRT_TRY
{
std::pair<bool, SubGraphSupportVector_t> result
= doSupportsModel(serialized_onnx_model, serialized_onnx_model_size, model_path);
bool supports = result.first;
SubGraphSupportVector_t supportVector = result.second;
sub_graph_collection.clear();
// SubGraphCollection uses size_t, while SubGraphSupportVector_t uses int64_t
for (const auto& pair : supportVector)
{
bool subgraphSupports = pair.second;
std::vector<int64_t> const& subgraphNodes = pair.first;
std::vector<size_t> subgraphNodesRet(subgraphNodes.begin(), subgraphNodes.end());
// Create a new pair and add it to vector b
sub_graph_collection.push_back(std::make_pair(subgraphNodesRet, subgraphSupports));
}
return supports;
}
ONNXTRT_CATCH_RECORD
return false;
}
bool ModelImporter::supportsModelV2(
void const* serialized_onnx_model, size_t serialized_onnx_model_size, char const* model_path) noexcept
{
ONNXTRT_TRY
{
std::pair<bool, SubGraphSupportVector_t> result
= doSupportsModel(serialized_onnx_model, serialized_onnx_model_size, model_path);
bool supports = result.first;
SubGraphSupportVector_t supportVector = result.second;
mSubGraphSupportVector.resize(supportVector.size());
std::copy(supportVector.begin(), supportVector.end(), mSubGraphSupportVector.begin());
return supports;
}
ONNXTRT_CATCH_RECORD
return false;
}
int64_t ModelImporter::getNbSubgraphs() noexcept
{
ONNXTRT_TRY
{
return mSubGraphSupportVector.size();
}
ONNXTRT_CATCH_RECORD
return 0;
}
bool ModelImporter::isSubgraphSupported(int64_t const index) noexcept
{
ONNXTRT_TRY
{
std::ostringstream errorMessage;
errorMessage << "Query index " << index
<< " exceeds subgraph support vector (size = " << mSubGraphSupportVector.size()
<< "). Have you called supports_model_v2?";
ONNXTRT_CHECK(mSubGraphSupportVector.size() > static_cast<uint64_t>(index) && errorMessage.str().c_str(),
ErrorCode::kINVALID_VALUE);
return mSubGraphSupportVector[index].second;
}
ONNXTRT_CATCH_RECORD
return false;
}
int64_t* ModelImporter::getSubgraphNodes(int64_t const index, int64_t& subgraphLength) noexcept
{
ONNXTRT_TRY
{
std::ostringstream errorMessage;
errorMessage << "Query index " << index
<< " exceeds subgraph support vector (size = " << mSubGraphSupportVector.size()
<< "). Have you called supports_model_v2?";
ONNXTRT_CHECK(mSubGraphSupportVector.size() > static_cast<uint64_t>(index) && errorMessage.str().c_str(),
ErrorCode::kINVALID_VALUE);
subgraphLength = mSubGraphSupportVector[index].first.size();
return mSubGraphSupportVector[index].first.data();
}
ONNXTRT_CATCH_RECORD
subgraphLength = 0;
return nullptr;
}
bool ModelImporter::supportsOperator(char const* op_name) const noexcept
{
ONNXTRT_TRY
{
return _op_importers.count(op_name);
}
ONNXTRT_CATCH_RECORD
return false;
}
bool ModelImporter::parseWithWeightDescriptors(
void const* serialized_onnx_model, size_t serialized_onnx_model_size) noexcept
{
ONNXTRT_TRY
{
mCurrentNode = -1;
// TODO: This function (and its overload below) could do with some cleaning,
// particularly wrt error handling.
// Note: We store a copy of the model so that weight arrays will persist
mONNXModels.emplace_back();
::ONNX_NAMESPACE::ModelProto& model = mONNXModels.back();
deserializeOnnxModel(serialized_onnx_model, serialized_onnx_model_size, &model);
importModel(model);
return true;
}
ONNXTRT_CATCH_RECORD
return false;
}
bool ModelImporter::parse(
void const* serialized_onnx_model, size_t serialized_onnx_model_size, const char* model_path) noexcept
{
ONNXTRT_TRY
{
auto* const ctx = &mImporterCtx;
if (ctx->network()->getNbLayers() > 0)
{
LOG_ERROR("Parse was called with a non-empty network definition");
return false;
}
if (model_path)
{
mImporterCtx.setOnnxFileLocation(model_path);
}
return this->parseWithWeightDescriptors(serialized_onnx_model, serialized_onnx_model_size);
}
ONNXTRT_CATCH_RECORD
return false;
}
void ModelImporter::importModel(::ONNX_NAMESPACE::ModelProto const& model)
{
auto* ctx = &mImporterCtx;
mImporterCtx.clearOpsets();
// Add domain import limit for security reasons
int32_t const MAX_DOMAINS = 1024;
ONNXTRT_CHECK(model.opset_import().size() <= MAX_DOMAINS
&& "Model contains more than 1024 domains! Parsing will halt for security reasons.",
ErrorCode::kUNSUPPORTED_GRAPH);
for (int32_t i = 0; i < model.opset_import().size(); ++i)
{
std::string domain = model.opset_import(i).domain();
int64_t version = model.opset_import(i).version();
// TensorRT requires an ONNX graph to be generated with at least ai.onnx version 7.
// ONNX spec says that the default domain is either an empty string or is "ai.onnx".
if ((domain.empty() || domain == "ai.onnx") && version < 7)
{
LOG_WARNING(
"TensorRT supports ONNX graphs generated with at least opset 7. Models using older opsets are not "
"guaranteed to work.");
}
mImporterCtx.addOpset(domain, version);
}
::ONNX_NAMESPACE::GraphProto const& graph = model.graph();
// Create a dummy tensors so that we can reserve output names. If the output names are encountered elsewhere
// in the graph, the ctx will know to make the names unique.
for (::ONNX_NAMESPACE::ValueInfoProto const& output : graph.output())
{
mImporterCtx.registerTensor(TensorOrWeights{}, output.name());
}
// Import LocalFunctions
importLocalFunctions(&mImporterCtx, model);
// Propagate OnnxParserFlags down to the importer context.
mImporterCtx.setFlags(getFlags());
mCurrentNode = -1;
importInputs(&mImporterCtx, graph, &mImporterCtx.tensors(), mErrors);
parseGraph(&mImporterCtx, graph, mErrors, model.producer_name() == "TensorRT", &mCurrentNode);
mCurrentNode = -1;
// Mark outputs defined in the ONNX model (unless tensors are user-requested)
for (::ONNX_NAMESPACE::ValueInfoProto const& output : graph.output())
{
ONNXTRT_CHECK((mImporterCtx.tensors().count(output.name())) && "The output tensor was not registered.",
ErrorCode::kINVALID_GRAPH);
nvinfer1::ITensor* output_tensor_ptr
= &convertToTensor(mImporterCtx.tensors().at(output.name()), &mImporterCtx);
LOG_VERBOSE("Marking " << output_tensor_ptr->getName() << " as output: " << output.name());
output_tensor_ptr->setName(output.name().c_str());
if (output_tensor_ptr->isNetworkInput())
{
// HACK WAR for TRT not allowing input == output
// TODO: Does this break things by changing the name of the input tensor?
output_tensor_ptr->setName(("__" + output.name()).c_str());
output_tensor_ptr = &identity(&mImporterCtx, output_tensor_ptr).tensor();
ONNXTRT_CHECK(output_tensor_ptr && "Failed to add an Identity layer.", ErrorCode::kUNSUPPORTED_NODE);
output_tensor_ptr->setName(output.name().c_str());
}
mImporterCtx.network()->markOutput(*output_tensor_ptr);
nvinfer1::DataType output_trt_dtype;
ONNXTRT_CHECK(convertDtype(output.type().tensor_type().elem_type(), &output_trt_dtype)
&& "Failed to convert ONNX date type to TensorRT data type.",
ErrorCode::kUNSUPPORTED_NODE);
// For INT32 data type, output type must match tensor type
ONNXTRT_CHECK((output_tensor_ptr->getType() != nvinfer1::DataType::kINT32
|| output_trt_dtype == nvinfer1::DataType::kINT32)
&& "For INT32 tensors, the output type must also be INT32.",
ErrorCode::kUNSUPPORTED_NODE);
// Note: Without this, output type is always float32
output_tensor_ptr->setType(output_trt_dtype);
if (output_trt_dtype == nvinfer1::DataType::kINT64)
{
LOG_WARNING("Make sure output " << output.name() << " has Int64 binding.");
}
}
if (model.producer_name() == "TensorRT")
{
// iterate over all tensors in the network and add them to "tensors" map
StringMap<nvinfer1::ITensor*> tensors;
StringMap<nvinfer1::ILayer*> layers;
for (int32_t idx = 0; idx < mImporterCtx.network()->getNbInputs(); ++idx)
{
nvinfer1::ITensor* tensor = mImporterCtx.network()->getInput(idx);
if (tensor != nullptr)
{
tensors[tensor->getName()] = tensor;
}
}
for (int32_t idx = 0; idx < mImporterCtx.network()->getNbOutputs(); ++idx)
{
nvinfer1::ITensor* tensor = mImporterCtx.network()->getOutput(idx);
if (tensor != nullptr)
{
tensors[tensor->getName()] = tensor;
}
}
for (int32_t layerIdx = 0; layerIdx < mImporterCtx.network()->getNbLayers(); ++layerIdx)
{
nvinfer1::ILayer* layer = mImporterCtx.network()->getLayer(layerIdx);
for (int32_t idx = 0; idx < layer->getNbInputs(); ++idx)
{
nvinfer1::ITensor* tensor = layer->getInput(idx);
if (tensor != nullptr)
{
tensors[tensor->getName()] = tensor;
}
}
for (int32_t idx = 0; idx < layer->getNbOutputs(); ++idx)
{
nvinfer1::ITensor* tensor = layer->getOutput(idx);
if (tensor != nullptr)
{
tensors[tensor->getName()] = tensor;
}
}
layers[layer->getName()] = layer;
}
// Set locations for all tensors
for (auto const& tensor : ctx->tensorLocations())
{
ONNXTRT_CHECK((tensors.count(tensor.first) > 0) && "The tensor does not have an assigned location.",
nvonnxparser::ErrorCode::kINVALID_GRAPH);
tensors.at(tensor.first)->setLocation(tensor.second);
}
// Set dynamic range for all tensors
for (auto const& tensor : ctx->tensorRangeMins())
{
// if there's a min range, there must be a max range as well
ONNXTRT_CHECK((tensors.count(tensor.first) > 0) && "The tensor does not have an assigned location.",
nvonnxparser::ErrorCode::kINVALID_GRAPH);
if (!std::isnan(tensor.second))
{
tensors.at(tensor.first)->setDynamicRange(tensor.second, ctx->tensorRangeMaxes().at(tensor.first));
}
}
// Avoid setting layer precision if graph is strongly typed.
if (!ctx->network()->getFlag(nvinfer1::NetworkDefinitionCreationFlag::kSTRONGLY_TYPED))
{
// Set precisions for all layers.
for (auto const& layer : ctx->layerPrecisions())
{
ONNXTRT_CHECK((layers.count(layer.first) > 0) && "The layer does not have an assigned precision.",
nvonnxparser::ErrorCode::kINVALID_GRAPH);
layers.at(layer.first)->setPrecision(layer.second);
}
}
}
// Regenerate the plugin library list
mPluginLibraryList = ctx->getUsedVCPluginLibraries();
mPluginLibraryListCStr.clear();
mPluginLibraryListCStr.reserve(mPluginLibraryList.size());
for (auto const& s : mPluginLibraryList)
{
mPluginLibraryListCStr.push_back(s.c_str());
}
}
bool ModelImporter::parseFromFile(char const* onnxModelFile, int32_t verbosity) noexcept
{
ONNXTRT_TRY
{
auto* ctx = &mImporterCtx;
// Define S_ISREG macro for Windows
#if !defined(S_ISREG)
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
#endif
struct stat sb;
if (stat(onnxModelFile, &sb) == 0 && !S_ISREG(sb.st_mode))
{
LOG_ERROR("Input is not a regular file: " << onnxModelFile);
return false;
}
GOOGLE_PROTOBUF_VERIFY_VERSION;
// Own the ONNX model for weights to persist.
mONNXModels.emplace_back();
::ONNX_NAMESPACE::ModelProto& onnxModel = mONNXModels.back();
bool const fileLoadSuccess = ParseFromFileAsBinary(&onnxModel, onnxModelFile);
if (!fileLoadSuccess)
{
LOG_ERROR("Failed to parse ONNX model from file: " << onnxModelFile << "!");
return false;
}
// Keep track of the absolute path to the ONNX file.
mImporterCtx.setOnnxFileLocation(onnxModelFile);
int64_t const opset_version = (onnxModel.opset_import().size() ? onnxModel.opset_import(0).version() : 0);
LOG_INFO("----------------------------------------------------------------");
LOG_INFO("Input filename: " << onnxModelFile);
LOG_INFO("ONNX IR version: " << onnxIRVersionAsString(onnxModel.ir_version()));
LOG_INFO("Opset version: " << opset_version);
LOG_INFO("Producer name: " << onnxModel.producer_name());
LOG_INFO("Producer version: " << onnxModel.producer_version());
LOG_INFO("Domain: " << onnxModel.domain());
LOG_INFO("Model version: " << onnxModel.model_version());
LOG_INFO("Doc string: " << onnxModel.doc_string());
LOG_INFO("----------------------------------------------------------------");
// Set currentNode count to -1
mCurrentNode = -1;
// Prevent failure of importModel from early-exiting
try
{
this->importModel(onnxModel);
}
catch (OnnxTrtException& e)
{
mErrors.push_back(e.getStatus());
}
catch (std::exception& e)
{
mErrors.push_back(MAKE_ERROR(e.what(), ErrorCode::kINTERNAL_ERROR));
}
int32_t const numErrors = getNbErrors();
for (int32_t i = 0; i < numErrors; ++i)
{
nvonnxparser::IParserError const* error = getError(i);
if (error->node() != -1)
{
::ONNX_NAMESPACE::NodeProto const& node = onnxModel.graph().node(error->node());
LOG_ERROR("While parsing node number " << error->node() << " [" << node.op_type() << " -> \""
<< node.output(0) << "\""
<< "]:");
LOG_ERROR("--- Begin node ---" << "\n" << node);
LOG_ERROR("--- End node ---");
}
LOG_ERROR("ERROR: " << error->file() << ":" << error->line() << " In function " << error->func() << ":\n"
<< "[" << static_cast<int>(error->code()) << "] " << error->desc());
}
return numErrors == 0;
}
ONNXTRT_CATCH_RECORD
return false;
}
char const* const* ModelImporter::getUsedVCPluginLibraries(int64_t& nbPluginLibs) const noexcept
{
nbPluginLibs = mPluginLibraryListCStr.size();
return (nbPluginLibs > 0) ? mPluginLibraryListCStr.data() : nullptr;
}
} // namespace onnx2trt