Skip to content

Commit

Permalink
Merge pull request karpathy#10 from LaihoE/master
Browse files Browse the repository at this point in the history
batch file write
  • Loading branch information
karpathy authored Apr 13, 2023
2 parents b288f4c + 0a2ea95 commit d9f4735
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions data/openwebtext/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@ def process(example):
filename = os.path.join(os.path.dirname(__file__), f'{split}.bin')
dtype = np.uint16 # (can do since enc.max_token_value == 50256 is < 2**16)
arr = np.memmap(filename, dtype=dtype, mode='w+', shape=(arr_len,))
total_batches = 1024

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

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

0 comments on commit d9f4735

Please sign in to comment.