Skip to content

Commit 3c49608

Browse files
authored
Merge pull request #2097 from andi4191/anurag.dixit/aten_unflatten
feat: Added support for aten::unflatten converter
2 parents 76800bc + a47b5fe commit 3c49608

File tree

3 files changed

+228
-1
lines changed

3 files changed

+228
-1
lines changed

core/conversion/converters/impl/shuffle.cpp

+98
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,104 @@ static auto shuffle_registrations TORCHTRT_UNUSED =
6565
return true;
6666
}})
6767
.pattern(
68+
{"aten::unflatten.int(Tensor self, int dim, int[] sizes) -> (Tensor)",
69+
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
70+
auto in = args[0].ITensorOrFreeze(ctx);
71+
auto dim = args[1].unwrapToInt();
72+
auto in_shape = util::toVec(in->getDimensions());
73+
std::vector<int64_t> new_shape;
74+
nvinfer1::ITensor* shape_tensor;
75+
if (ctx->input_is_dynamic) {
76+
/*
77+
* In case the dim is negative
78+
* If the dim in negative range is larger than in_shape,
79+
* then it should run into index out of bound error as expected
80+
*/
81+
if (dim < 0) {
82+
dim = in_shape.size() + dim;
83+
}
84+
std::cout << "Dynamic shape case" << std::endl;
85+
LOG_DEBUG("Using dynamic version of reshape layer");
86+
if (args[2].isITensorList()) {
87+
std::cout << "isTensorList case" << std::endl;
88+
LOG_DEBUG("Shape tensor is an ITensorList");
89+
auto expand_shape = args[2].unwrapToITensorList();
90+
auto shape_layer = ctx->net->addShape(*in);
91+
TORCHTRT_CHECK(shape_layer, "Unable to create shape layer from node: " << *n);
92+
auto shape_1d_tensor = shape_layer->getOutput(0);
93+
94+
std::vector<int> before_dim_indices_vector(dim);
95+
std::iota(before_dim_indices_vector.begin(), before_dim_indices_vector.end(), 0);
96+
97+
nvinfer1::ITensor* before_dim_gather_out = nullptr;
98+
if (before_dim_indices_vector.size()) {
99+
at::Tensor before_dim_indices = torch::tensor(before_dim_indices_vector).to(torch::kI32);
100+
auto before_dim_indices_out = converters::tensor_to_const(ctx, before_dim_indices);
101+
auto before_dim_gather_layer = ctx->net->addGather(*shape_1d_tensor, *before_dim_indices_out, 0);
102+
TORCHTRT_CHECK(before_dim_gather_layer, "Unable to create gather layer from node: " << *n);
103+
before_dim_gather_out = before_dim_gather_layer->getOutput(0);
104+
}
105+
106+
std::vector<int> after_dim_indices_vector(in_shape.size() - (dim + 1));
107+
std::iota(after_dim_indices_vector.begin(), after_dim_indices_vector.end(), dim + 1);
108+
109+
nvinfer1::ITensor* after_dim_gather_out = nullptr;
110+
if (after_dim_indices_vector.size()) {
111+
at::Tensor after_dim_indices = torch::tensor(after_dim_indices_vector).to(torch::kI32);
112+
auto after_dim_indices_out = converters::tensor_to_const(ctx, after_dim_indices);
113+
auto after_dim_gather_layer = ctx->net->addGather(*shape_1d_tensor, *after_dim_indices_out, 0);
114+
TORCHTRT_CHECK(after_dim_gather_layer, "Unable to create gather layer from node: " << *n);
115+
after_dim_gather_out = after_dim_gather_layer->getOutput(0);
116+
}
117+
118+
std::vector<nvinfer1::ITensor*> shape_tensors;
119+
if (before_dim_gather_out) {
120+
shape_tensors.push_back(before_dim_gather_out);
121+
}
122+
for (auto new_shape_tensor : expand_shape) {
123+
shape_tensors.push_back(new_shape_tensor);
124+
}
125+
if (after_dim_gather_out) {
126+
shape_tensors.push_back(after_dim_gather_out);
127+
}
128+
129+
auto shape_cat_layer = ctx->net->addConcatenation(shape_tensors.data(), shape_tensors.size());
130+
TORCHTRT_CHECK(shape_cat_layer, "Unable to create cat layer from node: " << *n);
131+
shape_tensor = shape_cat_layer->getOutput(0);
132+
LOG_DEBUG("Shape tensor shape: " << shape_tensor->getDimensions());
133+
} else if (args[2].isIntList()) {
134+
auto shape_vec = args[2].unwrapToIntList().vec();
135+
// New shape
136+
new_shape.insert(new_shape.end(), in_shape.begin(), in_shape.begin() + dim);
137+
new_shape.insert(new_shape.end(), shape_vec.begin(), shape_vec.end());
138+
new_shape.insert(new_shape.end(), in_shape.begin() + dim + 1, in_shape.end());
139+
140+
shape_tensor = tensor_to_const(ctx, torch::tensor(new_shape).to(torch::kI32));
141+
} else {
142+
LOG_ERROR(
143+
"Invalid IValue type of " << args[2].IValue()->type()
144+
<< " detected for shape tensor from node: " << *n);
145+
}
146+
} else {
147+
new_shape =
148+
torch::unflatten(torch::rand(in_shape), dim, args[2].unwrapToIntList().vec()).sizes().vec();
149+
}
150+
auto shuffle = ctx->net->addShuffle(*in);
151+
shuffle->setName(util::node_info(n).c_str());
152+
TORCHTRT_CHECK(shuffle, "Unable to create shuffle layer from node: " << *n);
153+
154+
if (ctx->input_is_dynamic) {
155+
shuffle->setInput(1, *shape_tensor);
156+
} else {
157+
shuffle->setReshapeDimensions(util::toDims(new_shape));
158+
}
159+
160+
auto out_tensor = ctx->AssociateValueAndTensor(n->outputs()[0], shuffle->getOutput(0));
161+
LOG_DEBUG("Output tensor shape: " << out_tensor->getDimensions());
162+
163+
return true;
164+
}})
165+
.pattern(
68166
{"aten::reshape(Tensor self, int[] shape) -> (Tensor)",
69167
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
70168
auto in = args[0].ITensorOrFreeze(ctx);

tests/core/conversion/converters/test_shuffle.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,55 @@ TEST(Converters, ATenPixelShuffle5DConvertsCorrectly) {
364364

365365
ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
366366
}
367+
368+
TEST(Converters, ATenUnflattenConvertsCorrectly) {
369+
const auto graph = R"IR(
370+
graph(%x.1 : Tensor):
371+
%2 : int = prim::Constant[value=1]()
372+
%3 : int = prim::Constant[value=512]()
373+
%4 : int = prim::Constant[value=1]()
374+
%5 : int = prim::Constant[value=1]()
375+
%6 : int[] = prim::ListConstruct(%3, %4, %5)
376+
%7 : Tensor = aten::unflatten(%x.1, %2, %6)
377+
return (%7))IR";
378+
379+
auto g = std::make_shared<torch::jit::Graph>();
380+
torch::jit::parseIR(graph, g.get());
381+
382+
auto in = at::randint(0, 5, {1, 512}, {at::kCUDA});
383+
auto params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {});
384+
385+
auto jit_results = torch_tensorrt::tests::util::RunGraph(g, params, {in});
386+
387+
in = at::clone(in);
388+
params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {});
389+
auto trt_results = torch_tensorrt::tests::util::RunGraphEngine(g, params, {in});
390+
391+
ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
392+
}
393+
394+
TEST(Converters, ATenUnflattenNegativeDimConvertsCorrectly) {
395+
const auto graph = R"IR(
396+
graph(%x.1 : Tensor):
397+
%2 : int = prim::Constant[value=-1]()
398+
%3 : int = prim::Constant[value=512]()
399+
%4 : int = prim::Constant[value=1]()
400+
%5 : int = prim::Constant[value=1]()
401+
%6 : int[] = prim::ListConstruct(%3, %4, %5)
402+
%7 : Tensor = aten::unflatten(%x.1, %2, %6)
403+
return (%7))IR";
404+
405+
auto g = std::make_shared<torch::jit::Graph>();
406+
torch::jit::parseIR(graph, g.get());
407+
408+
auto in = at::randint(0, 5, {1, 512}, {at::kCUDA});
409+
auto params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {});
410+
411+
auto jit_results = torch_tensorrt::tests::util::RunGraph(g, params, {in});
412+
413+
in = at::clone(in);
414+
params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {});
415+
auto trt_results = torch_tensorrt::tests::util::RunGraphEngine(g, params, {in});
416+
417+
ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
418+
}

