-
Notifications
You must be signed in to change notification settings - Fork 74k
/
ctc_loss_op.cc
384 lines (324 loc) · 16.3 KB
/
ctc_loss_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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* Copyright 2016 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.
==============================================================================*/
// See docs in ../ops/ctc_ops.cc.
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
#define EIGEN_USE_GPU
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
#include <utility>
#include "tensorflow/core/framework/bounds_check.h"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/macros.h"
#include "tensorflow/core/util/ctc/ctc_loss_calculator.h"
#include "tensorflow/core/util/sparse/sparse_tensor.h"
#if GOOGLE_CUDA
#include "third_party/gpus/cudnn/cudnn.h"
#endif // GOOGLE_CUDA
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
#include "tensorflow/core/kernels/conv_ops_gpu.h"
#include "tensorflow/core/kernels/numeric_options_utils.h"
#include "tensorflow/core/util/stream_executor_util.h"
#include "tensorflow/core/util/tensor_format.h"
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
namespace tensorflow {
typedef Eigen::ThreadPoolDevice CPUDevice;
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
using GPUDevice = Eigen::GpuDevice;
namespace {
using se::Stream;
using se::StreamExecutor;
using se::dnn::RnnStateTensorDescriptor;
using se::dnn::ToDataType;
template <typename T>
void DoHistogram(OpKernelContext* ctx, const Tensor* labels_indices,
int num_indices, int batch_size,
std::vector<int>* labels_lengths) {
const T* h_in = labels_indices->flat<T>().data();
for (int i = 0; i < num_indices; i++) {
const T& key = h_in[i * 2];
(*labels_lengths)[key]++;
}
}
} // end namespace
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
template <typename T>
class CTCLossOp : public OpKernel {
typedef Eigen::Map<
const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> >
InputMap;
typedef Eigen::Map<
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> >
OutputMap;
public:
explicit CTCLossOp(OpKernelConstruction* ctx) : OpKernel(ctx) {
OP_REQUIRES_OK(ctx, ctx->GetAttr("preprocess_collapse_repeated",
&preprocess_collapse_repeated_));
OP_REQUIRES_OK(ctx,
ctx->GetAttr("ctc_merge_repeated", &ctc_merge_repeated_));
OP_REQUIRES_OK(ctx, ctx->GetAttr("ignore_longer_outputs_than_inputs",
&ignore_longer_outputs_than_inputs_));
}
void Compute(OpKernelContext* ctx) override {
const Tensor* inputs;
const Tensor* labels_indices;
const Tensor* labels_values;
const Tensor* seq_len;
OP_REQUIRES_OK(ctx, ctx->input("inputs", &inputs));
OP_REQUIRES_OK(ctx, ctx->input("labels_indices", &labels_indices));
OP_REQUIRES_OK(ctx, ctx->input("labels_values", &labels_values));
OP_REQUIRES_OK(ctx, ctx->input("sequence_length", &seq_len));
OP_REQUIRES(ctx, inputs->shape().dims() == 3,
errors::InvalidArgument("inputs is not a 3-Tensor"));
OP_REQUIRES(ctx, TensorShapeUtils::IsVector(seq_len->shape()),
errors::InvalidArgument("sequence_length is not a vector"));
OP_REQUIRES(ctx, TensorShapeUtils::IsMatrix(labels_indices->shape()),
errors::InvalidArgument("labels_indices is not a matrix"));
OP_REQUIRES(ctx, labels_indices->dim_size(1) > 1,
errors::InvalidArgument(
"labels_indices second dimension must be >= 1. Received ",
labels_indices->dim_size(1)));
OP_REQUIRES(ctx, TensorShapeUtils::IsVector(labels_values->shape()),
errors::InvalidArgument("labels_values is not a vector"));
const TensorShape& inputs_shape = inputs->shape();
const int64_t max_time = inputs_shape.dim_size(0);
OP_REQUIRES(ctx, max_time != 0,
errors::InvalidArgument(
"Max time or first dimension of input cannot be 0."));
const int64_t batch_size = inputs_shape.dim_size(1);
const int64_t num_classes_raw = inputs_shape.dim_size(2);
OP_REQUIRES(
ctx, FastBoundsCheck(num_classes_raw, std::numeric_limits<int>::max()),
errors::InvalidArgument("num_classes cannot exceed max int"));
const int num_classes = static_cast<const int>(num_classes_raw);
OP_REQUIRES(
ctx, batch_size == seq_len->dim_size(0),
errors::InvalidArgument("len(sequence_length) != batch_size. ",
"len(sequence_length): ", seq_len->dim_size(0),
" batch_size: ", batch_size));
auto seq_len_t = seq_len->vec<int32>();
OP_REQUIRES(ctx, labels_indices->dim_size(0) == labels_values->dim_size(0),
errors::InvalidArgument(
"labels_indices and labels_values must contain the "
"same number of rows, but saw shapes: ",
labels_indices->shape().DebugString(), " vs. ",
labels_values->shape().DebugString()));
OP_REQUIRES(ctx, batch_size != 0,
errors::InvalidArgument("batch_size must not be 0"));
// Figure out the maximum label length to use as sparse tensor dimension.
auto labels_indices_t = labels_indices->matrix<int64_t>();
int64_t max_label_len = 0;
for (int i = 0; i < labels_indices->dim_size(0); i++) {
max_label_len = std::max(max_label_len, labels_indices_t(i, 1) + 1);
}
TensorShape labels_shape({batch_size, max_label_len});
std::vector<int64_t> order{0, 1};
sparse::SparseTensor labels_sp;
OP_REQUIRES_OK(
ctx, sparse::SparseTensor::Create(*labels_indices, *labels_values,
labels_shape, order, &labels_sp));
Status labels_sp_valid = labels_sp.IndicesValid();
OP_REQUIRES(ctx, labels_sp_valid.ok(),
errors::InvalidArgument("label SparseTensor is not valid: ",
labels_sp_valid.message()));
typename ctc::CTCLossCalculator<T>::LabelSequences labels_t(batch_size);
for (const auto& g : labels_sp.group({0})) { // iterate by batch
const int64_t batch_indices = g.group()[0];
OP_REQUIRES(ctx, FastBoundsCheck(batch_indices, batch_size),
errors::InvalidArgument("labels batch index must be between ",
0, " and ", batch_size,
" but saw: ", batch_indices));
auto values = g.values<int32>();
std::vector<int>* b_values = &labels_t[batch_indices];
b_values->resize(values.size());
for (int i = 0; i < values.size(); ++i) (*b_values)[i] = values(i);
}
OP_REQUIRES(ctx, static_cast<size_t>(batch_size) == labels_t.size(),
errors::InvalidArgument("len(labels) != batch_size. ",
"len(labels): ", labels_t.size(),
" batch_size: ", batch_size));
for (int64_t b = 0; b < batch_size; ++b) {
OP_REQUIRES(
ctx, seq_len_t(b) <= max_time,
errors::InvalidArgument("sequence_length(", b, ") <= ", max_time));
}
Tensor* loss = nullptr;
OP_REQUIRES_OK(ctx, ctx->allocate_output("loss", seq_len->shape(), &loss));
auto loss_t = loss->vec<T>();
Tensor* gradient;
OP_REQUIRES_OK(ctx,
ctx->allocate_output("gradient", inputs_shape, &gradient));
auto gradient_t = gradient->tensor<T, 3>();
auto inputs_t = inputs->tensor<T, 3>();
std::vector<OutputMap> gradient_list_t;
std::vector<InputMap> input_list_t;
for (std::size_t t = 0; t < max_time; ++t) {
input_list_t.emplace_back(inputs_t.data() + t * batch_size * num_classes,
batch_size, num_classes);
gradient_list_t.emplace_back(
gradient_t.data() + t * batch_size * num_classes, batch_size,
num_classes);
}
gradient_t.setZero();
// Assumption: the blank index is num_classes - 1
ctc::CTCLossCalculator<T> ctc_loss_calculator(num_classes - 1, 0);
DeviceBase::CpuWorkerThreads workers =
*ctx->device()->tensorflow_cpu_worker_threads();
OP_REQUIRES_OK(ctx, ctc_loss_calculator.CalculateLoss(
seq_len_t, labels_t, input_list_t,
preprocess_collapse_repeated_, ctc_merge_repeated_,
ignore_longer_outputs_than_inputs_, &loss_t,
&gradient_list_t, &workers));
}
private:
bool preprocess_collapse_repeated_;
bool ctc_merge_repeated_;
bool ignore_longer_outputs_than_inputs_;
TF_DISALLOW_COPY_AND_ASSIGN(CTCLossOp<T>);
};
#define REGISTER_CPU(T) \
REGISTER_KERNEL_BUILDER( \
Name("CTCLoss").Device(DEVICE_CPU).TypeConstraint<T>("T"), \
CTCLossOp<T>);
REGISTER_CPU(float);
REGISTER_CPU(double);
#undef REGISTER_CPU
#if ((GOOGLE_CUDA && CUDNN_VERSION >= 7603) || TENSORFLOW_USE_ROCM)
class CTCLossOpGPU : public OpKernel {
public:
explicit CTCLossOpGPU(OpKernelConstruction* ctx) : OpKernel(ctx) {
bool preprocess_collapse_repeated;
bool ctc_merge_repeated;
bool ignore_longer_outputs_than_inputs;
OP_REQUIRES_OK(ctx, ctx->GetAttr("preprocess_collapse_repeated",
&preprocess_collapse_repeated));
OP_REQUIRES_OK(ctx,
ctx->GetAttr("ctc_merge_repeated", &ctc_merge_repeated));
OP_REQUIRES_OK(ctx, ctx->GetAttr("ignore_longer_outputs_than_inputs",
&ignore_longer_outputs_than_inputs));
OP_REQUIRES(ctx, !preprocess_collapse_repeated,
errors::InvalidArgument("GPU CTCLossOp requires "
"preprocess_collapse_repeated to be "
"false"));
OP_REQUIRES(ctx, ctc_merge_repeated,
errors::InvalidArgument("GPU CTCLossOp requires "
"ctc_merge_repeated to be "
"true"));
OP_REQUIRES(ctx, !ignore_longer_outputs_than_inputs,
errors::InvalidArgument("GPU CTCLossOp requires "
"ignore_longer_outputs_than_inputs to"
"be false"));
}
void Compute(OpKernelContext* ctx) override {
const Tensor* inputs;
const Tensor* labels_indices;
const Tensor* labels_values;
const Tensor* seq_len;
OP_REQUIRES_OK(ctx, ctx->input("inputs", &inputs));
OP_REQUIRES_OK(ctx, ctx->input("labels_indices", &labels_indices));
OP_REQUIRES_OK(ctx, ctx->input("labels_values", &labels_values));
OP_REQUIRES_OK(ctx, ctx->input("sequence_length", &seq_len));
OP_REQUIRES(ctx, inputs->shape().dims() == 3,
errors::InvalidArgument("inputs is not a 3-Tensor"));
OP_REQUIRES(ctx, TensorShapeUtils::IsVector(seq_len->shape()),
errors::InvalidArgument("sequence_length is not a vector"));
OP_REQUIRES(ctx, TensorShapeUtils::IsMatrix(labels_indices->shape()),
errors::InvalidArgument("labels_indices is not a matrix"));
OP_REQUIRES(ctx, TensorShapeUtils::IsVector(labels_values->shape()),
errors::InvalidArgument("labels_values is not a vector"));
const TensorShape& inputs_shape = inputs->shape();
const int64_t max_time_raw = inputs_shape.dim_size(0);
const int64_t batch_size_raw = inputs_shape.dim_size(1);
const int64_t num_classes_raw = inputs_shape.dim_size(2);
OP_REQUIRES(ctx,
FastBoundsCheck(max_time_raw, std::numeric_limits<int>::max()),
errors::InvalidArgument("max_time_ cannot exceed max int"));
OP_REQUIRES(
ctx, FastBoundsCheck(batch_size_raw, std::numeric_limits<int>::max()),
errors::InvalidArgument("batch_size cannot exceed max int"));
OP_REQUIRES(
ctx, FastBoundsCheck(num_classes_raw, std::numeric_limits<int>::max()),
errors::InvalidArgument("num_classes cannot exceed max int"));
const int max_time = static_cast<const int>(max_time_raw);
const int batch_size = static_cast<const int>(batch_size_raw);
const int num_classes = static_cast<const int>(num_classes_raw);
OP_REQUIRES(
ctx, batch_size == seq_len->dim_size(0),
errors::InvalidArgument("len(sequence_length) != batch_size. ",
"len(sequence_length): ", seq_len->dim_size(0),
" batch_size: ", batch_size));
OP_REQUIRES(ctx, labels_indices->dim_size(0) == labels_values->dim_size(0),
errors::InvalidArgument(
"labels_indices and labels_values must contain the "
"same number of rows, but saw shapes: ",
labels_indices->shape().DebugString(), " vs. ",
labels_values->shape().DebugString()));
auto num_indices = labels_indices->dim_size(0);
OP_REQUIRES(ctx, batch_size != 0,
errors::InvalidArgument("batch_size must not be 0"));
Tensor* loss = nullptr;
OP_REQUIRES_OK(ctx, ctx->allocate_output("loss", seq_len->shape(), &loss));
Tensor* gradient = nullptr;
OP_REQUIRES_OK(ctx,
ctx->allocate_output("gradient", inputs_shape, &gradient));
// Convert the labels_indices to labels_lengths.
std::vector<int> labels_lengths(batch_size, 0);
DoHistogram<int64_t>(ctx, labels_indices, num_indices, batch_size,
&labels_lengths);
StreamExecutor* executor = ctx->op_device_context()->stream()->parent();
se::dnn::DataType data_type = ToDataType<float>::value;
auto probs_desc_s = executor->createRnnStateTensorDescriptor(
max_time, batch_size, num_classes, data_type);
OP_REQUIRES_OK(ctx, probs_desc_s.status());
std::unique_ptr<RnnStateTensorDescriptor> probs_desc =
std::move(probs_desc_s).value();
auto grads_desc_s = executor->createRnnStateTensorDescriptor(
max_time, batch_size, num_classes, data_type);
OP_REQUIRES_OK(ctx, grads_desc_s.status());
std::unique_ptr<RnnStateTensorDescriptor> grads_desc =
std::move(grads_desc_s).value();
absl::Span<const int32> labels_data(labels_values->flat<int32>().data(),
num_indices);
absl::Span<const int32> labels_lengths_data(labels_lengths.data(),
batch_size);
absl::Span<const int32> input_lengths_data(seq_len->flat<int32>().data(),
batch_size);
auto probs_data = StreamExecutorUtil::AsDeviceMemory<float>(*inputs);
auto costs_data = StreamExecutorUtil::AsDeviceMemory<float>(*loss);
auto grads_data = StreamExecutorUtil::AsDeviceMemory<float>(*gradient);
// Set the memory limitation to 4GB for workspace memory.
DnnScratchAllocator workspace_allocator(1LL << 32, ctx);
Stream* stream = ctx->op_device_context()->stream();
bool cudnn_launch_status =
stream
->ThenCtcLoss(*probs_desc, probs_data, labels_data,
labels_lengths_data, input_lengths_data,
GetNumericOptions(), &costs_data, *grads_desc,
&grads_data, &workspace_allocator)
.ok();
if (!cudnn_launch_status) {
ctx->SetStatus(errors::Internal("cuDNN CTCLoss launch failure"));
}
}
private:
TF_DISALLOW_COPY_AND_ASSIGN(CTCLossOpGPU);
};
REGISTER_KERNEL_BUILDER(Name("CTCLossV2")
.Device(DEVICE_GPU)
.HostMemory("labels_indices")
.HostMemory("labels_values")
.HostMemory("sequence_length"),
CTCLossOpGPU);
#endif // ((GOOGLE_CUDA && CUDNN_VERSION >= 7603) || TENSORFLOW_USE_ROCM)
} // end namespace tensorflow