-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
262 lines (235 loc) · 9.79 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
import random
import warnings
import torch
from datasets import load_dataset
from peft import LoraConfig
from peft.tuners.lora import LoraLayer
from torch.utils.data import IterableDataset
from tqdm import tqdm
from transformers import (
AutoModelForCausalLM,
BitsAndBytesConfig,
AutoTokenizer,
TrainingArguments,
AutoConfig
)
from transformers import TrainerCallback, TrainerState, TrainerControl
class SaveDeepSpeedPeftModelCallback(TrainerCallback):
def __init__(self, trainer, save_steps=500):
self.trainer = trainer
self.save_steps = save_steps
def on_step_end(
self,
args: TrainingArguments,
state: TrainerState,
control: TrainerControl,
**kwargs,
):
if (state.global_step + 1) % self.save_steps == 0:
self.trainer.accelerator.wait_for_everyone()
state_dict = self.trainer.accelerator.get_state_dict(self.trainer.deepspeed)
unwrapped_model = self.trainer.accelerator.unwrap_model(self.trainer.deepspeed)
if self.trainer.accelerator.is_main_process:
unwrapped_model.save_pretrained(args.output_dir, state_dict=state_dict)
self.trainer.accelerator.wait_for_everyone()
return control
class ConstantLengthDataset(IterableDataset):
"""
Iterable dataset that returns constant length chunks of tokens from stream of text files.
Args:
tokenizer (Tokenizer): The processor used for proccessing the data.
dataset (dataset.Dataset): Dataset with text files.
infinite (bool): If True the iterator is reset after dataset reaches end else stops.
seq_length (int): Length of token sequences to return.
num_of_sequences (int): Number of token sequences to keep in buffer.
chars_per_token (int): Number of characters per token used to estimate number of tokens in text buffer.
shuffle (bool): If true, the samples in each buffer are suffled. Default is `True`.
add_eos_token (bool): If true, each buffer is delimited with eos token. Default is `True`.
"""
def __init__(
self,
tokenizer,
dataset,
infinite=False,
seq_length=1024,
num_of_sequences=1024,
chars_per_token=3.6,
content_field="content",
shuffle=True,
add_eos_token=True,
):
self.tokenizer = tokenizer
self.concat_token_id = tokenizer.eos_token_id
self.dataset = dataset
self.seq_length = seq_length
self.infinite = infinite
self.current_size = 0
self.max_buffer_size = seq_length * chars_per_token * num_of_sequences
self.content_field = content_field
self.shuffle = shuffle
self.add_eos_token = add_eos_token
def __iter__(self):
iterator = iter(self.dataset)
more_examples = True
while more_examples:
buffer, buffer_len = [], 0
while True:
if buffer_len >= self.max_buffer_size:
break
try:
buffer.append(next(iterator)[self.content_field])
buffer_len += len(buffer[-1])
except StopIteration:
if self.infinite:
iterator = iter(self.dataset)
else:
more_examples = False
break
tokenized_inputs = self.tokenizer(buffer, truncation=False)["input_ids"]
all_token_ids = []
for tokenized_input in tokenized_inputs:
if self.add_eos_token:
tokenized_input = tokenized_input + [self.concat_token_id]
all_token_ids.extend(tokenized_input)
examples = []
for i in range(0, len(all_token_ids), self.seq_length):
input_ids = all_token_ids[i: i + self.seq_length]
if len(input_ids) == self.seq_length:
examples.append(input_ids)
if self.shuffle:
random.shuffle(examples)
for example in examples:
self.current_size += 1
yield {
"input_ids": torch.LongTensor(example),
"labels": torch.LongTensor(example),
}
def chars_token_ratio(dataset, tokenizer, data_column, nb_examples=400):
"""
Estimate the average number of characters per token in the dataset.
"""
total_characters, total_tokens = 0, 0
for _, example in tqdm(zip(range(nb_examples), iter(dataset)), total=nb_examples):
total_characters += len(example[data_column])
total_tokens += len(tokenizer(example[data_column]).tokens())
return total_characters / total_tokens
def create_datasets(tokenizer, args):
dataset = load_dataset(args.dataset_name, use_auth_token=True, num_proc=args.num_workers)
train_data = dataset["train"]
try:
valid_data = dataset["test"]
except:
warnings.warn(
"No test data found in dataset. Using 5% instances from train. Please provide test data for accurate results."
)
dataset_split = dataset.train_test_split(test_size=0.05, seed=1)
train_data = dataset_split["train"]
valid_data = dataset_split["test"]
print(f"Size of the train set: {len(train_data)}. Size of the validation set: {len(valid_data)}")
chars_per_token = chars_token_ratio(train_data, tokenizer, args.dataset_text_field)
print(f"The character to token ratio of the dataset is: {chars_per_token:.2f}")
train_dataset = ConstantLengthDataset(
tokenizer,
train_data,
infinite=True,
seq_length=args.max_seq_length,
chars_per_token=chars_per_token,
content_field=args.dataset_text_field,
shuffle=True,
add_eos_token=False,
)
valid_dataset = ConstantLengthDataset(
tokenizer,
valid_data,
infinite=False,
seq_length=args.max_seq_length,
chars_per_token=chars_per_token,
content_field=args.dataset_text_field,
shuffle=False,
add_eos_token=False,
)
return train_dataset, valid_dataset
def create_and_prepare_model(args):
device_map = None
bnb_config = None
load_in_8bit = args.use_8bit_qunatization
if args.use_4bit_qunatization:
compute_dtype = getattr(torch, args.bnb_4bit_compute_dtype)
bnb_config = BitsAndBytesConfig(
load_in_4bit=args.use_4bit_qunatization,
bnb_4bit_quant_type=args.bnb_4bit_quant_type,
bnb_4bit_compute_dtype=compute_dtype,
bnb_4bit_use_double_quant=args.use_nested_quant,
)
if compute_dtype == torch.float16 and args.use_4bit_qunatization:
major, _ = torch.cuda.get_device_capability()
if major >= 8:
print("=" * 80)
print("Your GPU supports bfloat16, you can accelerate training with the argument --bf16")
print("=" * 80)
if args.use_4bit_qunatization or args.use_8bit_qunatization:
device_map = "auto" # {"": 0}
model_config = AutoConfig.from_pretrained(args.model_name, trust_remote_code=True)
if args.max_seq_length > model_config.max_position_embeddings:
model_config.max_position_embeddings = args.max_seq_length
model_config.use_cache = not args.use_gradient_checkpointing
if args.bf16:
model = AutoModelForCausalLM.from_pretrained(
args.model_name,
config=model_config,
load_in_8bit=load_in_8bit,
quantization_config=bnb_config,
device_map=device_map,
trust_remote_code=True,
use_flash_attention_2=True if args.use_flash_attn else False,
torch_dtype=torch.bfloat16
)
else:
model = AutoModelForCausalLM.from_pretrained(
args.model_name,
config=model_config,
load_in_8bit=load_in_8bit,
quantization_config=bnb_config,
device_map=device_map,
trust_remote_code=True,
use_flash_attention_2=True if args.use_flash_attn else False,
torch_dtype=torch.bfloat16
)
peft_config = None
if args.use_peft_lora:
if args.lora_target_modules:
peft_config = LoraConfig(
lora_alpha=args.lora_alpha,
lora_dropout=args.lora_dropout,
r=args.lora_r,
bias="none",
task_type="CAUSAL_LM",
target_modules=args.lora_target_modules.split(","),
)
else:
peft_config = LoraConfig(
lora_alpha=args.lora_alpha,
lora_dropout=args.lora_dropout,
r=args.lora_r,
bias="none",
task_type="CAUSAL_LM",
)
if args.use_gradient_checkpointing:
model.gradient_checkpointing_enable()
model.print_trainable_parameters()
tokenizer = AutoTokenizer.from_pretrained(args.model_name, trust_remote_code=True)
tokenizer.add_special_tokens({'pad_token': '[PAD]'}) # Add a [PAD] token to the vocabulary.
tokenizer.padding_side = "right"
tokenizer.model_max_length = args.max_seq_length
return model, peft_config, tokenizer
def peft_module_casting_to_bf16(model, args):
for name, module in model.named_modules():
if isinstance(module, LoraLayer):
if args.bf16:
module = module.to(torch.bfloat16)
if "norm" in name:
module = module.to(torch.float32)
if any(x in name for x in ["lm_head", "embed_tokens", "wte", "wpe"]):
if hasattr(module, "weight"):
if args.bf16 and module.weight.dtype == torch.float32:
module = module.to(torch.bfloat16)