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
|
from typing import Union
from torch.utils.data import Dataset
from torch import LongTensor
from transformers import PreTrainedTokenizerFast
import pyarrow.parquet as pq
from numpy import array, int64
from numpy.random import shuffle
class MyDataset(Dataset):
def __init__(self,
parquet_file: str,
tokenizer_dir: str,
keep_in_memory: bool=False,
max_seq_len: int=512,
buffer_size: int=40960,
) -> None:
'''
keep_in_memory: 是否将parquet文件转换为pandas.DataFrame格式存放到内存,
False将使用迭代生成器(迭代生成器不支持打乱数据),减少大数据集内存占用
'''
super().__init__()
self.keep_in_memory = keep_in_memory
self.max_seq_len = max_seq_len
# 使用pyarrow.parquet读取,to_pandas、for遍历速度更快
parquet_table = pq.read_table(parquet_file)
# 获取数据集长度
self.length = parquet_table.num_rows
# 缓冲区大小不能超过数据长度
self.buffer_size = self.length if buffer_size > self.length else buffer_size
if keep_in_memory:
# 转化为pandas放到内存中
self.data = parquet_table.to_pandas()
else:
self.data = parquet_table
# 初始化tokenizer
self.tokenizer = PreTrainedTokenizerFast.from_pretrained(tokenizer_dir)
# 在这里初始化generator
self.sample_generator = self.item_generator()
def item_generator(self,) -> tuple:
'''
一条数据的生成器,防止大数据集OOM
'''
parquet_table = self.data
# 生成器是死循环,不用退出,训练结束(epoch结束)会停止调用next()
buffer_list = []
while True:
for prompt, response in zip(parquet_table['prompt'], parquet_table['response']):
# 缓存数据不够,添加数据
if len(buffer_list) < self.buffer_size:
buffer_list.append( (prompt.as_py(), response.as_py()) )
continue
# 执行到这里,缓存区够了,打乱数据
shuffle(buffer_list)
for p, r in buffer_list:
# 在这里迭代
yield p, r
# 迭代完成,清空缓存区
buffer_list = []
def __getitem__(self, index):
'''
返回一条样本
'''
if self.keep_in_memory:
data = self.data
prompt, response = data.iloc[index].prompt, data.iloc[index].response
else:
prompt, response = next(self.sample_generator)
max_seq_len = self.max_seq_len - 5 # len('[EOS]') = 5
# add an eos token note that end of resopnse, using in generate.
return f"{prompt[0: max_seq_len]}[EOS]", f"{response[0: max_seq_len]}[EOS]"
def collate_fn(self, data: list[list]) -> dict:
'''
合并一个批次数据返回
'''
tokenizer = self.tokenizer
prompt = tokenizer([item[0] for item in data], padding=True, return_token_type_ids=False)
response = tokenizer([item[1] for item in data], padding=True, return_token_type_ids=False)
input_ids = array(prompt.input_ids, dtype=int64)
input_mask = array(prompt.attention_mask, dtype=int64)
target_ids = array(response.input_ids, dtype=int64)
ret = {
'input_ids': LongTensor(input_ids),
'input_mask': LongTensor(input_mask),
'target_ids': LongTensor(target_ids),
}
return ret
def __len__(self) -> int:
return self.length
|