-
Notifications
You must be signed in to change notification settings - Fork 531
/
Copy pathop_full_test.cpp
124 lines (102 loc) · 4.02 KB
/
op_full_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* 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/test/utils/DeathTest.h>
#include <gtest/gtest.h>
using namespace ::testing;
using executorch::aten::IntArrayRef;
using executorch::aten::MemoryFormat;
using executorch::aten::optional;
using executorch::aten::Scalar;
using executorch::aten::ScalarType;
using executorch::aten::Tensor;
using torch::executor::testing::TensorFactory;
class OpFullOutTest : public OperatorTest {
protected:
Tensor&
op_full_out(const IntArrayRef sizes, const Scalar& fill_value, Tensor& out) {
return torch::executor::aten::full_outf(context_, sizes, fill_value, out);
}
template <ScalarType DTYPE>
void test_ones_out(std::vector<int32_t>&& size_int32_t) {
TensorFactory<DTYPE> tf;
std::vector<int64_t> size_int64_t(size_int32_t.begin(), size_int32_t.end());
auto aref = IntArrayRef(size_int64_t.data(), size_int64_t.size());
// Boolean Scalar
// Before: `out` consists of 0s.
Tensor out = tf.zeros(size_int32_t);
// After: `out` consists of 1s.
op_full_out(aref, true, out);
EXPECT_TENSOR_EQ(out, tf.ones(size_int32_t));
// Integral Scalar
// Before: `out` consists of 0s.
out = tf.zeros(size_int32_t);
// After: `out` consists of 1s.
op_full_out(aref, 1, out);
EXPECT_TENSOR_EQ(out, tf.ones(size_int32_t));
// Floating Point Scalar
// Before: `out` consists of 0s.
out = tf.zeros(size_int32_t);
// After: `out` consists of 1s.
op_full_out(aref, 1.0, out);
EXPECT_TENSOR_EQ(out, tf.ones(size_int32_t));
}
};
#define GENERATE_TEST(_, DTYPE) \
TEST_F(OpFullOutTest, DTYPE##Tensors) { \
test_ones_out<ScalarType::DTYPE>({}); \
test_ones_out<ScalarType::DTYPE>({1}); \
test_ones_out<ScalarType::DTYPE>({1, 1, 1}); \
test_ones_out<ScalarType::DTYPE>({2, 0, 4}); \
test_ones_out<ScalarType::DTYPE>({2, 3, 4}); \
}
ET_FORALL_REALHBF16_TYPES(GENERATE_TEST)
TEST_F(OpFullOutTest, ValueOverflow) {
if (torch::executor::testing::SupportedFeatures::get()->is_aten) {
GTEST_SKIP() << "ATen kernel doesn't handle overflow";
}
TensorFactory<ScalarType::Byte> tf;
std::vector<int64_t> sizes_int64_t_vec = {2, 3};
std::vector<int32_t> sizes_in32_t_vec = {2, 3};
auto sizes = IntArrayRef(sizes_int64_t_vec.data(), sizes_int64_t_vec.size());
Tensor out = tf.zeros(sizes_in32_t_vec);
op_full_out(sizes, 1000, out);
}
TEST_F(OpFullOutTest, HalfSupport) {
TensorFactory<ScalarType::Half> tf;
std::vector<int64_t> sizes_int64_t_vec = {2, 3};
std::vector<int32_t> sizes_in32_t_vec = {2, 3};
auto sizes = IntArrayRef(sizes_int64_t_vec.data(), sizes_int64_t_vec.size());
// Boolean Scalar
Tensor out = tf.zeros(sizes_in32_t_vec);
op_full_out(sizes, true, out);
EXPECT_TENSOR_EQ(out, tf.ones(sizes_in32_t_vec));
// Integral Scalar
out = tf.zeros(sizes_in32_t_vec);
op_full_out(sizes, 1, out);
EXPECT_TENSOR_EQ(out, tf.ones(sizes_in32_t_vec));
// Floating Point Scalar
out = tf.zeros(sizes_in32_t_vec);
op_full_out(sizes, 3.1415926535, out);
EXPECT_TENSOR_EQ(out, tf.full(sizes_in32_t_vec, 3.1415926535));
}
TEST_F(OpFullOutTest, ZeroDim) {
TensorFactory<ScalarType::Half> tf;
std::vector<int64_t> sizes_int64_t_vec = {};
std::vector<int32_t> sizes_in32_t_vec = {};
auto sizes = IntArrayRef(sizes_int64_t_vec.data(), sizes_int64_t_vec.size());
// Boolean Scalar
Tensor out = tf.zeros(sizes_in32_t_vec);
op_full_out(sizes, true, out);
EXPECT_TENSOR_EQ(out, tf.ones(sizes_in32_t_vec));
}