Skip to content

Commit c6abb4b

Browse files
committed
Addressing code review comments
Signed-off-by: Boris Fomitchev <[email protected]>
1 parent e1ba7c0 commit c6abb4b

File tree

8 files changed

+82
-90
lines changed

8 files changed

+82
-90
lines changed

core/conversion/converters/BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ cc_library(
3030
name = "converters",
3131
hdrs = [
3232
"converters.h",
33-
"converter_util.h",
33+
"converter_util.h",
3434
],
3535
srcs = [
3636
"NodeConverterRegistry.cpp",
37-
"converter_util.cpp",
37+
"converter_util.cpp",
3838
"impl/activation.cpp",
3939
"impl/batch_norm.cpp",
4040
"impl/concat.cpp",

core/conversion/converters/converter_util.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,44 @@ namespace core {
77
namespace conversion {
88
namespace converters {
99

10-
nvinfer1::ILayer* addPaddingLayer(ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* tensor, int nDim, bool trailing, bool use_zeros) {
10+
nvinfer1::ITensor* addPadding(ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* tensor, int nDim, bool trailing, bool use_zeros) {
1111
const auto dims = tensor->getDimensions();
1212

1313
if (dims.nbDims < nDim) {
1414
auto newDims = dims;
15-
for (int dim = dims.nbDims; dim < nDim; ++dim)
15+
for (int dim = dims.nbDims; dim < nDim; ++dim) {
1616
newDims = util::unsqueezeDims(newDims, trailing ? dim : 0, 1, use_zeros);
17-
LOG_DEBUG(
18-
"Input shape is less than 4D got: " << dims
19-
<< ", inserting shuffle layer to reshape to 4D tensor shape: " << newDims);
17+
}
2018

2119
LOG_DEBUG("Original shape: " << dims << ", reshaping to: " << newDims);
2220
auto shuffle_layer = ctx->net->addShuffle(*tensor);
2321
TRTORCH_CHECK(shuffle_layer, "Unable to create shuffle layer");
2422
shuffle_layer->setReshapeDimensions(newDims);
2523
shuffle_layer->setZeroIsPlaceholder(use_zeros);
2624
shuffle_layer->setName((util::node_info(n) + " [Reshape to " + util::toStr(newDims) + ']').c_str());
27-
return shuffle_layer;
28-
} else
25+
return shuffle_layer->getOutput(0);
26+
} else {
2927
return nullptr;
28+
}
3029
}
3130

32-
nvinfer1::ILayer* addUnpaddingLayer(ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* tensor, int nDim, bool trailing, bool use_zeros) {
31+
nvinfer1::ITensor* addUnpadding(ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* tensor, int nDim, bool trailing, bool use_zeros) {
3332
const auto dims = tensor->getDimensions();
3433
if (dims.nbDims > nDim) {
3534
auto newDims = dims;
36-
for (int dim = dims.nbDims; dim > nDim; --dim)
35+
for (int dim = dims.nbDims; dim > nDim; --dim) {
3736
newDims = util::squeezeDims(newDims, trailing ? dim - 1 : 0);
37+
}
3838
LOG_DEBUG("Original shape: " << dims << ", reshaping to: " << newDims);
3939
auto shuffle_layer = ctx->net->addShuffle(*tensor);
4040
TRTORCH_CHECK(shuffle_layer, "Unable to create shuffle layer");
4141
shuffle_layer->setReshapeDimensions(newDims);
4242
shuffle_layer->setZeroIsPlaceholder(use_zeros);
4343
shuffle_layer->setName((util::node_info(n) + " [Reshape to " + util::toStr(newDims)).c_str() + ']');
44-
return shuffle_layer;
45-
} else
46-
return nullptr;
44+
return shuffle_layer->getOutput(0);
45+
} else {
46+
return tensor;
47+
}
4748
}
4849

4950
} // namespace converters

core/conversion/converters/converter_util.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@ namespace trtorch {
1212
namespace core {
1313
namespace conversion {
1414
namespace converters {
15-
nvinfer1::ILayer* addPaddingLayer(ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* tensor, int nDim, bool trailing=true, bool use_zeros=true);
16-
nvinfer1::ILayer* addUnpaddingLayer(ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* tensor, int nDim, bool trailing=true, bool use_zeros=true);
15+
16+
// If nDim < tensor size, adds shuffle layer to pad tensor with 1s (at the end if trailing) and returns (nDim-dimensional) shuffle layer's output.
17+
// Otherwise, does nothing and passes tensor through.
18+
// use _zeros controls whether we should be using 0 instead of -1 on the shape.
19+
nvinfer1::ITensor* addPadding(ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* tensor, int nDim, bool trailing=true, bool use_zeros=true);
20+
21+
// If nDim < tensor size, adds shuffle layer to un-pad tensor (at the end if trailing) and returns (nDim-dimensional) shuffle layer's output
22+
// Otherwise, does nothing and passes tensor through.
23+
// use _zeros controls whether we should be using 0 instead of -1 on the shape.
24+
nvinfer1::ITensor* addUnpadding(ConversionCtx* ctx, const torch::jit::Node* n, nvinfer1::ITensor* tensor, int nDim, bool trailing=true, bool use_zeros=true);
25+
1726
} // namespace converters
1827
} // namespace conversion
1928
} // namespace core

core/conversion/converters/impl/batch_norm.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ auto batch_norm_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns().
4141
LOG_DEBUG("training disregarded");
4242
LOG_DEBUG("cudnn disregarded");
4343

44-
auto expandDims = addPaddingLayer(ctx, n, input, 4);
44+
// Expand spatial dims from 1D to 2D if needed
45+
bool expandDims = (orig_shape.nbDims < 4);
46+
4547
if (expandDims) {
46-
input = expandDims->getOutput(0);
48+
input = addPadding(ctx, n, input, 4);
4749
}
4850

4951
auto scale = gamma / torch::sqrt(var + eps);
@@ -56,13 +58,8 @@ auto batch_norm_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns().
5658
auto bn = ctx->net->addScaleNd(
5759
*input, nvinfer1::ScaleMode::kCHANNEL, bias_weights.data, scale_weights.data, power.data, 1);
5860
bn->setName(util::node_info(n).c_str());
59-
auto out_tensor = bn->getOutput(0);
60-
61-
if (expandDims) {
62-
LOG_DEBUG("Inserting shuffle layer to reshape to back to original shape: " << orig_shape);
63-
auto new_layer = addUnpaddingLayer(ctx, n, out_tensor, orig_shape.nbDims);
64-
out_tensor = new_layer->getOutput(0);
65-
}
61+
// Un-pad bn output if needed
62+
auto out_tensor = addUnpadding(ctx, n, bn->getOutput(0), orig_shape.nbDims);
6663
ctx->AssociateValueAndTensor(n->outputs()[0], out_tensor);
6764
return true;
6865
}});

core/conversion/converters/impl/conv_deconv.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,32 @@ bool add_conv_deconv(ConversionCtx* ctx, const torch::jit::Node* n, args& args)
2525
LOG_DEBUG("Original input dims: " << orig_dims);
2626

2727
// Expand spatial dims from 1D to 2D if needed
28-
auto expandDims = addPaddingLayer(ctx, n, in, 4);
28+
bool expandDims = (orig_dims.nbDims < 4);
2929
if (expandDims) {
30-
auto tensorPtr = expandDims->getOutput(0);
31-
assert(tensorPtr);
32-
dims = tensorPtr->getDimensions();
33-
in = tensorPtr;
30+
in = addPadding(ctx, n, in, 4);
31+
dims = in->getDimensions();
3432
}
3533
if (w.shape.nbDims < 4) {
36-
for (int i = w.shape.nbDims; i < 4; ++i)
34+
for (int i = w.shape.nbDims; i < 4; ++i) {
3735
w.shape.d[i] = 1;
36+
}
3837
w.shape.nbDims = 4;
3938
w.kernel_shape.nbDims = 2;
4039
w.kernel_shape.d[1] = 1;
4140
}
42-
if (stride.nbDims==1)
41+
if (stride.nbDims==1) {
4342
stride = util::unsqueezeDims(stride, 1, 1);
44-
if (dilation.nbDims==1)
43+
}
44+
if (dilation.nbDims==1) {
4545
dilation = util::unsqueezeDims(dilation, 1, 1);
46-
if (padding.nbDims==1)
46+
}
47+
if (padding.nbDims==1) {
4748
padding = util::unsqueezeDims(padding, 1, 0);
48-
if (out_padding.nbDims==1)
49+
}
50+
if (out_padding.nbDims==1) {
4951
out_padding = util::unsqueezeDims(out_padding, 1, 0);
50-
52+
}
53+
5154
LOG_DEBUG("Input dims: " << dims);
5255
LOG_DEBUG("Weights: " << w);
5356
LOG_DEBUG("stride: " << stride);
@@ -101,14 +104,13 @@ bool add_conv_deconv(ConversionCtx* ctx, const torch::jit::Node* n, args& args)
101104
conv->setNbGroups(groups);
102105
new_layer = conv;
103106
}
107+
104108
new_layer->setName(util::node_info(n).c_str());
105-
106-
if (expandDims) {
107-
// Un-expand spatial dims back to 1D
108-
new_layer = addUnpaddingLayer(ctx, n, new_layer->getOutput(0), orig_dims.nbDims);
109-
}
110-
111-
auto out = ctx->AssociateValueAndTensor(n->outputs()[0], new_layer->getOutput(0));
109+
110+
// Un-expand spatial dims back to 1D if needed
111+
auto out = addUnpadding(ctx, n, new_layer->getOutput(0), orig_dims.nbDims);
112+
113+
ctx->AssociateValueAndTensor(n->outputs()[0], out);
112114

113115
LOG_DEBUG("Output tensor shape: " << out->getDimensions());
114116

core/conversion/converters/impl/pooling.cpp

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,23 @@ bool GlobalPoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, args&
2929
bool AdaptivePoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, args& args, nvinfer1::PoolingType pool_type) {
3030
auto in = args[0].ITensorOrFreeze(ctx);
3131
auto out_size = util::toDims(args[1].unwrapToIntList());
32-
bool shuffle_back = false;
3332

3433
// Corner case: when out dimension is all ones, replace with simpler operation
3534
if (out_size.d[0] == 1 && (out_size.nbDims < 2 || out_size.d[1] == 1 ) && (out_size.nbDims < 3 || out_size.d[2] == 1 )) {
3635
return GlobalPoolingConverter(ctx, n, args, pool_type);
3736
}
3837

39-
auto shuffle = addPaddingLayer(ctx, n, in, 4, false, false);
40-
if (shuffle) {
41-
in = shuffle->getOutput(0);
38+
auto orig_dims = in->getDimensions();
39+
bool expandDims = (orig_dims.nbDims < 4);
40+
41+
if (expandDims) {
42+
in = addPadding(ctx, n, in, 4, false, false);
4243
}
4344

4445
if (out_size.nbDims == 1) {
4546
out_size = util::unsqueezeDims(out_size, 0, 1);
46-
shuffle_back = true;
4747
}
48+
4849
auto in_shape = util::toVec(in->getDimensions());
4950
nvinfer1::ILayer* new_layer = nullptr;
5051

@@ -91,13 +92,10 @@ bool AdaptivePoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, arg
9192
new_layer = pooling_layer;
9293
}
9394

94-
new_layer->setName(util::node_info(n).c_str());
95-
96-
if (shuffle_back ) {
97-
new_layer = addUnpaddingLayer(ctx, n, new_layer->getOutput(0), 3, false, false);
98-
}
95+
new_layer->setName(util::node_info(n).c_str());
96+
auto layer_output = addUnpadding(ctx, n, new_layer->getOutput(0), orig_dims.nbDims, false, false);
9997

100-
auto layer_output = ctx->AssociateValueAndTensor(n->outputs()[0], new_layer->getOutput(0));
98+
ctx->AssociateValueAndTensor(n->outputs()[0], layer_output);
10199
LOG_DEBUG("Output tensor shape: " << layer_output->getDimensions());
102100

103101
return true;
@@ -107,10 +105,11 @@ bool PoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, args& args,
107105
auto in = args[0].ITensorOrFreeze(ctx);
108106

109107
// Max Pool needs at least 4D input
110-
auto shuffle = addPaddingLayer(ctx, n, in, 4, false, true);
108+
auto orig_dims = in->getDimensions();
109+
bool expandDims = (orig_dims.nbDims < 4);
111110

112-
if (shuffle) {
113-
in = shuffle->getOutput(0);
111+
if (expandDims) {
112+
in = addPadding(ctx, n, in, 4, false, true);
114113
}
115114

116115
auto kernel_size = util::toDims(args[1].unwrapToIntList());
@@ -121,19 +120,18 @@ bool PoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, args& args,
121120
stride = util::toDims(args[1].unwrapToIntList());
122121
}
123122

124-
bool shuffle_back = false;
125123
if (kernel_size.nbDims == 1) {
126124
kernel_size = util::unsqueezeDims(kernel_size, 0, 1);
127-
if (shuffle)
128-
shuffle_back = true;
129125
LOG_DEBUG("kernel_size.nbDims < 2, padding:" << kernel_size);
130126
LOG_DEBUG("kernel_size: " << kernel_size);
131127
}
132-
if (padding.nbDims == 1)
133-
padding = util::unsqueezeDims(padding, 0, 0);
134-
if (stride.nbDims == 1)
128+
if (padding.nbDims == 1) {
129+
padding = util::unsqueezeDims(padding, 0, 0);
130+
}
131+
if (stride.nbDims == 1) {
135132
stride = util::unsqueezeDims(stride, 0, 1);
136-
133+
}
134+
137135
LOG_DEBUG("kernel_size: " << kernel_size);
138136
LOG_DEBUG("padding: " << padding);
139137
LOG_DEBUG("stride: " << stride);
@@ -161,37 +159,28 @@ bool PoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, args& args,
161159
new_layer = ctx->net->addPoolingNd(*in, pool_type, kernel_size);
162160
TRTORCH_CHECK(new_layer, "Unable to create Avg Pooling layer from node: " << *n);
163161
new_layer->setAverageCountExcludesPadding(!count_inlcude_pad);
164-
// if (!(args[6].IValue()->isNone())) {
165-
// LOG_WARNING("Divisor override is now handled by Avg Pooling Converter");
166-
// }
167162
} else {
168163
TRTORCH_ASSERT(0, "Unsupported pool mode!");
169164
}
170165

