Skip to content

Commit d9f4735

Browse files
authored
Merge pull request karpathy#10 from LaihoE/master
batch file write
2 parents b288f4c + 0a2ea95 commit d9f4735

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
@@ -54,12 +54,16 @@ def process(example):
5454
filename = os.path.join(os.path.dirname(__file__), f'{split}.bin')
5555
dtype = np.uint16 # (can do since enc.max_token_value == 50256 is < 2**16)
5656
arr = np.memmap(filename, dtype=dtype, mode='w+', shape=(arr_len,))
57+
total_batches = 1024
5758

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

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

0 commit comments

Comments
 (0)