-
Notifications
You must be signed in to change notification settings - Fork 26
/
distributed_train.py
291 lines (246 loc) · 9.95 KB
/
distributed_train.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
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
import argparse
import os
from functools import partial
import friendlywords as fw
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.utils.data import DataLoader
from torch.utils.data.distributed import DistributedSampler
from tqdm import tqdm
from transformers import (AdamW, AutoModelForCausalLM, AutoProcessor,
get_scheduler)
import wandb
from data import DocVQADataset, TheCauldronDataset, VQAInstructDataset
from peft import LoraConfig, get_peft_model
def setup(rank, world_size):
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "12355"
dist.init_process_group("nccl", rank=rank, world_size=world_size)
torch.cuda.set_device(rank)
def cleanup():
dist.destroy_process_group()
def collate_fn(batch, processor, device):
questions, answers, images = zip(*batch)
inputs = processor(
text=list(questions), images=list(images), return_tensors="pt", padding=True, truncation=True, max_length=800
).to(device)
return inputs, answers
def create_data_loaders(
train_dataset,
val_datasets,
batch_size,
num_workers,
rank,
world_size,
processor,
device,
):
train_sampler = DistributedSampler(
train_dataset, num_replicas=world_size, rank=rank
)
train_loader = DataLoader(
train_dataset,
batch_size=batch_size,
collate_fn=partial(collate_fn, processor=processor, device=device),
num_workers=num_workers,
sampler=train_sampler,
)
val_loaders = {}
for name, val_dataset in val_datasets.items():
val_sampler = DistributedSampler(val_dataset, num_replicas=world_size, rank=rank)
val_loader = DataLoader(
val_dataset,
batch_size=batch_size//2,
collate_fn=partial(collate_fn, processor=processor, device=device),
num_workers=num_workers,
sampler=val_sampler,
)
val_loaders[name] = val_loader
return train_loader, val_loaders
def evaluate_model(rank, world_size, model, val_loaders, device, train_loss, processor, global_step, batch_size, max_val_item_count):
if rank == 0:
avg_train_loss = train_loss / (global_step*batch_size*world_size)
wandb.log({"step": global_step, "train_loss": avg_train_loss})
print(f"Rank {rank} - Average Training Loss: {avg_train_loss}")
# Evaluation phase
model.eval()
for val_name, val_loader in val_loaders.items():
val_loss = 0
with torch.no_grad():
val_item_count = 0
for batch in tqdm(val_loader, desc=f"Evaluation on {val_name} at step {global_step}", position=rank):
val_item_count += len(batch)
inputs, answers = batch
# Prepare the input and target tensors
input_ids = inputs["input_ids"].to(device)
pixel_values = inputs["pixel_values"].to(device)
labels = processor.tokenizer(
text=answers,
return_tensors="pt",
padding=True,
return_token_type_ids=False,
truncation=True,
max_length=800,
).input_ids.to(device)
outputs = model(
input_ids=input_ids, pixel_values=pixel_values, labels=labels
)
loss = outputs.loss
val_loss += loss.item()
if val_item_count > max_val_item_count:
break
avg_val_loss = val_loss / val_item_count
print(f"Rank {rank} - Step {global_step} - Average Validation Loss ({val_name}): {avg_val_loss}")
# Log metrics to wandb
if rank == 0:
wandb.log({f"{val_name}_val_loss": avg_val_loss, "step": global_step})
model.train()
def train_model(rank, world_size, dataset_name, batch_size=6, use_lora=False, epochs=10, lr=1e-6, eval_steps=10, run_name=None, max_val_item_count=1000):
setup(rank, world_size)
device = torch.device(f"cuda:{rank}")
if run_name is None:
run_name = fw.generate(2, separator="_")
# Initialize wandb
if rank == 0: # Only initialize wandb in the main process
wandb.init(project="DocVQA-instruct", name=run_name)
wandb.config.update({
"dataset": dataset_name,
"batch_size": batch_size,
"use_lora": use_lora,
"epochs": epochs,
"learning_rate": lr,
"eval_steps": eval_steps,
"world_size": world_size,
})
# Load the dataset based on the dataset_name argument
if dataset_name == "docvqa":
train_dataset = DocVQADataset(split='train')
val_datasets = {"docvqa": DocVQADataset(split='validation')}
elif dataset_name == "cauldron":
train_dataset = TheCauldronDataset(split='train')
val_datasets = {
"cauldron": TheCauldronDataset(split='validation'),
"docvqa": DocVQADataset(split='validation')
}
elif dataset_name == 'vqainstruct':
train_dataset = VQAInstructDataset(split='train')
val_datasets = {
"vqainstruct": VQAInstructDataset(split='validation'),
"docvqa": DocVQADataset(split='validation')
}
else:
raise ValueError(f"Unknown dataset: {dataset_name}")
# Load the model and processor
model = AutoModelForCausalLM.from_pretrained(
"andito/Florence-2-large-ft", trust_remote_code=True
).to(device)
processor = AutoProcessor.from_pretrained(
"andito/Florence-2-large-ft", trust_remote_code=True
)
if use_lora:
TARGET_MODULES = [
"q_proj", "o_proj", "k_proj", "v_proj",
"linear", "Conv2d", "lm_head", "fc2"
]
config = LoraConfig(
r=8,
lora_alpha=8,
target_modules=TARGET_MODULES,
task_type="CAUSAL_LM",
lora_dropout=0.05,
bias="none",
inference_mode=False,
use_rslora=True,
init_lora_weights="gaussian",
)
model = get_peft_model(model, config)
model = DDP(model, device_ids=[rank])
# Create DataLoaders
num_workers = 0
train_loader, val_loaders = create_data_loaders(
train_dataset,
val_datasets,
batch_size,
num_workers,
rank,
world_size,
processor,
device,
)
optimizer = AdamW(model.parameters(), lr=lr)
num_training_steps = epochs * len(train_loader)
lr_scheduler = get_scheduler(
name="linear",
optimizer=optimizer,
num_warmup_steps=0,
num_training_steps=num_training_steps,
)
global_step = 0
for epoch in range(epochs):
# Training phase
model.train()
train_loss = 0
for batch in tqdm(
train_loader, desc=f"Training Epoch {epoch + 1}/{epochs}", position=rank
):
inputs, answers = batch
# Prepare the input and target tensors
input_ids = inputs["input_ids"].to(device)
pixel_values = inputs["pixel_values"].to(device)
labels = processor.tokenizer(
text=answers,
return_tensors="pt",
padding=True,
return_token_type_ids=False,
truncation=True,
max_length=800,
).input_ids.to(device)
outputs = model(
input_ids=input_ids, pixel_values=pixel_values, labels=labels
)
loss = outputs.loss
loss.backward()
optimizer.step()
lr_scheduler.step()
optimizer.zero_grad()
train_loss += loss.item()
global_step += 1
if global_step % eval_steps == 0:
evaluate_model(rank, world_size, model, val_loaders, device, train_loss, processor, global_step, batch_size, max_val_item_count)
evaluate_model(rank, world_size, model, val_loaders, device, train_loss, processor, global_step, batch_size, max_val_item_count)
# Log training loss to wandb
avg_train_loss = train_loss / len(train_loader)
if rank == 0:
wandb.log({"epoch": epoch + 1, "epoch_train_loss": avg_train_loss})
# Save model checkpoint
if rank == 0: # Only the main process saves the checkpoint
output_dir = f"./model_checkpoints/{run_name}/epoch_{epoch + 1}"
os.makedirs(output_dir, exist_ok=True)
model.module.save_pretrained(output_dir)
processor.save_pretrained(output_dir)
# Finish the wandb run
if rank == 0:
wandb.finish()
cleanup()
def main():
parser = argparse.ArgumentParser(description="Train Florence-2 model on specified dataset")
parser.add_argument("--dataset", type=str, required=True, choices=["docvqa", "cauldron", "vqainstruct"], help="Dataset to train on")
parser.add_argument("--batch-size", type=int, default=6, help="Batch size for training")
parser.add_argument("--use-lora", action='store_true', help="Use LoRA if this flag is passed")
parser.add_argument("--epochs", type=int, default=10, help="Number of epochs to train for")
parser.add_argument("--lr", type=float, default=1e-6, help="Learning rate")
parser.add_argument("--eval-steps", type=int, default=1000, help="Number of steps between evaluations")
parser.add_argument("--run-name", type=str, default=None, help="Run name for wandb")
parser.add_argument("--max-val-item-count", type=int, default=1000, help="Maximum number of items to evaluate on during validation")
args = parser.parse_args()
world_size = torch.cuda.device_count()
mp.spawn(
train_model,
args=(world_size, args.dataset, args.batch_size, args.use_lora, args.epochs, args.lr, args.eval_steps, args.run_name, args.max_val_item_count),
nprocs=world_size,
join=True
)
if __name__ == "__main__":
main()