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 #include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
16
17 #include <gtest/gtest.h>
18
19 using namespace ::testing;
20 using exec_aten::IntArrayRef;
21 using exec_aten::MemoryFormat;
22 using exec_aten::optional;
23 using exec_aten::ScalarType;
24 using exec_aten::Tensor;
25 using torch::executor::testing::TensorFactory;
26
27 class OpEmptyOutTest : public OperatorTest {
28 protected:
op_empty_out(IntArrayRef size,optional<MemoryFormat> memory_format,Tensor & out)29 Tensor& op_empty_out(
30 IntArrayRef size,
31 optional<MemoryFormat> memory_format,
32 Tensor& out) {
33 return torch::executor::aten::empty_outf(
34 context_, size, memory_format, out);
35 }
36
37 template <ScalarType DTYPE>
test_empty_out(std::vector<int32_t> && size_int32_t)38 void test_empty_out(std::vector<int32_t>&& size_int32_t) {
39 TensorFactory<DTYPE> tf;
40 std::vector<int64_t> sizes(size_int32_t.begin(), size_int32_t.end());
41 auto aref = exec_aten::ArrayRef<int64_t>(sizes.data(), sizes.size());
42 optional<MemoryFormat> memory_format;
43 Tensor out = tf.ones(size_int32_t);
44
45 op_empty_out(aref, memory_format, out);
46 }
47 };
48
49 #define GENERATE_TEST(_, DTYPE) \
50 TEST_F(OpEmptyOutTest, DTYPE##Tensors) { \
51 test_empty_out<ScalarType::DTYPE>({2, 3, 4}); \
52 test_empty_out<ScalarType::DTYPE>({2, 0, 4}); \
53 test_empty_out<ScalarType::DTYPE>({}); \
54 }
55
ET_FORALL_REAL_TYPES_AND(Bool,GENERATE_TEST)56 ET_FORALL_REAL_TYPES_AND(Bool, GENERATE_TEST)
57
58 TEST_F(OpEmptyOutTest, DynamicShapeUpperBoundSameAsExpected) {
59 TensorFactory<ScalarType::Float> tf;
60
61 int64_t sizes[2] = {3, 2};
62 auto sizes_aref = exec_aten::ArrayRef<int64_t>(sizes);
63 optional<MemoryFormat> memory_format;
64 Tensor out =
65 tf.ones({3, 2}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND);
66 op_empty_out(sizes_aref, memory_format, out);
67 }
68
TEST_F(OpEmptyOutTest,DynamicShapeUpperBoundLargerThanExpected)69 TEST_F(OpEmptyOutTest, DynamicShapeUpperBoundLargerThanExpected) {
70 TensorFactory<ScalarType::Float> tf;
71
72 int64_t sizes[2] = {3, 2};
73 auto sizes_aref = exec_aten::ArrayRef<int64_t>(sizes);
74 optional<MemoryFormat> memory_format;
75 Tensor out =
76 tf.ones({10, 10}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND);
77 op_empty_out(sizes_aref, memory_format, out);
78 }
79
TEST_F(OpEmptyOutTest,DynamicShapeUnbound)80 TEST_F(OpEmptyOutTest, DynamicShapeUnbound) {
81 if (!torch::executor::testing::SupportedFeatures::get()->output_resize) {
82 GTEST_SKIP() << "Dynamic shape unbound not supported";
83 }
84 TensorFactory<ScalarType::Float> tf;
85
86 int64_t sizes[2] = {3, 2};
87 auto sizes_aref = exec_aten::ArrayRef<int64_t>(sizes);
88 optional<MemoryFormat> memory_format;
89 Tensor out =
90 tf.ones({1, 1}, torch::executor::TensorShapeDynamism::DYNAMIC_UNBOUND);
91 op_empty_out(sizes_aref, memory_format, out);
92 }
93