166+
auto padding_mode =
167+
ceil_mode ? nvinfer1::PaddingMode::kEXPLICIT_ROUND_UP : nvinfer1::PaddingMode::kEXPLICIT_ROUND_DOWN;
168+
171169
new_layer->setName(util::node_info(n).c_str());
170+
new_layer->setPaddingMode(padding_mode);
172171
new_layer->setPaddingNd(padding);
172+
new_layer->setStrideNd(stride);
173+
173174
if (stride.nbDims != 2 && ctx->settings.device.device_type == nvinfer1::DeviceType::kDLA) {
174175
if (!ctx->settings.device.allow_gpu_fallback) {
175176
TRTORCH_THROW_ERROR("DLA Pooling stride is limited to 2D, allow GPU fallback");
176177
} else {
177178
LOG_WARNING("DLA Pooling stride is limited to 2D, will run on GPU");
178179
}
179180
}
180-
new_layer->setStrideNd(stride);
181-
182-
auto padding_mode =
183-
ceil_mode ? nvinfer1::PaddingMode::kEXPLICIT_ROUND_UP : nvinfer1::PaddingMode::kEXPLICIT_ROUND_DOWN;
184-
new_layer->setPaddingMode(padding_mode);
185-
186-
new_layer->setName(util::node_info(n).c_str());
187-
188-
nvinfer1::ILayer* out_layer = new_layer;
189181

