-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathllm_fo_fine_tune_main.py
137 lines (112 loc) · 4.33 KB
/
llm_fo_fine_tune_main.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
import torch
from tqdm import tqdm
from cezo_fl.util.metrics import Metric
from cezo_fl.fl_helpers import get_server_name
from experiment_helper import prepare_settings
from experiment_helper.cli_parser import (
GeneralSetting,
DeviceSetting,
DataSetting,
ModelSetting,
OptimizerSetting,
NormalTrainingLoopSetting,
)
from experiment_helper.data import get_dataloaders
from experiment_helper.device import use_device
class CliSetting(
GeneralSetting,
DeviceSetting,
DataSetting,
ModelSetting,
OptimizerSetting,
NormalTrainingLoopSetting,
):
"""
This is a replacement for regular argparse module.
We used a third party library pydantic_setting to make command line interface easier to manage.
Example:
if __name__ == "__main__":
args = CliSetting()
args will have all parameters defined by all components.
"""
pass
if __name__ == "__main__":
args = CliSetting()
device_map = use_device(args.device_setting, 1)
train_loaders, test_loader = get_dataloaders(
args.data_setting, 1, args.seed, hf_model_name=args.get_hf_model_name()
)
device = device_map[get_server_name()]
def inf_loader(dl):
while True:
for v in dl:
yield v
inf_test_loader = inf_loader(test_loader)
(_, metrics) = prepare_settings.get_model_inferences_and_metrics(
args.dataset, args.model_setting
)
model = prepare_settings.get_model(args.dataset, args.model_setting, args.seed)
model.to(device)
optimizer = prepare_settings.get_optimizer(model, args.dataset, args.optimizer_setting)
acc = Metric("accuracy")
model.eval()
with torch.no_grad():
for batch_input_dict, batch_output_tensor in test_loader:
batch_input_dict = batch_input_dict.to("cuda")
batch_output_tensor = batch_output_tensor.to("cuda")
# Forward pass to get logits
outputs = model(
input_ids=batch_input_dict.input_ids, attention_mask=batch_input_dict.attention_mask
)
batch_acc = metrics.test_acc(outputs, batch_output_tensor)
acc.update(batch_acc)
del batch_input_dict, batch_output_tensor, outputs, batch_acc
torch.cuda.empty_cache()
print(f"Start, Accuracy: {acc.avg:.4f}")
num_epochs = 20
train_loader = train_loaders[0]
model.train()
total_loss = 0.0
inf_train_loader = inf_loader(train_loader)
eval_iterations = 200
train_losses = []
eval_accs = []
for i in tqdm(range(10000)):
batch_input_dict, batch_output_tensor = next(inf_train_loader)
batch_input_dict = batch_input_dict.to("cuda")
batch_output_tensor = batch_output_tensor.to("cuda")
optimizer.zero_grad()
# Forward pass to get logits
outputs = model(
input_ids=batch_input_dict.input_ids, attention_mask=batch_input_dict.attention_mask
)
# Calculate the loss
loss = metrics.train_loss(outputs, batch_output_tensor)
total_loss += loss.item()
if (i + 1) % 50 == 0:
print(f"Iteration: {i}, Loss: {(total_loss/50):.6f}")
train_losses += [(i, total_loss / 50)]
total_loss = 0.0
# Backward pass and optimization step
loss.backward()
optimizer.step()
# Print average loss for the epoch
average_loss = total_loss / len(train_loader)
if (i + 1) % eval_iterations == 0:
acc = Metric("accuracy")
model.eval()
with torch.no_grad():
for batch_input_dict, batch_output_tensor in test_loader:
batch_input_dict = batch_input_dict.to("cuda")
batch_output_tensor = batch_output_tensor.to("cuda")
# Forward pass to get logits
outputs = model(
input_ids=batch_input_dict.input_ids,
attention_mask=batch_input_dict.attention_mask,
)
batch_acc = metrics.test_acc(outputs, batch_output_tensor)
acc.update(batch_acc)
del batch_input_dict, batch_output_tensor, outputs, batch_acc
torch.cuda.empty_cache()
print(f"Iteration: {i}, Accuracy: {acc.avg:.4f}")
eval_accs += [(i, acc.avg)]