xref: /aosp_15_r20/external/executorch/kernels/test/op_replication_pad1d_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_pad1d_out(const Tensor & input,ArrayRef<int64_t> padding,Tensor & out)24 Tensor& op_replication_pad1d_out(
25     const Tensor& input,
26     ArrayRef<int64_t> padding,
27     Tensor& out) {
28   executorch::runtime::KernelRuntimeContext context{};
29   return torch::executor::aten::replication_pad1d_outf(
30       context, input, padding, out);
31 }
32 
33 class OpReplicationPad1DOutTest : 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(OpReplicationPad1DOutTest,SmokeTest)42 TEST_F(OpReplicationPad1DOutTest, SmokeTest) {
43   TensorFactory<ScalarType::Float> tfFloat;
44 
45   Tensor self = tfFloat.make({2, 3}, {0, 1, 2, 3, 4, 5});
46   int64_t padding_data[2] = {1, 2};
47   ArrayRef<int64_t> padding = ArrayRef<int64_t>(padding_data, 2);
48   Tensor out = tfFloat.zeros({2, 6});
49   Tensor out_expected =
50       tfFloat.make({2, 6}, {0, 0, 1, 2, 2, 2, 3, 3, 4, 5, 5, 5});
51   op_replication_pad1d_out(self, padding, out);
52   EXPECT_TENSOR_CLOSE(out, out_expected);
53 }
54 
TEST_F(OpReplicationPad1DOutTest,SmokeTestNegLeftPad)55 TEST_F(OpReplicationPad1DOutTest, SmokeTestNegLeftPad) {
56   TensorFactory<ScalarType::Float> tfFloat;
57 
58   Tensor self = tfFloat.make({2, 3}, {0, 1, 2, 3, 4, 5});
59   int64_t padding_data[2] = {-1, 1};
60   ArrayRef<int64_t> padding = ArrayRef<int64_t>(padding_data, 2);
61   Tensor out = tfFloat.zeros({2, 3});
62   Tensor out_expected = tfFloat.make({2, 3}, {1, 2, 2, 4, 5, 5});
63   op_replication_pad1d_out(self, padding, out);
64   EXPECT_TENSOR_CLOSE(out, out_expected);
65 }
66 
TEST_F(OpReplicationPad1DOutTest,SmokeTestNegRightPad)67 TEST_F(OpReplicationPad1DOutTest, SmokeTestNegRightPad) {
68   TensorFactory<ScalarType::Float> tfFloat;
69 
70   Tensor self = tfFloat.make({2, 3}, {0, 1, 2, 3, 4, 5});
71   int64_t padding_data[2] = {3, -5};
72   ArrayRef<int64_t> padding = ArrayRef<int64_t>(padding_data, 2);
73   Tensor out = tfFloat.zeros({2, 1});
74   Tensor out_expected = tfFloat.make({2, 1}, {0, 3});
75   op_replication_pad1d_out(self, padding, out);
76   EXPECT_TENSOR_CLOSE(out, out_expected);
77 }
78