@@ -10,13 +10,20 @@ namespace converters {
10
10
namespace impl {
11
11
namespace {
12
12
13
- bool GlobalPoolingConverter (ConversionCtx* ctx, const torch::jit::Node* n, args& args, nvinfer1::PoolingType pool_type)
14
- {
13
+ bool GlobalPoolingConverter (
14
+ ConversionCtx* ctx,
15
+ const torch::jit::Node* n,
16
+ args& args,
17
+ nvinfer1::PoolingType pool_type) {
15
18
auto in = args[0 ].ITensorOrFreeze (ctx);
16
19
nvinfer1::Dims dims = in->getDimensions ();
17
20
// Generate a bitmask of all 1s except the last 2 bits (N and C axes)
18
21
uint32_t reduceAxes = ((1 << dims.nbDims ) - 1 ) & ~0b11 ;
19
- auto * new_layer = ctx->net ->addReduce (*in, pool_type == nvinfer1::PoolingType::kMAX ? nvinfer1::ReduceOperation::kMAX : nvinfer1::ReduceOperation::kAVG , reduceAxes, /* keepDimensions=*/ true );
22
+ auto * new_layer = ctx->net ->addReduce (
23
+ *in,
24
+ pool_type == nvinfer1::PoolingType::kMAX ? nvinfer1::ReduceOperation::kMAX : nvinfer1::ReduceOperation::kAVG ,
25
+ reduceAxes,
26
+ /* keepDimensions=*/ true );
20
27
21
28
new_layer->setName (util::node_info (n).c_str ());
22
29
@@ -26,26 +33,31 @@ bool GlobalPoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, args&
26
33
return true ;
27
34
}
28
35
29
- bool AdaptivePoolingConverter (ConversionCtx* ctx, const torch::jit::Node* n, args& args, nvinfer1::PoolingType pool_type) {
36
+ bool AdaptivePoolingConverter (
37
+ ConversionCtx* ctx,
38
+ const torch::jit::Node* n,
39
+ args& args,
40
+ nvinfer1::PoolingType pool_type) {
30
41
auto in = args[0 ].ITensorOrFreeze (ctx);
31
42
auto out_size = util::toDims (args[1 ].unwrapToIntList ());
32
43
33
44
// Corner case: when out dimension is all ones, replace with simpler operation
34
- if (out_size.d [0 ] == 1 && (out_size.nbDims < 2 || out_size.d [1 ] == 1 ) && (out_size.nbDims < 3 || out_size.d [2 ] == 1 )) {
45
+ if (out_size.d [0 ] == 1 && (out_size.nbDims < 2 || out_size.d [1 ] == 1 ) &&
46
+ (out_size.nbDims < 3 || out_size.d [2 ] == 1 )) {
35
47
return GlobalPoolingConverter (ctx, n, args, pool_type);
36
48
}
37
49
38
50
auto orig_dims = in->getDimensions ();
39
- bool expandDims = (orig_dims.nbDims < 4 );
40
-
51
+ bool expandDims = (orig_dims.nbDims < 4 );
52
+
41
53
if (expandDims) {
42
54
in = addPadding (ctx, n, in, 4 , false , false );
43
55
}
44
-
56
+
45
57
if (out_size.nbDims == 1 ) {
46
58
out_size = util::unsqueezeDims (out_size, 0 , 1 );
47
59
}
48
-
60
+
49
61
auto in_shape = util::toVec (in->getDimensions ());
50
62
nvinfer1::ILayer* new_layer = nullptr ;
51
63
@@ -57,29 +69,37 @@ bool AdaptivePoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, arg
57
69
LOG_WARNING (
58
70
" Adaptive pooling layer will be run through ATen (on CPU), via not TensorRT, performace will suffer. Consider switching either to static input shape or moving to non adaptive pooling" );
59
71
#endif
60
-
61
- TRTORCH_CHECK (pool_type == nvinfer1::PoolingType::kAVERAGE ,
62
- " Unable to create MAX pooling (interpolation) plugin from node" << *n);
72
+
73
+ TRTORCH_CHECK (
74
+ pool_type == nvinfer1::PoolingType::kAVERAGE ,
75
+ " Unable to create MAX pooling (interpolation) plugin from node" << *n);
63
76
64
77
auto out_shape = in_shape;
65
78
std::copy_n (out_size.d , out_size.nbDims , out_shape.begin () + (in_shape.size () - out_size.nbDims ));
66
79
67
80
auto creator = new plugins::InterpolatePluginCreator ();
68
- auto plugin = creator->createPlugin (" adaptive_pool2d" , in_shape, out_shape,
69
- util::toVec (out_size), {}, std::string (" adaptive_pool2d" ), false , false );
81
+ auto plugin = creator->createPlugin (
82
+ " adaptive_pool2d" ,
83
+ in_shape,
84
+ out_shape,
85
+ util::toVec (out_size),
86
+ {},
87
+ std::string (" adaptive_pool2d" ),
88
+ false ,
89
+ false );
70
90
71
91
new_layer = ctx->net ->addPluginV2 (reinterpret_cast <nvinfer1::ITensor* const *>(&in), 1 , *plugin);
72
92
TRTORCH_CHECK (new_layer, " Unable to create pooling (interpolation) plugin from node" << *n);
73
93
74
94
} else {
75
95
std::vector<int64_t > stride (out_size.nbDims );
76
- for (size_t i = 0 ; i < out_size.nbDims ; i++) {
96
+ for (int64_t i = 0 ; i < out_size.nbDims ; i++) {
77
97
stride[(stride.size () - 1 ) - i] = in_shape[(in_shape.size () - 1 ) - i] / out_size.d [(out_size.nbDims - 1 ) - i];
78
98
}
79
99
LOG_DEBUG (" Stride: " << util::toDims (stride));
80
100
81
101
std::vector<int64_t > window (out_size.nbDims );
82
- for (size_t i = 0 ; i < out_size.nbDims ; i++) {
102
+ for (int64_t i = 0 ; i < out_size.nbDims ; i++) {
83
103
window[window.size () - 1 - i] =
84
104
in_shape[in_shape.size () - 1 - i] - (out_size.d [out_size.nbDims - 1 - i] - 1 ) * stride[stride.size () - 1 - i];
85
105
}
@@ -92,18 +112,18 @@ bool AdaptivePoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, arg
92
112
new_layer = pooling_layer;
93
113
}
94
114
95
- new_layer->setName (util::node_info (n).c_str ());
115
+ new_layer->setName (util::node_info (n).c_str ());
96
116
auto layer_output = addUnpadding (ctx, n, new_layer->getOutput (0 ), orig_dims.nbDims , false , false );
97
117
98
118
ctx->AssociateValueAndTensor (n->outputs ()[0 ], layer_output);
99
119
LOG_DEBUG (" Output tensor shape: " << layer_output->getDimensions ());
100
120
101
121
return true ;
102
122
}
103
-
123
+
104
124
bool PoolingConverter (ConversionCtx* ctx, const torch::jit::Node* n, args& args, nvinfer1::PoolingType pool_type) {
105
125
auto in = args[0 ].ITensorOrFreeze (ctx);
106
-
126
+
107
127
// Max Pool needs at least 4D input
108
128
auto orig_dims = in->getDimensions ();
109
129
bool expandDims = (orig_dims.nbDims < 4 );
@@ -131,7 +151,7 @@ bool PoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, args& args,
131
151
if (stride.nbDims == 1 ) {
132
152
stride = util::unsqueezeDims (stride, 0 , 1 );
133
153
}
134
-
154
+
135
155
LOG_DEBUG (" kernel_size: " << kernel_size);
136
156
LOG_DEBUG (" padding: " << padding);
137
157
LOG_DEBUG (" stride: " << stride);
@@ -165,20 +185,20 @@ bool PoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, args& args,
165
185
166
186
auto padding_mode =
167
187
ceil_mode ? nvinfer1::PaddingMode::kEXPLICIT_ROUND_UP : nvinfer1::PaddingMode::kEXPLICIT_ROUND_DOWN ;
168
-
188
+
169
189
new_layer->setName (util::node_info (n).c_str ());
170
190
new_layer->setPaddingMode (padding_mode);
171
191
new_layer->setPaddingNd (padding);
172
192
new_layer->setStrideNd (stride);
173
-
193
+
174
194
if (stride.nbDims != 2 && ctx->settings .device .device_type == nvinfer1::DeviceType::kDLA ) {
175
195
if (!ctx->settings .device .allow_gpu_fallback ) {
176
196
TRTORCH_THROW_ERROR (" DLA Pooling stride is limited to 2D, allow GPU fallback" );
177
197
} else {
178
198
LOG_WARNING (" DLA Pooling stride is limited to 2D, will run on GPU" );
179
199
}
180
200
}
181
-
201
+
182
202
auto out_tensor = addUnpadding (ctx, n, new_layer->getOutput (0 ), orig_dims.nbDims , false , true );
183
203
ctx->AssociateValueAndTensor (n->outputs ()[0 ], out_tensor);
184
204
@@ -220,12 +240,12 @@ auto pooling_registrations TRTORCH_UNUSED =
220
240
}})
221
241
.pattern({" aten::adaptive_avg_pool1d(Tensor self, int[1] output_size) -> (Tensor)" ,
222
242
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
223
- return AdaptivePoolingConverter (ctx, n, args, nvinfer1::PoolingType::kAVERAGE );
224
- }})
243
+ return AdaptivePoolingConverter (ctx, n, args, nvinfer1::PoolingType::kAVERAGE );
244
+ }})
225
245
.pattern({" aten::adaptive_avg_pool2d(Tensor self, int[2] output_size) -> (Tensor)" ,
226
246
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
227
- return AdaptivePoolingConverter (ctx, n, args, nvinfer1::PoolingType::kAVERAGE );
228
- }});
247
+ return AdaptivePoolingConverter (ctx, n, args, nvinfer1::PoolingType::kAVERAGE );
248
+ }});
229
249
} // namespace
230
250
} // namespace impl
231
251
} // namespace converters
0 commit comments