xref: /aosp_15_r20/external/executorch/kernels/test/op_erf_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/UnaryUfuncRealHBBF16ToFloatHBF16Test.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 OpErfOutTest
24     : public torch::executor::testing::UnaryUfuncRealHBBF16ToFloatHBF16Test {
25  protected:
op_out(const Tensor & self,Tensor & out)26   Tensor& op_out(const Tensor& self, Tensor& out) override {
27     return torch::executor::aten::erf_outf(context_, self, out);
28   }
29 
op_reference(double x) const30   double op_reference(double x) const override {
31     return std::erf(x);
32   }
33 
34   torch::executor::testing::SupportedFeatures* get_supported_features()
35       const override;
36 };
37 
TEST_F(OpErfOutTest,SanityCheck)38 TEST_F(OpErfOutTest, SanityCheck) {
39   TensorFactory<ScalarType::Float> tf;
40 
41   Tensor in = tf.make({1, 7}, {-3.0, -2.99, -1.01, 0.0, 1.01, 2.99, 3.0});
42   Tensor out = tf.zeros({1, 7});
43   // clang-format off
44   Tensor expected = tf.make({1, 7}, {-0.999978, -0.999976, -0.846811,  0.000000,  0.846811,  0.999976, 0.999978});
45   // clang-format on
46 
47   Tensor ret = op_out(in, out);
48 
49   EXPECT_TENSOR_EQ(out, ret);
50   EXPECT_TENSOR_CLOSE(out, expected);
51 }
52 
53 IMPLEMENT_UNARY_UFUNC_REALHB_TO_FLOATH_TEST(OpErfOutTest)
54