xref: /aosp_15_r20/external/executorch/kernels/test/op_abs_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 
15 #include <gtest/gtest.h>
16 
17 using namespace ::testing;
18 using exec_aten::ScalarType;
19 using exec_aten::Tensor;
20 using torch::executor::testing::TensorFactory;
21 
22 class OpAbsTest : public OperatorTest {
23  protected:
op_abs_out(const Tensor & self,Tensor & out)24   Tensor& op_abs_out(const Tensor& self, Tensor& out) {
25     return torch::executor::aten::abs_outf(context_, self, out);
26   }
27 };
28 
TEST_F(OpAbsTest,SanityCheck)29 TEST_F(OpAbsTest, SanityCheck) {
30   TensorFactory<ScalarType::Float> tf;
31 
32   Tensor in = tf.make({1, 7}, {-3.0, -2.5, -1.01, 0.0, 1.01, 2.5, 3.0});
33   Tensor out = tf.zeros({1, 7});
34   Tensor expected = tf.make({1, 7}, {3.0, 2.5, 1.01, 0.0, 1.01, 2.5, 3.0});
35 
36   Tensor ret = op_abs_out(in, out);
37 
38   EXPECT_TENSOR_EQ(out, ret);
39   EXPECT_TENSOR_EQ(out, expected);
40 }
41 
TEST_F(OpAbsTest,MemoryFormatCheck)42 TEST_F(OpAbsTest, MemoryFormatCheck) {
43   TensorFactory<ScalarType::Float> tf;
44 
45   std::vector<int32_t> sizes = {2, 3, 1, 5};
46 
47   Tensor input_contiguous =
48       tf.make(sizes, {0.8737,  0.5359,  0.3743,  -0.3040, -0.7800, -0.2306,
49                       -0.7684, -0.5364, 0.3478,  -0.3289, 0.0829,  0.2939,
50                       -0.8211, 0.8572,  -0.0802, 0.9252,  -0.2093, 0.9013,
51                       -0.4197, 0.3987,  -0.5291, -0.5567, 0.2691,  0.7819,
52                       -0.8009, -0.4286, -0.9299, 0.2143,  0.2565,  -0.5701});
53   Tensor expected_contiguous = tf.make(
54       sizes, {0.8737, 0.5359, 0.3743, 0.3040, 0.7800, 0.2306, 0.7684, 0.5364,
55               0.3478, 0.3289, 0.0829, 0.2939, 0.8211, 0.8572, 0.0802, 0.9252,
56               0.2093, 0.9013, 0.4197, 0.3987, 0.5291, 0.5567, 0.2691, 0.7819,
57               0.8009, 0.4286, 0.9299, 0.2143, 0.2565, 0.5701});
58 
59   ET_TEST_OP_SUPPORTS_MEMORY_FORMATS(
60       tf,
61       op_abs_out,
62       input_contiguous,
63       expected_contiguous,
64       /*channels_last_support=*/true);
65 }
66