xref: /aosp_15_r20/external/executorch/kernels/test/op_lift_fresh_copy_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::optional;
20 using exec_aten::ScalarType;
21 using exec_aten::Tensor;
22 using torch::executor::testing::TensorFactory;
23 
24 class OpLiftFreshCopyTest : public OperatorTest {
25  protected:
op_lift_fresh_copy_out(const Tensor & self,Tensor & out)26   Tensor& op_lift_fresh_copy_out(const Tensor& self, Tensor& out) {
27     return torch::executor::aten::lift_fresh_copy_outf(context_, self, out);
28   }
29 
30   // test if lift_fresh_copy.out works well under all kinds of legal input type.
31   template <class CTYPE, exec_aten::ScalarType DTYPE>
test_dtype()32   void test_dtype() {
33     TensorFactory<DTYPE> tf;
34     Tensor self = tf.ones(/*sizes=*/{2, 4});
35     Tensor out = tf.zeros(/*sizes=*/{2, 4});
36 
37     op_lift_fresh_copy_out(self, out);
38     EXPECT_TENSOR_EQ(self, out);
39 
40     Tensor self_empty = tf.make(/*sizes=*/{}, /*data=*/{1});
41     Tensor out_empty = tf.make(/*sizes=*/{}, /*data=*/{0});
42 
43     op_lift_fresh_copy_out(self_empty, out_empty);
44     EXPECT_TENSOR_EQ(self_empty, out_empty);
45   }
46 
47   template <class CTYPE, ScalarType DTYPE>
test_empty_input()48   void test_empty_input() {
49     TensorFactory<DTYPE> tf;
50     Tensor self = tf.make(/*sizes=*/{3, 0, 1, 2}, /*data=*/{});
51     Tensor out = tf.zeros({3, 0, 1, 2});
52     op_lift_fresh_copy_out(self, out);
53     EXPECT_TENSOR_EQ(self, out);
54   }
55 };
56 
57 // regular test for lift_fresh_copy.out
TEST_F(OpLiftFreshCopyTest,AllDtypesSupported)58 TEST_F(OpLiftFreshCopyTest, AllDtypesSupported) {
59 #define TEST_ENTRY(ctype, dtype) test_dtype<ctype, ScalarType::dtype>();
60   ET_FORALL_REAL_TYPES_AND(Bool, TEST_ENTRY);
61 #undef TEST_ENTRY
62 }
63 
TEST_F(OpLiftFreshCopyTest,EmptyInputSupported)64 TEST_F(OpLiftFreshCopyTest, EmptyInputSupported) {
65 #define TEST_ENTRY(ctype, dtype) test_empty_input<ctype, ScalarType::dtype>();
66   ET_FORALL_REAL_TYPES_AND(Bool, TEST_ENTRY);
67 #undef TEST_ENTRY
68 }
69 
TEST_F(OpLiftFreshCopyTest,MismatchedSizesDie)70 TEST_F(OpLiftFreshCopyTest, MismatchedSizesDie) {
71   if (torch::executor::testing::SupportedFeatures::get()->is_aten) {
72     GTEST_SKIP() << "ATen kernel can handle mismatched sizes";
73   }
74   TensorFactory<ScalarType::Int> tf;
75   Tensor self = tf.make(/*sizes=*/{3, 1, 1, 2}, /*data=*/{1, 2, 3, 4, 5, 6});
76   Tensor out = tf.zeros({3, 2, 1, 1});
77   ET_EXPECT_KERNEL_FAILURE(context_, op_lift_fresh_copy_out(self, out));
78 }
79 
TEST_F(OpLiftFreshCopyTest,MismatchedDTypeDie)80 TEST_F(OpLiftFreshCopyTest, MismatchedDTypeDie) {
81   TensorFactory<ScalarType::Int> tf_in;
82   TensorFactory<ScalarType::Float> tf_out;
83   Tensor self = tf_in.make(/*sizes=*/{3, 1, 1, 2}, /*data=*/{1, 2, 3, 4, 5, 6});
84   Tensor out = tf_out.zeros({3, 1, 1, 2});
85   ET_EXPECT_KERNEL_FAILURE(context_, op_lift_fresh_copy_out(self, out));
86 }
87