Add C++ nn::functional pad

Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/26601

Test Plan: Imported from OSS

Differential Revision: D17517468

Pulled By: yf225

fbshipit-source-id: 9ee8b93b88a60f91f2ae78c242f9eaa246b3293c
This commit is contained in:
Will Feng
2019-10-21 22:16:21 -07:00
committed by Facebook Github Bot
parent 94757e035d
commit 079b3cc02c
11 changed files with 291 additions and 2 deletions
+141
View File
@@ -1126,3 +1126,144 @@ TEST_F(FunctionalTest, Threshold) {
}
}
}
TEST_F(FunctionalTest, Pad) {
{
auto input = torch::arange(6, torch::kDouble).reshape({1, 2, 3});
auto output = F::pad(input, PadOptions({1, 2}).mode(torch::kCircular));
auto expected = torch::tensor({{{2., 0., 1., 2., 0., 1.},
{5., 3., 4., 5., 3., 4.}}}, torch::kDouble);
ASSERT_EQ(output.sizes(), std::vector<int64_t>({1, 2, 6}));
ASSERT_TRUE(output.allclose(expected, 1e-04));
}
{
auto input = torch::arange(9, torch::kDouble).reshape({1, 1, 3, 3});
auto output = F::pad(input, PadOptions({3, 3, 3, 1}).mode(torch::kCircular));
auto expected = torch::tensor(
{{{{0., 1., 2., 0., 1., 2., 0., 1., 2.},
{3., 4., 5., 3., 4., 5., 3., 4., 5.},
{6., 7., 8., 6., 7., 8., 6., 7., 8.},
{0., 1., 2., 0., 1., 2., 0., 1., 2.},
{3., 4., 5., 3., 4., 5., 3., 4., 5.},
{6., 7., 8., 6., 7., 8., 6., 7., 8.},
{0., 1., 2., 0., 1., 2., 0., 1., 2.}}}}, torch::kDouble);
ASSERT_EQ(output.sizes(), std::vector<int64_t>({1, 1, 7, 9}));
ASSERT_TRUE(output.allclose(expected, 1e-04));
}
{
auto input = torch::arange(12, torch::kDouble).reshape({1, 1, 2, 2, 3});
auto output = F::pad(input, PadOptions({3, 3, 2, 1, 2, 2}).mode(torch::kCircular));
auto expected = torch::tensor(
{{{{{ 0., 1., 2., 0., 1., 2., 0., 1., 2.},
{ 3., 4., 5., 3., 4., 5., 3., 4., 5.},
{ 0., 1., 2., 0., 1., 2., 0., 1., 2.},
{ 3., 4., 5., 3., 4., 5., 3., 4., 5.},
{ 0., 1., 2., 0., 1., 2., 0., 1., 2.}},
{{ 6., 7., 8., 6., 7., 8., 6., 7., 8.},
{ 9., 10., 11., 9., 10., 11., 9., 10., 11.},
{ 6., 7., 8., 6., 7., 8., 6., 7., 8.},
{ 9., 10., 11., 9., 10., 11., 9., 10., 11.},
{ 6., 7., 8., 6., 7., 8., 6., 7., 8.}},
{{ 0., 1., 2., 0., 1., 2., 0., 1., 2.},
{ 3., 4., 5., 3., 4., 5., 3., 4., 5.},
{ 0., 1., 2., 0., 1., 2., 0., 1., 2.},
{ 3., 4., 5., 3., 4., 5., 3., 4., 5.},
{ 0., 1., 2., 0., 1., 2., 0., 1., 2.}},
{{ 6., 7., 8., 6., 7., 8., 6., 7., 8.},
{ 9., 10., 11., 9., 10., 11., 9., 10., 11.},
{ 6., 7., 8., 6., 7., 8., 6., 7., 8.},
{ 9., 10., 11., 9., 10., 11., 9., 10., 11.},
{ 6., 7., 8., 6., 7., 8., 6., 7., 8.}},
{{ 0., 1., 2., 0., 1., 2., 0., 1., 2.},
{ 3., 4., 5., 3., 4., 5., 3., 4., 5.},
{ 0., 1., 2., 0., 1., 2., 0., 1., 2.},
{ 3., 4., 5., 3., 4., 5., 3., 4., 5.},
{ 0., 1., 2., 0., 1., 2., 0., 1., 2.}},
{{ 6., 7., 8., 6., 7., 8., 6., 7., 8.},
{ 9., 10., 11., 9., 10., 11., 9., 10., 11.},
{ 6., 7., 8., 6., 7., 8., 6., 7., 8.},
{ 9., 10., 11., 9., 10., 11., 9., 10., 11.},
{ 6., 7., 8., 6., 7., 8., 6., 7., 8.}}}}}, torch::kDouble);
ASSERT_EQ(output.sizes(), std::vector<int64_t>({1, 1, 6, 5, 9}));
ASSERT_TRUE(output.allclose(expected, 1e-04));
}
{
auto input = torch::arange(16, torch::kDouble).reshape({2, 2, 2, 2});
auto output = F::pad(input, PadOptions({1, 1, 1, 1}).mode(torch::kReflect));
auto expected = torch::tensor(
{{{{ 3., 2., 3., 2.},
{ 1., 0., 1., 0.},
{ 3., 2., 3., 2.},
{ 1., 0., 1., 0.}},
{{ 7., 6., 7., 6.},
{ 5., 4., 5., 4.},
{ 7., 6., 7., 6.},
{ 5., 4., 5., 4.}}},
{{{11., 10., 11., 10.},
{ 9., 8., 9., 8.},
{11., 10., 11., 10.},
{ 9., 8., 9., 8.}},
{{15., 14., 15., 14.},
{13., 12., 13., 12.},
{15., 14., 15., 14.},
{13., 12., 13., 12.}}}}, torch::kDouble);
ASSERT_EQ(output.sizes(), std::vector<int64_t>({2, 2, 4, 4}));
ASSERT_TRUE(output.allclose(expected, 1e-04));
}
{
auto input = torch::arange(12, torch::kDouble).reshape({1, 1, 2, 2, 3});
auto output = F::pad(input, PadOptions({1, 2, 2, 1, 1, 2}).mode(torch::kReplicate));
auto expected = torch::tensor(
{{{{{ 0., 0., 1., 2., 2., 2.},
{ 0., 0., 1., 2., 2., 2.},
{ 0., 0., 1., 2., 2., 2.},
{ 3., 3., 4., 5., 5., 5.},
{ 3., 3., 4., 5., 5., 5.}},
{{ 0., 0., 1., 2., 2., 2.},
{ 0., 0., 1., 2., 2., 2.},
{ 0., 0., 1., 2., 2., 2.},
{ 3., 3., 4., 5., 5., 5.},
{ 3., 3., 4., 5., 5., 5.}},
{{ 6., 6., 7., 8., 8., 8.},
{ 6., 6., 7., 8., 8., 8.},
{ 6., 6., 7., 8., 8., 8.},
{ 9., 9., 10., 11., 11., 11.},
{ 9., 9., 10., 11., 11., 11.}},
{{ 6., 6., 7., 8., 8., 8.},
{ 6., 6., 7., 8., 8., 8.},
{ 6., 6., 7., 8., 8., 8.},
{ 9., 9., 10., 11., 11., 11.},
{ 9., 9., 10., 11., 11., 11.}},
{{ 6., 6., 7., 8., 8., 8.},
{ 6., 6., 7., 8., 8., 8.},
{ 6., 6., 7., 8., 8., 8.},
{ 9., 9., 10., 11., 11., 11.},
{ 9., 9., 10., 11., 11., 11.}}}}}, torch::kDouble);
ASSERT_EQ(output.sizes(), std::vector<int64_t>({1, 1, 5, 5, 6}));
ASSERT_TRUE(output.allclose(expected, 1e-04));
}
{
auto input = torch::ones({1, 1, 1, 1}, torch::kDouble);
auto output = F::pad(input, PadOptions({1, 1}).mode(torch::kConstant).value(0));
ASSERT_EQ(output.sizes(), std::vector<int64_t>({1, 1, 1, 3}));
auto expected = torch::tensor({{{{0., 1., 0.}}}}, torch::kDouble);
}
{
auto input = torch::ones({1, 1, 1, 1}, torch::kDouble);
auto output = F::pad(input, PadOptions({1, 1}));
ASSERT_EQ(output.sizes(), std::vector<int64_t>({1, 1, 1, 3}));
auto expected = torch::tensor({{{{0., 1., 0.}}}}, torch::kDouble);
}
}