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/kernels/test/supported_features.h>
12 #include <executorch/runtime/core/exec_aten/exec_aten.h>
13 #include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h>
14 #include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h>
15
16 #include <gtest/gtest.h>
17
18 using namespace ::testing;
19 using exec_aten::ScalarType;
20 using exec_aten::Tensor;
21 using torch::executor::testing::TensorFactory;
22
23 class OpSignTest : public OperatorTest {
24 protected:
op_sign_out(const Tensor & self,Tensor & out)25 Tensor& op_sign_out(const Tensor& self, Tensor& out) {
26 return torch::executor::aten::sign_outf(context_, self, out);
27 }
28 };
29
TEST_F(OpSignTest,ETSanityCheckFloat)30 TEST_F(OpSignTest, ETSanityCheckFloat) {
31 if (torch::executor::testing::SupportedFeatures::get()->is_aten) {
32 GTEST_SKIP() << "ATen returns 0 on NAN input";
33 }
34 TensorFactory<ScalarType::Float> tf;
35
36 Tensor in = tf.make({1, 7}, {-INFINITY, -3., -1.5, 0., 1.5, NAN, INFINITY});
37 Tensor out = tf.zeros({1, 7});
38 Tensor expected = tf.make({1, 7}, {-1., -1., -1., 0., 1., NAN, 1.});
39
40 Tensor ret = op_sign_out(in, out);
41
42 EXPECT_TENSOR_EQ(out, ret);
43 EXPECT_TENSOR_CLOSE(out, expected);
44 }
45
TEST_F(OpSignTest,ATenSanityCheckFloat)46 TEST_F(OpSignTest, ATenSanityCheckFloat) {
47 if (!torch::executor::testing::SupportedFeatures::get()->is_aten) {
48 GTEST_SKIP() << "ET returns NAN on NAN input";
49 }
50 TensorFactory<ScalarType::Float> tf;
51
52 Tensor in = tf.make({1, 7}, {-INFINITY, -3., -1.5, 0., 1.5, NAN, INFINITY});
53 Tensor out = tf.zeros({1, 7});
54 Tensor expected = tf.make({1, 7}, {-1., -1., -1., 0., 1., 0., 1.});
55
56 Tensor ret = op_sign_out(in, out);
57
58 EXPECT_TENSOR_EQ(out, ret);
59 EXPECT_TENSOR_CLOSE(out, expected);
60 }
61
TEST_F(OpSignTest,SanityCheckBool)62 TEST_F(OpSignTest, SanityCheckBool) {
63 TensorFactory<ScalarType::Bool> tf;
64
65 Tensor in = tf.make({1, 6}, {false, true, false, false, true, true});
66 Tensor out = tf.zeros({1, 6});
67 // clang-format off
68 Tensor expected = tf.make({1, 6}, {false, true, false, false, true, true});
69 // clang-format on
70
71 Tensor ret = op_sign_out(in, out);
72
73 EXPECT_TENSOR_EQ(out, ret);
74 EXPECT_TENSOR_CLOSE(out, expected);
75 }
76