-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathctc_decode_operation.cpp
More file actions
190 lines (168 loc) · 8.31 KB
/
ctc_decode_operation.cpp
File metadata and controls
190 lines (168 loc) · 8.31 KB
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
// Copyright (c) 2019 Shahrzad Shirzad
// Copyright (c) 2018-2019 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <phylanx/config.hpp>
#if defined(PHYLANX_HAVE_BLAZE_TENSOR)
#include <phylanx/ir/node_data.hpp>
#include <phylanx/plugins/keras_support/ctc_decode_operation.hpp>
#include <phylanx/util/matrix_iterators.hpp>
#include <hpx/include/lcos.hpp>
#include <hpx/include/naming.hpp>
#include <hpx/include/util.hpp>
#include <hpx/throw_exception.hpp>
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <blaze/Math.h>
#include <blaze_tensor/Math.h>
///////////////////////////////////////////////////////////////////////////////
namespace phylanx { namespace execution_tree { namespace primitives
{
///////////////////////////////////////////////////////////////////////////
match_pattern_type const ctc_decode_operation::match_data = {
hpx::util::make_tuple("ctc_decode",
std::vector<std::string>{
"ctc_decode(_1, _2, __arg(_3_greedy, 1), __arg(_4_beam_width, "
"100), __arg(_5_top_paths, 1))"},
&create_ctc_decode_operation,
&create_primitive<ctc_decode_operation>,
R"(y_pred, input_length, greedy, beam_width, top_paths
Args:
y_pred : The scalar, vector, matrix, or tensor to perform ctc_decode over
input_length
greedy : boolean, if True performs best-path search otherwise beam-search
beam_width : Integer, if greedy is False specifies the width of the beam.
top_paths : Integer, if greedy is False specifies the number of top paths
desired.
Returns:
Returns the result of Connectionist temporal classification applied to a
squence.)")};
///////////////////////////////////////////////////////////////////////////
ctc_decode_operation::ctc_decode_operation(
primitive_arguments_type&& operands, std::string const& name,
std::string const& codename)
: primitive_component_base(std::move(operands), name, codename)
{
}
///////////////////////////////////////////////////////////////////////////
hpx::future<primitive_argument_type> ctc_decode_operation::eval(
primitive_arguments_type const& operands,
primitive_arguments_type const& args,
eval_context ctx) const
{
if (operands.size() < 2 || operands.size() > 5)
{
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"ctc_decode_operation::eval",
generate_error_message("the ctc_decode_operation primitive "
"requires at least two and at "
"most five operands"));
}
if (!valid(operands[0]))
{
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"ctc_decode_operation::eval",
generate_error_message(
"the ctc_decode_operation primitive requires that the "
"argument given by the operands array is valid"));
}
auto this_ = this->shared_from_this();
return hpx::dataflow(hpx::launch::sync,
hpx::util::unwrapping(
[this_ = std::move(this_)](ir::node_data<double>&& arg1,
ir::node_data<std::int64_t>&& arg2, std::uint8_t greedy,
std::int64_t beam_width,
std::int64_t top_paths) -> primitive_argument_type {
if (arg1.num_dimensions() != 3)
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"ctc_decode_operation::eval",
this_->generate_error_message(
"y_pred should be a tensor"));
auto y_pred = arg1.tensor();
std::size_t num_samples = y_pred.pages();
std::size_t seq_length = y_pred.rows();
std::size_t num_classes = y_pred.columns();
if (arg2.num_dimensions() != 1)
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"ctc_decode_operation::eval",
this_->generate_error_message(
"input_length should be a vector"));
auto input_length = arg2.vector();
blaze::DynamicMatrix<double> log_prob(num_samples, 1, 0.);
blaze::DynamicMatrix<double> decoded_dense(
num_samples, seq_length, -1.);
blaze::DynamicVector<std::int64_t> decoded_length(
num_samples, 0.);
if (!greedy)
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"ctc_decode_operation::eval",
this_->generate_error_message(
"has not been implemented yet"));
using phylanx::util::matrix_row_iterator;
for (std::size_t i = 0; i < num_samples; ++i)
{
std::int64_t length = input_length[i];
auto prob = blaze::pageslice(y_pred, i);
auto tmp =
blaze::submatrix(prob, 0, 0, length, num_classes);
matrix_row_iterator<decltype(prob)> tmp_begin(prob);
matrix_row_iterator<decltype(prob)> tmp_end(
prob, length);
blaze::DynamicVector<double> decoded(length);
auto decoded_it = decoded.begin();
double sum = 0.;
for (auto it = tmp_begin; it != tmp_end;
++it, ++decoded_it)
{
auto local_max =
std::max_element(it->begin(), it->end());
sum += blaze::log(*local_max);
*decoded_it = std::distance(it->begin(), local_max);
}
log_prob(i, 0) = -sum;
std::size_t k = 0;
for (std::size_t j = 0; j < decoded.size() - 1; ++j)
{
if ((decoded[j] != decoded[j + 1]) &&
(decoded[j] < num_classes - 1))
decoded[k++] = decoded[j];
}
decoded[k++] = decoded[decoded.size() - 1];
decoded.resize(k);
decoded_length[i] = k;
auto decoded_row = blaze::row(decoded_dense, i);
auto decoded_row_length =
blaze::subvector(decoded_row, 0, k);
decoded_row_length = blaze::trans(decoded);
}
blaze::DynamicMatrix<double> decoded_dense_final =
blaze::submatrix(decoded_dense, 0, 0, num_samples,
(blaze::max)(decoded_length));
primitive_arguments_type result;
result.reserve(2);
primitive_arguments_type first;
first.reserve(1);
first.push_back(primitive_argument_type{
std::move(decoded_dense_final)});
result.push_back(primitive_argument_type{std::move(first)});
result.push_back(
primitive_argument_type{std::move(log_prob)});
return primitive_argument_type{std::move(result)};
}),
numeric_operand(operands[0], args, name_, codename_, ctx),
integer_operand_strict(operands[1], args, name_, codename_, ctx),
scalar_boolean_operand(operands[2], args, name_, codename_, ctx),
scalar_integer_operand_strict(
operands[3], args, name_, codename_, ctx),
scalar_integer_operand_strict(
operands[4], args, name_, codename_, ctx));
}
}}}
#endif