| 136 | |
| 137 | template<typename DocIdx> |
| 138 | py::array build_mapping_impl(const py::array_t<int64_t>& docs_, |
| 139 | const py::array_t<int32_t>& sizes_, |
| 140 | const int32_t num_epochs, |
| 141 | const uint64_t max_num_samples, |
| 142 | const int32_t max_seq_length, |
| 143 | const double short_seq_prob, |
| 144 | const int32_t seed, |
| 145 | const bool verbose) { |
| 146 | /* Build a mapping of (start-index, end-index, sequence-length) where |
| 147 | start and end index are the indices of the sentences in the sample |
| 148 | and sequence-length is the target sequence length. |
| 149 | */ |
| 150 | |
| 151 | // Consistency checks. |
| 152 | assert(num_epochs > 0); |
| 153 | assert(max_seq_length > 1); |
| 154 | assert(short_seq_prob > 0.0); |
| 155 | assert(short_seq_prob <= 1.0); |
| 156 | assert(seed > 0); |
| 157 | |
| 158 | // Remove bound checks. |
| 159 | auto docs = docs_.unchecked<1>(); |
| 160 | auto sizes = sizes_.unchecked<1>(); |
| 161 | |
| 162 | // For efficiency, convert probability to ratio. Note: rand() generates int. |
| 163 | const auto short_seq_ratio = static_cast<int32_t>(round(1.0 / short_seq_prob)); |
| 164 | |
| 165 | if (verbose) { |
| 166 | const auto sent_start_index = docs[0]; |
| 167 | const auto sent_end_index = docs[docs_.shape(0) - 1]; |
| 168 | const auto num_sentences = sent_end_index - sent_start_index; |
| 169 | cout << " using:" << endl << std::flush; |
| 170 | cout << " number of documents: " << docs_.shape(0) - 1 << |
| 171 | endl << std::flush; |
| 172 | cout << " sentences range: [" << sent_start_index << |
| 173 | ", " << sent_end_index << ")" << endl << std::flush; |
| 174 | cout << " total number of sentences: " << num_sentences << |
| 175 | endl << std::flush; |
| 176 | cout << " number of epochs: " << num_epochs << |
| 177 | endl << std::flush; |
| 178 | cout << " maximum number of samples: " << max_num_samples << |
| 179 | endl << std::flush; |
| 180 | cout << " maximum sequence length: " << max_seq_length << |
| 181 | endl << std::flush; |
| 182 | cout << " short sequence probability: " << short_seq_prob << |
| 183 | endl << std::flush; |
| 184 | cout << " short sequence ration (1/prob): " << short_seq_ratio << |
| 185 | endl << std::flush; |
| 186 | cout << " seed: " << seed << endl << |
| 187 | std::flush; |
| 188 | } |
| 189 | |
| 190 | // Mapping and it's length (1D). |
| 191 | int64_t num_samples = -1; |
| 192 | DocIdx* maps = NULL; |
| 193 | |
| 194 | // Perform two iterations, in the first iteration get the size |
| 195 | // and allocate memory and in the second iteration populate the map. |
nothing calls this directly
no test coverage detected