Skip to content

Commit 3e12647

Browse files
kevinch-nvpranavm-nvidiajywu-msftJuHyung-Sondsandler-bos-msk
authored
Merge master into 7.0 (onnx#494)
* Updating linear resize dimensions check (onnx#362) * Updates global pooling functions to work correctly with dynamic shapes (onnx#365) * Support empty initializers for optional inputs (onnx#366) * Add support for empty initializers for optional inputs * Alphabetize importPluginFactory * Support ceiling mode padding for dynamic inputs (onnx#368) * Register empty Constant node outputs to support empty weights (onnx#369) * Update myelin library name on Windows (onnx#371) * Update logic to import ONNX initializers (onnx#375) * Adding more type checks (onnx#380) * Add type check for gather and shapedweights attribute imports (onnx#384) * Throw warning if seed input is provided for randomuniform nodes (onnx#386) * Update spacetodepth importer to support fulldims and dynamic shapes (onnx#392) * Add check to avoid console spam of warnings (onnx#402) * fix some build warnings/errors on Windows VS2019 (onnx#403) * remove c++11/14 non-compliant constexpr lambdas * fix build warnings on VS2019 * disable shape input tensor * Revert "disable shape input tensor" This reverts commit 9a49e03. * Support opset11 padding (onnx#408) * Fix loop importer scan output calculation (onnx#412) * Fix typo in operators.md supported onnx operators (onnx#399) There are two overlapping RNN operators, one supporting and the other not. Since onnx supports RNN, the one with supported N should be removed. Signed-off-by: juhyung <sonju0427@gmail.com> * Added optimization only mode which runs optimization passes on the model without converting it to tensorrt. (onnx#420) * New command line options. * Updated documentation. * Currently requires linking against onnx project. * Support opset8 scan (onnx#433) * Fix deconv importer and remote instancenormalization epsilon clamp value (onnx#434) * Fix deconv importer and remote instancenormalization epsilon clamp value * Remove dilations * Add check for shape tensor outputs (onnx#437) * Fix slice caclulation for -INT_MAX (onnx#438) * Support boolean weight conversion to tensors (onnx#439) * Fix node output accesser for older versions of protobuf (onnx#441) * Add const qualifier to isNullTensor() (onnx#446) * Support negative slicing across an entire axis (onnx#453) * Keep track of Loop tensor mappings (onnx#454) * Fix fp16 weight import (onnx#484) * Fix GEMM import assertion (onnx#485) Co-authored-by: pranavm-nvidia <49246958+pranavm-nvidia@users.noreply.github.com> Co-authored-by: George Wu <jywu@microsoft.com> Co-authored-by: JuHyung Son <sonju0427@gmail.com> Co-authored-by: Dennis Sandler <sandler.denis@gmail.com>
1 parent 84b5be1 commit 3e12647

13 files changed

Lines changed: 823 additions & 283 deletions

CMakeLists.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,15 @@ find_library(TENSORRT_LIBRARY_INFER nvinfer
111111
find_library(TENSORRT_LIBRARY_INFER_PLUGIN nvinfer_plugin
112112
HINTS ${TENSORRT_ROOT} ${TENSORRT_BUILD} ${CUDA_TOOLKIT_ROOT_DIR}
113113
PATH_SUFFIXES lib lib64 lib/x64)
114-
find_library(TENSORRT_LIBRARY_MYELIN myelin
115-
HINTS ${TENSORRT_ROOT} ${TENSORRT_BUILD} ${CUDA_TOOLKIT_ROOT_DIR}
116-
PATH_SUFFIXES lib lib64 lib/x64)
114+
if(WIN32)
115+
find_library(TENSORRT_LIBRARY_MYELIN myelin64_1
116+
HINTS ${TENSORRT_ROOT} ${TENSORRT_BUILD} ${CUDA_TOOLKIT_ROOT_DIR}
117+
PATH_SUFFIXES lib lib64 lib/x64)
118+
else()
119+
find_library(TENSORRT_LIBRARY_MYELIN myelin
120+
HINTS ${TENSORRT_ROOT} ${TENSORRT_BUILD} ${CUDA_TOOLKIT_ROOT_DIR}
121+
PATH_SUFFIXES lib lib64 lib/x64)
122+
endif()
117123
set(TENSORRT_LIBRARY ${TENSORRT_LIBRARY_INFER} ${TENSORRT_LIBRARY_INFER_PLUGIN} ${TENSORRT_LIBRARY_MYELIN})
118124
MESSAGE(STATUS "Find TensorRT libs at ${TENSORRT_LIBRARY}")
119125
find_package_handle_standard_args(
@@ -152,7 +158,7 @@ endif()
152158
# --------------------------------
153159
add_executable(onnx2trt ${EXECUTABLE_SOURCES})
154160
target_include_directories(onnx2trt PUBLIC ${ONNX_INCLUDE_DIRS})
155-
target_link_libraries(onnx2trt PUBLIC ${PROTOBUF_LIB} nvonnxparser_static ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS}) #${CUDA_LIBRARIES}
161+
target_link_libraries(onnx2trt PUBLIC ${PROTOBUF_LIB} onnx nvonnxparser_static ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS}) #${CUDA_LIBRARIES}
156162

157163
# --------------------------------
158164
# API Tests

ImporterContext.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class ImporterContext final : public IImporterContext
4848
mTensorNameCounts; // Keep track of how many times a tensor name shows up, to avoid duplicate naming in TRT.
4949
StringMap<size_t>
5050
mLayerNameCounts; // Keep track of how many times a tensor name shows up, to avoid duplicate naming in TRT.
51+
std::unordered_set<std::string> mUnsupportedShapeTensors; // Container to hold any shape tensors that are the output of layers that do not support shape tensors.
52+
StringMap<std::string> mLoopTensors; // Container to map subgraph tensors to their original outer graph names.
5153
public:
5254
ImporterContext(nvinfer1::INetworkDefinition* network, nvinfer1::ILogger* logger)
5355
: _network(network)
@@ -78,6 +80,14 @@ class ImporterContext final : public IImporterContext
7880
{
7981
return mLayerPrecisions;
8082
}
83+
virtual std::unordered_set<std::string>& unsupportedShapeTensors() override
84+
{
85+
return mUnsupportedShapeTensors;
86+
}
87+
virtual StringMap<std::string>& loopTensors() override
88+
{
89+
return mLoopTensors;
90+
}
8191

8292
// This actually handles weights as well, but is named this way to be consistent with the tensors()
8393
virtual void registerTensor(TensorOrWeights tensor, const std::string& basename) override

ModelImporter.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ Status parseGraph(
172172
auto& output = outputs.at(i);
173173
ssOutputs << "[" << outputName << " -> " << output.shape() << "], ";
174174
// Note: This condition is to allow ONNX outputs to be ignored
175-
if (output && !outputName.empty())
175+
// Always register output weights (even empty ones) as it may be mapped to an unused input
176+
if ((output || output.is_weights()) && !outputName.empty())
176177
{
177178
ctx->registerTensor(std::move(output), outputName);
178179
}
@@ -331,11 +332,11 @@ bool ModelImporter::supportsModel(
331332
}
332333
}
333334
}
334-
335-
auto checkForInput = [&input_node](::ONNX_NAMESPACE::NodeProto const& node) {
335+
auto* ctx = &_importer_ctx;
336+
auto checkForInput = [&input_node, &ctx](::ONNX_NAMESPACE::NodeProto const& node) {
336337
for (auto input : node.input())
337338
{
338-
if (input_node == input)
339+
if (input_node == input || ctx->loopTensors()[input_node] == input)
339340
{
340341
return true;
341342
}
@@ -351,17 +352,23 @@ bool ModelImporter::supportsModel(
351352
cout << "Failed to sort model topologically, exiting ..." << endl;
352353
return false;
353354
}
355+
354356
for (int node_idx : topological_order)
355357
{
356358
::ONNX_NAMESPACE::NodeProto const& node = model.graph().node(node_idx);
359+
357360
// Add the node to the subgraph if:
358-
// 1. Importer function is regestiered for the operator type
359-
// 2. It is not directly connected to an unsupported input
360-
// 3. Parsing did not hit an error on the node
361+
// 1. Importer function is registered for the operator type
362+
// 2. It is NOT directly connected to an unsupported input
363+
// 3. Parsing did NOT hit an error on the node
364+
// 4. Any shape tensor output is coming from a supported node
361365
bool registered = supportsOperator(node.op_type().c_str());
362366
bool containsInput = (input_node.empty()) ? false : checkForInput(node);
363367
bool containsIndex = node_idx == error_node;
364-
if (registered && !containsInput && !containsIndex)
368+
auto const tensor = node.output(0);
369+
bool supportedShapeTensorOutput = ctx->unsupportedShapeTensors().count(tensor) == 0 ? true : false;
370+
371+
if (registered && !containsInput && !containsIndex && supportedShapeTensorOutput)
365372
{
366373
if (newSubGraph)
367374
{
@@ -447,6 +454,18 @@ void removeShapeTensorCasts(IImporterContext* ctx)
447454
{
448455
t.setType(SHAPE_TENSOR_TYPE);
449456
}
457+
// Some layers do not support shape tensor outputs. Keep track of these tensor names
458+
// for supportsModel().
459+
auto type = layer->getType();
460+
auto elementwiseOp = layer->getType() == nvinfer1::LayerType::kELEMENTWISE ? (static_cast<nvinfer1::IElementWiseLayer*>(layer))->getOperation() : nvinfer1::ElementWiseOperation::kSUM;
461+
auto reduceOp = layer->getType() == nvinfer1::LayerType::kREDUCE ? (static_cast<nvinfer1::IReduceLayer*>(layer))->getOperation() : nvinfer1::ReduceOperation::kSUM;
462+
463+
if (!supportsShapeTensor(type, elementwiseOp, reduceOp))
464+
{
465+
auto name = layer->getOutput(0)->getName();
466+
ctx->unsupportedShapeTensors().insert(name);
467+
LOG_ERROR("Found " << name << " as a shape tensor output from a layer that does not support it!");
468+
}
450469
}
451470
}
452471
}

OnnxAttrs.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ onnx2trt::ShapedWeights OnnxAttrs::get<onnx2trt::ShapedWeights>(const std::strin
109109
{
110110
::ONNX_NAMESPACE::TensorProto const& onnx_weights_tensor = this->at(key)->t();
111111
onnx2trt::ShapedWeights weights;
112-
convertOnnxWeights(onnx_weights_tensor, &weights, mCtx);
112+
// Return empty weights if conversion failed
113+
if (!convertOnnxWeights(onnx_weights_tensor, &weights, mCtx))
114+
{
115+
return onnx2trt::ShapedWeights::empty(::ONNX_NAMESPACE::TensorProto_DataType_FLOAT);
116+
}
113117
return weights;
114118
}
115119

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ ONNX models can also be converted to human-readable text:
5858

5959
onnx2trt my_model.onnx -t my_model.onnx.txt
6060

61+
ONNX models can also be optimized by ONNX's optimization libraries.
62+
To optimize an ONNX model and output a new one use `-m` to specify the output model name and `-O` to specify a semicolon-separated list of optimization passes to apply:
63+
64+
onnx2trt my_model.onnx -O "pass_1;pass_2;pass_3" -m my_model_optimized.onnx
65+
66+
See more all available optimization passes by running:
67+
68+
onnx2trt -p
69+
6170
See more usage information by running:
6271

6372
onnx2trt -h

TensorOrWeights.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,18 @@ class TensorOrWeights
6767
{
6868
return _variant == NODE_WEIGHTS;
6969
}
70+
bool isNullTensor() const
71+
{
72+
return is_tensor() && _tensor == nullptr;
73+
}
7074
nvinfer1::ITensor& tensor()
7175
{
72-
assert(is_tensor());
76+
assert(!isNullTensor());
7377
return *_tensor;
7478
}
7579
nvinfer1::ITensor const& tensor() const
7680
{
77-
assert(is_tensor());
81+
assert(!isNullTensor());
7882
return *_tensor;
7983
}
8084
ShapedWeights& weights()
@@ -99,6 +103,10 @@ class TensorOrWeights
99103
{
100104
return is_tensor() ? _tensor->getType() == nvinfer1::DataType::kINT32 : _weights.type == ::ONNX_NAMESPACE::TensorProto_DataType_INT32;
101105
}
106+
bool isBool() const
107+
{
108+
return is_tensor() ? _tensor->getType() == nvinfer1::DataType::kBOOL : _weights.type == ::ONNX_NAMESPACE::TensorProto_DataType_BOOL;
109+
}
102110
};
103111

104112
} // namespace onnx2trt

0 commit comments

Comments
 (0)