andrew-healey commited on
Commit
9c7f0a4
·
verified ·
1 Parent(s): 728a96e

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. prepare.py +81 -0
  2. readme.md +15 -0
  3. train.bin +3 -0
  4. val.bin +3 -0
prepare.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # saves the openwebtext dataset to a binary file for training. following was helpful:
2
+ # https://github.com/HazyResearch/flash-attention/blob/main/training/src/datamodules/language_modeling_hf.py
3
+
4
+ import os
5
+ from tqdm import tqdm
6
+ import numpy as np
7
+ import tiktoken
8
+ from datasets import load_dataset # huggingface datasets
9
+
10
+ # number of workers in .map() call
11
+ # good number to use is ~order number of cpu cores // 2
12
+ num_proc = 8
13
+
14
+ # number of workers in load_dataset() call
15
+ # best number might be different from num_proc above as it also depends on NW speed.
16
+ # it is better than 1 usually though
17
+ num_proc_load_dataset = num_proc
18
+
19
+ enc = tiktoken.get_encoding("gpt2")
20
+
21
+ if __name__ == '__main__':
22
+ # takes 54GB in huggingface .cache dir, about 8M documents (8,013,769)
23
+ dataset = load_dataset("openwebtext", num_proc=num_proc_load_dataset)
24
+
25
+ # owt by default only contains the 'train' split, so create a test split
26
+ split_dataset = dataset["train"].train_test_split(test_size=0.0005, seed=2357, shuffle=True)
27
+ split_dataset['val'] = split_dataset.pop('test') # rename the test split to val
28
+
29
+ # this results in:
30
+ # >>> split_dataset
31
+ # DatasetDict({
32
+ # train: Dataset({
33
+ # features: ['text'],
34
+ # num_rows: 8009762
35
+ # })
36
+ # val: Dataset({
37
+ # features: ['text'],
38
+ # num_rows: 4007
39
+ # })
40
+ # })
41
+
42
+ # we now want to tokenize the dataset. first define the encoding function (gpt2 bpe)
43
+ def process(example):
44
+ ids = enc.encode_ordinary(example['text']) # encode_ordinary ignores any special tokens
45
+ ids.append(enc.eot_token) # add the end of text token, e.g. 50256 for gpt2 bpe
46
+ # note: I think eot should be prepended not appended... hmm. it's called "eot" though...
47
+ out = {'ids': ids, 'len': len(ids)}
48
+ return out
49
+
50
+ # tokenize the dataset
51
+ tokenized = split_dataset.map(
52
+ process,
53
+ remove_columns=['text'],
54
+ desc="tokenizing the splits",
55
+ num_proc=num_proc,
56
+ )
57
+
58
+ # concatenate all the ids in each dataset into one large file we can use for training
59
+ for split, dset in tokenized.items():
60
+ arr_len = np.sum(dset['len'], dtype=np.uint64)
61
+ filename = os.path.join(os.path.dirname(__file__), f'{split}.bin')
62
+ dtype = np.uint16 # (can do since enc.max_token_value == 50256 is < 2**16)
63
+ arr = np.memmap(filename, dtype=dtype, mode='w+', shape=(arr_len,))
64
+ total_batches = 1024
65
+
66
+ idx = 0
67
+ for batch_idx in tqdm(range(total_batches), desc=f'writing {filename}'):
68
+ # Batch together samples for faster write
69
+ batch = dset.shard(num_shards=total_batches, index=batch_idx, contiguous=True).with_format('numpy')
70
+ arr_batch = np.concatenate(batch['ids'])
71
+ # Write into mmap
72
+ arr[idx : idx + len(arr_batch)] = arr_batch
73
+ idx += len(arr_batch)
74
+ arr.flush()
75
+
76
+ # train.bin is ~17GB, val.bin ~8.5MB
77
+ # train has ~9B tokens (9,035,582,198)
78
+ # val has ~4M tokens (4,434,897)
79
+
80
+ # to read the bin files later, e.g. with numpy:
81
+ # m = np.memmap('train.bin', dtype=np.uint16, mode='r')
readme.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ## openwebtext dataset
3
+
4
+ after running `prepare.py` (preprocess) we get:
5
+
6
+ - train.bin is ~17GB, val.bin ~8.5MB
7
+ - train has ~9B tokens (9,035,582,198)
8
+ - val has ~4M tokens (4,434,897)
9
+
10
+ this came from 8,013,769 documents in total.
11
+
12
+ references:
13
+
14
+ - OpenAI's WebText dataset is discussed in [GPT-2 paper](https://d4mucfpksywv.cloudfront.net/better-language-models/language_models_are_unsupervised_multitask_learners.pdf)
15
+ - [OpenWebText](https://skylion007.github.io/OpenWebTextCorpus/) dataset
train.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:68a352b5dbd52e85b7a8a1556c1ccae65ce14b02e4913788b0883b89ceddc1d1
3
+ size 18071164978
val.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:009ecdd6da34f1ae915ff1f0f3aeb0416b02f2252356505b32ce44166dd5cfcc
3
+ size 8869212