-
Notifications
You must be signed in to change notification settings - Fork 531
/
Copy pathop_eq_test.cpp
182 lines (147 loc) · 5.45 KB
/
op_eq_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* 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 <gtest/gtest.h>
using namespace ::testing;
using executorch::aten::Scalar;
using executorch::aten::ScalarType;
using executorch::aten::Tensor;
using torch::executor::testing::TensorFactory;
class OpEqScalarOutTest : public OperatorTest {
protected:
Tensor& op_eq_scalar_out(const Tensor& self, Scalar& other, Tensor& out) {
return torch::executor::aten::eq_outf(context_, self, other, out);
}
// Common testing for eq operator
template <ScalarType DTYPE>
void test_eq_scalar_out() {
TensorFactory<DTYPE> tf;
TensorFactory<ScalarType::Bool> tf_out;
const std::vector<int32_t> sizes = {2, 2};
// Destination for the eq
Tensor out = tf_out.ones(sizes);
Scalar other = 3;
// Valid input should give the expected output
op_eq_scalar_out(tf.make(sizes, /*data=*/{2, 3, 3, 3}), other, out);
EXPECT_TENSOR_EQ(
out, tf_out.make(sizes, /*data=*/{false, true, true, true}));
}
// Handle all output dtypes.
template <ScalarType OUTPUT_DTYPE>
void test_eq_all_output_dtypes() {
TensorFactory<ScalarType::Float> tf_float;
TensorFactory<OUTPUT_DTYPE> tf_out;
const std::vector<int32_t> sizes = {2, 5};
Tensor in = tf_float.ones(sizes);
Tensor out = tf_out.zeros(sizes);
Scalar other = 1;
op_eq_scalar_out(in, other, out);
EXPECT_TENSOR_EQ(out, tf_out.ones(sizes));
}
};
TEST_F(OpEqScalarOutTest, AllRealInputBoolOutputSupport) {
#define TEST_ENTRY(ctype, dtype) test_eq_scalar_out<ScalarType::dtype>();
ET_FORALL_REAL_TYPES(TEST_ENTRY);
#undef TEST_ENTRY
}
TEST_F(OpEqScalarOutTest, BoolInputDtype) {
TensorFactory<ScalarType::Bool> tf_bool;
const std::vector<int32_t> sizes = {2, 2};
Tensor a = tf_bool.make(sizes, /*data=*/{false, true, false, true});
Tensor out = tf_bool.zeros(sizes);
Scalar other = 1;
op_eq_scalar_out(a, other, out);
EXPECT_TENSOR_EQ(
out, tf_bool.make(sizes, /*data=*/{false, true, false, true}));
}
// Mismatched shape tests.
TEST_F(OpEqScalarOutTest, MismatchedShapesDies) {
if (torch::executor::testing::SupportedFeatures::get()->is_aten) {
GTEST_SKIP() << "ATen kernel can handle mismatched shapes";
}
TensorFactory<ScalarType::Int> tf_int;
TensorFactory<ScalarType::Bool> tf_bool;
Tensor a = tf_int.ones(/*sizes=*/{4});
Tensor out = tf_bool.ones(/*sizes=*/{2, 2});
Scalar other = 3;
ET_EXPECT_KERNEL_FAILURE(context_, op_eq_scalar_out(a, other, out));
}
TEST_F(OpEqScalarOutTest, AllRealOutputDTypes) {
if (torch::executor::testing::SupportedFeatures::get()->is_aten) {
GTEST_SKIP() << "ATen kernel can handle non-bool output dtype";
}
#define TEST_ENTRY(ctype, dtype) test_eq_all_output_dtypes<ScalarType::dtype>();
ET_FORALL_REAL_TYPES(TEST_ENTRY);
#undef TEST_ENTRY
}
/* %python
import torch
torch.manual_seed(0)
x = torch.randint(3, (3, 2))
res = torch.eq(x, 2)
op = "op_eq_scalar_out"
opt_setup_params = """
Scalar other = 2;
"""
opt_extra_params = "other,"
dtype = "ScalarType::Int"
out_dtype = "ScalarType::Bool"
check = "EXPECT_TENSOR_EQ" */
TEST_F(OpEqScalarOutTest, DynamicShapeUpperBoundSameAsExpected) {
/* %python
out_args = "{3, 2}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND"
%rewrite(unary_op_out_dtype) */
TensorFactory<ScalarType::Int> tf;
TensorFactory<ScalarType::Bool> tfOut;
Tensor x = tf.make({3, 2}, {2, 0, 2, 0, 1, 0});
Tensor expected =
tfOut.make({3, 2}, {true, false, true, false, false, false});
Scalar other = 2;
Tensor out =
tfOut.zeros({3, 2}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND);
op_eq_scalar_out(x, other, out);
EXPECT_TENSOR_EQ(out, expected);
}
TEST_F(OpEqScalarOutTest, DynamicShapeUpperBoundLargerThanExpected) {
/* %python
out_args = "{10, 10}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND"
%rewrite(unary_op_out_dtype) */
TensorFactory<ScalarType::Int> tf;
TensorFactory<ScalarType::Bool> tfOut;
Tensor x = tf.make({3, 2}, {2, 0, 2, 0, 1, 0});
Tensor expected =
tfOut.make({3, 2}, {true, false, true, false, false, false});
Scalar other = 2;
Tensor out = tfOut.zeros(
{10, 10}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND);
op_eq_scalar_out(x, other, out);
EXPECT_TENSOR_EQ(out, expected);
}
TEST_F(OpEqScalarOutTest, DynamicShapeUnbound) {
if (!torch::executor::testing::SupportedFeatures::get()->output_resize) {
GTEST_SKIP() << "Dynamic shape unbound not supported";
}
/* %python
out_args = "{1, 1}, torch::executor::TensorShapeDynamism::DYNAMIC_UNBOUND"
%rewrite(unary_op_out_dtype) */
TensorFactory<ScalarType::Int> tf;
TensorFactory<ScalarType::Bool> tfOut;
Tensor x = tf.make({3, 2}, {2, 0, 2, 0, 1, 0});
Tensor expected =
tfOut.make({3, 2}, {true, false, true, false, false, false});
Scalar other = 2;
Tensor out = tfOut.zeros(
{1, 1}, torch::executor::TensorShapeDynamism::DYNAMIC_UNBOUND);
op_eq_scalar_out(x, other, out);
EXPECT_TENSOR_EQ(out, expected);
}