-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathllama2_finetuning.py
More file actions
370 lines (284 loc) · 12.1 KB
/
llama2_finetuning.py
File metadata and controls
370 lines (284 loc) · 12.1 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
####
# https://blog.ovhcloud.com/fine-tuning-llama-2-models-using-a-single-gpu-qlora-and-ai-notebooks/
####
# Import libraries
import bitsandbytes as bnb
from functools import partial
import os
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training, AutoPeftModelForCausalLM
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed, BitsAndBytesConfig, \
DataCollatorForLanguageModeling, Trainer, TrainingArguments, LlamaTokenizer, EarlyStoppingCallback
from datasets import load_dataset
import random
import pandas as pd
# Reproducibility
seed = 42
set_seed(seed)
def load_model(model_name, bnb_config):
n_gpus = torch.cuda.device_count()
print("N GPUS: ", n_gpus)
max_memory = f'{40960}MB'
model = AutoModelForCausalLM.from_pretrained(
model_name,
quantization_config=bnb_config,
device_map="auto", # dispatch efficiently the model on the available ressources
max_memory = {i: max_memory for i in range(n_gpus)},
)
#tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=True) # use it for llama
tokenizer = LlamaTokenizer.from_pretrained(model_name, trust_remote_code=True, use_fast=False) # use it for pmc-llama, , trust_remote_code=True, use_fast=False
# for pmc llama :
print("BEFORE: ")
print("Tokenizer BOS: ", tokenizer.bos_token)
print("Tokenizer EOS: ", tokenizer.eos_token)
print("Tokenizer PAD: ", tokenizer.pad_token)
print("Tokenizer UNK: ", tokenizer.unk_token)
tokenizer.bos_token = "<s>"
tokenizer.eos_token = "</s>"
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
model.resize_token_embeddings(len(tokenizer))
tokenizer.padding_side = 'right'
print("AFTER: ")
print("Tokenizer BOS: ", tokenizer.bos_token)
print("Tokenizer EOS: ", tokenizer.eos_token)
print("Tokenizer PAD: ", tokenizer.pad_token)
print("Tokenizer UNK: ", tokenizer.unk_token)
# Needed for LLaMA tokenizer
tokenizer.pad_token = tokenizer.eos_token
return model, tokenizer
def create_prompt_formats(sample):
"""
Format various fields of the sample ('instruction', 'context', 'response')
Then concatenate them using two newline characters
:param sample: Sample dictionnary
"""
INTRO_BLURB = "Below is an instruction that describes a task. Write a response that appropriately completes the request."
# Instruction Key without protein tags:
# INSTRUCTION_KEY = "### Instruction: What is the key word that represents the interaction between the proteins " + sample["Gene1"] + " and " + sample["Gene2"] + " in the given sentence?"
# Instruction Key with protein tags:
INSTRUCTION_KEY = "### Instruction: What is the key word that represents the interaction between the proteins which are tagged with [Protein1] and [Protein2] in the given sentence?"
INPUT_KEY = "### Input:"
RESPONSE_KEY = "### Response:"
END_KEY = "### End"
blurb = f"{INTRO_BLURB}"
instruction = INSTRUCTION_KEY
input_context = f"{INPUT_KEY}\n{sample['Sentence']}"
response = f"{RESPONSE_KEY}\n{sample['Keywords']}"
end = f"{END_KEY}"
parts = [part for part in [blurb, instruction, input_context, response, end] if part]
formatted_prompt = "\n\n".join(parts)
sample["text"] = formatted_prompt
return sample
# SOURCE https://github.com/databrickslabs/dolly/blob/master/training/trainer.py
def get_max_length(model):
conf = model.config
max_length = None
for length_setting in ["n_positions", "max_position_embeddings", "seq_length"]:
max_length = getattr(model.config, length_setting, None)
if max_length:
print(f"Found max lenth: {max_length}")
break
if not max_length:
max_length = 1024
print(f"Using default max length: {max_length}")
return max_length
def preprocess_batch(batch, tokenizer, max_length):
"""
Tokenizing a batch
"""
return tokenizer(
batch["text"],
max_length=max_length,
truncation=True,
)
# SOURCE https://github.com/databrickslabs/dolly/blob/master/training/trainer.py
def preprocess_dataset(tokenizer: AutoTokenizer, max_length: int, seed, dataset: str):
"""Format & tokenize it so it is ready for training
:param tokenizer (AutoTokenizer): Model Tokenizer
:param max_length (int): Maximum number of tokens to emit from tokenizer
"""
# Add prompt to each sample
print("Preprocessing dataset...")
dataset = dataset.map(create_prompt_formats) # , batched=True)
print("First example")
print(dataset[0]["text"])
# Apply preprocessing to each batch of the dataset & and remove 'instruction', 'context', 'response', 'category' fields
_preprocessing_function = partial(preprocess_batch, max_length=max_length, tokenizer=tokenizer)
dataset = dataset.map(
_preprocessing_function,
batched=True,
# remove_columns=["instruction", "context", "response", "text", "category"],
)
# Filter out samples that have input_ids exceeding max_length
dataset = dataset.filter(lambda sample: len(sample["input_ids"]) < max_length)
# Shuffle dataset
dataset = dataset.shuffle(seed=seed)
return dataset
def create_bnb_config():
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
return bnb_config
def create_peft_config(modules):
"""
Create Parameter-Efficient Fine-Tuning config for your model
:param modules: Names of the modules to apply Lora to
"""
config = LoraConfig(
r=8, # dimension of the updated matrices
lora_alpha=32, # parameter for scaling
target_modules=modules,
lora_dropout=0.0, # dropout probability for layers
bias="none",
task_type="CAUSAL_LM",
)
print(config)
return config
# SOURCE https://github.com/artidoro/qlora/blob/main/qlora.py
def find_all_linear_names(model):
cls = bnb.nn.Linear4bit #if args.bits == 4 else (bnb.nn.Linear8bitLt if args.bits == 8 else torch.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 print_trainable_parameters(model):
"""
Prints the number of trainable parameters in the model.
"""
trainable_params = 0
all_param = 0
for _, param in model.named_parameters():
num_params = param.numel()
# if using DS Zero 3 and the weights are initialized empty
if num_params == 0 and hasattr(param, "ds_numel"):
num_params = param.ds_numel
all_param += num_params
if param.requires_grad:
trainable_params += num_params
print(
f"all params: {all_param:,f} || trainable params: {trainable_params:,f} || trainable%: {100 * trainable_params / all_param}"
)
def train(model, tokenizer, train_dataset, val_dataset, output_dir):
# Apply preprocessing to the model to prepare it by
# 1 - Enabling gradient checkpointing to reduce memory usage during fine-tuning
model.gradient_checkpointing_enable()
# 2 - Using the prepare_model_for_kbit_training method from PEFT
model = prepare_model_for_kbit_training(model)
# Get lora module names
modules = find_all_linear_names(model)
# Create PEFT config for these modules and wrap the model to PEFT
peft_config = create_peft_config(modules)
model = get_peft_model(model, peft_config)
# Print information about the percentage of trainable parameters
print_trainable_parameters(model)
# Training parameters
trainer = Trainer(
model=model,
train_dataset=train_dataset,
eval_dataset=val_dataset,
args=TrainingArguments(
num_train_epochs=4, #num_train_epochs = max_steps / len(train_dataloader)
per_device_train_batch_size=1,
gradient_accumulation_steps=4,
warmup_steps=2,
# max_steps=40, #this overrides the num_train_epochs
learning_rate=2e-4,
fp16=True,
logging_steps=1,
output_dir="outputs",
optim="paged_adamw_8bit",
load_best_model_at_end = True, # for EarlyStoppingCallback, it is needed
evaluation_strategy = 'steps', # for EarlyStoppingCallback, it is needed
metric_for_best_model='eval_loss',
save_strategy='steps',
eval_steps=5,
),
data_collator=DataCollatorForLanguageModeling(tokenizer, mlm=False),
callbacks = [EarlyStoppingCallback(early_stopping_patience = 1, early_stopping_threshold = 0.0)]
)
model.config.use_cache = False # re-enable for inference to speed up predictions for similar inputs
### SOURCE https://github.com/artidoro/qlora/blob/main/qlora.py
# Verifying the datatypes before training
dtypes = {}
for _, p in model.named_parameters():
dtype = p.dtype
if dtype not in dtypes: dtypes[dtype] = 0
dtypes[dtype] += p.numel()
total = 0
for k, v in dtypes.items(): total += v
for k, v in dtypes.items():
print(k, v, v / total)
do_train = True
do_eval = True
if do_train:
# Launch training
print("Training...")
train_result = trainer.train()
metrics = train_result.metrics
trainer.log_metrics("train", metrics)
trainer.save_metrics("train", metrics)
trainer.save_state()
print(metrics)
###
if do_eval:
# Launch evaluation
print("Evaluating...")
eval_result = trainer.evaluate()
print(eval_result)
# Saving model
print("Saving last checkpoint of the model...")
os.makedirs(output_dir, exist_ok=True)
trainer.model.save_pretrained(output_dir)
# save tokenizer for easy inference
tokenizer = LlamaTokenizer.from_pretrained(model_name)
tokenizer.save_pretrained(output_dir)
# Free memory for merging weights
del model
del trainer
torch.cuda.empty_cache()
if __name__ == '__main__':
#dataset = load_dataset("databricks/databricks-dolly-15k", split="train")
dataset = load_dataset("bengisucam/LLL_INO-tagged", split="train")
train_test_dataset = dataset.train_test_split(test_size=0.1) # train and test (val)
train_dataset = train_test_dataset["train"]
val_dataset = train_test_dataset["test"]
print("train dataset length : ", len(train_dataset))
print("val dataset length : ", len(val_dataset))
##########################
#### EXPLORE DATASET ####
# Generate random indices
##########################
nb_samples = 3
random_indices = random.sample(range(len(train_dataset)), nb_samples)
train_samples = []
for idx in random_indices:
sample = train_dataset[idx]
sample_data = {
'context': sample['Sentence'],
'response': sample['Keywords'],
'genes': [sample['Gene1'], sample["Gene2"]]
}
train_samples.append(sample_data)
# Create a DataFrame and display it
train_df = pd.DataFrame(train_samples)
#print(train_df[:3])
# Load model from HF with user's token and with bitsandbytes config
model_name = "meta-llama/Llama-2-13b-chat-hf"
#model_name = "meta-llama/Llama-2-7b-chat-hf"
bnb_config = create_bnb_config()
model, tokenizer = load_model(model_name, bnb_config)
print_trainable_parameters(model)
## Preprocess dataset
max_length = get_max_length(model)
train_dataset_processed = preprocess_dataset(tokenizer, max_length, seed, train_dataset)
val_dataset_processed = preprocess_dataset(tokenizer, max_length, seed, val_dataset)
output_dir = "results/llama2/final_checkpoint"
train(model, tokenizer, train_dataset_processed, val_dataset_processed, output_dir)