()
| 141 | return args |
| 142 | |
| 143 | def main(): |
| 144 | args = get_args() |
| 145 | startup_start = time.time() |
| 146 | |
| 147 | print("Opening", args.input) |
| 148 | fin = open(args.input, 'r', encoding='utf-8') |
| 149 | |
| 150 | if nltk_available and args.split_sentences: |
| 151 | nltk.download("punkt", quiet=True) |
| 152 | |
| 153 | encoder = Encoder(args) |
| 154 | tokenizer = build_tokenizer(args) |
| 155 | pool = multiprocessing.Pool(args.workers, initializer=encoder.initializer) |
| 156 | encoded_docs = pool.imap(encoder.encode, fin, 25) |
| 157 | #encoded_docs = map(encoder.encode, fin) |
| 158 | |
| 159 | level = "document" |
| 160 | if args.split_sentences: |
| 161 | level = "sentence" |
| 162 | |
| 163 | print(f"Vocab size: {tokenizer.vocab_size}") |
| 164 | print(f"Output prefix: {args.output_prefix}") |
| 165 | output_bin_files = {} |
| 166 | output_idx_files = {} |
| 167 | builders = {} |
| 168 | for key in args.json_keys: |
| 169 | output_bin_files[key] = "{}_{}_{}.bin".format(args.output_prefix, |
| 170 | key, level) |
| 171 | output_idx_files[key] = "{}_{}_{}.idx".format(args.output_prefix, |
| 172 | key, level) |
| 173 | builders[key] = indexed_dataset.make_builder(output_bin_files[key], |
| 174 | impl=args.dataset_impl, |
| 175 | vocab_size=tokenizer.vocab_size) |
| 176 | |
| 177 | startup_end = time.time() |
| 178 | proc_start = time.time() |
| 179 | total_bytes_processed = 0 |
| 180 | print("Time to startup:", startup_end - startup_start) |
| 181 | |
| 182 | for i, (doc, bytes_processed) in enumerate(encoded_docs, start=1): |
| 183 | total_bytes_processed += bytes_processed |
| 184 | for key, sentences in doc.items(): |
| 185 | for sentence in sentences: |
| 186 | builders[key].add_item(torch.IntTensor(sentence)) |
| 187 | builders[key].end_document() |
| 188 | if i % args.log_interval == 0: |
| 189 | current = time.time() |
| 190 | elapsed = current - proc_start |
| 191 | mbs = total_bytes_processed/elapsed/1024/1024 |
| 192 | print(f"Processed {i} documents", |
| 193 | f"({i/elapsed} docs/s, {mbs} MB/s).", |
| 194 | file=sys.stderr) |
| 195 | |
| 196 | for key in args.json_keys: |
| 197 | builders[key].finalize(output_idx_files[key]) |
| 198 | |
| 199 | if __name__ == '__main__': |
| 200 | main() |
no test coverage detected