tests/cpp/test_dynamic_size.cpp

+78-1
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,81 @@ TEST(Converters, ATenResizeGetItemDynShapeMulCorrectly) {
124124
auto trt = trt_results[0].reshape(jit_results[0].sizes());
125125

126126
ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(jit_results[0], trt, 2e-6));
127-
}
127+
}
128+
129+
TEST(Converters, ATenUnflattenDynShapeITensorShapeCorrectly) {
130+
const auto graph = R"IR(
131+
graph(%x.1 : Tensor):
132+
%2 : int = prim::Constant[value=1]()
133+
%3 : int = aten::size(%x.1, %2)
134+
%4 : int = prim::Constant[value=256]()
135+
%5 : int = prim::Constant[value=2]()
136+
%6 : int[] = prim::ListConstruct(%4, %5)
137+
%7 : Tensor = aten::unflatten(%x.1, %2, %6)
138+
return (%7))IR";
139+
auto g = std::make_shared<torch::jit::Graph>();
140+
torch::jit::parseIR(graph, g.get());
141+
142+
auto in = at::randint(0, 10, {1, 512, 1}, {at::kCUDA});
143+
144+
auto jit_in = at::clone(in);
145+
auto params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {});
146+
auto jit_results = torch_tensorrt::tests::util::RunGraph(g, params, {jit_in});
147+
148+
auto trt_in = at::clone(in);
149+
params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {});
150+
auto trt_results = torch_tensorrt::tests::util::RunGraphEngineDynamic(g, params, {in}, true);
151+
152+
ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
153+
}
154+
155+
TEST(Converters, ATenUnflattenDynShapeITensorShapeCorrectlyFirstDim) {
156+
const auto graph = R"IR(
157+
graph(%x.1 : Tensor):
158+
%1 : int = prim::Constant[value=0]()
159+
%2 : int = prim::Constant[value=1]()
160+
%3 : int = aten::size(%x.1, %1)
161+
%6 : int[] = prim::ListConstruct(%2, %2, %3, %2, %2)
162+
%7 : Tensor = aten::unflatten(%x.1, %1, %6)
163+
return (%7))IR";
164+
auto g = std::make_shared<torch::jit::Graph>();
165+
torch::jit::parseIR(graph, g.get());
166+
167+
auto in = at::randint(0, 10, {64, 512, 1}, {at::kCUDA});
168+
169+
auto jit_in = at::clone(in);
170+
auto params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {});
171+
auto jit_results = torch_tensorrt::tests::util::RunGraph(g, params, {jit_in});
172+
173+
auto trt_in = at::clone(in);
174+
params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {});
175+
auto trt_results = torch_tensorrt::tests::util::RunGraphEngineDynamic(g, params, {in}, true);
176+
177+
ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
178+
}
179+
180+
TEST(Converters, ATenUnflattenDynShapeITensorShapeCorrectlyLastDim) {
181+
const auto graph = R"IR(
182+
graph(%x.1 : Tensor):
183+
%1 : int = prim::Constant[value=2]()
184+
%2 : int = prim::Constant[value=1]()
185+
%3 : int = aten::size(%x.1, %1)
186+
%5 : int = prim::Constant[value=2]()
187+
%6 : int[] = prim::ListConstruct(%3, %2, %2)
188+
%7 : Tensor = aten::unflatten(%x.1, %5, %6)
189+
return (%7))IR";
190+
auto g = std::make_shared<torch::jit::Graph>();
191+
torch::jit::parseIR(graph, g.get());
192+
193+
auto in = at::randint(0, 10, {1, 512, 9}, {at::kCUDA});
194+
195+
auto jit_in = at::clone(in);
196+
auto params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {});
197+
auto jit_results = torch_tensorrt::tests::util::RunGraph(g, params, {jit_in});
198+
199+
auto trt_in = at::clone(in);
200+
params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {});
201+
auto trt_results = torch_tensorrt::tests::util::RunGraphEngineDynamic(g, params, {in}, true);
202+
203+
ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
204+
}

0 commit comments

Comments
 (0)