xref: /aosp_15_r20/external/executorch/kernels/test/op_isnan_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::TensorFactory;
22 
23 class OpIsNanTest : public OperatorTest {
24  protected:
op_isnan_out(const Tensor & self,Tensor & out)25   Tensor& op_isnan_out(const Tensor& self, Tensor& out) {
26     return torch::executor::aten::isnan_outf(context_, self, out);
27   }
28 
29   template <ScalarType DTYPE>
test_sanity_check()30   void test_sanity_check() {
31     TensorFactory<DTYPE> tf;
32     TensorFactory<ScalarType::Bool> tfb;
33 
34     using CTYPE = typename TensorFactory<DTYPE>::ctype;
35     Tensor in = tf.make(
36         {1, 5}, {-1, 0, 1, NAN, std::numeric_limits<CTYPE>::infinity()});
37     Tensor out = tfb.zeros({1, 5});
38     Tensor expected = tfb.make({1, 5}, {false, false, false, true, false});
39 
40     Tensor ret = op_isnan_out(in, out);
41 
42     EXPECT_TENSOR_EQ(out, ret);
43     EXPECT_TENSOR_EQ(out, expected);
44   }
45 };
46 
TEST_F(OpIsNanTest,SanityCheck)47 TEST_F(OpIsNanTest, SanityCheck) {
48 #define TEST_ENTRY(ctype, dtype) test_sanity_check<ScalarType::dtype>();
49   ET_FORALL_FLOATHBF16_TYPES(TEST_ENTRY);
50 #undef TEST_ENTRY
51 }
52 
TEST_F(OpIsNanTest,SanityCheckOutDtype)53 TEST_F(OpIsNanTest, SanityCheckOutDtype) {
54   TensorFactory<ScalarType::Int> tf;
55 
56   Tensor in = tf.make({1, 5}, {1, 2, 3, 4, 5});
57   Tensor out = tf.zeros({1, 5});
58 
59   ET_EXPECT_KERNEL_FAILURE(context_, op_isnan_out(in, out));
60 }
61