-
Notifications
You must be signed in to change notification settings - Fork 531
/
Copy pathop_masked_scatter_test.cpp
145 lines (109 loc) · 4.35 KB
/
op_masked_scatter_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
/*
* 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::ScalarType;
using executorch::aten::Tensor;
using torch::executor::testing::SupportedFeatures;
using torch::executor::testing::TensorFactory;
class OpMaskedScatterOutTest : public OperatorTest {
protected:
Tensor& op_masked_scatter_out(
const Tensor& in,
const Tensor& mask,
const Tensor& src,
Tensor& out) {
return torch::executor::aten::masked_scatter_outf(
context_, in, mask, src, out);
}
};
TEST_F(OpMaskedScatterOutTest, SmokeTest) {
TensorFactory<ScalarType::Int> tf;
TensorFactory<ScalarType::Bool> tfBool;
Tensor in = tf.make({2, 3}, {1, 2, 3, 4, 5, 6});
Tensor mask = tfBool.make({2, 3}, {true, false, false, true, false, true});
Tensor src = tf.make({3}, {10, 20, 30});
Tensor out = tf.zeros({2, 3});
op_masked_scatter_out(in, mask, src, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 3}, {10, 2, 3, 20, 5, 30}));
}
TEST_F(OpMaskedScatterOutTest, BroadcastInput) {
TensorFactory<ScalarType::Int> tf;
TensorFactory<ScalarType::Bool> tfBool;
Tensor in = tf.make({3}, {1, 2, 3});
Tensor mask = tfBool.make({2, 3}, {true, false, false, true, false, true});
Tensor src = tf.make({3}, {10, 20, 30});
Tensor out = tf.zeros({2, 3});
op_masked_scatter_out(in, mask, src, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 3}, {10, 2, 3, 20, 2, 30}));
}
TEST_F(OpMaskedScatterOutTest, BroadcastMask) {
TensorFactory<ScalarType::Int> tf;
TensorFactory<ScalarType::Bool> tfBool;
Tensor in = tf.make({2, 3}, {1, 2, 3, 4, 5, 6});
Tensor mask = tfBool.make({3}, {false, true, false});
Tensor src = tf.make({2}, {10, 20});
Tensor out = tf.zeros({2, 3});
op_masked_scatter_out(in, mask, src, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 3}, {1, 10, 3, 4, 20, 6}));
}
TEST_F(OpMaskedScatterOutTest, SrcWithMoreElements) {
TensorFactory<ScalarType::Int> tf;
TensorFactory<ScalarType::Bool> tfBool;
Tensor in = tf.make({2, 3}, {1, 2, 3, 4, 5, 6});
Tensor mask = tfBool.make({2, 3}, {true, false, false, true, false, true});
Tensor src = tf.make({4}, {10, 20, 30, 40});
Tensor out = tf.zeros({2, 3});
op_masked_scatter_out(in, mask, src, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 3}, {10, 2, 3, 20, 5, 30}));
}
TEST_F(OpMaskedScatterOutTest, SrcWithLessElementsFails) {
TensorFactory<ScalarType::Int> tf;
TensorFactory<ScalarType::Bool> tfBool;
Tensor in = tf.make({2, 3}, {1, 2, 3, 4, 5, 6});
Tensor mask = tfBool.make({2, 3}, {true, false, false, true, false, true});
Tensor src = tf.make({2}, {10, 20});
Tensor out = tf.zeros({2, 3});
ET_EXPECT_KERNEL_FAILURE(context_, op_masked_scatter_out(in, mask, src, out));
}
TEST_F(OpMaskedScatterOutTest, EmptyMask) {
TensorFactory<ScalarType::Int> tf;
TensorFactory<ScalarType::Bool> tfBool;
Tensor in = tf.make({2, 1}, {100, 200});
Tensor mask = tfBool.make({2, 0}, {});
Tensor src = tf.make({4}, {10, 20, 30, 40});
Tensor out = tf.zeros({2, 0});
op_masked_scatter_out(in, mask, src, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 0}, {}));
}
TEST_F(OpMaskedScatterOutTest, EmptySrc) {
TensorFactory<ScalarType::Int> tf;
TensorFactory<ScalarType::Bool> tfBool;
Tensor in = tf.make({2, 1}, {100, 200});
Tensor mask = tfBool.make({2, 1}, {false, false});
Tensor src = tf.make({0}, {});
Tensor out = tf.zeros({2, 1});
op_masked_scatter_out(in, mask, src, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 1}, {100, 200}));
}
TEST_F(OpMaskedScatterOutTest, EmptyMaskAndSrc) {
TensorFactory<ScalarType::Int> tf;
TensorFactory<ScalarType::Bool> tfBool;
Tensor in = tf.make({2, 1}, {100, 200});
Tensor mask = tfBool.make({0}, {});
Tensor src = tf.make({0}, {});
Tensor out = tf.zeros({2, 0});
op_masked_scatter_out(in, mask, src, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 0}, {}));
}