JavRedstone's picture
download
raw
2.98 kB
import os
from tqdm import tqdm
import numpy as np
import tiktoken
from datasets import load_dataset # huggingface datasets
# number of workers in .map() call
# good number to use is ~order number of cpu cores // 2
num_proc = 8
# number of workers in load_dataset() call
# best number might be different from num_proc above as it also depends on NW speed.
# it is better than 1 usually though
num_proc_load_dataset = num_proc
enc = tiktoken.get_encoding("gpt2")
if __name__ == '__main__':
# takes ~740MB total disk space, about 100M tokens from Wikipedia
dataset = load_dataset("Salesforce/wikitext", "wikitext-103-v1", num_proc=num_proc_load_dataset)
# wikitext-103-v1 by default contains 'train', 'validation' and 'test' splits
# this results in:
# >>> dataset
# DatasetDict({
# train: Dataset({
# features: ['text'],
# num_rows: 1801350
# })
# validation: Dataset({
# features: ['text'],
# num_rows: 3760
# })
# test: Dataset({
# features: ['text'],
# num_rows: 4358
# })
# we now want to tokenize the dataset. first define the encoding function (gpt2 bpe)
def process(example):
ids = enc.encode_ordinary(example['text']) # encode_ordinary ignores any special tokens
ids.append(enc.eot_token) # add the end of text token, e.g. 50256 for gpt2 bpe
# note: I think eot should be prepended not appended... hmm. it's called "eot" though...
out = {'ids': ids, 'len': len(ids)}
return out
# tokenize the dataset
tokenized = dataset.map(
process,
remove_columns=['text'],
desc="tokenizing the splits",
num_proc=num_proc,
)
# concatenate all the ids in each dataset into one large file we can use for training
for split, dset in tokenized.items():
arr_len = np.sum(dset['len'], dtype=np.uint64)
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
idx = 0
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()
# TODO: update these dataset stats!
# train.bin is ~240MB, validation.bin ~500kB, test.bin ~575kB
# train has ~100M tokens
# validation has ~0.4M tokens
# test has ~0.4M tokens
# to read the bin files later, e.g. with numpy:
# m = np.memmap('train.bin', dtype=np.uint16, mode='r')

Xet Storage Details

Size:
2.98 kB
·
Xet hash:
e7106a2bea032c34444972f08be8bbb9a6c761af9f5c56941c7da9d2b49632e5

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.