Skip to content

Commit 0a2ea95

Browse files
committed
batch file write
1 parent ea4de19 commit 0a2ea95

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

data/openwebtext/prepare.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ def process(example):
5353
filename = f'{split}.bin'
5454
dtype = np.uint16 # (can do since enc.max_token_value == 50256 is < 2**16)
5555
arr = np.memmap(filename, dtype=dtype, mode='w+', shape=(arr_len,))
56+
total_batches = 1024
5657

57-
print(f"writing {filename}...")
5858
idx = 0
59-
for example in tqdm(dset):
60-
arr[idx : idx + example['len']] = example['ids']
61-
idx += example['len']
59+
for batch_idx in tqdm(range(total_batches), desc=f'writing {filename}'):
60+
# Batch together samples for faster write
61+
batch = dset.shard(num_shards=total_batches, index=batch_idx, contiguous=True).with_format('numpy')
62+
arr_batch = np.concatenate(batch['ids'])
63+
# Write into mmap
64+
arr[idx : idx + len(arr_batch)] = arr_batch
65+
idx += len(arr_batch)
6266
arr.flush()
6367

6468
# train.bin is ~17GB, val.bin ~8.5MB

0 commit comments

Comments
 (0)