-
Notifications
You must be signed in to change notification settings - Fork 74k
/
ragged_range_op.cc
147 lines (131 loc) · 6.43 KB
/
ragged_range_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
/* 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 <cstdint>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/tsl/platform/errors.h"
namespace tensorflow {
using errors::InvalidArgument;
template <typename T, typename SPLITS_TYPE>
class RaggedRangeOp : public OpKernel {
public:
using OpKernel::OpKernel;
void Compute(OpKernelContext* context) override {
const Tensor& starts_in = context->input(0);
const Tensor& limits_in = context->input(1);
const Tensor& deltas_in = context->input(2);
// Check input tensor shapes.
OP_REQUIRES(context, starts_in.shape().dims() <= 1,
InvalidArgument("starts must be a scalar or vector"));
OP_REQUIRES(context, limits_in.shape().dims() <= 1,
InvalidArgument("limits must be a scalar or vector"));
OP_REQUIRES(context, deltas_in.shape().dims() <= 1,
InvalidArgument("deltas must be a scalar or vector"));
// Determine which tensors we need to broadcast.
bool broadcast_starts = starts_in.shape().dims() == 0;
bool broadcast_limits = limits_in.shape().dims() == 0;
bool broadcast_deltas = deltas_in.shape().dims() == 0;
// nrows (number of output rows) is the size of the non-broadcast inputs,
// or 1 if all inputs are scalars.
std::vector<int> in_sizes;
if (!broadcast_starts) in_sizes.push_back(starts_in.shape().dim_size(0));
if (!broadcast_limits) in_sizes.push_back(limits_in.shape().dim_size(0));
if (!broadcast_deltas) in_sizes.push_back(deltas_in.shape().dim_size(0));
for (int i = 1; i < in_sizes.size(); ++i) {
OP_REQUIRES(context, in_sizes[i] == in_sizes[i - 1],
InvalidArgument("starts, limits, and deltas must have the "
"same shape"));
}
SPLITS_TYPE nrows = in_sizes.empty() ? 1 : in_sizes[0];
const auto& starts = starts_in.flat<T>();
const auto& limits = limits_in.flat<T>();
const auto& deltas = deltas_in.flat<T>();
// Construct the rt_nested_splits tensor.
Tensor* rt_nested_splits_out = nullptr;
OP_REQUIRES_OK(context,
context->allocate_output(0, TensorShape({nrows + 1}),
&rt_nested_splits_out));
auto rt_nested_splits = rt_nested_splits_out->flat<SPLITS_TYPE>();
rt_nested_splits(0) = 0;
for (int row = 0; row < nrows; ++row) {
T start = broadcast_starts ? starts(0) : starts(row);
T limit = broadcast_limits ? limits(0) : limits(row);
T delta = broadcast_deltas ? deltas(0) : deltas(row);
OP_REQUIRES(context, delta != 0, InvalidArgument("Requires delta != 0"));
SPLITS_TYPE size; // The number of elements in the specified range.
if (((delta > 0) && (limit < start)) ||
((delta < 0) && (limit > start))) {
size = 0;
} else if (std::is_integral<T>::value) {
// The following is copied from tensorflow::RangeOp::Compute().
size = Eigen::divup(Eigen::numext::abs(limit - start),
Eigen::numext::abs(delta));
} else {
// The following is copied from tensorflow::RangeOp::Compute().
auto size_auto =
Eigen::numext::ceil(Eigen::numext::abs((limit - start) / delta));
OP_REQUIRES(
context, size_auto <= std::numeric_limits<int64_t>::max(),
errors::InvalidArgument("Requires ((limit - start) / delta) <= ",
std::numeric_limits<int64_t>::max()));
size = static_cast<SPLITS_TYPE>(size_auto);
}
OP_REQUIRES(context, size >= 0, InvalidArgument("Requires size >= 0"));
OP_REQUIRES(
context,
size <=
std::numeric_limits<SPLITS_TYPE>::max() - rt_nested_splits(row),
InvalidArgument("The total range size overflowed. Consider using "
"int64 instead of int32 for row_splits_dtype."));
rt_nested_splits(row + 1) = rt_nested_splits(row) + size;
}
SPLITS_TYPE nvals = rt_nested_splits(nrows);
// Construct the rt_dense_values tensor.
Tensor* rt_dense_values_out = nullptr;
OP_REQUIRES_OK(context, context->allocate_output(1, TensorShape({nvals}),
&rt_dense_values_out));
auto rt_dense_values = rt_dense_values_out->flat<T>();
int value_index = 0;
for (int row = 0; row < nrows; ++row) {
SPLITS_TYPE row_size = rt_nested_splits(row + 1) - rt_nested_splits(row);
T value = broadcast_starts ? starts(0) : starts(row);
T delta = broadcast_deltas ? deltas(0) : deltas(row);
for (SPLITS_TYPE i = 0; i < row_size; ++i) {
rt_dense_values(value_index++) = T(value);
value += delta;
}
}
}
};
#define REGISTER_CPU_KERNEL(TYPE) \
REGISTER_KERNEL_BUILDER(Name("RaggedRange") \
.Device(DEVICE_CPU) \
.TypeConstraint<TYPE>("T") \
.TypeConstraint<int32>("Tsplits"), \
RaggedRangeOp<TYPE, int32>); \
REGISTER_KERNEL_BUILDER(Name("RaggedRange") \
.Device(DEVICE_CPU) \
.TypeConstraint<TYPE>("T") \
.TypeConstraint<int64_t>("Tsplits"), \
RaggedRangeOp<TYPE, int64>);
TF_CALL_float(REGISTER_CPU_KERNEL);
TF_CALL_double(REGISTER_CPU_KERNEL);
TF_CALL_int32(REGISTER_CPU_KERNEL);
TF_CALL_int64(REGISTER_CPU_KERNEL);
#undef REGISTER_CPU_KERNEL
} // namespace tensorflow