-
Notifications
You must be signed in to change notification settings - Fork 10
/
finetune.py
148 lines (125 loc) · 4.8 KB
/
finetune.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
import os
import torch.nn as nn
import argparse
import torch
from torch.utils.data import DataLoader
from torch import optim
import tqdm
import numpy as np
from torch.cuda.amp import autocast
import peft
import loralib as lora
from llama import LlamaTokenizer, LlamaForCausalLM
from peft import (
prepare_model_for_int8_training,
LoraConfig,
get_peft_model,
get_peft_model_state_dict,
)
from peft.tuners import lora
from utils import ImageTextDataSet
from model import MultimodalLlamaLLM
special_tokens_dict = {'additional_special_tokens': ['[boi]','[eoi]', '[quest]', '[ans]']}
def parse_args():
parser = argparse.ArgumentParser(description="Instructing tuning a multimodal llama model")
parser.add_argument(
"--train_file", type=str, default='train.pkl', help="A pkl file containing the training data."
)
parser.add_argument(
"--model_name_or_path",
type=str,
default='./ckpt',
help="Path to pretrained model or model identifier from huggingface.co/models.",
)
parser.add_argument(
"--resume_path",
type=str,
default='out.pt',
help="Path to ckpt.",
)
parser.add_argument("--output_dir", type=str, default='out', help="Where to store the final model.")
parser.add_argument(
"--image_length",
type=int,
default=10,
)
parser.add_argument(
"--per_device_train_batch_size",
type=int,
default=1,
help="Batch size (per device) for the training dataloader.",
)
parser.add_argument(
"--gradient_accumulation_steps",
type=int,
default=8,
help="Number of updates steps to accumulate before performing a backward/update pass.",
)
parser.add_argument("--num_train_epochs", type=int, default=10, help="Total number of training epochs to perform.")
parser.add_argument(
"--lr",
type=float,
default=4e-3,
help="Initial learning rate (after the potential warmup period) to use.",
)
args = parser.parse_args()
return args
def save_tunable_parameters(model, path):
saved_params = {
k: v.to("cpu")
for k, v in model.named_parameters()
if v.requires_grad
}
torch.save(saved_params, path)
def main():
args = parse_args()
print(args)
device = 'cuda'
tokenizer = LlamaTokenizer.from_pretrained(args.model_name_or_path)
num_added_tokens = tokenizer.add_special_tokens(special_tokens_dict)
llama_model = LlamaForCausalLM.from_pretrained(args.model_name_or_path)
model = MultimodalLlamaLLM(image_length=args.image_length, llama=llama_model,)
if args.resume_path is not None:
model.llm.resize_token_embeddings(len(tokenizer) - 2)
model.load_state_dict(torch.load(args.resume_path))
model.llm.resize_token_embeddings(len(tokenizer))
config = LoraConfig(
r=8,
lora_alpha=16,
lora_dropout=0.05,
target_modules=['q_proj', 'v_proj'],
bias='none',
task_type='CAUSAL_LM'
)
model.llm = get_peft_model(model.llm, config)
model_parameters = filter(lambda p: p.requires_grad, model.parameters())
trainable_params = sum([np.prod(p.size()) for p in model_parameters])
model_parameters = filter(lambda p: not p.requires_grad, model.parameters())
non_trainable_params = sum([np.prod(p.size()) for p in model_parameters])
print('trainable_params:{} ({:.2f}%)'.format(trainable_params, trainable_params/non_trainable_params*100,))
train_dataset = ImageTextDataSet(args.train_file, tokenizer=tokenizer, image_length=args.image_length)
train_loader = DataLoader(train_dataset, batch_size=args.per_device_train_batch_size)
optimizer = torch.optim.AdamW(model.llm.parameters(), lr=args.lr)
model.to(device)
model.llm.train()
print(model.llm.model.model)
for param in model.llm.model.model.named_parameters():
print(param[0])
for epoch in range(args.num_train_epochs):
total_loss = 0
for step, batch in enumerate(t:=tqdm.tqdm(train_loader)):
image_embedding, tokens, mask = batch
image_embedding, tokens, mask = image_embedding.to(device), tokens.to(device), mask.to(device)
outputs = model(tokens=tokens, labels=tokens, image_embedding=image_embedding, mask=mask)
loss_d = outputs.loss.detach().float()
t.set_description(f"loss: {loss_d}")
total_loss += loss_d
loss = outputs.loss / args.gradient_accumulation_steps
loss.backward()
if (step+1) % args.gradient_accumulation_steps == 0:
optimizer.step()
optimizer.zero_grad()
print('save modeling')
save_tunable_parameters(model, "adapter_model.bin")
if __name__ == "__main__":
main()