forked from Lightning-AI/litgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinetune_adapter_v2.py
244 lines (191 loc) · 8.48 KB
/
finetune_adapter_v2.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
import shutil
import time
from pathlib import Path
import os
from typing import Literal
import lightning as L
import numpy as np
import torch
import torch.nn as nn
from lightning.fabric.strategies import DeepSpeedStrategy
from lightning.fabric.accelerators.mps import MPSAccelerator
from generate import generate
from lit_parrot.adapter import Parrot, Config
from lit_parrot.adapter_v2 import (mark_only_adapter_v2_as_trainable,
add_adapter_v2_parameters_to_linear_layers,
adapter_v2_state_from_state_dict)
from lit_parrot.tokenizer import Tokenizer
from lit_parrot.utils import EmptyInitOnDevice, lazy_load, check_valid_checkpoint_dir
from scripts.prepare_alpaca import generate_prompt
eval_interval = 600
save_interval = 1000
eval_iters = 100
log_interval = 1
devices = 1
# Hyperparameters
learning_rate = 9e-3
batch_size = 128 / devices
micro_batch_size = 2 #set to 2 because this is fit into 12GB Vram
gradient_accumulation_iters = batch_size // micro_batch_size
assert gradient_accumulation_iters > 0
epoch_size = 50000 # train dataset size
num_epochs = 5
max_iters = num_epochs * (epoch_size // micro_batch_size) // devices
weight_decay = 0.02
max_seq_length = 256 # see scripts/prepare_alpaca.py
warmup_iters = 2 * (epoch_size // micro_batch_size) // devices # 2 epochs
ds_config = {
"train_micro_batch_size_per_gpu": micro_batch_size,
"gradient_accumulation_steps": gradient_accumulation_iters,
"zero_optimization": {"stage": 2},
}
def main(
data_dir: Path = Path("data/alpaca"),
checkpoint_dir: Path = Path("checkpoints/stabilityai/stablelm-base-alpha-3b"),
out_dir: Path = Path("out/adapter_v2/alpaca"),
accelerator = "cuda",
precision: Literal["bf16-mixed", "32-true"] = "bf16-mixed",
):
check_valid_checkpoint_dir(checkpoint_dir)
fabric = L.Fabric(
accelerator=accelerator,
devices=devices,
strategy=(DeepSpeedStrategy(config=ds_config) if devices > 1 else "auto"),
precision=precision,
)
fabric.launch()
fabric.seed_everything(1337 + fabric.global_rank)
if fabric.global_rank == 0:
os.makedirs(out_dir, exist_ok=True)
train_data, val_data = load_datasets(data_dir=data_dir)
config = Config.from_name(name=checkpoint_dir.name, block_size=max_seq_length)
with EmptyInitOnDevice(
device=fabric.device,
dtype=torch.float32 if fabric.strategy.precision.precision == "32-true" else torch.bfloat16
):
with lazy_load(checkpoint_dir / "lit_model.pth") as checkpoint:
model.load_state_dict(checkpoint, strict=False)
add_adapter_v2_parameters_to_linear_layers(model)
mark_only_adapter_v2_as_trainable(model)
num_params = sum([p.numel() for p in model.parameters() if p.requires_grad])
print(f"Number of trainable parameters: {num_params}")
optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate, weight_decay=weight_decay)
model, optimizer = fabric.setup(model, optimizer)
train(fabric, model, optimizer, train_data, val_data, checkpoint_dir, out_dir)
# Save the final checkpoint at the end of training
save_model_checkpoint(fabric, model, out_dir / "lit_model_adapter_finetuned.pth")
def train(
fabric: L.Fabric,
model: torch.nn.Module,
optimizer: torch.optim.Optimizer,
train_data: np.ndarray,
val_data: np.ndarray,
checkpoint_dir: Path,
out_dir: Path,
) -> None:
"""The training loop.
Loosely based on the nanoGPT implementation: https://github.com/karpathy/nanoGPT.
"""
step_count = 0
tokenizer = Tokenizer(checkpoint_dir / "tokenizer.json", checkpoint_dir / "tokenizer_config.json")
for iter_num in range(max_iters):
if step_count <= warmup_iters:
# linear warmup
lr = learning_rate * step_count / warmup_iters
for param_group in optimizer.param_groups:
param_group["lr"] = lr
t0 = time.time()
input_ids, targets = get_batch(fabric, train_data)
logits = model(input_ids)
loss = loss_fn(logits, targets)
with fabric.no_backward_sync(model, enabled=((iter_num + 1) % gradient_accumulation_iters != 0)):
fabric.backward(loss / gradient_accumulation_iters)
if (iter_num + 1) % gradient_accumulation_steps == 0:
optimizer.step()
optimizer.zero_grad()
step_count += 1
if step_count % eval_interval == 0:
val_loss = validate(fabric, model, val_data, tokenizer)
fabric.print(f"step {iter_num}: val loss {val_loss:.4f}")
fabric.barrier()
if step_count % save_interval == 0:
save_path = out_dir / f"iter-{iter_num:06d}.pth"
print(f"Saving adapter weights to {str(save_path)!r}")
# TODO: Provide a function/script to merge the adapter weights with pretrained weights
save_model_checkpoint(fabric, model, save_path)
dt = time.time() - t0
if iter_num % log_interval == 0:
fabric.print(f"iter {iter_num}: loss {loss.item():.4f}, time: {dt*1000:.2f}ms")
@torch.no_grad()
def validate(fabric: L.Fabric, model: torch.nn.Module, val_data: np.ndarray, tokenizer: Tokenizer) -> torch.Tensor:
fabric.print("Validating ...")
model.eval()
losses = torch.zeros(eval_iters)
for k in range(eval_iters):
input_ids, targets = get_batch(fabric, val_data)
logits = model(input_ids)
loss = loss_fn(logits, targets)
losses[k] = loss.item()
val_loss = losses.mean()
# produce an example:
instruction = "Recommend a movie for me to watch during the weekend and explain the reason."
fabric.print(instruction)
sample = {"instruction": instruction, "input": ""}
prompt = generate_prompt(sample)
encoded = tokenizer.encode(prompt, device=model.device)
output = generate(model, idx=encoded, max_seq_length=max_seq_length, max_new_tokens=100, temperature=0.8)
output = tokenizer.decode(output)
fabric.print(output)
model.train()
return val_loss.item()
def loss_fn(logits, targets):
# shift the targets such that output n predicts token n+1
logits = logits[..., :-1, :].contiguous()
targets = targets[..., 1:].contiguous()
loss = torch.nn.functional.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-1)
return loss
def get_batch(fabric: L.Fabric, data: list):
ix = torch.randint(len(data), (micro_batch_size,))
input_ids = [data[i]["input_ids"].type(torch.int64) for i in ix]
labels = [data[i]["labels"].type(torch.int64) for i in ix]
max_len = max(len(s) for s in input_ids)
def pad_right(x, pad_id):
# pad right based on the longest sequence
n = max_len - len(x)
return torch.cat((x, torch.full((n,), pad_id, dtype=x.dtype)))
x = torch.stack([pad_right(x, pad_id=0) for x in input_ids])
y = torch.stack([pad_right(x, pad_id=-1) for x in labels])
if isinstance(fabric.accelerator, MPSAccelerator):
x, y = fabric.to_device((x, y))
else:
x, y = fabric.to_device((x.pin_memory(), y.pin_memory()))
return x, y
def load_datasets(data_dir: Path):
train_data = torch.load(data_dir / "train.pt")
val_data = torch.load(data_dir / "test.pt")
return train_data, val_data
def save_model_checkpoint(fabric, model, file_path: Path):
file_path = Path(file_path)
if isinstance(fabric.strategy, DeepSpeedStrategy):
from deepspeed.utils.zero_to_fp32 import get_fp32_state_dict_from_zero_checkpoint
tmp_path = file_path.with_suffix(".tmp")
fabric.save(tmp_path, {"model": model})
fabric.barrier()
if fabric.global_rank == 0:
# Create a consolidated checkpoint with the same name next to the deepspeed checkpoint
# and only keep the adapter weights
state_dict = get_fp32_state_dict_from_zero_checkpoint(tmp_path)
state_dict = adapter_v2_state_from_state_dict(state_dict)
torch.save(state_dict, file_path)
shutil.rmtree(tmp_path)
else:
state_dict = adapter_state_v2_from_state_dict(model.state_dict())
if fabric.global_rank == 0:
torch.save(state_dict, file_path)
fabric.barrier()
if __name__ == "__main__":
# Uncomment this line if you see an error: "Expected is_sm80 to be true, but got false"
# torch.backends.cuda.enable_flash_sdp(False)
torch.set_float32_matmul_precision("high")
from jsonargparse.cli import CLI
CLI(main)