| 34 | |
| 35 | |
| 36 | py::array build_sample_idx(const py::array_t<int32_t>& sizes_, |
| 37 | const py::array_t<int32_t>& doc_idx_, |
| 38 | const int32_t seq_length, |
| 39 | const int32_t num_epochs, |
| 40 | const int64_t tokens_per_epoch) { |
| 41 | /* Sample index (sample_idx) is used for gpt2 like dataset for which |
| 42 | the documents are flattened and the samples are built based on this |
| 43 | 1-D flatten array. It is a 2D array with sizes [number-of-samples + 1, 2] |
| 44 | where [..., 0] contains the index into `doc_idx` and [..., 1] is the |
| 45 | starting offset in that document.*/ |
| 46 | |
| 47 | // Consistency checks. |
| 48 | assert(seq_length > 1); |
| 49 | assert(num_epochs > 0); |
| 50 | assert(tokens_per_epoch > 1); |
| 51 | |
| 52 | // Remove bound checks. |
| 53 | auto sizes = sizes_.unchecked<1>(); |
| 54 | auto doc_idx = doc_idx_.unchecked<1>(); |
| 55 | |
| 56 | // Mapping and it's length (1D). |
| 57 | int64_t num_samples = (num_epochs * tokens_per_epoch - 1) / seq_length; |
| 58 | int32_t* sample_idx = new int32_t[2*(num_samples+1)]; |
| 59 | |
| 60 | cout << " using:" << endl << std::flush; |
| 61 | cout << " number of documents: " << |
| 62 | doc_idx_.shape(0) / num_epochs << endl << std::flush; |
| 63 | cout << " number of epochs: " << num_epochs << |
| 64 | endl << std::flush; |
| 65 | cout << " sequence length: " << seq_length << |
| 66 | endl << std::flush; |
| 67 | cout << " total number of samples: " << num_samples << |
| 68 | endl << std::flush; |
| 69 | |
| 70 | // Index into sample_idx. |
| 71 | int64_t sample_index = 0; |
| 72 | // Index into doc_idx. |
| 73 | int64_t doc_idx_index = 0; |
| 74 | // Begining offset for each document. |
| 75 | int32_t doc_offset = 0; |
| 76 | // Start with first document and no offset. |
| 77 | sample_idx[2 * sample_index] = doc_idx_index; |
| 78 | sample_idx[2 * sample_index + 1] = doc_offset; |
| 79 | ++sample_index; |
| 80 | |
| 81 | while (sample_index <= num_samples) { |
| 82 | // Start with a fresh sequence. |
| 83 | int32_t remaining_seq_length = seq_length + 1; |
| 84 | while (remaining_seq_length != 0) { |
| 85 | // Get the document length. |
| 86 | auto doc_id = doc_idx[doc_idx_index]; |
| 87 | auto doc_length = sizes[doc_id] - doc_offset; |
| 88 | // And add it to the current sequence. |
| 89 | remaining_seq_length -= doc_length; |
| 90 | // If we have more than a full sequence, adjust offset and set |
| 91 | // remaining length to zero so we return from the while loop. |
| 92 | // Note that -1 here is for the same reason we have -1 in |
| 93 | // `_num_epochs` calculations. |
nothing calls this directly
no outgoing calls
no test coverage detected