forked from Lightning-AI/litgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_redpajama.py
166 lines (131 loc) · 5.33 KB
/
prepare_redpajama.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import glob
import json
import os
import sys
from pathlib import Path
import numpy as np
from tqdm import tqdm
# support running without installing as a package
wd = Path(__file__).parent.parent.resolve()
sys.path.append(str(wd))
import lit_gpt.packed_dataset as packed_dataset
from lit_gpt import Config, Tokenizer
filenames_sample = [
"arxiv_sample.jsonl",
"book_sample.jsonl",
"c4_sample.jsonl",
"cc_2019-30_sample.jsonl",
"cc_2020-05_sample.jsonl",
"cc_2021-04_sample.jsonl",
"cc_2022-05_sample.jsonl",
"cc_2023-06_sample.jsonl",
"github_sample.jsonl",
"stackexchange_sample.jsonl",
"wikipedia_sample.jsonl",
]
filename_sets = {
"arxiv": "arxiv/arxiv*",
"book": "book/book*",
"c4": "c4/c4-train*",
"common_crawl": "common_crawl/*",
"github": "github/filtered*",
"stackexchange": "stackexchange/stackexchange*",
"wikipedia": "wikipedia/wiki*",
}
def prepare_sample(
source_path: Path, checkpoint_dir: Path, destination_path: Path, chunk_size: int, match: str = ""
) -> None:
"""Prepare the "Red Pajama" dataset using the original tokenizer."""
destination_path.mkdir(parents=True, exist_ok=True)
tokenizer = Tokenizer(checkpoint_dir)
for name in filenames_sample:
if match and match not in name:
continue
filepath = source_path / name
if not filepath.is_file():
raise RuntimeError(
f"Input file not found at {filepath}. \nMake sure you download the data, e.g. wget -i"
" https://data.together.xyz/redpajama-data-1T/v1.0.0/urls.txt or through"
" \nhttps://huggingface.co/datasets/togethercomputer/RedPajama-Data-1T"
" \nhttps://huggingface.co/datasets/togethercomputer/RedPajama-Data-1T-Sample \n"
)
prefix, _ = os.path.splitext(name)
builder = packed_dataset.PackedDatasetBuilder(
outdir=destination_path,
prefix=prefix,
chunk_size=chunk_size,
sep_token=tokenizer.eos_id,
dtype="auto",
vocab_size=tokenizer.vocab_size,
)
print(f"Processing {name}")
with open(filepath, encoding="utf-8") as f:
for row in tqdm(f):
text = json.loads(row)["text"]
text_ids = tokenizer.encode(text)
builder.add_array(np.array(text_ids, dtype=builder.dtype))
builder.write_reminder()
def prepare_full(
source_path: Path, checkpoint_dir: Path, destination_path: Path, chunk_size: int, match: str = ""
) -> None:
"""Prepare the "Red Pajama" dataset using the original tokenizer."""
import zstandard as zstd
destination_path.mkdir(parents=True, exist_ok=True)
tokenizer = Tokenizer(checkpoint_dir)
for set_name, pattern in filename_sets.items():
if match and match not in set_name:
continue
is_cc = set_name == "common_crawl"
filenames = glob.glob(os.path.join(source_path, pattern), recursive=True)
if not filenames:
raise RuntimeError(
f"No files matching {pattern} found at {source_path}. \nMake sure you download the data, e.g. wget -i"
" https://data.together.xyz/redpajama-data-1T/v1.0.0/urls.txt or through"
" \nhttps://huggingface.co/datasets/togethercomputer/RedPajama-Data-1T"
" \nhttps://huggingface.co/datasets/togethercomputer/RedPajama-Data-1T-Sample \n"
)
builder = packed_dataset.PackedDatasetBuilder(
outdir=destination_path,
prefix=set_name,
chunk_size=chunk_size,
sep_token=tokenizer.eos_id,
dtype="auto",
vocab_size=tokenizer.vocab_size,
)
for name in filenames:
filepath = source_path / name
print(f"Processing {name}")
if is_cc:
with zstd.open(open(filepath, "rb"), "rt", encoding="utf-8") as f:
for row in tqdm(f):
text = json.loads(row)["text"]
text_ids = tokenizer.encode(text)
builder.add_array(np.array(text_ids, dtype=builder.dtype))
else:
with open(filepath, encoding="utf-8") as f:
for row in tqdm(f):
text = json.loads(row)["text"]
text_ids = tokenizer.encode(text)
builder.add_array(np.array(text_ids, dtype=builder.dtype))
builder.write_reminder()
def prepare(
source_path: Path = Path("data/RedPajama-Data-1T-Sample"),
checkpoint_dir: Path = Path("checkpoints/stabilityai/stablelm-base-alpha-3b"),
destination_path: Path = Path("data/redpajama_sample"),
sample: bool = True,
match: str = "",
) -> None:
"""Prepare the "Red Pajama" dataset. We assume tokenizer has been trained."""
with open(checkpoint_dir / "lit_config.json") as fp:
config = Config(**json.load(fp))
prepare_fn = prepare_sample if sample else prepare_full
prepare_fn(
source_path=source_path,
checkpoint_dir=checkpoint_dir,
destination_path=destination_path,
chunk_size=(config.block_size + 1) * 1024, # block size + 1 for causal, 1024 blocks
match=match,
)
if __name__ == "__main__":
from jsonargparse import CLI
CLI(prepare)