forked from unslothai/unsloth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
271 lines (225 loc) · 9.18 KB
/
data_utils.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
# Copyright 2023 Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import io
import json
import os
import gc
import copy
import logging
from dataclasses import dataclass, field
from typing import Dict, Optional, Sequence, Callable, List, Tuple, Union, Any
import transformers
import torch
from torch.utils.data import Dataset
import warnings
warnings.filterwarnings("ignore")
IGNORE_INDEX = -100
DEFAULT_PAD_TOKEN = "[PAD]"
class SavePeftModelCallback(transformers.TrainerCallback):
def save_model(self, args, state, kwargs):
# print('Saving PEFT checkpoint...')
if state.best_model_checkpoint is not None:
checkpoint_folder = os.path.join(
state.best_model_checkpoint, "adapter_model"
)
else:
checkpoint_folder = os.path.join(
args.output_dir, f"checkpoint-{state.global_step}"
)
peft_model_path = os.path.join(checkpoint_folder, "adapter_model")
model = kwargs["model"]
model.save_pretrained(peft_model_path)
moe_state = {}
for param_tensor in model.state_dict():
if "adapter" in param_tensor:
moe_state.update({param_tensor: model.state_dict()[param_tensor]})
# if "adapter" in param_tensor or "norm" in param_tensor:
# moe_state.update({param_tensor: model.state_dict()[param_tensor]})
moe_model_path = os.path.join(checkpoint_folder, "moe_model.bin")
# print(moe_state.keys())
torch.save(moe_state, moe_model_path)
pytorch_model_path = os.path.join(checkpoint_folder, "pytorch_model.bin")
if os.path.exists(pytorch_model_path):
os.remove(pytorch_model_path)
def on_save(self, args, state, control, **kwargs):
self.save_model(args, state, kwargs)
return control
def on_train_end(self, args, state, control, **kwargs):
def touch(fname, times=None):
with open(fname, "a"):
os.utime(fname, times)
touch(os.path.join(args.output_dir, "completed"))
self.save_model(args, state, kwargs)
def _tokenize_fn(
strings: Sequence[str], tokenizer: transformers.PreTrainedTokenizer
) -> Dict:
"""Tokenize a list of strings."""
tokenized_list = [
tokenizer(
text,
return_tensors="pt",
padding="longest",
max_length=tokenizer.model_max_length,
truncation=True,
)
for text in strings
]
input_ids = labels = [tokenized.input_ids[0] for tokenized in tokenized_list]
input_ids_lens = labels_lens = [
tokenized.input_ids.ne(tokenizer.pad_token_id).sum().item()
for tokenized in tokenized_list
]
return dict(
input_ids=input_ids,
labels=labels,
input_ids_lens=input_ids_lens,
labels_lens=labels_lens,
)
def preprocess(
sources: Sequence[str],
targets: Sequence[str],
tokenizer: transformers.PreTrainedTokenizer,
) -> Dict:
"""Preprocess the data by tokenizing."""
examples = [s + t for s, t in zip(sources, targets)]
examples_tokenized, sources_tokenized = [
_tokenize_fn(strings, tokenizer) for strings in (examples, sources)
]
input_ids = examples_tokenized["input_ids"]
labels = copy.deepcopy(input_ids)
for label, source_len in zip(labels, sources_tokenized["input_ids_lens"]):
label[:source_len] = IGNORE_INDEX
return dict(input_ids=input_ids, labels=labels)
class SupervisedDataset(Dataset):
"""Dataset for supervised fine-tuning."""
def __init__(self, data_path: str, tokenizer: transformers.PreTrainedTokenizer):
super(SupervisedDataset, self).__init__()
logging.warning("Loading data: {}".format(data_path))
data_list = jload(data_path)
# Preprocess Data
logging.warning("Processing data")
self.tokenizer = tokenizer
self.sources = []
self.targets = []
system_str = "<|im_start|>system\n{message}<|im_end|>\n"
user_str = "<|im_start|>user\n{message}<|im_end|>\n"
assistant_str = "<|im_start|>assistant\n{message}<|im_end|>\n"
# pylint: disable-next=consider-using-enumerate
for idx in range(len(data_list)):
data = data_list[idx]
corpus = data.get("corpus", "")
if corpus != "":
# pretrain mode
source = f"{tokenizer.bos_token}"
self.sources.append(source)
target = f"{corpus}{tokenizer.eos_token}"
self.targets.append(target)
else:
conversations = data["conversations"]
conversation_str = f"{tokenizer.bos_token}"
for conversation in conversations[:-1]:
if conversation["from"] in ["human", "user"]:
conversation_str += user_str.format(
message=conversation["value"]
)
elif conversation["from"] in ["gpt", "assistant"]:
conversation_str += assistant_str.format(
message=conversation["value"]
)
elif conversation["from"] in ["system", "instruction"]:
conversation_str += system_str.format(
message=conversation["value"]
)
else:
raise ValueError(
f"Unknown conversation type: {conversation['from']}"
)
conversation_str += "<|im_start|>assistant\n"
self.sources.append(conversation_str)
last_message = conversations[-1]
self.targets.append(f"{last_message['value']}{tokenizer.eos_token}")
del data_list
gc.collect()
logging.warning("there are {} data in dataset".format(len(self.sources)))
def __len__(self):
return len(self.sources)
def __getitem__(self, i):
source = [self.sources[i]]
target = [self.targets[i]]
data_dict = preprocess(source, target, self.tokenizer)
input_ids = data_dict["input_ids"][0]
labels = data_dict["labels"][0]
return dict(input_ids=input_ids, labels=labels)
@dataclass
class DataCollatorForSupervisedDataset(object):
"""Collate examples for supervised fine-tuning."""
tokenizer: transformers.PreTrainedTokenizer
def __call__(self, instances: Sequence[Dict]) -> Dict[str, torch.Tensor]:
input_ids, labels = tuple(
[instance[key] for instance in instances] for key in ("input_ids", "labels")
)
input_ids = torch.nn.utils.rnn.pad_sequence(
input_ids, batch_first=True, padding_value=self.tokenizer.pad_token_id
)
labels = torch.nn.utils.rnn.pad_sequence(
labels, batch_first=True, padding_value=IGNORE_INDEX
)
return dict(
input_ids=input_ids,
labels=labels,
attention_mask=input_ids.ne(self.tokenizer.pad_token_id),
)
def make_supervised_data_module(
tokenizer: transformers.PreTrainedTokenizer, data_path: str
) -> Dict:
"""Make dataset and collator for supervised fine-tuning."""
train_dataset = SupervisedDataset(tokenizer=tokenizer, data_path=data_path)
data_collator = DataCollatorForSupervisedDataset(tokenizer=tokenizer)
return dict(
train_dataset=train_dataset, eval_dataset=None, data_collator=data_collator
)
def _make_w_io_base(f, mode: str):
if not isinstance(f, io.IOBase):
f_dirname = os.path.dirname(f)
if f_dirname != "":
os.makedirs(f_dirname, exist_ok=True)
f = open(f, mode=mode)
return f
def _make_r_io_base(f, mode: str):
if not isinstance(f, io.IOBase):
f = open(f, mode=mode)
return f
def jdump(obj, f, mode="w", indent=4, default=str):
"""Dump a str or dictionary to a file in json format.
Args:
obj: An object to be written.
f: A string path to the location on disk.
mode: Mode for opening the file.
indent: Indent for storing json dictionaries.
default: A function to handle non-serializable entries; defaults to `str`.
"""
f = _make_w_io_base(f, mode)
if isinstance(obj, (dict, list)):
json.dump(obj, f, indent=indent, default=default)
elif isinstance(obj, str):
f.write(obj)
else:
raise ValueError(f"Unexpected type: {type(obj)}")
f.close()
def jload(f, mode="r"):
"""Load a .json file into a dictionary."""
f = _make_r_io_base(f, mode)
jdict = json.load(f)
f.close()
return jdict