Skip to content

Commit

Permalink
feat(aten::avg_pool2d): Implement Average Pooling 2D
Browse files Browse the repository at this point in the history
Signed-off-by: Naren Dasan <[email protected]>
Signed-off-by: Naren Dasan <[email protected]>
  • Loading branch information
narendasan committed Jun 15, 2020
1 parent 461e2ca commit 0c39519
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 1 deletion.
52 changes: 51 additions & 1 deletion core/conversion/converters/impl/pooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ auto pooling_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()

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

auto new_layer = ctx->net->addPoolingNd(*in, nvinfer1::PoolingType::kMAX, kernel_size);
TRTORCH_CHECK(new_layer, "Unable to create Max Pool 2D layer from node: " << *n);
Expand All @@ -58,6 +58,56 @@ auto pooling_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
return true;
}
}).pattern({
"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)",
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
auto in = args[0].ITensor();
auto shape = util::toVec(in->getDimensions());

// Abg Pool needs at least 4D input
if (shape.size() < 4) {
auto new_shape = util::toDimsPad(shape, 4);
LOG_DEBUG("Input shape is less than 4D got: " << util::toDims(shape) << ", inserting shuffle layer to reshape to 4D tensor shape: " << new_shape);
auto shuffle = ctx->net->addShuffle(*in);
shuffle->setReshapeDimensions(new_shape);
shuffle->setName((util::node_info(n) + " [Reshape to " + util::toStr(new_shape) + ']').c_str());
in = shuffle->getOutput(0);
}


auto kernel_size = util::toDimsHW(args[1].unwrapToIntList());
LOG_DEBUG("kernel_size: " << kernel_size);
auto padding = util::toDimsHW(args[3].unwrapToIntList());
LOG_DEBUG("padding: " << padding);

bool ceil_mode = args[4].unwrapToBool();
bool count_inlcude_pad = args[5].unwrapToBool();

auto new_layer = ctx->net->addPoolingNd(*in, nvinfer1::PoolingType::kAVERAGE, kernel_size);
TRTORCH_CHECK(new_layer, "Unable to create Avg Pool 2D layer from node: " << *n);

new_layer->setName(util::node_info(n).c_str());
new_layer->setPaddingNd(padding);
if (args[2].unwrapToIntList().size() == 2) {
auto stride = util::toDims(args[2].unwrapToIntList());
LOG_DEBUG("stride: " << stride);
new_layer->setStrideNd(stride);
}

auto padding_mode = ceil_mode ? nvinfer1::PaddingMode::kEXPLICIT_ROUND_UP : nvinfer1::PaddingMode::kEXPLICIT_ROUND_DOWN;
new_layer->setPaddingMode(padding_mode);
new_layer->setAverageCountExcludesPadding(!count_inlcude_pad);

if (!(args[6].IValue()->isNone())) {
LOG_WARNING("Divisor override is now handled by Avg Pooling Converter");
}

new_layer->setName(util::node_info(n).c_str());
auto out_tensor = ctx->AssociateValueAndTensor(n->outputs()[0], new_layer->getOutput(0));

LOG_DEBUG("Output tensor shape: " << out_tensor->getDimensions());
return true;
}
}).pattern({
"aten::adaptive_avg_pool2d(Tensor self, int[2] output_size) -> (Tensor)",
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
auto in = args[0].ITensor();
Expand Down
91 changes: 91 additions & 0 deletions tests/core/converters/test_pooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,97 @@ TEST(Converters, ATenMaxPool2DConvertsCorrectly) {
ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
}

TEST(Converters, ATenAvgPool2DConvertsCorrectly) {
const auto graph = R"IR(
graph(%0 : Tensor):
%1 : int = prim::Constant[value=0]()
%2 : int = prim::Constant[value=1]()
%3 : int = prim::Constant[value=2]()
%4 : bool = prim::Constant[value=0]()
%5 : bool = prim::Constant[value=1]()
%6 : int[] = prim::ListConstruct(%1, %1)
%7 : int[] = prim::ListConstruct(%2, %2)
%8 : int[] = prim::ListConstruct(%3, %3)
%9 : None = prim::Constant()
%10 : Tensor = aten::avg_pool2d(%0, %8, %7, %6, %4, %5, %9)
return (%10))IR";

auto g = std::make_shared<torch::jit::Graph>();
torch::jit::parseIR(graph, &*g);

//PyTorch MaxPool needs a 3D input
auto in = at::randint(-5, 5, {1, 4, 4}, at::kCUDA);
auto params = trtorch::core::conversion::get_named_params(g->inputs(), {});
auto jit_results = trtorch::tests::util::RunGraph(g, params, {in});

in = at::clone(in);
params = trtorch::core::conversion::get_named_params(g->inputs(), {});
auto trt_results = trtorch::tests::util::RunGraphEngine(g, params, {in});

ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
}


TEST(Converters, ATenAvgPool2DCeilConvertsCorrectly) {
const auto graph = R"IR(
graph(%0 : Tensor):
%1 : int = prim::Constant[value=0]()
%2 : int = prim::Constant[value=1]()
%3 : int = prim::Constant[value=2]()
%4 : bool = prim::Constant[value=0]()
%5 : bool = prim::Constant[value=1]()
%6 : int[] = prim::ListConstruct(%1, %1)
%7 : int[] = prim::ListConstruct(%2, %2)
%8 : int[] = prim::ListConstruct(%3, %3)
%9 : None = prim::Constant()
%10 : Tensor = aten::avg_pool2d(%0, %8, %7, %6, %5, %5, %9)
return (%10))IR";

auto g = std::make_shared<torch::jit::Graph>();
torch::jit::parseIR(graph, &*g);

//PyTorch MaxPool needs a 3D input
auto in = at::randint(-5, 5, {1, 4, 4}, at::kCUDA);
auto params = trtorch::core::conversion::get_named_params(g->inputs(), {});
auto jit_results = trtorch::tests::util::RunGraph(g, params, {in});

in = at::clone(in);
params = trtorch::core::conversion::get_named_params(g->inputs(), {});
auto trt_results = trtorch::tests::util::RunGraphEngine(g, params, {in});

ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
}

