xref: /aosp_15_r20/external/executorch/kernels/test/op_replication_pad3d_test.cpp (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <executorch/kernels/test/FunctionHeaderWrapper.h> // Declares the operator
10 #include <executorch/kernels/test/TestUtil.h>
11 #include <executorch/runtime/core/exec_aten/exec_aten.h>
12 #include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h>
13 #include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h>
14 #include <executorch/runtime/platform/runtime.h>
15 
16 #include <gtest/gtest.h>
17 
18 using namespace ::testing;
19 using exec_aten::ArrayRef;
20 using exec_aten::ScalarType;
21 using exec_aten::Tensor;
22 using torch::executor::testing::TensorFactory;
23 
op_replication_pad3d_out(const Tensor & input,ArrayRef<int64_t> padding,Tensor & out)24 Tensor& op_replication_pad3d_out(
25     const Tensor& input,
26     ArrayRef<int64_t> padding,
27     Tensor& out) {
28   executorch::runtime::KernelRuntimeContext context{};
29   return torch::executor::aten::replication_pad3d_outf(
30       context, input, padding, out);
31 }
32 
33 class OpReplicationPad3DOutTest : public ::testing::Test {
34  protected:
SetUp()35   void SetUp() override {
36     // Since these tests cause ET_LOG to be called, the PAL must be initialized
37     // first.
38     torch::executor::runtime_init();
39   }
40 };
41 
TEST_F(OpReplicationPad3DOutTest,SmokeTest)42 TEST_F(OpReplicationPad3DOutTest, SmokeTest) {
43   TensorFactory<ScalarType::Float> tfFloat;
44 
45   Tensor self =
46       tfFloat.make({1, 2, 3, 2}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
47   int64_t padding_data[6] = {1, 2, 2, 1, 1, 0};
48   ArrayRef<int64_t> padding = ArrayRef<int64_t>(padding_data, 6);
49   Tensor out = tfFloat.zeros({1, 3, 6, 5});
50   // clang-format off
51   Tensor out_expected = tfFloat.make(
52     {1, 3, 6, 5},
53     {
54       0.,  0.,  1.,  1.,  1.,
55       0.,  0.,  1.,  1.,  1.,
56       0.,  0.,  1.,  1.,  1.,
57       2.,  2.,  3.,  3.,  3.,
58       4.,  4.,  5.,  5.,  5.,
59       4.,  4.,  5.,  5.,  5.,
60 
61       0.,  0.,  1.,  1.,  1.,
62       0.,  0.,  1.,  1.,  1.,
63       0.,  0.,  1.,  1.,  1.,
64       2.,  2.,  3.,  3.,  3.,
65       4.,  4.,  5.,  5.,  5.,
66       4.,  4.,  5.,  5.,  5.,
67 
68       6.,  6.,  7.,  7.,  7.,
69       6.,  6.,  7.,  7.,  7.,
70       6.,  6.,  7.,  7.,  7.,
71       8.,  8.,  9.,  9.,  9.,
72       10., 10., 11., 11., 11.,
73       10., 10., 11., 11., 11.
74     }
75   );
76   // clang-format on
77   op_replication_pad3d_out(self, padding, out);
78   EXPECT_TENSOR_CLOSE(out, out_expected);
79 }
80 
TEST_F(OpReplicationPad3DOutTest,SmokeTestNegFrontPad)81 TEST_F(OpReplicationPad3DOutTest, SmokeTestNegFrontPad) {
82   TensorFactory<ScalarType::Float> tfFloat;
83 
84   Tensor self =
85       tfFloat.make({1, 2, 3, 2}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
86   int64_t padding_data[6] = {1, 1, 1, -2, -1, 0};
87   ArrayRef<int64_t> padding = ArrayRef<int64_t>(padding_data, 6);
88   Tensor out = tfFloat.zeros({1, 1, 2, 4});
89   Tensor out_expected = tfFloat.make({1, 1, 2, 4}, {6, 6, 7, 7, 6, 6, 7, 7});
90   op_replication_pad3d_out(self, padding, out);
91   EXPECT_TENSOR_CLOSE(out, out_expected);
92 }
93 
TEST_F(OpReplicationPad3DOutTest,SmokeTestNegBackPad)94 TEST_F(OpReplicationPad3DOutTest, SmokeTestNegBackPad) {
95   TensorFactory<ScalarType::Float> tfFloat;
96 
97   Tensor self =
98       tfFloat.make({1, 2, 3, 2}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
99   int64_t padding_data[6] = {1, 1, 1, 1, 4, -5};
100   ArrayRef<int64_t> padding = ArrayRef<int64_t>(padding_data, 6);
101   Tensor out = tfFloat.zeros({1, 1, 5, 4});
102   // clang-format off
103   Tensor out_expected = tfFloat.make(
104     {1, 1, 5, 4},
105     {
106       0., 0., 1., 1.,
107       0., 0., 1., 1.,
108       2., 2., 3., 3.,
109       4., 4., 5., 5.,
110       4., 4., 5., 5.
111     }
112   );
113   op_replication_pad3d_out(self, padding, out);
114   EXPECT_TENSOR_CLOSE(out, out_expected);
115 }
116