xref: /aosp_15_r20/external/executorch/kernels/test/op_replication_pad2d_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_pad2d_out(const Tensor & input,ArrayRef<int64_t> padding,Tensor & out)24 Tensor& op_replication_pad2d_out(
25     const Tensor& input,
26     ArrayRef<int64_t> padding,
27     Tensor& out) {
28   executorch::runtime::KernelRuntimeContext context{};
29   return torch::executor::aten::replication_pad2d_outf(
30       context, input, padding, out);
31 }
32 
33 class OpReplicationPad2DOutTest : 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(OpReplicationPad2DOutTest,SmokeTest)42 TEST_F(OpReplicationPad2DOutTest, SmokeTest) {
43   TensorFactory<ScalarType::Float> tfFloat;
44 
45   Tensor self = tfFloat.make({2, 3, 2}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
46   int64_t padding_data[4] = {1, 2, 2, 1};
47   ArrayRef<int64_t> padding = ArrayRef<int64_t>(padding_data, 4);
48   Tensor out = tfFloat.zeros({2, 6, 5});
49   // clang-format off
50   Tensor out_expected = tfFloat.make(
51     {2, 6, 5},
52     {
53       0.,  0.,  1.,  1.,  1.,
54       0.,  0.,  1.,  1.,  1.,
55       0.,  0.,  1.,  1.,  1.,
56       2.,  2.,  3.,  3.,  3.,
57       4.,  4.,  5.,  5.,  5.,
58       4.,  4.,  5.,  5.,  5.,
59 
60       6.,  6.,  7.,  7.,  7.,
61       6.,  6.,  7.,  7.,  7.,
62       6.,  6.,  7.,  7.,  7.,
63       8.,  8.,  9.,  9.,  9.,
64       10., 10., 11., 11., 11.,
65       10., 10., 11., 11., 11.
66     }
67   );
68   // clang-format on
69   op_replication_pad2d_out(self, padding, out);
70   EXPECT_TENSOR_CLOSE(out, out_expected);
71 }
72 
TEST_F(OpReplicationPad2DOutTest,SmokeTestNegTopPad)73 TEST_F(OpReplicationPad2DOutTest, SmokeTestNegTopPad) {
74   TensorFactory<ScalarType::Float> tfFloat;
75 
76   Tensor self = tfFloat.make({2, 3, 2}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
77   int64_t padding_data[4] = {1, 1, -2, 0};
78   ArrayRef<int64_t> padding = ArrayRef<int64_t>(padding_data, 4);
79   Tensor out = tfFloat.zeros({2, 1, 4});
80   Tensor out_expected = tfFloat.make({2, 1, 4}, {4, 4, 5, 5, 10, 10, 11, 11});
81   op_replication_pad2d_out(self, padding, out);
82   EXPECT_TENSOR_CLOSE(out, out_expected);
83 }
84 
TEST_F(OpReplicationPad2DOutTest,SmokeTestNegBottomPad)85 TEST_F(OpReplicationPad2DOutTest, SmokeTestNegBottomPad) {
86   TensorFactory<ScalarType::Float> tfFloat;
87 
88   Tensor self = tfFloat.make({2, 3, 2}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
89   int64_t padding_data[4] = {1, 1, 3, -5};
90   ArrayRef<int64_t> padding = ArrayRef<int64_t>(padding_data, 4);
91   Tensor out = tfFloat.zeros({2, 1, 4});
92   Tensor out_expected = tfFloat.make({2, 1, 4}, {0, 0, 1, 1, 6, 6, 7, 7});
93   op_replication_pad2d_out(self, padding, out);
94   EXPECT_TENSOR_CLOSE(out, out_expected);
95 }
96