Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / cldnn_engine / cldnn_global_custom_kernels / ctc_greedy_decoder.cl
1 // Copyright (C) 2018-2019 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #pragma OPENCL EXTENSION cl_khr_fp16 : enable
16
17 __kernel void ctc_greedy_decoder(const __global INPUT0_TYPE*  probabilities,
18                                  const __global INPUT1_TYPE*  sequence_indicators,
19                                        __global OUTPUT0_TYPE* output_sequences)
20 {
21     const int dims = sizeof(INPUT0_DIMS) / sizeof(INPUT0_DIMS[0]);
22     int T_ = INPUT0_DIMS[0];
23     int N_ = INPUT0_DIMS[1];
24     int C_ = INPUT0_DIMS[2];
25
26     // Fill output_sequences with -1
27     for (int ii = 0; ii < T_*N_; ii++) {
28         output_sequences[ii] = (OUTPUT0_TYPE)(-1.0f);
29     }
30
31     for (int n = 0; n < N_; ++n) {
32         int prev_class_idx = -1;
33         int output_index = n*T_;
34
35         for (int t = 0; /* check at end */; ++t) {
36             // get maximum probability and its index
37             int max_class_idx = 0;
38
39             const __global INPUT0_TYPE* probs = probabilities + t*C_*N_ + n*C_;
40             INPUT0_TYPE max_prob = probs[0];
41             ++probs;
42
43             for (int c = 1; c < C_; ++c, ++probs) {
44                 if (*probs > max_prob) {
45                     max_class_idx = c;
46                     max_prob = *probs;
47                 }
48             }
49
50             if (max_class_idx != C_-1 && !(ctc_merge_repeated_ && max_class_idx == prev_class_idx)) {
51                 output_sequences[output_index] =  max_class_idx;
52                 output_index++;
53             }
54
55             prev_class_idx = max_class_idx;
56
57             if (t + 1 == T_ || sequence_indicators[(t + 1)*N_ + n] == 0) {
58                 break;
59             }
60         }
61     }
62 }