-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
284 lines (233 loc) · 9.88 KB
/
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
272
273
274
275
276
277
278
279
280
281
282
283
284
import io
import json
import os
import random
import shutil
import sys
from pathlib import Path
from abc import ABC
from collections import defaultdict
from datetime import timedelta
from typing import List, Tuple, Union
from datasets import Dataset, interleave_datasets, load_dataset
import deepspeed
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import bitsandbytes as bnb
from deepspeed.ops.adam import DeepSpeedCPUAdam, FusedAdam
from peft import PeftModel, get_peft_model_state_dict
from torch import distributed as dist
from torch.optim import Optimizer
from torch.utils.data import DataLoader, DistributedSampler
import torch.nn.functional as F
from transformers import AutoTokenizer
def zero_pad_sequences(sequences, side: str = "left", value=0):
assert side in ("left", "right")
max_len = max(seq.size(-1) for seq in sequences)
padded_sequences = []
for seq in sequences:
pad_len = max_len - seq.size(-1)
padding = (pad_len, 0) if side == "left" else (0, pad_len)
padded_sequences.append(F.pad(seq, padding, value=value))
return torch.stack(padded_sequences, dim=0)
def find_all_linear_names(model, load_in_4bit=False):
cls = bnb.nn.Linear4bit if load_in_4bit else nn.Linear
lora_module_names = set()
for name, module in model.named_modules():
if isinstance(module, cls):
names = name.split(".")
lora_module_names.add(names[0] if len(names) == 1 else names[-1])
if "lm_head" in lora_module_names: # needed for 16-bit
lora_module_names.remove("lm_head")
return list(lora_module_names)
def exist_and_not_none(d, key):
return key in d and d[key] is not None
def log_probs_from_logits(logits: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
log_probs = F.log_softmax(logits, dim=-1)
log_probs_labels = log_probs.gather(dim=-1, index=labels.unsqueeze(-1))
return log_probs_labels.squeeze(-1)
class GPTLMLoss(nn.Module):
"""
GPT Language Model Loss
"""
def __init__(self):
super().__init__()
self.IGNORE_INDEX = -100
self.loss = nn.CrossEntropyLoss(ignore_index=self.IGNORE_INDEX)
def forward(self, logits: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
shift_logits = logits[..., :-1, :].contiguous()
shift_labels = labels[..., 1:].contiguous()
# Flatten the tokens
return self.loss(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
def blending_datasets(
datasets,
probabilities,
strategy=None,
seed=42,
max_count=2000000,
max_eval_count=10000,
return_eval=True,
stopping_strategy="first_exhausted",
):
datasets = datasets.split(",")
probabilities = list(map(float, probabilities.split(",")))
assert len(probabilities) == len(datasets)
train_data_list = []
eval_data_list = []
for i, dataset in enumerate(datasets):
dataset = dataset.strip()
dataset_subfold_list = dataset.split("@")
print(f"dataset: {dataset}")
# local dir with python script or common local file
if os.path.isdir(os.path.join(os.getcwd(), dataset)) or dataset.endswith(
(".json", ".jsonl", ".csv", ".parquet", ".txt")
):
try:
if dataset.endswith((".json", ".jsonl", ".csv", ".parquet", ".txt")):
files = dataset
data_type = os.path.splitext(files)[1][1:]
else:
path = Path(dataset)
script = [str(file.resolve()) for file in Path(path).rglob("*.py")]
extensions = ("*.json", "*.jsonl", "*.csv", "*.parquet", "*.txt")
files = [str(file) for ext in extensions for file in Path(path).rglob(ext)]
print(f"script: {script}")
print(f"files: {files}")
# For dir, follow python script or first file type
data_type = script[0] if len(script) == 1 else os.path.splitext(files[0])[1][1:]
# reformat data type
if data_type in ["json", "jsonl"]:
data_type = "json"
elif data_type == "txt":
data_type = "text"
elif data_type.endswith(".py"):
# load local dir with python script
files = None
if data_type.endswith(".py"):
print(f"load {dataset} with script {data_type}")
else:
print(f"load {files} from {dataset}")
data = load_dataset(data_type, data_files=files, trust_remote_code=True)
except:
data = load_dataset(dataset, trust_remote_code=True)
elif len(dataset_subfold_list) == 2:
dataset = dataset_subfold_list[0]
subfold = dataset_subfold_list[1]
data = load_dataset(dataset, data_dir=subfold.strip(), trust_remote_code=True)
elif len(dataset_subfold_list) == 1:
dataset = dataset_subfold_list[0]
data = load_dataset(dataset, trust_remote_code=True)
else:
raise Exception(f"Dataset Name {dataset}: Format error")
if "train" in data:
train_data_list.append(data["train"].select(range(min(max_count, int(len(data["train"]) * 0.9)))))
else:
train_data_list.append(data.select(range(min(max_count, int(len(data["train"]) * 0.9))))) # train will contains eval? TODO
eval_data_candidate = data["train"].select(range(len(train_data_list[-1]), len(data["train"])))
if return_eval:
if "test" in data:
eval_data = data["test"].select(range(min(int(max_count * 0.1), len(data["test"]))))
elif "validation" in data:
eval_data = data["validation"].select(range(min(int(max_count * 0.1), len(data["validation"]))))
elif "train" in data:
eval_data = eval_data_candidate.select(range(min(max_eval_count, len(eval_data_candidate))))
else:
eval_data = data.select(range(min(int(max_count * 0.1), int(len(data) * 0.001))))
eval_data_list.append(eval_data)
# merge datasets
print(train_data_list)
train_dataset = interleave_datasets(
train_data_list,
probabilities=probabilities,
seed=seed,
stopping_strategy=stopping_strategy,
)
if return_eval:
eval_dataset = interleave_datasets(
eval_data_list,
probabilities=probabilities,
seed=seed,
stopping_strategy=stopping_strategy,
)
return train_dataset, eval_dataset
else:
return train_dataset
class Logger(object):
def __init__(self, log_path, on=True):
self.log_path = log_path
self.on = on
if self.on:
while os.path.isfile(self.log_path):
self.log_path += '+'
def log(self, string, newline=True, force=False):
if self.on or force:
with open(self.log_path, 'a') as logf:
logf.write(string)
if newline: logf.write('\n')
sys.stdout.write(string)
if newline: sys.stdout.write('\n')
sys.stdout.flush()
ModelOptimPair = Tuple[nn.Module, Optimizer]
ModelOrModelOptimPair = Union[nn.Module, ModelOptimPair]
def get_sp_tokens(args):
sp_tokens = dict()
for key in ("bos_token", "eos_token", "pad_token", "unk_token"):
sp_token = getattr(args, key, None)
if sp_token is not None:
sp_tokens[key] = sp_token
return sp_tokens
def get_tokenizer(pretrain, model, padding_side="left", strategy=None, use_fast=True):
sp_tokens = get_sp_tokens(strategy.args)
tokenizer = AutoTokenizer.from_pretrained(pretrain, trust_remote_code=True, **sp_tokens)
tokenizer.padding_side = padding_side
# NOTE: When enable vLLM, do not resize_token_embeddings, or the vocab size will mismatch with vLLM.
# https://github.com/facebookresearch/llama-recipes/pull/196
if "mistral" in pretrain.lower():
template_tokenizer = AutoTokenizer.from_pretrained('HuggingFaceH4/zephyr-7b-beta', trust_remote_code=True)
tokenizer.apply_chat_template = template_tokenizer.apply_chat_template
elif "llama" in pretrain.lower():
template_tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B-Instruct", trust_remote_code=True)
tokenizer.apply_chat_template = template_tokenizer.apply_chat_template
tokenizer.eos_token_id = 128001
tokenizer.eos_token = '<|end_of_text|>'
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
tokenizer.pad_token_id = tokenizer.eos_token_id
model.config.pad_token_id = tokenizer.pad_token_id
return tokenizer
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