forked from wyf3/llm_related
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
197 lines (166 loc) · 7.18 KB
/
dataset.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
import math
from typing import List, Optional, Tuple, Union
import torch
import torch.nn.functional as F
import torch.utils.checkpoint
from torch import nn
import os
import pandas as pd
from torch.utils.data import IterableDataset, Dataset
import json
import numpy as np
from transformers import PreTrainedModel
from transformers.modeling_outputs import CausalLMOutputWithPast
from transformers import PretrainedConfig
from transformers import Trainer, TrainingArguments, AutoModelForCausalLM, AutoTokenizer, DefaultDataCollator, DataCollatorForTokenClassification, AutoConfig
class LLMDataset(Dataset):
def __init__(self, data_path, tokenizer, max_seq_len):
super().__init__()
self.data_path = data_path
self.tokenizer = tokenizer
self.max_seq_len = max_seq_len
with open(self.data_path, 'r', encoding='utf-8') as f:
self.data = f.readlines()
def __len__(self):
return len(self.data)
def __getitem__(self, index: int):
line = self.data[index]
line = json.loads(line)
text = '<s>' + line['text'] + '</s>'
input_ids = self.tokenizer.encode(text)
text_len = len(input_ids)
if text_len > self.max_seq_len:
input_ids = input_ids[:self.max_seq_len]
else:
input_ids = input_ids + [0] * (self.max_seq_len - text_len)
input_ids = np.array(input_ids)
X = np.array(input_ids[:-1]).astype(np.int64)
Y = np.array(input_ids[1:]).astype(np.int64)
return {
'input_ids': torch.from_numpy(X),
'labels': torch.from_numpy(Y),
}
class SFTDataset(Dataset):
def __init__(self, data_path, tokenizer, max_seq_len):
super().__init__()
self.data_path = data_path
self.tokenizer = tokenizer
self.max_seq_len = max_seq_len
with open(self.data_path, 'r', encoding='utf-8') as f:
self.data = f.readlines()
def __len__(self):
return len(self.data)
def __getitem__(self, index):
line = self.data[index]
line = json.loads(line)
instruction_text = line['instruction']
input_text = line['input']
output_text = line['output']
history = line['history']
query = instruction_text + input_text
answer = output_text + self.tokenizer.eos_token
messages = []
if history:
for i in history:
messages.append({'role': 'user', 'content': i[0]})
messages.append({'role': 'assistant', 'content': i[1]})
messages.append({'role': 'user', 'content': query})
prompt = self.tokenizer.apply_chat_template(messages, tokenize=False)
prompt_input_ids = self.tokenizer.encode(prompt)
answer_input_ids = self.tokenizer.encode(answer)
input_ids = prompt_input_ids + answer_input_ids
labels = [0] * len(prompt_input_ids) + answer_input_ids
text_len = len(input_ids)
if text_len > self.max_seq_len:
input_ids = input_ids[:self.max_seq_len]
labels = labels[:self.max_seq_len]
else:
input_ids = input_ids + [0] * (self.max_seq_len - text_len)
labels = labels + [0] * (self.max_seq_len - text_len)
input_ids = input_ids[:-1]
labels = labels[1:]
return {'input_ids': torch.tensor(input_ids), 'labels': torch.tensor(labels)}
# 内存不够,可使用如下方法加载数据
# class LLMDataset(IterableDataset):
# def __init__(self, data_path, tokenizer, max_seq_len):
# super().__init__()
# self.data_path = data_path
# self.tokenizer = tokenizer
# self.max_seq_len = max_seq_len
# def __iter__(self):
# return self.data_process()
# def data_process(self):
# with open(self.data_path, 'r', encoding='utf-8') as f:
# for line in f:
# line = json.loads(line)
# text = '<s>' + line['text'] + '</s>'
# input_ids = self.tokenizer.encode(text)
# text_len = len(input_ids)
# if text_len > self.max_seq_len:
# input_ids = input_ids[:self.max_seq_len]
# else:
# input_ids = input_ids + [0] * (self.max_seq_len - text_len)
# input_ids = np.array(input_ids)
# X = np.array(input_ids[:-1]).astype(np.int64)
# Y = np.array(input_ids[1:]).astype(np.int64)
# yield {
# 'input_ids': torch.from_numpy(X),
# 'labels': torch.from_numpy(Y),
# }
class DPODataset(Dataset):
def __init__(self, data_path, tokenizer):
super().__init__()
self.data_path = data_path
self.tokenizer = tokenizer
with open(self.data_path, 'r', encoding='utf-8') as f:
self.datas = json.load(f)
def __getitem__(self, index):
sample = self.datas[index]
prompt = sample['prompt']
chosen = sample['chosen']
rejected = sample['rejected']
messages = [
{"role": "user", "content": prompt}
]
text = self.tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
prompt_inputs = self.tokenizer(text=text)['input_ids']
rejected_inputs = self.tokenizer(text=rejected)['input_ids'] + [self.tokenizer.eos_token_id]
chosen_inputs = self.tokenizer(text=chosen)['input_ids'] + [self.tokenizer.eos_token_id]
return [prompt_inputs, chosen_inputs, rejected_inputs]
def __len__(self):
return len(self.datas)
class DPODataCollator:
def __init__(self, tokenizer, max_seq_len):
self.tokenizer = tokenizer
self.max_seq_len = max_seq_len
def __call__(self, features):
inputs_ids = []
labels = []
for feature in features:
inputs_ids.append(feature[0] + feature[1])
labels.append([0]*len(feature[0]) + feature[1])
for feature in features:
inputs_ids.append(feature[0] + feature[2])
labels.append([0]*len(feature[0]) + feature[2])
def process(inputs_ids, labels):
inputs_ids = [input_ids[:self.max_seq_len] for input_ids in inputs_ids]
labels = [label[:self.max_seq_len] for label in labels]
max_len = max([len(input_ids) for input_ids in inputs_ids])
batch_input_ids = []
batch_labels = []
for input_ids, label in zip(inputs_ids, labels):
if len(input_ids) <= max_len:
input_ids = input_ids+[0]*(max_len-len(input_ids))
label = label+[0]*(max_len-len(label))
batch_input_ids.append(input_ids[:-1])
batch_labels.append(label[1:])
return batch_input_ids, batch_labels
inputs_ids, labels = process(inputs_ids, labels)
return {
"input_ids": torch.tensor(inputs_ids),
"labels": torch.tensor(labels)
}