| 25 | namespace fastertransformer { |
| 26 | |
| 27 | int read_start_ids(size_t batch_size, |
| 28 | std::vector<int>* v_start_lengths, |
| 29 | std::vector<int>* v_start_ids, |
| 30 | size_t& max_input_len, |
| 31 | const int end_id, |
| 32 | const int beam_width, |
| 33 | std::string file_name) |
| 34 | { |
| 35 | std::vector<std::vector<int>> tmp_start_ids; |
| 36 | std::vector<int> tmp_start_lengths; |
| 37 | |
| 38 | std::ifstream start_id_file(file_name, std::ios::in); |
| 39 | int line_num = 0; |
| 40 | if (start_id_file.is_open()) { |
| 41 | std::string line; |
| 42 | while (std::getline(start_id_file, line)) { |
| 43 | std::stringstream lineStream(line); |
| 44 | std::string vals; |
| 45 | int i1 = 0; |
| 46 | std::vector<int> tmp_vec; |
| 47 | while (std::getline(lineStream, vals, ',')) { |
| 48 | tmp_vec.push_back(std::stoi(vals)); |
| 49 | i1++; |
| 50 | } |
| 51 | tmp_start_ids.push_back(tmp_vec); |
| 52 | tmp_start_lengths.push_back(i1); |
| 53 | line_num++; |
| 54 | } |
| 55 | if (batch_size == 0) { |
| 56 | batch_size = line_num; |
| 57 | } |
| 58 | } |
| 59 | else { |
| 60 | printf("[WARNING] Cannot open the file '%s'. \n", file_name.c_str()); |
| 61 | max_input_len = 0; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | max_input_len = tmp_start_lengths.data()[0]; |
| 66 | for (uint i = 1; i < (uint)tmp_start_lengths.size(); i++) { |
| 67 | max_input_len = max_input_len > tmp_start_lengths.data()[i] ? max_input_len : tmp_start_lengths.data()[i]; |
| 68 | } |
| 69 | |
| 70 | while ((int)tmp_start_lengths.size() < batch_size) { |
| 71 | std::vector<int> padding_ids; |
| 72 | for (int i = 0; i < max_input_len; i++) { |
| 73 | padding_ids.push_back(end_id); |
| 74 | } |
| 75 | tmp_start_ids.push_back(padding_ids); |
| 76 | tmp_start_lengths.push_back(max_input_len); |
| 77 | } |
| 78 | |
| 79 | // Add padding |
| 80 | for (int i = 0; i < (int)tmp_start_ids.size(); i++) { |
| 81 | for (int j = (int)tmp_start_ids[i].size(); j < max_input_len; j++) { |
| 82 | tmp_start_ids[i].push_back(end_id); |
| 83 | } |
| 84 | } |
no test coverage detected