-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdata.py
300 lines (241 loc) · 10.7 KB
/
data.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import os
import random
import pickle
import base64
import json
from multiprocessing import Pool
from tqdm import tqdm
from tokenizer import Tokenizer
BLOCK_SIZE = 512
PRETRAIN_DATASETS = [
"dataset/pretrain_psycho.txt"
]
SFT_DATASET = "dataset/sft-general.jsonl"
STAGE_FLAG = {
"pretrain": 1, # 1 - do ; else - pass
"sft": 0 # 1 - do ; else - pass
}
USE_MP = True # 多进程加速?
#############################################################
base_path = os.path.dirname(__file__)
os.makedirs(os.path.join(base_path, "dataset_preprocessed"), exist_ok=True)
TOKENIZER_PATH = "tokenizer/tokenizer_16384.json"
# tokenizer = build_from_files(PRETRAIN_DATASETS + [SFT_DATASET], os.path.join(base_path, TOKENIZER_PATH))
tokenizer = Tokenizer()
tokenizer.load_from_config_file(os.path.join(base_path, TOKENIZER_PATH))
def get_file_chunk_iterator(filepath, chunk_size=65536):
with open(filepath, mode="r", encoding="utf-8") as f:
while True:
chunk = f.read(chunk_size)
if not chunk:
return
yield chunk
def get_some_chunks(iter, n_chunks=1):
for _ in range(n_chunks):
try:
c = next(iter)
yield c
except StopIteration:
return
def get_file_line_iterator(filepath):
with open(filepath, mode="r", encoding="utf-8") as f:
while True:
line = f.readline()
if not line:
return
yield line
def get_some_lines(iter, lines=1):
for _ in range(lines):
try:
c = next(iter)
yield c
except StopIteration:
return
# 将数据集分为若干块,块内打乱,块间乱序拼接,以节约内存
def generate_pretrain_dataset(input_path, train_output_path, val_output_path):
print(f"Counting character num...")
cmd = f"powershell -Command (Get-Content {input_path} -Raw).Length" if os.name == "nt" else f"wc --chars {input_path}"
with os.popen(cmd) as f:
res = f.readlines()[0]
charcount = int(res.split(" ")[0])
print(f" Total char = {charcount}")
BLOCKS_PER_PART = 32768
BLOCKS_PER_CHUNK = 8
CHUNK_LENGTH = BLOCKS_PER_CHUNK * (BLOCK_SIZE+1)
CHUNKS_PER_PART = BLOCKS_PER_PART // BLOCKS_PER_CHUNK
CHARS_PER_PART = CHUNK_LENGTH * CHUNKS_PER_PART
TOTAL_PARTS = charcount // CHARS_PER_PART + 1
print(f"Total parts = {TOTAL_PARTS}")
train_temp_file_paths = []
val_temp_file_paths = []
text_iterator = get_file_chunk_iterator(input_path, chunk_size=CHUNK_LENGTH)
for part_index in range(TOTAL_PARTS):
print(f"Reading and encoding raw text file of Part {part_index+1}/{TOTAL_PARTS}...")
train_temp = os.path.join(base_path, f"dataset_preprocessed/train_temp_{part_index}.base64")
val_temp = os.path.join(base_path, f"dataset_preprocessed/val_temp_{part_index}.base64")
token_buffer = []
blocks = []
chunk_iterator = get_some_chunks(text_iterator, CHUNKS_PER_PART)
pool = Pool(os.cpu_count())
res = pool.imap(func=tokenizer.encode, iterable=chunk_iterator, chunksize=64)
for tokens in tqdm(res, total=CHUNKS_PER_PART):
token_buffer.extend(tokens)
pool.close()
pool.join()
print(f" Split tokens into blocks ...")
for offset in range(0, len(token_buffer), (BLOCK_SIZE+1)):
# TODO 在<|eos|>处切割chunk
# 每一条数据都比BLOCK_SIZE多一个,用于预测下一字符的训练
blk = token_buffer[offset : offset + (BLOCK_SIZE+1)]
# 如果长度不足(BLOCK_SIZE+1),则放弃
if len(blk) < (BLOCK_SIZE+1):
continue
blocks.append(blk)
del token_buffer
# 在part内部打乱,并写入临时文件
print(f" Shuffling text blocks and write to file...")
line_indexes = list(range(len(blocks)))
random.shuffle(line_indexes)
with open(train_temp, "w", encoding="utf-8") as f_train:
for li in range(len(blocks)):
train_block = pickle.dumps([blocks[line_indexes[li]], None])
f_train.writelines(str(base64.b64encode(train_block), encoding="utf-8") + "\n")
# f_train.writelines(f"Block {line_indexes[li]} : " + tokenizer.decode(blocks[line_indexes[li]]) + "\n")
with open(val_temp, "w", encoding="utf-8") as f_val:
for li in range(int(len(blocks) * 0.95), len(blocks)):
val_block = pickle.dumps([blocks[line_indexes[li]], None])
f_val.writelines(str(base64.b64encode(val_block), encoding="utf-8") + "\n")
train_temp_file_paths.append(train_temp)
val_temp_file_paths.append(val_temp)
# 在part之间打乱,写入统一文件,并删除临时文件
print(f"Shuffling all parts and write to file...")
part_indexes = list(range(len(train_temp_file_paths)))
random.shuffle(part_indexes)
with open(train_output_path, "w", encoding="utf-8") as f_train:
for pindex in part_indexes:
p = train_temp_file_paths[pindex]
print(f" Writing part {p}...")
with open(p, "r", encoding="utf-8") as tp:
lines = tp.read()
f_train.write(lines)
print(f" Delete temp file {p}...")
os.remove(p)
random.shuffle(part_indexes)
with open(val_output_path, "w", encoding="utf-8") as f_val:
for pindex in part_indexes:
p = val_temp_file_paths[pindex]
print(f" Writing part {p}...")
with open(p, "r", encoding="utf-8") as tp:
lines = tp.read()
f_val.write(lines)
print(f" Delete temp file {p}...")
os.remove(p)
print(f"Done.")
def apply_template_and_encode(line):
line = line.strip()
try:
qa = json.loads(line)
question = qa["question"]
answer = qa["answer"]
if len(question) + len(answer) + 3 > BLOCK_SIZE:
answer = answer[0 : BLOCK_SIZE - 3 - len(question)]
# print(f"超长QA对,裁剪:{answer}")
return False
template = f"<|instruct_mark|>{question}<|response_mark|>{answer}<|eos|>"
ids = tokenizer.encode(template)
if len(ids) > BLOCK_SIZE:
return False
ids = [ids[i] if i < len(ids) else tokenizer.special_tokens["<|padding|>"] for i in range(BLOCK_SIZE + 1)]
mask = [0] * (1 + len(question) + 1) + [1] * (len(answer) + 1)
mask = [mask[i] if i < len(mask) else 0 for i in range(BLOCK_SIZE + 1)]
return (ids, mask)
except:
print(line)
return False
def generate_sft_dataset(input_jsonl_path, train_output_path, val_output_path):
print(f"Counting character num...")
cmd = f"powershell -Command (Get-Content {input_jsonl_path}).Count" if os.name == "nt" else f"wc -l {input_jsonl_path}"
with os.popen(cmd) as f:
res = f.readlines()[0]
linecount = int(res.split(" ")[0])
print(f" Total lines = {linecount}")
LINES_PER_PART = 131072
TOTAL_PARTS = linecount // LINES_PER_PART + 1
print(f"Total parts = {TOTAL_PARTS}")
train_temp_file_paths = []
val_temp_file_paths = []
text_iterator = get_file_line_iterator(input_jsonl_path)
for part_index in range(TOTAL_PARTS):
print(f"Reading and encoding raw text file of Part {part_index+1}/{TOTAL_PARTS}...")
train_temp = os.path.join(base_path, f"dataset_preprocessed/sft_train_temp_{part_index}.base64")
val_temp = os.path.join(base_path, f"dataset_preprocessed/sft_val_temp_{part_index}.base64")
all_items = []
line_iterator = get_some_lines(text_iterator, LINES_PER_PART)
pool = Pool(os.cpu_count())
res = pool.imap(func=apply_template_and_encode, iterable=line_iterator, chunksize=64)
for item in tqdm(res, total=LINES_PER_PART):
if not item:
continue
all_items.append(item)
pool.close()
pool.join()
# 在part内部打乱,并写入临时文件
print(f" Shuffling SFT items and write to file...")
line_indexes = list(range(len(all_items)))
random.shuffle(line_indexes)
with open(train_temp, "w", encoding="utf-8") as f_train:
for i in range(len(all_items)):
item = all_items[i]
ids = item[0]
mask = item[1]
train_data = pickle.dumps([ids, mask])
f_train.writelines(str(base64.b64encode(train_data), encoding="utf-8") + "\n")
with open(val_temp, "w", encoding="utf-8") as f_val:
for i in range(int(len(all_items) * 0.95), len(all_items)):
item = all_items[i]
ids = item[0]
mask = item[1]
val_data = pickle.dumps([ids, mask])
f_val.writelines(str(base64.b64encode(val_data), encoding="utf-8") + "\n")
train_temp_file_paths.append(train_temp)
val_temp_file_paths.append(val_temp)
# 在part之间打乱,写入统一文件,并删除临时文件
print(f"Shuffling all parts and write to file...")
part_indexes = list(range(len(train_temp_file_paths)))
random.shuffle(part_indexes)
with open(train_output_path, "w", encoding="utf-8") as f_train:
for pindex in part_indexes:
p = train_temp_file_paths[pindex]
print(f" Writing part {p}...")
with open(p, "r", encoding="utf-8") as tp:
lines = tp.read()
f_train.write(lines)
print(f" Delete temp file {p}...")
os.remove(p)
random.shuffle(part_indexes)
with open(val_output_path, "w", encoding="utf-8") as f_val:
for pindex in part_indexes:
p = val_temp_file_paths[pindex]
print(f" Writing part {p}...")
with open(p, "r", encoding="utf-8") as tp:
lines = tp.read()
f_val.write(lines)
print(f" Delete temp file {p}...")
os.remove(p)
print(f"Done.")
def main():
if STAGE_FLAG["pretrain"] == 1:
print("Pre-processing pretrain dataset...")
for index, pt in enumerate(PRETRAIN_DATASETS):
generate_pretrain_dataset(
os.path.join(base_path, pt),
os.path.join(base_path, f"dataset_preprocessed/pt_train_{index}.base64"),
os.path.join(base_path, f"dataset_preprocessed/pt_val_{index}.base64"))
if STAGE_FLAG["sft"] == 1:
print("Pre-processing SFT dataset...")
generate_sft_dataset(
os.path.join(base_path, SFT_DATASET),
os.path.join(base_path, f"dataset_preprocessed/sft_train.base64"),
os.path.join(base_path, f"dataset_preprocessed/sft_val.base64"))
if __name__ == "__main__":
main()