190-
if (shuffle_back) {
191-
out_layer = addUnpaddingLayer(ctx, n, new_layer->getOutput(0), 3, false, true);
192-
}
193-
194-
auto out_tensor = ctx->AssociateValueAndTensor(n->outputs()[0], out_layer->getOutput(0));
182+
auto out_tensor = addUnpadding(ctx, n, new_layer->getOutput(0), orig_dims.nbDims, false, true);
183+
ctx->AssociateValueAndTensor(n->outputs()[0], out_tensor);
195184

196185
LOG_DEBUG("Output tensor shape: " << out_tensor->getDimensions());
197186
return true;

core/conversion/evaluators/prim.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,7 @@ auto prim_registrations =
4848
c10::List<int64_t> list;
4949
list.reserve(num_inputs);
5050
for (auto in : n->inputs()) {
51-
if (args.at(in).IValue()->isInt()) {
52-
list.emplace_back(std::move(args.at(in).unwrapToInt()));
53-
} else if (args.at(in).IValue()->isTuple()) {
54-
auto unpack_tuple = args.at(in).IValue()->toTuple();
55-
for (size_t j = 0; j < unpack_tuple->elements().size(); ++j)
56-
list.emplace_back(unpack_tuple->elements()[j].toInt());
57-
}
51+
list.emplace_back(std::move(args.at(in).unwrapToInt()));
5852
}
5953
return c10::optional<torch::jit::IValue>(std::move(torch::jit::IValue(list)));
6054
} else if (torch::jit::FloatType::get() == lt->getElementType()) {

tests/core/conversion/converters/test_conv_deconv.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ TEST(Converters, ATenConvolution1dConvertsCorrectly) {
9898
auto g = std::make_shared<torch::jit::Graph>();
9999
torch::jit::parseIR(graph, g.get());
100100

101-
auto in = at::randint(1, 2, {1, 1, 3, 3}, {at::kCUDA});
102-
auto w = at::randint(1, 2, {4, 1, 2, 2}, {at::kCUDA});
101+
auto in = at::randint(1, 2, {1, 3, 3}, {at::kCUDA});
102+
auto w = at::randint(1, 2, {4, 3, 3}, {at::kCUDA});
103103

104104
auto jit_in = at::clone(in);
105105
auto jit_w = at::clone(w);

0 commit comments

Comments
 (0)