1
1
#include " torch/torch.h"
2
2
#include " core/util/prelude.h"
3
3
#include " core/conversion/converters/converters.h"
4
-
5
- #include < csignal>
4
+ #include " plugins/interpolate_plugin.h"
5
+ #include " NvInfer.h"
6
+ #include " NvInferRuntimeCommon.h"
6
7
7
8
namespace trtorch {
8
9
namespace core {
@@ -11,6 +12,54 @@ namespace converters {
11
12
namespace impl {
12
13
namespace {
13
14
15
+ /*
16
+ * Helper functions
17
+ */
18
+
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
+
39
+ void resize_layer_size (ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* in, std::vector<int64_t > out_shape,
40
+ nvinfer1::ResizeMode mode) {
41
+ auto resize_layer = ctx->net ->addResize (*in);
42
+ TRTORCH_CHECK (resize_layer, " Unable to create interpolation (resizing) layer from node" << *n);
43
+
44
+ resize_layer->setOutputDimensions (util::toDims (out_shape));
45
+ resize_layer->setResizeMode (mode);
46
+ resize_layer->setName (util::node_info (n).c_str ());
47
+
48
+ // if interpolation mode is linear, align corners must have been set to true. else, don't use align corners.
49
+ if (mode == nvinfer1::ResizeMode::kLINEAR ) {
50
+ resize_layer->setAlignCorners (true );
51
+ }
52
+
53
+ auto layer_output = ctx->AssociateValueAndTensor (n->outputs ()[0 ], resize_layer->getOutput (0 ));
54
+
55
+ LOG_DEBUG (" Output tensor shape: " << layer_output->getDimensions ());
56
+ }
57
+
58
+
59
+ /*
60
+ * Interpolate Converter
61
+ */
62
+
14
63
auto interpolate_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
15
64
.pattern({
16
65
" aten::upsample_nearest1d(Tensor self, int[1] output_size, float? scales=None) -> (Tensor)" ,
@@ -27,15 +76,7 @@ auto interpolate_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
27
76
auto out_shape = in_shape;
28
77
std::copy (out_size.begin (), out_size.end (), out_shape.begin () + (in_shape.size () - out_size.size ()));
29
78
30
- auto resize_layer = ctx->net ->addResize (*in);
31
- TRTORCH_CHECK (resize_layer, " Unable to create interpolation (resizing) layer from node" << *n);
32
-
33
- resize_layer->setOutputDimensions (util::toDims (out_shape));
34
- resize_layer->setResizeMode (nvinfer1::ResizeMode::kNEAREST );
35
- resize_layer->setName (util::node_info (n).c_str ());
36
-
37
- auto layer_output = ctx->AssociateValueAndTensor (n->outputs ()[0 ], resize_layer->getOutput (0 ));
38
- LOG_DEBUG (" Output tensor shape: " << layer_output->getDimensions ());
79
+ resize_layer_size (ctx, n, in, out_shape, nvinfer1::ResizeMode::kNEAREST );
39
80
} else {
40
81
TRTORCH_THROW_ERROR (" Unable to convert node: " << util::node_info (n) << " \n Scale factor parameter for upsample_nearest1d not supported yet." );
41
82
}
@@ -57,15 +98,7 @@ auto interpolate_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
57
98
auto out_shape = in_shape;
58
99
std::copy (out_size.begin (), out_size.end (), out_shape.begin () + (in_shape.size () - out_size.size ()));
59
100
60
- auto resize_layer = ctx->net ->addResize (*in);
61
- TRTORCH_CHECK (resize_layer, " Unable to create interpolation (resizing) layer from node" << *n);
62
-
63
- resize_layer->setOutputDimensions (util::toDims (out_shape));
64
- resize_layer->setResizeMode (nvinfer1::ResizeMode::kNEAREST );
65
- resize_layer->setName (util::node_info (n).c_str ());
66
-
67
- auto layer_output = ctx->AssociateValueAndTensor (n->outputs ()[0 ], resize_layer->getOutput (0 ));
68
- LOG_DEBUG (" Output tensor shape: " << layer_output->getDimensions ());
101
+ resize_layer_size (ctx, n, in, out_shape, nvinfer1::ResizeMode::kNEAREST );
69
102
} else {
70
103
TRTORCH_THROW_ERROR (" Unable to convert node: " << util::node_info (n) << " \n Scale factor parameter for upsample_nearest2d not supported yet." );
71
104
}
@@ -86,18 +119,94 @@ auto interpolate_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
86
119
87
120
auto out_shape = in_shape;
88
121
std::copy (out_size.begin (), out_size.end (), out_shape.begin () + (in_shape.size () - out_size.size ()));
122
+
123
+ resize_layer_size (ctx, n, in, out_shape, nvinfer1::ResizeMode::kNEAREST );
124
+ } else {
125
+ TRTORCH_THROW_ERROR (" Unable to convert node: " << util::node_info (n) << " \n Scale factor parameter for upsample_nearest3d not supported yet." );
126
+ }
127
+
128
+ return true ;
129
+ }
130
+ }).pattern({
131
+ " aten::upsample_linear1d(Tensor self, int[1] output_size, bool align_corners, float? scales=None) -> (Tensor)" ,
132
+ [](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
133
+ auto in = args[0 ].ITensor ();
134
+ auto in_shape = util::toVec (in->getDimensions ());
135
+ bool align_corners = args[2 ].unwrapToBool ();
136
+
137
+ // Case 1: user uses output size and not scales
138
+ if (!args[1 ].IValue ()->isNone () && args[3 ].IValue ()->isNone ()) {
139
+ auto out_size = util::toVec (util::toDims (args[1 ].unwrapToIntList ()));
140
+
141
+ TRTORCH_ASSERT (out_size.size () == 1 , " aten::upsample_linear1d input Tensor and output size dimension mismatch" );
89
142
90
- auto resize_layer = ctx-> net -> addResize (*in);
91
- TRTORCH_CHECK (resize_layer, " Unable to create interpolation (resizing) layer from node " << *n );
143
+ auto out_shape = in_shape;
144
+ std::copy (out_size. begin (), out_size. end (), out_shape. begin () + (in_shape. size () - out_size. size ()) );
92
145
93
- resize_layer->setOutputDimensions (util::toDims (out_shape));
94
- resize_layer->setResizeMode (nvinfer1::ResizeMode::kNEAREST );
95
- resize_layer->setName (util::node_info (n).c_str ());
146
+ if (!align_corners) {
147
+ // align_corners not supported in TensorRT, create plugin and run layer through PyTorch
148
+ create_plugin (ctx, n, in, " linear1d" , in_shape, out_shape, out_size, std::string (" linear" ));
149
+ } else {
150
+ resize_layer_size (ctx, n, in, out_shape, nvinfer1::ResizeMode::kLINEAR );
151
+ }
152
+ } else {
153
+ TRTORCH_THROW_ERROR (" Unable to convert node: " << util::node_info (n) << " \n Scale factor parameter for upsample_linear1d not supported yet." );
154
+ }
155
+
156
+ return true ;
157
+ }
158
+ }).pattern({
159
+ " aten::upsample_bilinear2d(Tensor self, int[2] output_size, bool align_corners, float? scales_h=None, float? scales_w=None) -> (Tensor)" ,
160
+ [](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
161
+ auto in = args[0 ].ITensor ();
162
+ auto in_shape = util::toVec (in->getDimensions ());
163
+ bool align_corners = args[2 ].unwrapToBool ();
96
164
97
- auto layer_output = ctx->AssociateValueAndTensor (n->outputs ()[0 ], resize_layer->getOutput (0 ));
98
- LOG_DEBUG (" Output tensor shape: " << layer_output->getDimensions ());
165
+ // Case 1: user uses output size and not scales_h, scales_w
166
+ if (!args[1 ].IValue ()->isNone () && args[3 ].IValue ()->isNone () && args[4 ].IValue ()->isNone ()) {
167
+ auto out_size = util::toVec (util::toDims (args[1 ].unwrapToIntList ()));
168
+
169
+ TRTORCH_ASSERT (out_size.size () == 2 , " aten::upsample_bilinear2d input Tensor and output size dimension mismatch" );
170
+
171
+ auto out_shape = in_shape;
172
+ std::copy (out_size.begin (), out_size.end (), out_shape.begin () + (in_shape.size () - out_size.size ()));
173
+
174
+ if (!align_corners) {
175
+ // align_corners not supported in TensorRT, create plugin and run layer through PyTorch
176
+ create_plugin (ctx, n, in, " bilinear2d" , in_shape, out_shape, out_size, std::string (" bilinear" ));
177
+ } else {
178
+ resize_layer_size (ctx, n, in, out_shape, nvinfer1::ResizeMode::kLINEAR );
179
+ }
99
180
} else {
100
- TRTORCH_THROW_ERROR (" Unable to convert node: " << util::node_info (n) << " \n Scale factor parameter for upsample_nearest3d not supported yet." );
181
+ TRTORCH_THROW_ERROR (" Unable to convert node: " << util::node_info (n) << " \n Scale factor parameter for upsample_bilinear2d not supported yet." );
182
+ }
183
+
184
+ return true ;
185
+ }
186
+ }).pattern({
187
+ " aten::upsample_trilinear3d(Tensor self, int[3] output_size, bool align_corners, float? scales_d=None, float? scales_h=None, float? scales_w=None) -> (Tensor)" ,
188
+ [](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
189
+ auto in = args[0 ].ITensor ();
190
+ auto in_shape = util::toVec (in->getDimensions ());
191
+ bool align_corners = args[2 ].unwrapToBool ();
192
+
193
+ // Case 1: user uses output size and not scales_d, scales_h, scales_w
194
+ if (!args[1 ].IValue ()->isNone () && args[3 ].IValue ()->isNone () && args[4 ].IValue ()->isNone () && args[5 ].IValue ()->isNone ()) {
195
+ auto out_size = util::toVec (util::toDims (args[1 ].unwrapToIntList ()));
196
+
197
+ TRTORCH_ASSERT (out_size.size () == 3 , " aten::upsample_trilinear3d input Tensor and output size dimension mismatch" );
198
+
199
+ auto out_shape = in_shape;
200
+ std::copy (out_size.begin (), out_size.end (), out_shape.begin () + (in_shape.size () - out_size.size ()));
201
+
202
+ if (!align_corners) {
203
+ // align_corners not supported in TensorRT, create plugin and run layer through PyTorch
204
+ create_plugin (ctx, n, in, " trilinear3d" , in_shape, out_shape, out_size, std::string (" trilinear" ));
205
+ } else {
206
+ resize_layer_size (ctx, n, in, out_shape, nvinfer1::ResizeMode::kLINEAR );
207
+ }
208
+ } else {
209
+ TRTORCH_THROW_ERROR (" Unable to convert node: " << util::node_info (n) << " \n Scale factor parameter for upsample_trilinear3d not supported yet." );
101
210
}
102
211
103
212
return true ;
@@ -110,4 +219,4 @@ auto interpolate_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
110
219
} // namespace converters
111
220
} // namespace conversion
112
221
} // namespace core
113
- } // namespace trtorch
222
+ } // namespace trtorch
0 commit comments