Skip to content

Commit 0c39519

Browse files
committed
feat(aten::avg_pool2d): Implement Average Pooling 2D
Signed-off-by: Naren Dasan <[email protected]> Signed-off-by: Naren Dasan <[email protected]>
1 parent 461e2ca commit 0c39519

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

Diff for: core/conversion/converters/impl/pooling.cpp

+51-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ auto pooling_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
3636

3737
LOG_DEBUG("dilation: " << dilation);
3838
LOG_WARNING("Dilation not used in max pooling converter");
39-
bool ceil_mode = args[5].IValue()->to<bool>();
39+
bool ceil_mode = args[5].unwrapToBool();
4040

4141
auto new_layer = ctx->net->addPoolingNd(*in, nvinfer1::PoolingType::kMAX, kernel_size);
4242
TRTORCH_CHECK(new_layer, "Unable to create Max Pool 2D layer from node: " << *n);
@@ -58,6 +58,56 @@ auto pooling_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
5858
return true;
5959
}
6060
}).pattern({
61+
"aten::avg_pool2d(Tensor self, int[2] kernel_size, int[2] stride=[], int[2] padding=[0, 0], bool ceil_mode=False, bool count_include_pad=True, int? divisor_override=None) -> (Tensor)",
62+
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
63+
auto in = args[0].ITensor();
64+
auto shape = util::toVec(in->getDimensions());
65+
66+
// Abg Pool needs at least 4D input
67+
if (shape.size() < 4) {
68+
auto new_shape = util::toDimsPad(shape, 4);
69+
LOG_DEBUG("Input shape is less than 4D got: " << util::toDims(shape) << ", inserting shuffle layer to reshape to 4D tensor shape: " << new_shape);
70+
auto shuffle = ctx->net->addShuffle(*in);
71+
shuffle->setReshapeDimensions(new_shape);
72+
shuffle->setName((util::node_info(n) + " [Reshape to " + util::toStr(new_shape) + ']').c_str());
73+
in = shuffle->getOutput(0);
74+
}
75+
76+
77+
auto kernel_size = util::toDimsHW(args[1].unwrapToIntList());
78+
LOG_DEBUG("kernel_size: " << kernel_size);
79+
auto padding = util::toDimsHW(args[3].unwrapToIntList());
80+
LOG_DEBUG("padding: " << padding);
81+
82+
bool ceil_mode = args[4].unwrapToBool();
83+
bool count_inlcude_pad = args[5].unwrapToBool();
84+
85+
auto new_layer = ctx->net->addPoolingNd(*in, nvinfer1::PoolingType::kAVERAGE, kernel_size);
86+
TRTORCH_CHECK(new_layer, "Unable to create Avg Pool 2D layer from node: " << *n);
87+
88+
new_layer->setName(util::node_info(n).c_str());
89+
new_layer->setPaddingNd(padding);
90+
if (args[2].unwrapToIntList().size() == 2) {
91+
auto stride = util::toDims(args[2].unwrapToIntList());
92+
LOG_DEBUG("stride: " << stride);
93+
new_layer->setStrideNd(stride);
94+
}
95+
96+
auto padding_mode = ceil_mode ? nvinfer1::PaddingMode::kEXPLICIT_ROUND_UP : nvinfer1::PaddingMode::kEXPLICIT_ROUND_DOWN;
97+
new_layer->setPaddingMode(padding_mode);
98+
new_layer->setAverageCountExcludesPadding(!count_inlcude_pad);
99+
100+
if (!(args[6].IValue()->isNone())) {
101+
LOG_WARNING("Divisor override is now handled by Avg Pooling Converter");
102+
}
103+
104+
new_layer->setName(util::node_info(n).c_str());
105+
auto out_tensor = ctx->AssociateValueAndTensor(n->outputs()[0], new_layer->getOutput(0));
106+
107+
LOG_DEBUG("Output tensor shape: " << out_tensor->getDimensions());
108+
return true;
109+
}
110+
}).pattern({
61111
"aten::adaptive_avg_pool2d(Tensor self, int[2] output_size) -> (Tensor)",
62112
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
63113
auto in = args[0].ITensor();

Diff for: tests/core/converters/test_pooling.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,97 @@ TEST(Converters, ATenMaxPool2DConvertsCorrectly) {
3232
ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
3333
}
3434

35+
TEST(Converters, ATenAvgPool2DConvertsCorrectly) {
36+
const auto graph = R"IR(
37+
graph(%0 : Tensor):
38+
%1 : int = prim::Constant[value=0]()
39+
%2 : int = prim::Constant[value=1]()
40+
%3 : int = prim::Constant[value=2]()
41+
%4 : bool = prim::Constant[value=0]()
42+
%5 : bool = prim::Constant[value=1]()
43+
%6 : int[] = prim::ListConstruct(%1, %1)
44+
%7 : int[] = prim::ListConstruct(%2, %2)
45+
%8 : int[] = prim::ListConstruct(%3, %3)
46+
%9 : None = prim::Constant()
47+
%10 : Tensor = aten::avg_pool2d(%0, %8, %7, %6, %4, %5, %9)
48+
return (%10))IR";
49+
50+
auto g = std::make_shared<torch::jit::Graph>();
51+
torch::jit::parseIR(graph, &*g);
52+
53+
//PyTorch MaxPool needs a 3D input
54+
auto in = at::randint(-5, 5, {1, 4, 4}, at::kCUDA);
55+
auto params = trtorch::core::conversion::get_named_params(g->inputs(), {});
56+
auto jit_results = trtorch::tests::util::RunGraph(g, params, {in});
57+
58+
in = at::clone(in);
59+
params = trtorch::core::conversion::get_named_params(g->inputs(), {});
60+
auto trt_results = trtorch::tests::util::RunGraphEngine(g, params, {in});
61+
62+
ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
63+
}
64+
65+
66+
TEST(Converters, ATenAvgPool2DCeilConvertsCorrectly) {
67+
const auto graph = R"IR(
68+
graph(%0 : Tensor):
69+
%1 : int = prim::Constant[value=0]()
70+
%2 : int = prim::Constant[value=1]()
71+
%3 : int = prim::Constant[value=2]()
72+
%4 : bool = prim::Constant[value=0]()
73+
%5 : bool = prim::Constant[value=1]()
74+
%6 : int[] = prim::ListConstruct(%1, %1)
75+
%7 : int[] = prim::ListConstruct(%2, %2)
76+
%8 : int[] = prim::ListConstruct(%3, %3)
77+
%9 : None = prim::Constant()
78+
%10 : Tensor = aten::avg_pool2d(%0, %8, %7, %6, %5, %5, %9)
79+
return (%10))IR";
80+
81+
auto g = std::make_shared<torch::jit::Graph>();
82+
torch::jit::parseIR(graph, &*g);
83+
84+
//PyTorch MaxPool needs a 3D input
85+
auto in = at::randint(-5, 5, {1, 4, 4}, at::kCUDA);
86+
auto params = trtorch::core::conversion::get_named_params(g->inputs(), {});
87+
auto jit_results = trtorch::tests::util::RunGraph(g, params, {in});
88+
89+
in = at::clone(in);
90+
params = trtorch::core::conversion::get_named_params(g->inputs(), {});
91+
auto trt_results = trtorch::tests::util::RunGraphEngine(g, params, {in});
92+
93+
ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
94+
}
95+
96+
TEST(Converters, ATenAvgPool2DNoCountPadConvertsCorrectly) {
97+
const auto graph = R"IR(
98+
graph(%0 : Tensor):
99+
%1 : int = prim::Constant[value=0]()
100+
%2 : int = prim::Constant[value=1]()
101+
%3 : int = prim::Constant[value=2]()
102+
%4 : bool = prim::Constant[value=0]()
103+
%5 : bool = prim::Constant[value=1]()
104+
%6 : int[] = prim::ListConstruct(%1, %1)
105+
%7 : int[] = prim::ListConstruct(%2, %2)
106+
%8 : int[] = prim::ListConstruct(%3, %3)
107+
%9 : None = prim::Constant()
108+
%10 : Tensor = aten::avg_pool2d(%0, %8, %7, %6, %4, %4, %9)
109+
return (%10))IR";
110+
111+
auto g = std::make_shared<torch::jit::Graph>();
112+
torch::jit::parseIR(graph, &*g);
113+
114+
//PyTorch MaxPool needs a 3D input
115+
auto in = at::randint(-5, 5, {1, 4, 4}, at::kCUDA);
116+
auto params = trtorch::core::conversion::get_named_params(g->inputs(), {});
117+
auto jit_results = trtorch::tests::util::RunGraph(g, params, {in});
118+
119+
in = at::clone(in);
120+
params = trtorch::core::conversion::get_named_params(g->inputs(), {});
121+
auto trt_results = trtorch::tests::util::RunGraphEngine(g, params, {in});
122+
123+
ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
124+
}
125+
35126
TEST(Converters, ATenAdaptiveAvgPool2DConvertsCorrectly) {
36127
const auto graph = R"IR(
37128
graph(%0 : Tensor):

0 commit comments

Comments
 (0)