TEST(Converters, ATenAvgPool2DNoCountPadConvertsCorrectly) {
const auto graph = R"IR(
graph(%0 : Tensor):
%1 : int = prim::Constant[value=0]()
%2 : int = prim::Constant[value=1]()
%3 : int = prim::Constant[value=2]()
%4 : bool = prim::Constant[value=0]()
%5 : bool = prim::Constant[value=1]()
%6 : int[] = prim::ListConstruct(%1, %1)
%7 : int[] = prim::ListConstruct(%2, %2)
%8 : int[] = prim::ListConstruct(%3, %3)
%9 : None = prim::Constant()
%10 : Tensor = aten::avg_pool2d(%0, %8, %7, %6, %4, %4, %9)
return (%10))IR";

auto g = std::make_shared<torch::jit::Graph>();
torch::jit::parseIR(graph, &*g);

//PyTorch MaxPool needs a 3D input
auto in = at::randint(-5, 5, {1, 4, 4}, at::kCUDA);
auto params = trtorch::core::conversion::get_named_params(g->inputs(), {});
auto jit_results = trtorch::tests::util::RunGraph(g, params, {in});

in = at::clone(in);
params = trtorch::core::conversion::get_named_params(g->inputs(), {});
auto trt_results = trtorch::tests::util::RunGraphEngine(g, params, {in});

ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
}

TEST(Converters, ATenAdaptiveAvgPool2DConvertsCorrectly) {
const auto graph = R"IR(
graph(%0 : Tensor):
Expand Down

0 comments on commit 0c39519

Please sign in to comment.