xref: /aosp_15_r20/external/executorch/kernels/test/op_flip_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::IntArrayRef;
20 using exec_aten::ScalarType;
21 using exec_aten::Tensor;
22 using torch::executor::testing::TensorFactory;
23 
op_flip_out(const Tensor & input,IntArrayRef dims,Tensor & out)24 Tensor& op_flip_out(const Tensor& input, IntArrayRef dims, Tensor& out) {
25   executorch::runtime::KernelRuntimeContext context{};
26   return torch::executor::aten::flip_outf(context, input, dims, out);
27 }
28 
29 class OpFlipOutTest : public ::testing::Test {
30  protected:
SetUp()31   void SetUp() override {
32     // Since these tests cause ET_LOG to be called, the PAL must be initialized
33     // first.
34     torch::executor::runtime_init();
35   }
36 };
37 
TEST_F(OpFlipOutTest,SmokeTest1Dim)38 TEST_F(OpFlipOutTest, SmokeTest1Dim) {
39   TensorFactory<ScalarType::Float> tfFloat;
40 
41   Tensor input =
42       tfFloat.make({4, 1, 3}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
43   int64_t dims_data[1] = {-1};
44   IntArrayRef dims = IntArrayRef(dims_data, 1);
45   Tensor out = tfFloat.zeros({4, 1, 3});
46   Tensor out_expected =
47       tfFloat.make({4, 1, 3}, {3, 2, 1, 6, 5, 4, 9, 8, 7, 12, 11, 10});
48   op_flip_out(input, dims, out);
49   EXPECT_TENSOR_CLOSE(out, out_expected);
50 }
51 
TEST_F(OpFlipOutTest,SmokeTest2Dims)52 TEST_F(OpFlipOutTest, SmokeTest2Dims) {
53   TensorFactory<ScalarType::Float> tfFloat;
54 
55   Tensor input =
56       tfFloat.make({4, 1, 3}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
57   int64_t dims_data[2] = {-1, 0};
58   IntArrayRef dims = IntArrayRef(dims_data, 2);
59   Tensor out = tfFloat.zeros({4, 1, 3});
60   Tensor out_expected =
61       tfFloat.make({4, 1, 3}, {12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1});
62   op_flip_out(input, dims, out);
63   EXPECT_TENSOR_CLOSE(out, out_expected);
64 }
65