-
Notifications
You must be signed in to change notification settings - Fork 74k
/
ragged_range_op_test.cc
238 lines (206 loc) · 9.93 KB
/
ragged_range_op_test.cc
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include <gtest/gtest.h>
#include "tensorflow/core/framework/fake_input.h"
#include "tensorflow/core/framework/node_def_builder.h"
#include "tensorflow/core/framework/shape_inference.h"
#include "tensorflow/core/framework/shape_inference_testutil.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/framework/tensor_testutil.h"
#include "tensorflow/core/kernels/ops_testutil.h"
#include "tensorflow/core/lib/core/status_test_util.h"
#include "tensorflow/core/platform/test.h"
namespace tensorflow {
namespace {
class RaggedRangeOpTest : public ::tensorflow::OpsTestBase {
protected:
// Indices of output tensors.
static constexpr int kSplitsOutput = 0;
static constexpr int kValuesOutput = 1;
// Builds the tensorflow test graph for the RaggedRange op.
template <typename T>
void BuildRaggedRangeGraph() {
const auto& dtype = DataTypeToEnum<T>::v();
TF_ASSERT_OK(NodeDefBuilder("tested_op", "RaggedRange")
.Input(FakeInput(dtype)) // starts
.Input(FakeInput(dtype)) // limits
.Input(FakeInput(dtype)) // deltas
.Attr("T", dtype)
.Finalize(node_def()));
TF_ASSERT_OK(InitOp());
}
};
TEST_F(RaggedRangeOpTest, IntValues) {
BuildRaggedRangeGraph<int>();
AddInputFromArray<int>(TensorShape({4}), {0, 5, 8, 5}); // starts
AddInputFromArray<int>(TensorShape({4}), {8, 7, 8, 1}); // limits
AddInputFromArray<int>(TensorShape({4}), {2, 1, 1, -1}); // deltas
TF_ASSERT_OK(RunOpKernel());
// Expected: [[0, 2, 4, 6], [5, 6], [], [5, 4, 3, 2]]
test::ExpectTensorEqual<int64_t>(*GetOutput(kSplitsOutput),
test::AsTensor<int64_t>({0, 4, 6, 6, 10}));
test::ExpectTensorEqual<int>(
*GetOutput(kValuesOutput),
test::AsTensor<int>({0, 2, 4, 6, 5, 6, 5, 4, 3, 2}));
}
TEST_F(RaggedRangeOpTest, FloatValues) {
BuildRaggedRangeGraph<float>();
AddInputFromArray<float>(TensorShape({4}), {0, 5, 8, 5}); // starts
AddInputFromArray<float>(TensorShape({4}), {8, 7, 8, 1}); // limits
AddInputFromArray<float>(TensorShape({4}), {2, 1, 1, -1}); // deltas
TF_ASSERT_OK(RunOpKernel());
// Expected: [[0, 2, 4, 6], [5, 6], [], [5, 4, 3, 2]]
test::ExpectTensorEqual<int64_t>(*GetOutput(kSplitsOutput),
test::AsTensor<int64_t>({0, 4, 6, 6, 10}));
test::ExpectTensorNear<float>(
*GetOutput(kValuesOutput),
test::AsTensor<float>({0, 2, 4, 6, 5, 6, 5, 4, 3, 2}), 0.1);
}
TEST_F(RaggedRangeOpTest, RangeSizeOverflow) {
BuildRaggedRangeGraph<float>();
AddInputFromArray<float>(TensorShape({2}), {1.1, 0.1}); // starts
AddInputFromArray<float>(TensorShape({2}), {10.0, 1e10}); // limits
AddInputFromArray<float>(TensorShape({2}), {1, 1e-10}); // deltas
EXPECT_EQ(absl::StrCat("Requires ((limit - start) / delta) <= ",
std::numeric_limits<int64_t>::max()),
RunOpKernel().message());
}
TEST_F(RaggedRangeOpTest, BroadcastDeltas) {
BuildRaggedRangeGraph<int>();
AddInputFromArray<int>(TensorShape({3}), {0, 5, 8}); // starts
AddInputFromArray<int>(TensorShape({3}), {8, 7, 8}); // limits
AddInputFromArray<int>(TensorShape({}), {1}); // deltas
TF_ASSERT_OK(RunOpKernel());
// Expected: [[0, 1, 2, 3, 4, 5, 6, 7], [5, 6], []]
test::ExpectTensorEqual<int64_t>(*GetOutput(kSplitsOutput),
test::AsTensor<int64_t>({0, 8, 10, 10}));
test::ExpectTensorEqual<int>(
*GetOutput(kValuesOutput),
test::AsTensor<int>({0, 1, 2, 3, 4, 5, 6, 7, 5, 6}));
}
TEST_F(RaggedRangeOpTest, BroadcastLimitsAndDeltas) {
BuildRaggedRangeGraph<int>();
AddInputFromArray<int>(TensorShape({}), {0}); // starts
AddInputFromArray<int>(TensorShape({3}), {3, 0, 2}); // limits
AddInputFromArray<int>(TensorShape({}), {1}); // deltas
TF_ASSERT_OK(RunOpKernel());
// Expected: [[0, 1, 2], [], [0, 1]]
test::ExpectTensorEqual<int64_t>(*GetOutput(kSplitsOutput),
test::AsTensor<int64_t>({0, 3, 3, 5}));
test::ExpectTensorEqual<int>(*GetOutput(kValuesOutput),
test::AsTensor<int>({0, 1, 2, 0, 1}));
}
TEST_F(RaggedRangeOpTest, BroadcastStartsAndLimits) {
BuildRaggedRangeGraph<int>();
AddInputFromArray<int>(TensorShape({}), {0}); // starts
AddInputFromArray<int>(TensorShape({}), {12}); // limits
AddInputFromArray<int>(TensorShape({3}), {3, 4, 5}); // deltas
TF_ASSERT_OK(RunOpKernel());
// Expected: [[0, 3, 6, 9], [0, 4, 8], [0, 5, 10]]]
test::ExpectTensorEqual<int64_t>(*GetOutput(kSplitsOutput),
test::AsTensor<int64_t>({0, 4, 7, 10}));
test::ExpectTensorEqual<int>(
*GetOutput(kValuesOutput),
test::AsTensor<int>({0, 3, 6, 9, 0, 4, 8, 0, 5, 10}));
}
TEST_F(RaggedRangeOpTest, AllScalarInputs) {
BuildRaggedRangeGraph<int>();
AddInputFromArray<int>(TensorShape({}), {0}); // starts
AddInputFromArray<int>(TensorShape({}), {5}); // limits
AddInputFromArray<int>(TensorShape({}), {1}); // deltas
TF_ASSERT_OK(RunOpKernel());
// Expected: [[0, 1, 2, 3, 4]
test::ExpectTensorEqual<int64_t>(*GetOutput(kSplitsOutput),
test::AsTensor<int64_t>({0, 5}));
test::ExpectTensorEqual<int>(*GetOutput(kValuesOutput),
test::AsTensor<int>({0, 1, 2, 3, 4}));
}
TEST_F(RaggedRangeOpTest, InvalidArgsStarts) {
BuildRaggedRangeGraph<int>();
AddInputFromArray<int>(TensorShape({4, 1}), {0, 5, 8, 5}); // starts
AddInputFromArray<int>(TensorShape({4}), {8, 7, 8, 1}); // limits
AddInputFromArray<int>(TensorShape({4}), {2, 1, 1, -1}); // deltas
EXPECT_EQ("starts must be a scalar or vector", RunOpKernel().message());
}
TEST_F(RaggedRangeOpTest, InvalidArgsLimits) {
BuildRaggedRangeGraph<int>();
AddInputFromArray<int>(TensorShape({4}), {0, 5, 8, 5}); // starts
AddInputFromArray<int>(TensorShape({4, 1}), {8, 7, 8, 1}); // limits
AddInputFromArray<int>(TensorShape({4}), {2, 1, 1, -1}); // deltas
EXPECT_EQ("limits must be a scalar or vector", RunOpKernel().message());
}
TEST_F(RaggedRangeOpTest, InvalidArgsDeltas) {
BuildRaggedRangeGraph<int>();
AddInputFromArray<int>(TensorShape({4}), {0, 5, 8, 5}); // starts
AddInputFromArray<int>(TensorShape({4}), {8, 7, 8, 1}); // limits
AddInputFromArray<int>(TensorShape({4, 1}), {2, 1, 1, -1}); // deltas
EXPECT_EQ("deltas must be a scalar or vector", RunOpKernel().message());
}
TEST_F(RaggedRangeOpTest, InvalidArgsShapeMismatch) {
BuildRaggedRangeGraph<int>();
AddInputFromArray<int>(TensorShape({4}), {0, 5, 8, 5}); // starts
AddInputFromArray<int>(TensorShape({3}), {7, 8, 1}); // limits
AddInputFromArray<int>(TensorShape({4}), {2, 1, 1, -1}); // deltas
EXPECT_EQ("starts, limits, and deltas must have the same shape",
RunOpKernel().message());
}
TEST_F(RaggedRangeOpTest, InvalidArgsZeroDelta) {
BuildRaggedRangeGraph<int>();
AddInputFromArray<int>(TensorShape({4}), {0, 5, 8, 5}); // starts
AddInputFromArray<int>(TensorShape({4}), {7, 8, 8, 1}); // limits
AddInputFromArray<int>(TensorShape({4}), {2, 1, 0, -1}); // deltas
EXPECT_EQ("Requires delta != 0", RunOpKernel().message());
}
TEST_F(RaggedRangeOpTest, EmptyRangePositiveDelta) {
BuildRaggedRangeGraph<int>();
AddInputFromArray<int>(TensorShape({2}), {0, 5}); // starts
AddInputFromArray<int>(TensorShape({2}), {5, 0}); // limits
AddInputFromArray<int>(TensorShape({}), {2}); // deltas
TF_ASSERT_OK(RunOpKernel());
// Expected: [[0, 2, 4], []]
test::ExpectTensorEqual<int64_t>(*GetOutput(kSplitsOutput),
test::AsTensor<int64_t>({0, 3, 3}));
test::ExpectTensorEqual<int>(*GetOutput(kValuesOutput),
test::AsTensor<int>({0, 2, 4}));
}
TEST_F(RaggedRangeOpTest, EmptyRangeNegativeDelta) {
BuildRaggedRangeGraph<int>();
AddInputFromArray<int>(TensorShape({2}), {0, 5}); // starts
AddInputFromArray<int>(TensorShape({2}), {5, 0}); // limits
AddInputFromArray<int>(TensorShape({}), {-2}); // deltas
TF_ASSERT_OK(RunOpKernel());
// Expected: [[], [5, 3, 1]]
test::ExpectTensorEqual<int64_t>(*GetOutput(kSplitsOutput),
test::AsTensor<int64_t>({0, 0, 3}));
test::ExpectTensorEqual<int>(*GetOutput(kValuesOutput),
test::AsTensor<int>({5, 3, 1}));
}
TEST_F(RaggedRangeOpTest, ShapeFn) {
// RaggedRange(starts, limits, deltas) -> [splits, values]
ShapeInferenceTestOp op("RaggedRange");
INFER_OK(op, "?;?;?", "[?];[?]");
INFER_OK(op, "[3];[3];[3]", "[4];[?]");
INFER_OK(op, "[3];[3];[]", "[4];[?]"); // broadcast deltas
INFER_OK(op, "[3];[];[3]", "[4];[?]"); // broadcast limits
INFER_OK(op, "[];[3];[3]", "[4];[?]"); // broadcast starts
INFER_OK(op, "[];[];[]", "[2];[?]"); // degenerate case: all scalar inputs
INFER_ERROR("Shape must be at most rank 1 but is rank 2", op,
"[5,5];[5];[5]");
INFER_ERROR("Shape must be at most rank 1 but is rank 2", op,
"[5];[5,5];[5]");
INFER_ERROR("Shape must be at most rank 1 but is rank 2", op,
"[5];[5];[5,5]");
INFER_ERROR("Dimensions must be equal, but are 4 and 3", op, "[3];[4];[3]");
}
} // namespace
} // namespace tensorflow