xref: /aosp_15_r20/external/executorch/kernels/test/op_sigmoid_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/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::SupportedFeatures;
22 using torch::executor::testing::TensorFactory;
23 
24 class OpSigmoidOutTest : public OperatorTest {
25  protected:
op_sigmoid_out(const Tensor & self,Tensor & out)26   Tensor& op_sigmoid_out(const Tensor& self, Tensor& out) {
27     return torch::executor::aten::sigmoid_outf(context_, self, out);
28   }
29 
30   // Common testing for sigmoid operator
31   template <ScalarType DTYPE, ScalarType OUTPUT_DTYPE>
test_integer_sigmoid_out()32   void test_integer_sigmoid_out() {
33     TensorFactory<DTYPE> tf;
34     TensorFactory<OUTPUT_DTYPE> tf_out;
35 
36     const std::vector<int32_t> sizes = {2, 2};
37 
38     // Destination for the sigmoid operator.
39     Tensor out = tf_out.zeros(sizes);
40 
41     op_sigmoid_out(tf.make(sizes, /*data=*/{1, 2, 4, 8}), out);
42 
43     // Check that it matches (or close to) the expected output.
44     EXPECT_TENSOR_CLOSE(
45         out,
46         tf_out.make(sizes, /*data=*/{0.731059, 0.880797, 0.982014, 0.999665}));
47   }
48 
49   // Unhandled output dtypes.
50   template <ScalarType OUTPUT_DTYPE>
test_sigmoid_invalid_output_dtype_dies()51   void test_sigmoid_invalid_output_dtype_dies() {
52     TensorFactory<ScalarType::Float> tf;
53     TensorFactory<OUTPUT_DTYPE> tf_out;
54 
55     const std::vector<int32_t> sizes = {2, 5};
56 
57     Tensor in = tf.ones(sizes);
58     Tensor out = tf_out.zeros(sizes);
59 
60     ET_EXPECT_KERNEL_FAILURE(context_, op_sigmoid_out(in, out));
61   }
62 };
63 
TEST_F(OpSigmoidOutTest,AllRealInputHalfOutputSupport)64 TEST_F(OpSigmoidOutTest, AllRealInputHalfOutputSupport) {
65   if (torch::executor::testing::SupportedFeatures::get()->is_aten) {
66     GTEST_SKIP() << "Test Half support only for ExecuTorch mode";
67   }
68 #define TEST_ENTRY(ctype, dtype) \
69   test_integer_sigmoid_out<ScalarType::dtype, ScalarType::Half>();
70   ET_FORALL_REALH_TYPES(TEST_ENTRY);
71 #undef TEST_ENTRY
72 }
73 
TEST_F(OpSigmoidOutTest,AllRealInputFloatOutputSupport)74 TEST_F(OpSigmoidOutTest, AllRealInputFloatOutputSupport) {
75 #define TEST_ENTRY(ctype, dtype) \
76   test_integer_sigmoid_out<ScalarType::dtype, ScalarType::Float>();
77   ET_FORALL_REAL_TYPES(TEST_ENTRY);
78 #undef TEST_ENTRY
79 }
80 
TEST_F(OpSigmoidOutTest,AllRealInputDoubleOutputSupport)81 TEST_F(OpSigmoidOutTest, AllRealInputDoubleOutputSupport) {
82 #define TEST_ENTRY(ctype, dtype) \
83   test_integer_sigmoid_out<ScalarType::dtype, ScalarType::Double>();
84   ET_FORALL_REAL_TYPES(TEST_ENTRY);
85 #undef TEST_ENTRY
86 }
87 
88 // Mismatched shape tests.
TEST_F(OpSigmoidOutTest,MismatchedShapesDies)89 TEST_F(OpSigmoidOutTest, MismatchedShapesDies) {
90   if (SupportedFeatures::get()->is_aten) {
91     GTEST_SKIP() << "ATen kernel can handle mismatched shapes";
92   }
93 
94   TensorFactory<ScalarType::Int> tf;
95   TensorFactory<ScalarType::Float> tf_out;
96 
97   Tensor a = tf.ones(/*sizes=*/{4});
98   Tensor out = tf_out.ones(/*sizes=*/{2, 2});
99 
100   ET_EXPECT_KERNEL_FAILURE(context_, op_sigmoid_out(a, out));
101 }
102 
TEST_F(OpSigmoidOutTest,AllNonFloatOutputDTypeDies)103 TEST_F(OpSigmoidOutTest, AllNonFloatOutputDTypeDies) {
104 #define TEST_ENTRY(ctype, dtype) \
105   test_sigmoid_invalid_output_dtype_dies<ScalarType::dtype>();
106   ET_FORALL_INT_TYPES(TEST_ENTRY);
107 #undef TEST_ENTRY
108 }
109