| 33 | } |
| 34 | |
| 35 | int beam_decode(at::Tensor th_probs, |
| 36 | at::Tensor th_seq_lens, |
| 37 | std::vector<std::string> new_vocab, |
| 38 | int vocab_size, |
| 39 | size_t beam_size, |
| 40 | size_t num_processes, |
| 41 | double cutoff_prob, |
| 42 | size_t cutoff_top_n, |
| 43 | size_t blank_id, |
| 44 | bool log_input, |
| 45 | void *scorer, |
| 46 | at::Tensor th_output, |
| 47 | at::Tensor th_timesteps, |
| 48 | at::Tensor th_scores, |
| 49 | at::Tensor th_out_length) |
| 50 | { |
| 51 | Scorer *ext_scorer = NULL; |
| 52 | if (scorer != NULL) { |
| 53 | ext_scorer = static_cast<Scorer *>(scorer); |
| 54 | } |
| 55 | const int64_t max_time = th_probs.size(1); |
| 56 | const int64_t batch_size = th_probs.size(0); |
| 57 | const int64_t num_classes = th_probs.size(2); |
| 58 | |
| 59 | std::vector<std::vector<std::vector<double>>> inputs; |
| 60 | auto prob_accessor = th_probs.accessor<float, 3>(); |
| 61 | auto seq_len_accessor = th_seq_lens.accessor<int, 1>(); |
| 62 | |
| 63 | for (int b=0; b < batch_size; ++b) { |
| 64 | // avoid a crash by ensuring that an erroneous seq_len doesn't have us try to access memory we shouldn't |
| 65 | int seq_len = std::min((int)seq_len_accessor[b], (int)max_time); |
| 66 | std::vector<std::vector<double>> temp (seq_len, std::vector<double>(num_classes)); |
| 67 | for (int t=0; t < seq_len; ++t) { |
| 68 | for (int n=0; n < num_classes; ++n) { |
| 69 | float val = prob_accessor[b][t][n]; |
| 70 | temp[t][n] = val; |
| 71 | } |
| 72 | } |
| 73 | inputs.push_back(temp); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | std::vector<std::vector<std::pair<double, Output>>> batch_results = |
| 78 | ctc_beam_search_decoder_batch(inputs, new_vocab, beam_size, num_processes, cutoff_prob, cutoff_top_n, blank_id, log_input, ext_scorer); |
| 79 | auto outputs_accessor = th_output.accessor<int, 3>(); |
| 80 | auto timesteps_accessor = th_timesteps.accessor<int, 3>(); |
| 81 | auto scores_accessor = th_scores.accessor<float, 2>(); |
| 82 | auto out_length_accessor = th_out_length.accessor<int, 2>(); |
| 83 | |
| 84 | |
| 85 | for (int b = 0; b < batch_results.size(); ++b){ |
| 86 | std::vector<std::pair<double, Output>> results = batch_results[b]; |
| 87 | for (int p = 0; p < results.size();++p){ |
| 88 | std::pair<double, Output> n_path_result = results[p]; |
| 89 | Output output = n_path_result.second; |
| 90 | std::vector<int> output_tokens = output.tokens; |
| 91 | std::vector<int> output_timesteps = output.timesteps; |
| 92 | for (int t = 0; t < output_tokens.size(); ++t){ |
no test coverage detected