-
Notifications
You must be signed in to change notification settings - Fork 113
/
finetune_llama.py
264 lines (206 loc) · 9.91 KB
/
finetune_llama.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
from transformers import AutoTokenizer, AutoModelForCausalLM, Trainer, TrainingArguments
import torch
from datasets import load_dataset
import argparse
import os
from peft import PeftModel
from peft import get_peft_model, LoraConfig, TaskType
# Ensure bitsandbytes is available for 8-bit quantization
# import bitsandbytes as bnb
from sklearn.metrics import roc_auc_score, log_loss, accuracy_score
from torch.nn import BCEWithLogitsLoss
from transformers import DataCollatorWithPadding
from datasets import concatenate_datasets
parser = argparse.ArgumentParser()
parser.add_argument("--per_device_train_batch_size", type=int, default=4)
parser.add_argument("--per_device_eval_batch_size", type=int, default=4)
parser.add_argument("--total_batch_size", type=int, default=256)
parser.add_argument("--learning_rate", type=float, default=1e-4)
args = parser.parse_args()
good_token = '+'
bad_token = '-'
step_tag = '\n\n\n\n\n' #ки
step_tag2 = '\n\n'
model_path = "../../models/Llama-3.2-3B-Instruct"
# tokenizer = AutoTokenizer.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(
model_path,
add_eos_token=False,
)
print(tokenizer.encode('a ки b'))
print(tokenizer.encode('a b'))
print(tokenizer.encode('a \n\n b'))
print(tokenizer.encode('a b'))
print(tokenizer.encode('a \n\n\n\n\n b'))
print(tokenizer.encode('a b'))
print(tokenizer.encode('a \n\n\n\n\n\n\n b'))
print(tokenizer.encode('a b'))
print(tokenizer.encode('a \n\n\n\n\n\n\n\n b'))
print(tokenizer.encode('a b'))
print(tokenizer.encode('a + b'))
print(tokenizer.encode('a b'))
print(tokenizer.encode('a - b'))
print(tokenizer.encode('a b'))
print(tokenizer.encode(' + -'))
print(tokenizer.encode('+-'))
# if USE_8bit is True:
# model = prepare_model_for_int8_training(model)
print(tokenizer.eos_token_id)
tokenizer.pad_token_id = 0 # unk. we want this to be different from the eos token
tokenizer.padding_side = "left" # Allow batched inference
# tokenizer = AutoTokenizer.from_pretrained('peiyi9979/math-shepherd-mistral-7b-prm')
candidate_tokens = tokenizer.encode(f" {good_token} {bad_token}")[1:] # [489, 482]
print(candidate_tokens)
step_tag_id = tokenizer.encode(f" {step_tag}")[-1] # 77425
print(step_tag_id)
print('step_tag_id:',tokenizer.encode(f" {step_tag}"))
print('step_tag_id2:',tokenizer.encode(f"{step_tag2}"))
# model = AutoModelForCausalLM.from_pretrained('peiyi9979/math-shepherd-mistral-7b-prm').eval()
# model = AutoModelForCausalLM.from_pretrained(model_path).eval()
model = AutoModelForCausalLM.from_pretrained(
model_path,
# load_in_8bit=True, # Enables 8-bit quantization
# device_map="auto", # Automatically assigns the model to available GPUs/CPUs
torch_dtype=torch.bfloat16, # Mixed precision for faster inference
attn_implementation="flash_attention_2",
)
# for name,param in model.named_parameters():
# print(name)
print(model)
lora_config = LoraConfig(
task_type=TaskType.CAUSAL_LM, # LoRA for causal language modeling task
r=8, # Rank of LoRA
lora_alpha=32, # Alpha scaling factor for LoRA
lora_dropout=0.1, # Dropout rate for LoRA layers
target_modules=["q_proj", "v_proj"], # Apply LoRA to specific layers
)
model = get_peft_model(model, lora_config)
# model.to('cuda:0')
print(model.device)
question = "Janet\u2019s ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes muffins for her friends every day with four. She sells the remainder at the farmers' market daily for $2 per fresh duck egg. How much in dollars does she make every day at the farmers' market?"
output1 = "Step 1: Janet's ducks lay 16 eggs per day. ки\nStep 2: She eats three for breakfast every morning, so she has 16 - 3 = 13 eggs left. ки\nStep 3: She bakes muffins for her friends every day with four eggs, so she has 13 - 4 = 9 eggs left. ки\nStep 4: She sells the remainder at the farmers' market daily for $2 per fresh duck egg, so she makes 9 * $2 = $18 every day at the farmers' market. The answer is: 18 ки" # 18 is right
output2 = "Step 1: Janet's ducks lay 16 eggs per day. ки\nStep 2: She eats three for breakfast every morning, so she has 16 - 3 = 13 eggs left. ки\nStep 3: She bakes muffins for her friends every day with four eggs, so she has 13 - 4 = 9 eggs left. ки\nStep 4: She sells the remainder at the farmers' market daily for $2 per fresh duck egg, so she makes 9 * $2 = $17 every day at the farmers' market. The answer is: 17 ки" # 17 is wrong
def preprocess_function(example):
input = f"{example['question']} {example['process']}"
tokenized_inputs = tokenizer(
input,
truncation=True,
padding='max_length',
# padding=True,
max_length=2048,
)
def find_all_indices(lst, element):
return [i for i, x in enumerate(lst) if x == element]
length = len(tokenized_inputs['input_ids'])
# print(length)
indices = find_all_indices(tokenized_inputs['input_ids'],step_tag_id)
if len(indices) != len(example['label']):
# print(example)
example['label'] = example['label'][:len(indices)]
assert len(indices) == len(example['label'])
tokenized_inputs['labels'] = [-100] * length
# tokenized_inputs['attention_mask'] = [1] *length
# print(len(indices))
for i in range(len(indices)):
if example['label'][i] == '+' or example['label'][i] == 1:
tokenized_inputs['labels'][indices[i]] = candidate_tokens[0]
elif example['label'][i] == '-' or example['label'][i] == 0:
tokenized_inputs['labels'][indices[i]] = candidate_tokens[1]
else:
raise ValueError('label is wrong')
tokenized_inputs['attention_mask'][indices[i]] = 0
# tokenized_inputs['labels'] = [-100] *(length-1) + tokenized_inputs['input_ids'][length-1:]
return tokenized_inputs
DATA_PATH = {
# "train": 'multi-step.json',
# 'train': 'test.json',
"test": '../../datasets/processed_data/prm800k_test.json',
"train": "../../datasets/processed_data/math_aps.json",
# "train": "../../datasets/processed_data/prm800k/data/phase2_train_new.jsonl",
# "test": "../../datasets/prm800k-main/prm800k/data/phase2_test_new.jsonl",
}
dataset2 = load_dataset('json',data_files="../../datasets/processed_data/prm800k_train.json")
dataset = load_dataset('json', data_files=DATA_PATH)
# print(dataset['train'][1000:1002])
dataset['train'] = concatenate_datasets([dataset['train'], dataset2['train']])
# dataset['train'] = dataset['train'].select(range(10000))
print('start processing')
tokenized_datasets = dataset.map(preprocess_function)
tokenized_datasets['train'] = tokenized_datasets['train'].remove_columns(['question','process','label'])
tokenized_datasets['test'] = tokenized_datasets['test'].remove_columns(['question','process','label'])
print(tokenized_datasets['train'])
print('dataset processed')
# print(tokenized_datasets['train']['input_ids'])
# print(len(tokenized_datasets['train']['input_ids'][0]))
# Data collator for padding inputs dynamically
data_collator = DataCollatorWithPadding(tokenizer)
BATCH_SIZE = args.total_batch_size
GRADIENT_ACCUMULATION_STEPS = BATCH_SIZE // args.per_device_train_batch_size
world_size = int(os.environ.get("WORLD_SIZE", 1))
ddp = world_size != 1
if ddp:
GRADIENT_ACCUMULATION_STEPS = GRADIENT_ACCUMULATION_STEPS // world_size
print(world_size)
print(ddp)
fp = f'bs_{args.total_batch_size}_lr_{args.learning_rate}'
output_path = f'./prm_llama/{fp}'
# Training arguments
training_args = TrainingArguments(
output_dir=output_path,
evaluation_strategy="no", # Evaluate at the end of each epoch
learning_rate=args.learning_rate,
per_device_train_batch_size=args.per_device_train_batch_size,
per_device_eval_batch_size=args.per_device_eval_batch_size,
gradient_accumulation_steps=GRADIENT_ACCUMULATION_STEPS,
num_train_epochs=3,
weight_decay=0.01,
logging_dir="./logs",
logging_steps=10,
save_strategy="epoch",
bf16=True, # Enable mixed precision for better performance on supported hardware
report_to="none", # Set to "wandb" if you are using Weights and Biases for logging
dataloader_num_workers=4,
deepspeed=None,
ddp_find_unused_parameters=False,
)
# Define a custom metric function (e.g., accuracy for binary classification)
def compute_metrics(eval_pred):
# pass
# print(eval_pred)
print('bb')
pre, labels = eval_pred
auc = roc_auc_score(pre[1], pre[0])
ll = log_loss(pre[1], pre[0])
acc = accuracy_score(pre[1], pre[0] > 0.5)
result ={
'auc': auc,
'll': ll,
'acc': acc,
}
print(result)
return result
def preprocess_logits_for_metrics(logits,labels):
print('aa')
# return logits,labels
labels_index = torch.argwhere(torch.bitwise_or(labels == candidate_tokens[0], labels == candidate_tokens[1]))
gold = torch.where(labels[labels_index[:, 0], labels_index[:, 1]] == candidate_tokens[1], 0, 1)
# labels_index[: , 1] = labels_index[: , 1] - 1
logits = logits[labels_index[:, 0], labels_index[:, 1]][:, [candidate_tokens[1], candidate_tokens[0]]]
prob = torch.softmax(logits, dim=-1)
return prob[:, 1], gold
# Initialize the Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_datasets['train'],
eval_dataset=tokenized_datasets['test'], # Replace with a validation set if available
data_collator=data_collator,
tokenizer=tokenizer,
preprocess_logits_for_metrics=preprocess_logits_for_metrics,
compute_metrics=compute_metrics,
)
trainer.train()
# trainer.evaluate()
# Save the fine-tuned model and tokenizer
model.save_pretrained('./fine_tuned_llama_1b_mix_lora_16bit')
tokenizer.save_pretrained('./fine_tuned_llama_1b_mix_lora_16bit')