-
Notifications
You must be signed in to change notification settings - Fork 74k
/
ragged_tensor_to_variant_op.cc
366 lines (331 loc) · 15.7 KB
/
ragged_tensor_to_variant_op.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* Copyright 2019 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 <cstdint>
#include <utility>
#include <vector>
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/op_requires.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/framework/variant.h"
#include "tensorflow/core/framework/variant_encode_decode.h"
#include "tensorflow/core/framework/variant_op_registry.h"
#include "tensorflow/core/kernels/concat_lib.h"
#include "tensorflow/core/kernels/ragged_tensor_variant.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/platform/errors.h"
#include "tensorflow/core/util/tensor_ops_util.h"
namespace tensorflow {
namespace {
template <typename VALUE_TYPE>
Status UnbatchDenseZerothDim(
const RaggedTensorVariant& batched_ragged,
std::vector<RaggedTensorVariant>* ragged_components) {
Tensor batched_values = batched_ragged.values();
TensorShape values_shape = batched_values.shape();
if (values_shape.dims() < 1) {
return errors::InvalidArgument("Can't unbatch rank-0 tensor.");
}
auto num_components = values_shape.dim_size(0);
values_shape.RemoveDim(0);
auto num_values = values_shape.num_elements();
ragged_components->resize(num_components);
const auto& batched_flat = batched_values.flat<VALUE_TYPE>();
for (auto i = decltype(num_components){}; i < num_components; i++) {
(*ragged_components)[i].set_values(
Tensor(DataTypeToEnum<VALUE_TYPE>::value, values_shape));
auto ragged_component_values_flat =
(*ragged_components)[i].mutable_values()->flat<VALUE_TYPE>();
for (auto j = decltype(num_values){}; j < num_values; j++) {
ragged_component_values_flat(j) = batched_flat(j + i * num_values);
}
}
return OkStatus();
}
template <typename VALUE_TYPE, typename SPLIT_TYPE>
Status UnbatchRaggedZerothDim(
const RaggedTensorVariant& batched_ragged,
std::vector<RaggedTensorVariant>* ragged_components) {
// Set up the component Ragged Tensors.
int ragged_rank = batched_ragged.ragged_rank();
if (ragged_rank == 0) {
return UnbatchDenseZerothDim<VALUE_TYPE>(batched_ragged, ragged_components);
}
auto batched_splits_top_vec = batched_ragged.splits(0).vec<SPLIT_TYPE>();
auto num_components = batched_splits_top_vec.size() - 1;
if (num_components < 0) {
return errors::Internal("Invalid split argument.");
}
int num_splits = ragged_rank - 1;
ragged_components->resize(num_components);
for (RaggedTensorVariant& ragged_component : *ragged_components) {
ragged_component.mutable_nested_splits()->reserve(num_splits);
}
const auto& batched_flat = batched_ragged.values().flat<VALUE_TYPE>();
auto num_inner_elems = batched_ragged.values().NumElements();
if (batched_ragged.values().dim_size(0) > 1) {
num_inner_elems /= batched_ragged.values().dim_size(0);
}
TensorShape values_shape = batched_ragged.values().shape();
// Corner case: ragged_rank == 1, e.g. [[1, 2, 3], [4, 5]]
if (num_splits == 0) {
for (auto i = decltype(num_components){}; i < num_components; i++) {
auto start = batched_splits_top_vec(i);
auto limit = batched_splits_top_vec(i + 1);
auto num_values = limit - start;
values_shape.set_dim(0, num_values);
(*ragged_components)[i].set_values(
Tensor(DataTypeToEnum<VALUE_TYPE>::value, values_shape));
auto ragged_component_values_flat =
(*ragged_components)[i].mutable_values()->template flat<VALUE_TYPE>();
for (auto j = decltype(num_values * num_inner_elems){};
j < num_values * num_inner_elems; j++) {
ragged_component_values_flat(j) =
batched_flat(j + start * num_inner_elems);
}
}
return OkStatus();
}
// Unbatch nested splits.
std::vector<typename TTypes<SPLIT_TYPE>::ConstVec> batched_splits_vec;
batched_splits_vec.reserve(ragged_rank);
for (int i = 0; i < ragged_rank; i++) {
batched_splits_vec.push_back(batched_ragged.splits(i).vec<SPLIT_TYPE>());
}
std::vector<SPLIT_TYPE> index(num_splits, 1);
std::vector<SPLIT_TYPE> ragged_component_values_size(num_components, 0);
for (auto i = decltype(num_components){}; i < num_components; i++) {
std::vector<typename TTypes<SPLIT_TYPE>::Vec> ragged_component_splits_vec;
ragged_component_splits_vec.reserve(num_splits);
SPLIT_TYPE split_size = -1;
for (int j = 0; j < num_splits; j++) {
if (j == 0) {
split_size =
batched_splits_top_vec(i + 1) - batched_splits_top_vec(i) + 1;
} else {
// Update split size based on previous split.
SPLIT_TYPE last_index = ragged_component_splits_vec[j - 1].size() - 1;
split_size = ragged_component_splits_vec[j - 1](last_index) + 1;
}
(*ragged_components)[i].append_splits(
Tensor(DataTypeToEnum<SPLIT_TYPE>::value, TensorShape({split_size})));
ragged_component_splits_vec.push_back((*ragged_components)[i]
.mutable_splits(j)
->template vec<SPLIT_TYPE>());
SPLIT_TYPE last_split_value = batched_splits_vec[j + 1](index[j] - 1);
ragged_component_splits_vec[j](0) = 0;
for (SPLIT_TYPE k = 1; k < split_size; k++, index[j]++) {
ragged_component_splits_vec[j](k) =
batched_splits_vec[j + 1](index[j]) - last_split_value;
}
}
SPLIT_TYPE last_split_size =
ragged_component_splits_vec[num_splits - 1].size();
ragged_component_values_size[i] =
ragged_component_splits_vec[num_splits - 1](last_split_size - 1);
}
// Unbatch values.
int64_t value_index = 0;
for (auto i = decltype(num_components){}; i < num_components; i++) {
SPLIT_TYPE num_values = ragged_component_values_size[i];
values_shape.set_dim(0, num_values);
(*ragged_components)[i].set_values(
Tensor(DataTypeToEnum<VALUE_TYPE>::value, values_shape));
auto ragged_component_values_flat =
(*ragged_components)[i].mutable_values()->template flat<VALUE_TYPE>();
for (int64_t j = 0; j < num_values * num_inner_elems; j++, value_index++) {
ragged_component_values_flat(j) = batched_flat(value_index);
}
}
return OkStatus();
}
} // namespace
template <typename VALUE_TYPE, typename SPLIT_TYPE>
class RaggedTensorToVariantOp : public OpKernel {
public:
explicit RaggedTensorToVariantOp(OpKernelConstruction* context)
: OpKernel(context) {
OP_REQUIRES_OK(context, context->GetAttr("batched_input", &batched_input_));
}
void Compute(OpKernelContext* context) override {
// Read ragged_splits inputs.
OpInputList ragged_nested_splits_in;
OP_REQUIRES_OK(context, context->input_list("rt_nested_splits",
&ragged_nested_splits_in));
const int ragged_nested_splits_len = ragged_nested_splits_in.size();
RaggedTensorVariant batched_ragged_input;
// Read ragged_values input.
batched_ragged_input.set_values(context->input(ragged_nested_splits_len));
batched_ragged_input.mutable_nested_splits()->reserve(
ragged_nested_splits_len);
// Validate nested_row_splits.
for (int i = ragged_nested_splits_len - 1; i >= 0; --i) {
OP_REQUIRES(context, ragged_nested_splits_in[i].dims() == 1,
errors::InvalidArgument("Requires nested_row_splits[", i, "]",
" to be rank 1 but is rank ",
ragged_nested_splits_in[i].dims()));
OP_REQUIRES(
context, ragged_nested_splits_in[i].dim_size(0) >= 1,
errors::InvalidArgument("Requires nested_row_splits[", i, "]",
" has at least one splits, but is empty."));
OP_REQUIRES(context,
ragged_nested_splits_in[i].flat<SPLIT_TYPE>()(0) ==
static_cast<SPLIT_TYPE>(0),
errors::InvalidArgument(
"Requires the first element of nested_row_splits[", i,
"]", " to be 0 but is ",
ragged_nested_splits_in[i].flat<SPLIT_TYPE>()(0)));
SPLIT_TYPE last_split = 0;
for (int j = 1; j < ragged_nested_splits_in[i].dim_size(0); j++) {
auto split = ragged_nested_splits_in[i].flat<SPLIT_TYPE>()(j);
OP_REQUIRES(
context, split >= last_split,
errors::InvalidArgument("Requires splits to be monotonically "
"increasing, but nested_row_splits[",
i, "][", j, "]=", split,
" is smaller than nested_row_splits[", i,
"][", j - 1, "]=", last_split));
last_split = split;
}
SPLIT_TYPE nvals;
if (i == ragged_nested_splits_len - 1) {
OP_REQUIRES(context, batched_ragged_input.values().dims() >= 1,
errors::InvalidArgument(
"Requires flat_values to have rank>=1 when "
"nested_row_splits is not empty, but is 0."));
nvals = batched_ragged_input.values().dim_size(0);
} else {
nvals = ragged_nested_splits_in[i + 1].dim_size(0) - 1;
}
OP_REQUIRES(context, last_split == nvals,
errors::InvalidArgument("Requires nested_row_splits[", i,
"][-1]=", last_split,
" to be equal with the number of "
"values in this dimension, which is ",
nvals, "."));
}
for (int i = 0; i < ragged_nested_splits_len; i++) {
batched_ragged_input.append_splits(ragged_nested_splits_in[i]);
}
if (!batched_input_) {
// Encode as a Scalar Variant Tensor.
Tensor* encoded_scalar;
OP_REQUIRES_OK(context, context->allocate_output(0, TensorShape({}),
&encoded_scalar));
encoded_scalar->scalar<Variant>()() = std::move(batched_ragged_input);
return;
}
// Unbatch the Ragged Tensor and encode the components.
std::vector<RaggedTensorVariant> unbatched_ragged_input;
OP_REQUIRES_OK(context, UnbatchRaggedZerothDim<VALUE_TYPE, SPLIT_TYPE>(
batched_ragged_input, &unbatched_ragged_input));
// Bundle the encoded scalar Variant Tensors into a rank-1 Variant Tensor.
Tensor* encoded_vector;
// output_size will be used for calling TensorShape(int64_t ...). We
// cannot use `auto` type here, or there will be a narrowing error.
int64_t output_size = unbatched_ragged_input.size();
OP_REQUIRES_OK(context,
context->allocate_output(0, TensorShape({output_size}),
&encoded_vector));
auto encoded_vector_t = encoded_vector->vec<Variant>();
for (auto i = decltype(output_size){}; i < output_size; i++) {
encoded_vector_t(i) = unbatched_ragged_input[i];
}
}
private:
bool batched_input_;
};
template <typename VALUE_TYPE, typename SPLIT_TYPE>
class RaggedTensorToVariantGradientOp : public OpKernel {
public:
using OpKernel::OpKernel;
void Compute(OpKernelContext* context) override {
// Read inputs.
Tensor encoded_variant = context->input(0);
Tensor row_splits = context->input(1);
auto flat_row_splits = row_splits.flat<SPLIT_TYPE>();
TensorShape dense_values_shape;
OP_REQUIRES_OK(context,
TensorShapeUtils::MakeShape(context->input(2).vec<int32>(),
&dense_values_shape));
const auto& flat_variants = encoded_variant.flat<Variant>();
// Get a Tensor containing the flat_values for each variant.
std::vector<Tensor> values;
for (int i = 0; i < flat_variants.size(); ++i) {
if (const auto* encoded = flat_variants(i).get<RaggedTensorVariant>()) {
values.push_back(encoded->values());
} else {
// Missing value: this happens if only some of the variant values
// generated by ragged_tensor_to_variant impacted the value that we're
// calculating the gradient for. In this case, we will see a
// default-constructed variant; so treat it as a zero tensor with the
// appropriate shape.
const auto value_dtype = DataTypeToEnum<VALUE_TYPE>::v();
auto piece_size = flat_row_splits(i + 1) - flat_row_splits(i);
TensorShape zeros_shape = dense_values_shape;
zeros_shape.set_dim(0, piece_size);
Tensor zero(value_dtype, zeros_shape);
zero.flat<VALUE_TYPE>().setZero();
values.push_back(zero);
}
}
if (values.size() == 1) {
// Just one flat_value tensor: return as-is.
context->set_output(0, values[0]);
} else {
Tensor* out = nullptr;
OP_REQUIRES_OK(context,
context->allocate_output(0, dense_values_shape, &out));
// ConcatCPU assumes non-empty output.
if (dense_values_shape.num_elements() == 0) return;
// Multiple flat_values tensors: concatenate them together.
using Piece = typename TTypes<VALUE_TYPE, 2>::Matrix;
using ConstPiece = typename TTypes<VALUE_TYPE, 2>::ConstMatrix;
std::vector<std::unique_ptr<ConstPiece>> pieces;
pieces.reserve(values.size());
for (const Tensor& t : values) {
// ConcatCPU assumes non-empty inputs.
if (t.NumElements() == 0) continue;
pieces.emplace_back(
new ConstPiece(t.shaped<VALUE_TYPE, 2>({1, t.NumElements()})));
}
Piece out_flat =
out->shaped<VALUE_TYPE, 2>({1, dense_values_shape.num_elements()});
ConcatCPU<VALUE_TYPE>(context->device(), pieces, &out_flat);
}
}
};
#define REGISTER_KERNELS_WITH_SPLIT_TYPE(value_type, split_type) \
REGISTER_KERNEL_BUILDER(Name("RaggedTensorToVariant") \
.Device(DEVICE_CPU) \
.TypeConstraint<value_type>("Tvalues") \
.TypeConstraint<split_type>("Tsplits"), \
RaggedTensorToVariantOp<value_type, split_type>); \
REGISTER_KERNEL_BUILDER( \
Name("RaggedTensorToVariantGradient") \
.Device(DEVICE_CPU) \
.TypeConstraint<value_type>("Tvalues") \
.TypeConstraint<split_type>("Tsplits"), \
RaggedTensorToVariantGradientOp<value_type, split_type>);
#define REGISTER_KERNELS(value_type) \
REGISTER_KERNELS_WITH_SPLIT_TYPE(value_type, int32) \
REGISTER_KERNELS_WITH_SPLIT_TYPE(value_type, int64_t)
TF_CALL_POD_TYPES(REGISTER_KERNELS);
TF_CALL_tstring(REGISTER_KERNELS);
TF_CALL_QUANTIZED_TYPES(REGISTER_KERNELS);
TF_CALL_quint16(REGISTER_KERNELS);
TF_CALL_qint16(REGISTER_KERNELS);
#undef REGISTER_KERNELS
#undef REGISTER_KERNELS_WITH_SPLIT_TYPE
} // namespace tensorflow