Skip to content

Commit a3a4e38

Browse files
authored
Update resize limitations in TensorRT (#538)
Signed-off-by: Kevin Chen <kevinch@nvidia.com>
1 parent fbebb14 commit a3a4e38

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

builtin_op_importers.cpp

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,9 +2536,11 @@ DEFINE_BUILTIN_OP_IMPORTER(Resize)
25362536
{
25372537
nvinfer1::ITensor& input = convertToTensor(inputs.at(0), ctx);
25382538
// TRT does not support INT32 nor BOOL input types for this node
2539-
ASSERT(input.getType() != nvinfer1::DataType::kINT32 && input.getType() != nvinfer1::DataType::kBOOL, ErrorCode::kUNSUPPORTED_NODE);
2539+
ASSERT( (input.getType() != nvinfer1::DataType::kINT32
2540+
&& input.getType() != nvinfer1::DataType::kBOOL)
2541+
&& "This version of TensorRT does not support INT32 or BOOL input for the Resize operator.", ErrorCode::kUNSUPPORTED_NODE);
25402542
int inputRank = input.getDimensions().nbDims;
2541-
ASSERT(inputRank > 0, ErrorCode::kUNSUPPORTED_NODE);
2543+
ASSERT( (inputRank > 0) && "The input tensor cannot be a scalar.", ErrorCode::kUNSUPPORTED_NODE);
25422544
// Add resize layer
25432545
nvinfer1::IResizeLayer* layer = ctx->network()->addResize(input);
25442546
ctx->registerLayer(layer, node.name());
@@ -2563,22 +2565,57 @@ DEFINE_BUILTIN_OP_IMPORTER(Resize)
25632565
&& "This version of TensorRT only supports floor nearest_mode!",
25642566
ErrorCode::kUNSUPPORTED_NODE);
25652567

2566-
// Note both asymmetric and align_corners resize modes go through the same import path in TRT:
2567-
if (transformationMode == "asymmetric" || transformationMode == "align_corners")
2568-
{
2569-
layer->setAlignCorners(true);
2570-
}
2571-
25722568
// The existence of a fourth input means a shape was passed as the resize parameter
2569+
// For ONNX resize with the "sizes", TensorRT's resize maps to ONNX's in the following ways:
2570+
// Nearest:
2571+
// alignCorners = 0: ASYMMETRIC
2572+
// alignCorners = 1: ALIGN_CORNERS
2573+
// Linear:
2574+
// alignCorners = 0: HALF_PIXEL
2575+
// alignCorners = 1: ALIGN_CORNERS
25732576
if (inputs.size() == 4)
25742577
{
2578+
if (transformationMode == "align_corners")
2579+
{
2580+
layer->setAlignCorners(true);
2581+
}
2582+
if (mode == "nearest")
2583+
{
2584+
ASSERT((transformationMode == "asymmetric" || transformationMode == "align_corners") && "TensorRT only supports asymmetric and align_corners transformation modes for nearest neighbor resizes when sizes are provided!", ErrorCode::kUNSUPPORTED_NODE);
2585+
}
2586+
else if (mode == "linear")
2587+
{
2588+
ASSERT((transformationMode == "half_pixel" || transformationMode == "pytorch_half_pixel" || transformationMode == "align_corners") && "TensorRT only supports half_pixel, pytorch_half_pixel, and align_corners transofmration modes for linear resizes when sizes are provided!", ErrorCode::kUNSUPPORTED_NODE);
2589+
}
25752590
auto* resizeShape = &convertToTensor(inputs.at(3), ctx);
25762591
layer->setInput(1, *resizeShape);
25772592
layer->setResizeMode(resizeMode);
25782593
RETURN_FIRST_OUTPUT(layer);
25792594
}
2595+
// For ONNX resize with "scales", TensorRT's resize maps to ONNX's in the following ways:
2596+
// Nearest:
2597+
// alignCorners = 0: ASYMMETRIC
2598+
// alignCorners = 1: ASYMMETRIC
2599+
// Linear:
2600+
// alignCorners = 0: HALF_PIXEL
2601+
// alignCorners = 1: ASYMMETRIC
2602+
else
2603+
{
2604+
if (mode == "nearest")
2605+
{
2606+
ASSERT(transformationMode == "asymmetric" && "TensorRT only supports asymmetric tranformation mode for nearest neighbor resizes when scales are provided!",ErrorCode::kUNSUPPORTED_NODE);
2607+
}
2608+
else if (mode == "linear")
2609+
{
2610+
ASSERT((transformationMode == "asymmetric" || transformationMode == "pytorch_half_pixel" || transformationMode == "half_pixel") && "TensorRT only supports half pixel, pytorch half_pixel, and asymmetric tranformation mode for linear resizes when scales are provided!", ErrorCode::kUNSUPPORTED_NODE);
2611+
if (transformationMode == "asymmetric")
2612+
{
2613+
layer->setAlignCorners(true);
2614+
}
2615+
}
2616+
}
25802617
}
2581-
// For opset 10 resize, the only supported mode is asymmetric resize, which is mapped to TRT's alignCorners.
2618+
// For opset 10 resize, the only supported mode is asymmetric resize with scales.
25822619
else
25832620
{
25842621
transformationMode = "asymmetric";

0 commit comments

Comments
 (0)