-
Notifications
You must be signed in to change notification settings - Fork 531
/
Copy pathop_empty_test.cpp
92 lines (78 loc) · 3.13 KB
/
op_empty_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/kernels/test/FunctionHeaderWrapper.h> // Declares the operator
#include <executorch/kernels/test/TestUtil.h>
#include <executorch/kernels/test/supported_features.h>
#include <executorch/runtime/core/exec_aten/exec_aten.h>
#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h>
#include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h>
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
#include <gtest/gtest.h>
using namespace ::testing;
using executorch::aten::IntArrayRef;
using executorch::aten::MemoryFormat;
using executorch::aten::optional;
using executorch::aten::ScalarType;
using executorch::aten::Tensor;
using torch::executor::testing::TensorFactory;
class OpEmptyOutTest : public OperatorTest {
protected:
Tensor& op_empty_out(
IntArrayRef size,
optional<MemoryFormat> memory_format,
Tensor& out) {
return torch::executor::aten::empty_outf(
context_, size, memory_format, out);
}
template <ScalarType DTYPE>
void test_empty_out(std::vector<int32_t>&& size_int32_t) {
TensorFactory<DTYPE> tf;
std::vector<int64_t> sizes(size_int32_t.begin(), size_int32_t.end());
auto aref = executorch::aten::ArrayRef<int64_t>(sizes.data(), sizes.size());
optional<MemoryFormat> memory_format;
Tensor out = tf.ones(size_int32_t);
op_empty_out(aref, memory_format, out);
}
};
#define GENERATE_TEST(_, DTYPE) \
TEST_F(OpEmptyOutTest, DTYPE##Tensors) { \
test_empty_out<ScalarType::DTYPE>({2, 3, 4}); \
test_empty_out<ScalarType::DTYPE>({2, 0, 4}); \
test_empty_out<ScalarType::DTYPE>({}); \
}
ET_FORALL_REAL_TYPES_AND(Bool, GENERATE_TEST)
TEST_F(OpEmptyOutTest, DynamicShapeUpperBoundSameAsExpected) {
TensorFactory<ScalarType::Float> tf;
int64_t sizes[2] = {3, 2};
auto sizes_aref = executorch::aten::ArrayRef<int64_t>(sizes);
optional<MemoryFormat> memory_format;
Tensor out =
tf.ones({3, 2}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND);
op_empty_out(sizes_aref, memory_format, out);
}
TEST_F(OpEmptyOutTest, DynamicShapeUpperBoundLargerThanExpected) {
TensorFactory<ScalarType::Float> tf;
int64_t sizes[2] = {3, 2};
auto sizes_aref = executorch::aten::ArrayRef<int64_t>(sizes);
optional<MemoryFormat> memory_format;
Tensor out =
tf.ones({10, 10}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND);
op_empty_out(sizes_aref, memory_format, out);
}
TEST_F(OpEmptyOutTest, DynamicShapeUnbound) {
if (!torch::executor::testing::SupportedFeatures::get()->output_resize) {
GTEST_SKIP() << "Dynamic shape unbound not supported";
}
TensorFactory<ScalarType::Float> tf;
int64_t sizes[2] = {3, 2};
auto sizes_aref = executorch::aten::ArrayRef<int64_t>(sizes);
optional<MemoryFormat> memory_format;
Tensor out =
tf.ones({1, 1}, torch::executor::TensorShapeDynamism::DYNAMIC_UNBOUND);
op_empty_out(sizes_aref, memory_format, out);
}