@@ -15,6 +15,27 @@ namespace {
15
15
/*
16
16
* Helper functions
17
17
*/
18
+ #if NV_TENSORRT_MAJOR < 7 || (NV_TENSORRT_MAJOR == 7 && NV_TENSORRT_MINOR < 1)
19
+ void create_plugin (ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* in, const char * name,
20
+ std::vector<int64_t > in_shape,
21
+ std::vector<int64_t > out_shape,
22
+ std::vector<int64_t > out_size,
23
+ std::string mode) {
24
+ LOG_WARNING (" Interpolation layer will be run through ATen, not TensorRT. Performance may differ." );
25
+
26
+ auto creator = new plugins::InterpolatePluginCreator ();
27
+ auto plugin = creator->createPlugin (name, in_shape, out_shape, out_size, mode, false );
28
+
29
+ auto resize_layer = ctx->net ->addPluginV2 (reinterpret_cast <nvinfer1::ITensor* const *>(&in), 1 , *plugin);
30
+ TRTORCH_CHECK (resize_layer, " Unable to create interpolation plugin from node" << *n);
31
+
32
+ resize_layer->setName (util::node_info (n).c_str ());
33
+
34
+ auto layer_output = ctx->AssociateValueAndTensor (n->outputs ()[0 ], resize_layer->getOutput (0 ));
35
+
36
+ LOG_DEBUG (" Output tensor shape: " << layer_output->getDimensions ());
37
+ }
38
+ #endif
18
39
19
40
void resize_layer_size (ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* in, std::vector<int64_t > out_shape,
20
41
nvinfer1::ResizeMode mode, bool align_corners=false ) {
@@ -27,7 +48,11 @@ void resize_layer_size(ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::
27
48
28
49
// if interpolation mode is linear, align corners must have been set to true. else, don't use align corners.
29
50
if (mode == nvinfer1::ResizeMode::kLINEAR ) {
30
- resize_layer->setAlignCorners (align_corners);
51
+ #if NV_TENSORRT_MAJOR < 7 || (NV_TENSORRT_MAJOR == 7 && NV_TENSORRT_MINOR < 1)
52
+ resize_layer->setAlignCorners (true );
53
+ #else
54
+ resize_layer->setAlignCorners (align_corners);
55
+ #endif
31
56
}
32
57
33
58
auto layer_output = ctx->AssociateValueAndTensor (n->outputs ()[0 ], resize_layer->getOutput (0 ));
@@ -123,7 +148,16 @@ auto interpolate_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
123
148
auto out_shape = in_shape;
124
149
std::copy (out_size.begin (), out_size.end (), out_shape.begin () + (in_shape.size () - out_size.size ()));
125
150
151
+ #if NV_TENSORRT_MAJOR < 7 || (NV_TENSORRT_MAJOR == 7 && NV_TENSORRT_MINOR < 1)
152
+ if (!align_corners) {
153
+ // align_corners not supported in TensorRT, create plugin and run layer through PyTorch
154
+ create_plugin (ctx, n, in, " linear1d" , in_shape, out_shape, out_size, std::string (" linear" ));
155
+ } else {
156
+ resize_layer_size (ctx, n, in, out_shape, nvinfer1::ResizeMode::kLINEAR . true );
157
+ }
158
+ #else
126
159
resize_layer_size (ctx, n, in, out_shape, nvinfer1::ResizeMode::kLINEAR , align_corners);
160
+ #endif
127
161
} else {
128
162
TRTORCH_THROW_ERROR (" Unable to convert node: " << util::node_info (n) << " \n Scale factor parameter for upsample_linear1d not supported yet." );
129
163
}
@@ -146,7 +180,16 @@ auto interpolate_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
146
180
auto out_shape = in_shape;
147
181
std::copy (out_size.begin (), out_size.end (), out_shape.begin () + (in_shape.size () - out_size.size ()));
148
182
183
+ #if NV_TENSORRT_MAJOR < 7 || (NV_TENSORRT_MAJOR == 7 && NV_TENSORRT_MINOR < 1)
184
+ if (!align_corners) {
185
+ // align_corners not supported in TensorRT, create plugin and run layer through PyTorch
186
+ create_plugin (ctx, n, in, " bilinear2d" , in_shape, out_shape, out_size, std::string (" bilinear" ));
187
+ } else {
188
+ resize_layer_size (ctx, n, in, out_shape, nvinfer1::ResizeMode::kLINEAR . true );
189
+ }
190
+ #else
149
191
resize_layer_size (ctx, n, in, out_shape, nvinfer1::ResizeMode::kLINEAR , align_corners);
192
+ #endif
150
193
} else {
151
194
TRTORCH_THROW_ERROR (" Unable to convert node: " << util::node_info (n) << " \n Scale factor parameter for upsample_bilinear2d not supported yet." );
152
195
}
@@ -169,7 +212,16 @@ auto interpolate_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
169
212
auto out_shape = in_shape;
170
213
std::copy (out_size.begin (), out_size.end (), out_shape.begin () + (in_shape.size () - out_size.size ()));
171
214
215
+ #if NV_TENSORRT_MAJOR < 7 || (NV_TENSORRT_MAJOR == 7 && NV_TENSORRT_MINOR < 1)
216
+ if (!align_corners) {
217
+ // align_corners not supported in TensorRT, create plugin and run layer through PyTorch
218
+ create_plugin (ctx, n, in, " trilinear3d" , in_shape, out_shape, out_size, std::string (" trilinear" ));
219
+ } else {
220
+ resize_layer_size (ctx, n, in, out_shape, nvinfer1::ResizeMode::kLINEAR . true );
221
+ }
222
+ #else
172
223
resize_layer_size (ctx, n, in, out_shape, nvinfer1::ResizeMode::kLINEAR , align_corners);
224
+ #endif
173
225
} else {
174
226
TRTORCH_THROW_ERROR (" Unable to convert node: " << util::node_info (n) << " \n Scale factor parameter for upsample_trilinear3d not supported yet." );
175
227
}
0 commit comments