Skip to content

Commit e4cb117

Browse files
committed
fix(//core/conversion/converters/impl): code works for interpolate2d/3d, doesn't work for 1d yet
Signed-off-by: Abhiram Iyer <[email protected]> Signed-off-by: Abhiram Iyer <[email protected]>
1 parent 7f12160 commit e4cb117

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

core/conversion/converters/impl/interpolate.cpp

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,62 @@ auto interpolate_registrations = RegisterNodeConversionPatterns()
1515
.pattern({
1616
"aten::upsample_nearest1d(Tensor self, int[1] output_size, float? scales=None) -> (Tensor)",
1717
[](ConversionCtx* ctx, const torch::jit::Node*n, args& args) -> bool {
18-
TRTORCH_ASSERT(args[0].IValue()->isTensor(), "Input expected to be of type Tensor");
18+
std::cout << "GOT IN HERE!!!!!!" << std::endl;
1919

2020
auto in = args[0].ITensor();
2121
auto in_shape = util::toVec(in->getDimensions());
22-
22+
2323
// Case 1: user uses output size and not scales
2424
if (!args[1].IValue()->isNone() && args[2].IValue()->isNone()) {
25-
auto output_size = util::toDims(args[1].unwrapToIntList());
25+
auto out_size = util::toVec(util::toDims(args[1].unwrapToIntList()));
2626

27-
TRTORCH_ASSERT(output_size.nbDims == 1, "aten::upsample_nearest1d input Tensor and output size dimension mismatch");
27+
TRTORCH_ASSERT(out_size.size() == 1, "aten::upsample_nearest1d input Tensor and output size dimension mismatch");
28+
29+
auto out_shape = in_shape;
30+
std::copy(out_size.begin(), out_size.end(), out_shape.begin() + (in_shape.size() - out_size.size()));
31+
32+
// remove padding that TensorRT adds automatically
33+
// out_shape.erase(out_shape.begin(), out_shape.begin()+1);
2834

2935
auto resize_layer = ctx->net->addResize(*in);
3036
TRTORCH_CHECK(resize_layer, "Unable to create interpolation (resizing) layer from node" << *n);
3137

32-
resize_layer->setOutputDimensions(output_size);
38+
resize_layer->setOutputDimensions(util::toDims(out_shape));
3339
resize_layer->setResizeMode(nvinfer1::ResizeMode::kNEAREST);
40+
resize_layer->setName(util::node_info(n).c_str());
41+
42+
auto layer_output = ctx->AssociateValueAndTensor(n->outputs()[0], resize_layer->getOutput(0));
43+
LOG_DEBUG("Output tensor shape: " << layer_output->getDimensions());
3444
} else {
35-
LOG_DEBUG("scale factor parameters not supported yet.");
45+
LOG_DEBUG("scale factor parameter not supported yet.");
3646
}
3747

3848
return true;
3949
}
4050
}).pattern({
4151
"aten::upsample_nearest2d(Tensor self, int[2] output_size, float? scales_h=None, float? scales_w=None) -> (Tensor)",
4252
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
43-
// std::raise(SIGINT);
44-
45-
TRTORCH_ASSERT(args[0].IValue()->isTensor(), "Input expected to be of type Tensor");
46-
4753
auto in = args[0].ITensor();
4854
auto in_shape = util::toVec(in->getDimensions());
4955

5056
// Case 1: user uses output_size and not scales_h, scales_w
5157
if (!args[1].IValue()->isNone() && args[2].IValue()->isNone() && args[3].IValue()->isNone()){
52-
auto output_size = util::toDims(args[1].unwrapToIntList());
53-
54-
TRTORCH_ASSERT( (output_size.nbDims == 1 || output_size.nbDims == 2), "aten::upsample_nearest2d input Tensor and output size dimension mismatch");
58+
auto out_size = util::toVec(util::toDims(args[1].unwrapToIntList()));
5559

60+
TRTORCH_ASSERT(out_size.size() == 2, "aten::upsample_nearest2d input Tensor and output size dimension mismatch");
61+
62+
auto out_shape = in_shape;
63+
std::copy(out_size.begin(), out_size.end(), out_shape.begin() + (in_shape.size() - out_size.size()));
64+
5665
auto resize_layer = ctx->net->addResize(*in);
5766
TRTORCH_CHECK(resize_layer, "Unable to create interpolation (resizing) layer from node" << *n);
5867

59-
resize_layer->setOutputDimensions(output_size);
68+
resize_layer->setOutputDimensions(util::toDims(out_shape));
6069
resize_layer->setResizeMode(nvinfer1::ResizeMode::kNEAREST);
70+
resize_layer->setName(util::node_info(n).c_str());
71+
72+
auto layer_output = ctx->AssociateValueAndTensor(n->outputs()[0], resize_layer->getOutput(0));
73+
LOG_DEBUG("Output tensor shape: " << layer_output->getDimensions());
6174
} else {
6275
LOG_DEBUG("scale factor parameters not supported yet.");
6376
}
@@ -67,22 +80,27 @@ auto interpolate_registrations = RegisterNodeConversionPatterns()
6780
}).pattern({
6881
"aten::upsample_nearest3d(Tensor self, int[3] output_size, float? scales_d=None, float? scales_h=None, float? scales_w=None) -> (Tensor)",
6982
[](ConversionCtx* ctx, const torch::jit::Node*n, args& args) -> bool {
70-
TRTORCH_ASSERT(args[0].IValue()->isTensor(), "Input expected to be of type Tensor");
71-
7283
auto in = args[0].ITensor();
7384
auto in_shape = util::toVec(in->getDimensions());
7485

7586
// Case 1: user uses output size and not scales_d, scales_h, scales_w
7687
if (!args[1].IValue()->isNone() && args[2].IValue()->isNone() && args[3].IValue()->isNone() && args[4].IValue()->isNone()) {
77-
auto output_size = util::toDims(args[1].unwrapToIntList());
88+
auto out_size = util::toVec(util::toDims(args[1].unwrapToIntList()));
7889

79-
TRTORCH_ASSERT( (output_size.nbDims == 1 || output_size.nbDims == 3), "aten::upsample_nearest3d input Tensor and output size dimension mismatch");
90+
TRTORCH_ASSERT(out_size.size() == 3, "aten::upsample_nearest3d input Tensor and output size dimension mismatch");
91+
92+
auto out_shape = in_shape;
93+
std::copy(out_size.begin(), out_size.end(), out_shape.begin() + (in_shape.size() - out_size.size()));
8094

8195
auto resize_layer = ctx->net->addResize(*in);
8296
TRTORCH_CHECK(resize_layer, "Unable to create interpolation (resizing) layer from node" << *n);
8397

84-
resize_layer->setOutputDimensions(output_size);
98+
resize_layer->setOutputDimensions(util::toDims(out_shape));
8599
resize_layer->setResizeMode(nvinfer1::ResizeMode::kNEAREST);
100+
resize_layer->setName(util::node_info(n).c_str());
101+
102+
auto layer_output = ctx->AssociateValueAndTensor(n->outputs()[0], resize_layer->getOutput(0));
103+
LOG_DEBUG("Output tensor shape: " << layer_output->getDimensions());
86104
} else {
87105
LOG_DEBUG("scale factor parameters not supported yet.");
88106
}

0 commit comments

Comments
 (0)