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/UnaryUfuncRealHBBF16ToFloatHBF16Test.h>
11
12 #include <gtest/gtest.h>
13
14 #include <cmath>
15
16 using exec_aten::ScalarType;
17 using exec_aten::Tensor;
18 using torch::executor::testing::SupportedFeatures;
19 using torch::executor::testing::TensorFactory;
20 class OpLogOutTest
21 : public torch::executor::testing::UnaryUfuncRealHBBF16ToFloatHBF16Test {
22 protected:
op_out(const Tensor & self,Tensor & out)23 Tensor& op_out(const Tensor& self, Tensor& out) override {
24 return torch::executor::aten::log_outf(context_, self, out);
25 }
26
op_reference(double x) const27 double op_reference(double x) const override {
28 return std::log(x);
29 }
30
31 torch::executor::testing::SupportedFeatures* get_supported_features()
32 const override;
33 };
34
35 IMPLEMENT_UNARY_UFUNC_REALHB_TO_FLOATH_TEST(OpLogOutTest)
36
TEST_F(OpLogOutTest,SimpleGeneratedCase)37 TEST_F(OpLogOutTest, SimpleGeneratedCase) {
38 TensorFactory<ScalarType::Float> tf;
39
40 Tensor x = tf.make(
41 {10, 10},
42 {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
43 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
44 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
45 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
46 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
47 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
48 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
49 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0});
50 Tensor expected_result = tf.make(
51 {10, 10},
52 {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
53 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
54 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
55 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
56 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
57 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
58 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
59 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0});
60
61 Tensor out = tf.zeros({10, 10});
62 Tensor ret = op_out(x, out);
63 EXPECT_TENSOR_CLOSE(out, expected_result);
64 }
65
TEST_F(OpLogOutTest,DynamicShapeUpperBoundSameAsExpected)66 TEST_F(OpLogOutTest, DynamicShapeUpperBoundSameAsExpected) {
67 TensorFactory<ScalarType::Float> tf;
68
69 Tensor x = tf.make(
70 {3, 2},
71 {0.6879220604896545,
72 0.8289883136749268,
73 0.7889447808265686,
74 0.6339777112007141,
75 0.8719115853309631,
76 0.4185197353363037});
77 Tensor expected_result = tf.make(
78 {3, 2},
79 {-0.37407973408699036,
80 -0.18754921853542328,
81 -0.23705895245075226,
82 -0.4557414948940277,
83 -0.1370672583580017,
84 -0.8710312247276306});
85
86 Tensor out =
87 tf.zeros({3, 2}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND);
88 Tensor ret = op_out(x, out);
89 EXPECT_TENSOR_CLOSE(out, expected_result);
90 }
91