-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmydataset.py
341 lines (263 loc) · 12.7 KB
/
mydataset.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import json, lzma, math, logging, os, sys, pprint, glob
from typing import Dict, Optional, List
from dataclasses import dataclass, field
import torch
from torch.utils.data import Dataset
from transformers import PreTrainedTokenizer
from transformers.trainer_pt_utils import LabelSmoother
PACK_DATA = ( os.getenv("PACK_DATA", 0) == "1")
if PACK_DATA:
from packed_dataset import PackedDataset, monkey_patch
monkey_patch()
IGNORE_TOKEN_ID = LabelSmoother.ignore_index
assert IGNORE_TOKEN_ID == -100
####################
def preprocess(sources, tokenizer, max_len):
tknz_name = tokenizer.__class__.__name__.lower()
if "qwen" in tknz_name:
from qwen_vocab import old2new_tid, tknz_encode
def tknz(str):
return tknz_encode(str, tokenizer)
elif "gemma" in tknz_name:
from gemma_vocab import old2new_tid
def tknz(str):
token_ids = tokenizer(str, add_special_tokens=False).input_ids
## không cần nữa bỏ qua first token nữa vì đã có add_special_tokens=False
token_ids = [ old2new_tid(x, tokenizer) for x in token_ids ]
token_ids = [ x for x in token_ids if x is not None ]
return token_ids
else:
assert False, "Không hỗ trợ"
def add_tokens(input_id, target, tokens, ignore=False):
if isinstance(tokens, str): tokens = tknz(tokens)
input_id += tokens
if ignore: target += [IGNORE_TOKEN_ID]*len(tokens)
else: target += tokens
return input_id, target
input_ids, targets, texts = [], [], []
skips_count = 0
for d in sources:
if "text" in d:
text_tokens = tknz(d['text']) + [ tokenizer.eos_token_id ]
if tokenizer.bos_token_id:
text_tokens = [ tokenizer.bos_token_id ] + text_tokens
if PACK_DATA: # text sẽ được packing cùng instructs sau
while len(text_tokens) > 0:
input_ids.append(text_tokens[:max_len])
targets .append(text_tokens[:max_len])
text_tokens = text_tokens[max_len:]
else: # gắn thành chuỗi texts dài để chặn đoạn padding vào instructs ở đoạn sau
texts += text_tokens
continue
input_id, target = [], []
im_end = "<|im_end|>" # mặc định cho chatml format
# im_end = "</s>" # để viet-mistal ko phải học cách kết thúc câu mới khi ko finetune embeddings
# im_end = tokenizer.eos_token # sẽ tự động là <|im_end|> khi xài qwen-based
for c in d['conversations']:
# Bắt lỗi
try: c['from']
except: assert False, f"{d}\n\n{c}"
if c['value'] is None:
assert False, f"{d}\n\n{c}"
# END bắt lỗi
if c['from'] == "system":
add_tokens(input_id, target, "<|im_start|>system\n", ignore=True)
add_tokens(input_id, target, c['value'], ignore=True)
add_tokens(input_id, target, f"{im_end}\n", ignore=True)
elif c['from'] == "human" or c['from'] == "user":
ignore = True # mặc định là không học
if "weight" in c and c["weight"] == 1:
ignore = False # chỉ học khi weight đc gán là 1
add_tokens(input_id, target, "<|im_start|>user\n", ignore=True)
add_tokens(input_id, target, c['value'], ignore=ignore)
add_tokens(input_id, target, f"{im_end}\n", ignore=ignore)
else:
assert c['from'] == 'gpt', c
ignore = False # mặc định là học
if "weight" in c and c["weight"] == 0:
ignore = True # chỉ bỏ qua khi weight được gán là 0
add_tokens(input_id, target, "<|im_start|>assistant\n", ignore=True)
add_tokens(input_id, target, c['value'], ignore=ignore)
add_tokens(input_id, target, f"{im_end}\n", ignore=ignore)
if len(input_id) <= max_len:
######
assert len(input_id) == len(target)
input_ids.append(input_id)
targets.append(target)
######
else:
skips_count += 1
# print(f"!!! Bỏ qua sft sample này vì số tokens của nó > {max_len} ctxlen")
if not PACK_DATA: # Nhét texts vào cuối instructs
for threshold in [max_len / 2, max_len / 4, max_len / 8]:
if len(texts) < 100: break
for i in range(0, len(input_ids)):
if len(texts) < 100: break
remain = max_len - len(input_ids[i])
if remain > threshold:
text_tokens = texts[:remain]
texts = s[remain:]
input_ids[i] += text_tokens
targets[i] += text_tokens
paddings_count = 0
for i in range(0, len(input_ids)):
remain = max_len - len(input_ids[i])
paddings_count += remain
if not PACK_DATA: # Padding luôn
input_ids[i] += [tokenizer.pad_token_id]*remain
assert len(input_ids[i]) == max_len
targets[i] += [IGNORE_TOKEN_ID]*remain
assert len(targets[i]) == max_len
r = paddings_count / (len(input_ids)*max_len)
r = round(r * 10000) / 100
print(f"\n>>> Tỉ lệ padding {r}%")
print(f">>> Số lượng mẫu bỏ qua {skips_count}/{len(input_ids)}")
return input_ids, targets
#### Song song hóa việc chuyển hóa data
from multiprocessing import Pool
from functools import partial
import os, torch, random, gc
#####
class RandomAccessDataset(Dataset):
def __init__(self, input_ids, labels):
self.input_ids = input_ids
self.labels = labels
self.random_orders = [x for x in range(len(input_ids))]
random.shuffle(self.random_orders)
if os.getenv("TESTING_RANDOM_ACCESS", 0) == "1":
print(">>> TESTING_RANDOM_ACCESS")
for i, idx in enumerate(self.random_orders):
data = self[i]
assert input_ids[idx] == data["input_ids"]
assert labels[idx] == data["labels"]
print("TESTING_RANDOM_ACCESS DONE!")
def __len__(self):
return len(self.input_ids)
def __getitem__(self, i):
idx = self.random_orders[i]
return dict(
input_ids=self.input_ids[idx],
labels=self.labels[idx],
)
class SupervisedDataset(Dataset):
"""Dataset for supervised fine-tuning."""
def __init__(self):
super(SupervisedDataset, self).__init__()
def prepare(self, sources, tokenizer: PreTrainedTokenizer, max_len: int):
if os.cpu_count() > 80: num_proc = 80
else: num_proc = os.cpu_count() - 2
partial_preprocess = partial(preprocess, tokenizer=tokenizer, max_len=max_len)
chunk_size = 1024
chunks = [sources[i:i + chunk_size] for i in range(0, len(sources), chunk_size)]
print(">>> sources", len(sources))
print(">>> chunks", len(chunks))
assert sum([len(x) for x in chunks]) == len(sources)
import time
start_time = time.time()
print("Tiền xử lý dữ liệu instructs...")
input_ids, labels = [], []
count = 0
with Pool(processes=num_proc) as pool:
for i, l in pool.imap_unordered(partial_preprocess, chunks):
input_ids += i
labels += l
count += 1; print(f"\n{count}/{len(chunks)} DONE")
elapsed_time = time.time() - start_time
print(f"Tokenization time: {elapsed_time} seconds")
print(">>> sources", len(sources))
print(">>> input_ids", len(input_ids))
# Giải phóng bộ nhớ
sources = None; chunks = None; gc.collect()
CUTOFF = int(os.getenv("CUTOFF", -1))
if not PACK_DATA:
if CUTOFF > 0:
print(f"!!! chỉ giữ lại {CUTOFF} samples")
input_ids = input_ids[:CUTOFF]
labels = labels[:CUTOFF]
assert len(input_ids) == CUTOFF
assert len(labels) == CUTOFF
print("Biến input_ids thành tensors ...")
self.input_ids = torch.tensor(input_ids)#.cuda()
input_ids = None; gc.collect() # Giải phóng bộ nhớ
print("Biến labels thành tensors ...")
self.labels = torch.tensor(labels)#.cuda()
labels = None; gc.collect() # Giải phóng bộ nhớ
self.attention_mask = None
else:
print("Packing instructs...")
rand_access_data = RandomAccessDataset(input_ids, labels)
packed = PackedDataset(rand_access_data, tokenizer, pack_length=max_len)
packed.stat(); gc.collect() # Giải phóng bộ nhớ
if CUTOFF > 0 and CUTOFF < len(packed): print(f"!!! chỉ giữ lại {CUTOFF} samples")
else: CUTOFF = len(packed)
input_ids, labels, attention_mask = [], [], []
for i in range(0, CUTOFF):
x = packed[i]
input_ids .append(x[ "input_ids"])
labels .append(x[ "labels"])
attention_mask.append(x["attention_mask"])
self. input_ids = torch.tensor( input_ids)#.cuda()
self. labels = torch.tensor( labels)#.cuda()
self.attention_mask = torch.tensor(attention_mask)#.cuda()
packed = input_ids = labels = attention_mask = None; gc.collect() # Giải phóng bộ nhớ
def save(self, cache_path):
torch.save(self.input_ids.cpu(), os.path.join(cache_path, "input_ids.pt"))
self.input_ids = None
torch.save(self.labels.cpu(), os.path.join(cache_path, "labels.pt"))
self.labels = None
if PACK_DATA:
torch.save(self.attention_mask.cpu(), os.path.join(cache_path, "attention_mask.pt"))
self.attention_mask = None
# Giải phóng bộ nhớ
gc.collect(); torch.cuda.empty_cache()
def load(self, cache_path, tokenizer):
self.input_ids = torch.load(os.path.join(cache_path, "input_ids.pt"))
self.labels = torch.load(os.path.join(cache_path, "labels.pt"))
assert len(self.input_ids) == len(self.labels)
if PACK_DATA:
self.attention_mask = torch.load(os.path.join(cache_path, "attention_mask.pt"))
assert len(self.input_ids) == len(self.attention_mask)
def __len__(self):
return len(self.input_ids)
if not PACK_DATA:
def __getitem__(self, i) -> Dict[str, torch.Tensor]:
return dict(input_ids=self.input_ids[i], labels=self.labels[i])
else:
def __getitem__(self, i) -> Dict[str, torch.Tensor]:
return dict(input_ids=self.input_ids[i], labels=self.labels[i], attention_mask=self.attention_mask[i])
def make_supervised_data_module(
tokenizer: PreTrainedTokenizer, training_args, rank0_print = None
) -> Dict:
"""Make dataset and collator for supervised fine-tuning."""
cache_path = os.path.join("data_cached", training_args.data_path)
train_dataset = SupervisedDataset()
if os.path.exists(cache_path):
print(f">>> {cache_path} existed.")
else:
data_path = f"data/{training_args.data_path}.jsonl"
use_lzma = False
if not os.path.exists(data_path):
data_path = f"data/{training_args.data_path}.jsonl.xz"
use_lzma = True
print(f"!!! Loading data for supervised finetune from {data_path} ... !!!")
if use_lzma: file = lzma.open(data_path, 'rt')
else: file = open(data_path, 'rt')
sources = [ json.loads(line) for line in file]
file.close()
print("Formatting inputs...")
train_dataset.prepare(
sources,
tokenizer = tokenizer,
max_len = training_args.model_max_length,
)
print("Save to disk...")
os.makedirs(cache_path, exist_ok=True)
train_dataset.save(cache_path)
train_dataset.load(cache_path, tokenizer)
x = train_dataset[0]; print("!!!", x)
if PACK_DATA:
assert 'attention_mask' in x
assert x['attention_mask'] is not None
else:
assert 'attention_mask' not in x
return dict(train_dataset=train_dataset, eval_dataset=None)