forked from lessw2020/t5_11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_aot.py
585 lines (458 loc) · 16.7 KB
/
main_aot.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# main hq file for t5 training and prediction
import os
import argparse
from datasets_grammar.grammar_dataset import grammar
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
# from torchvision import datasets, transforms
import torchdynamo
from torchdynamo.optimizations.training import aot_autograd_speedup_strategy
# for grammar correction
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# for generation
from transformers import T5Tokenizer, T5ForConditionalGeneration
from transformers import DataCollatorForSeq2Seq
import functools
from torch.optim.lr_scheduler import StepLR
import torch.nn.functional as F
import torch.distributed as dist
import torch.multiprocessing as mp
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.utils.data.distributed import DistributedSampler
from torch.distributed.fsdp import (
FullyShardedDataParallel as FSDP,
CPUOffload,
MixedPrecision,
BackwardPrefetch,
ShardingStrategy,
FullStateDictConfig,
StateDictType,
)
from torch.distributed.fsdp.wrap import (
default_auto_wrap_policy,
enable_wrap,
wrap,
)
from policies import mixed_precision
from datasets import load_dataset, load_metric
from torch.utils.data import DataLoader
from pathlib import Path
from torch.utils.data import DataLoader
# from nlp import load_metric
# from nlp import load_dataset
from sklearn.model_selection import train_test_split
import time
# functorch
import functorch
from functorch.compile import memory_efficient_fusion
# local imports
import verify
import policies
import datasets_grammar as dg
import tqdm
# config
import config
# some globals
g_port = "12369"
g_addr = "localhost"
def _is_rank_0():
return 0 == os.getenv("RANK")
def parse_args():
parser = argparse.ArgumentParser(description="PyTorch fsdp T5.11 Example")
parser.add_argument("--save-dir", default="/model_chkpt", type=str)
parser.add_argument(
"--save-model",
action="store_true",
default=False,
help="For Saving the current Model",
)
parser.add_argument(
"--seed", type=int, default=1, metavar="S", help="random seed (default: 2022)"
)
parser.add_argument(
"--epochs",
type=int,
default=1,
metavar="N",
help="number of epochs to train (default: 14)",
)
args = parser.parse_args()
return args
# ---------------- Main functions --------------------
def get_policies(cfg, fsdp_unit_params=1000000):
"""establish current policies for mixed precision and fsdp wrapping"""
mixed_precision_policy = None
wrapping_policy = None
# mixed precision -----
if cfg.use_mixed_precision:
bf16_ready = verify.bf16_ready
if bf16_ready:
mixed_precision_policy = policies.bfSixteen
print(f"bFloat16 enabled for mixed precision - using bfSixteen policy")
else:
mixed_precision_policy = policies.fpSixteen
print(f"bFloat16 support not present. Using fp16 for mixed precision")
# wrapping policy -------
# print(f"**overriding mp to fp16 - remove")
# mixed_precision_policy = policies.fpSixteen
wrapping_policy = policies.get_t5_wrapper(fsdp_unit_params)
return mixed_precision_policy, wrapping_policy
def setup(rank, world_size, cfg):
os.environ["MASTER_ADDR"] = g_addr
os.environ["MASTER_PORT"] = cfg.host_port
# initialize the process group
dist.init_process_group("nccl", rank=rank, world_size=world_size)
def setup_environ_flags():
os.environ["TORCH_SHOW_CPP_STACKTRACES"] = str(1)
def cleanup():
dist.destroy_process_group()
def clear_gpu_cache(rank=None):
print(f"clearing cache for rank {rank}")
torch.cuda.empty_cache()
def setup_tasks(rank, world_size, cfg):
"""keep the basic setup list here"""
setup(rank, world_size, cfg)
# clear_gpu_cache() - need to call torch set device first?
# set_printing()
setup_environ_flags()
# ---------- Training ----------------------------------------------------------
def train(
args,
model,
rank,
world_size,
train_loader,
optimizer,
epoch,
sampler=None,
profiler=None,
):
model.train()
ddp_loss = torch.zeros(2).to(rank)
if sampler:
sampler.set_epoch(epoch)
if rank == 0:
inner_pbar = tqdm.tqdm(
range(len(train_loader)), colour="blue", desc="r0 Training Epoch"
)
for batch in train_loader:
for key in batch.keys():
batch[key] = batch[key].to(rank)
"""print("************************")
print(
"train_loader",
type(batch),
batch["source_ids"].size(),
batch["source_mask"].size(),
batch["target_ids"].size(),
)
print("************************")
"""
optimizer.zero_grad()
output = model(
input_ids=batch["source_ids"],
attention_mask=batch["source_mask"],
labels=batch["target_ids"],
)
# print("##############################")
# print(output.keys())
# print("##############################")
loss = output["loss"]
loss.backward()
optimizer.step()
ddp_loss[0] += loss.item()
ddp_loss[1] += len(batch)
if rank == 0:
inner_pbar.update(1)
if profiler:
profiler.step()
dist.reduce(ddp_loss, 0, op=dist.ReduceOp.SUM)
train_accuracy = ddp_loss[0] / ddp_loss[1]
if rank == 0:
inner_pbar.close()
print(
f"Train Epoch: \t{epoch}, Loss: \t{train_accuracy:.4f}"
) # .format(epoch, train_accuracy))
return train_accuracy
# ---- Validation ---------------
def test(model, rank, world_size, test_loader):
model.eval()
correct = 0
ddp_loss = torch.zeros(3).to(rank)
if rank == 0:
inner_pbar = tqdm.tqdm(
range(len(test_loader)), colour="green", desc="r0 Validation Epoch"
)
with torch.no_grad():
for batch in test_loader:
for key in batch.keys():
batch[key] = batch[key].to(rank)
output = model(
input_ids=batch["source_ids"],
attention_mask=batch["source_mask"],
labels=batch["target_ids"],
)
ddp_loss[0] += output["loss"].item() # sum up batch loss
ddp_loss[1] += len(batch)
if rank == 0:
inner_pbar.update(1)
# pred = output.logits.argmax(
# dim=1, keepdim=True
# ) # get the index of the max log-probability
# ddp_loss[1] += pred.eq(batch["target_ids"].view_as(pred)).sum().item()
# ddp_loss[2] += len(batch)
dist.reduce(ddp_loss, 0, op=dist.ReduceOp.SUM)
val_loss = ddp_loss[0] / ddp_loss[1]
if rank == 0:
# test_loss = ddp_loss[0] / ddp_loss[1]
inner_pbar.close()
print(f"Validation Loss: {val_loss:.4f}")
return val_loss
# ---- fsdp main ------------------------------------------------------------
def fsdp_main(rank, world_size, args):
"""main process within each process"""
cfg = config.train_config() # loads from defaults
if rank == 0:
print(f"--> running with these defaults {cfg}")
# setup_tasks(rank, world_size, cfg)
fsdp_unit_params = cfg.fsdp_unit_size
print(f"sharding with {fsdp_unit_params} min_param_size")
batch_size = cfg.batch_size
if rank == 0:
print(f"\n BatchSize = {batch_size}\n")
test_batch_size = 4
mp_policy, wrapping_policy = get_policies(cfg, fsdp_unit_params)
# temp_train()
# print(f"bailing early...remove")
# return
model_name = cfg.model_name # "google/t5-v1_1-small" # #
save_name = model_name + "-"
printable_model_name = str.replace(model_name, "/", "==")
# t5-base
# google/t5-v1_1-small
# google/t5-v1_1-base
# google/t5-v1_1-large
# google/t5-v1_1-xl #3b
# google/t5-v1_1-xxl #11b
# grammar correction
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
# summarization
# model = T5ForConditionalGeneration.from_pretrained(model_name)
# tokenizer = T5Tokenizer.from_pretrained(model_name)
# dataset_name = "jfleg_train.csv"
if rank == 0:
print(f"--> Training for {model_name}")
total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"\n--> {model_name} has {total_params/1e6} Million params\n")
# print(f"{dataset_name} contains: {dataset.keys()}")
# print("Size of {dataset_name} train dataset: ", dataset["train"].shape)
# print(
# "Size of {dataset_name} Validation dataset: ", dataset["validation"].shape
# )
# ____________ create batch dataset
train_name = None
if cfg.dataset_train:
train_name = cfg.dataset_train
train_dataset = dg.get_dataset(tokenizer, train_name, 512, 512, True)
if 0 == os.getenv("RANK"):
print(len(train_dataset))
print(f"using dataset {train_name}")
# print("bailing")
val_dataset = dg.get_dataset(tokenizer, cfg.dataset_test, 512, 150, True)
if 0 == os.getenv("RANK"):
print(f"--> Validatin set len = {len(val_dataset)}")
print(f"using dataset {cfg.dataset_test}")
sampler1 = DistributedSampler(
train_dataset, rank=rank, num_replicas=world_size, shuffle=True
)
sampler2 = DistributedSampler(val_dataset, rank=rank, num_replicas=world_size)
print(f"batch size = {batch_size}")
train_kwargs = {"batch_size": batch_size, "sampler": sampler1}
test_kwargs = {"batch_size": test_batch_size, "sampler": sampler2}
cuda_kwargs = {"num_workers": 2, "pin_memory": True, "shuffle": False}
train_kwargs.update(cuda_kwargs)
test_kwargs.update(cuda_kwargs)
train_loader = torch.utils.data.DataLoader(train_dataset, **train_kwargs)
test_loader = torch.utils.data.DataLoader(val_dataset, **test_kwargs)
torch.cuda.set_device(rank)
clear_gpu_cache(rank)
# init_start_event = torch.cuda.Event(enable_timing=True)
# init_end_event = torch.cuda.Event(enable_timing=True)
# init_start_event.record()
# model = model.to(rank)
# model = DDP(model)
if cfg.activation_checkpointing:
model.gradient_checkpointing_enable()
print(f"Activation checkpointing enabled\n")
print("mixed precision off!!")
# ddp
model = DDP(model)
model.to(rank)
"""model = FSDP(
model,
auto_wrap_policy=wrapping_policy,
backward_prefetch=BackwardPrefetch.BACKWARD_PRE,
mixed_precision=mp_policy,
).to(rank)
"""
# add in AOT Autograd
# model = memory_efficient_fusion(model)
# print(f"Fused model created on rank {rank}")
# if rank == 0:
# print(f"aot model = {model}")
if rank == 0 and cfg.print_sharding_plan:
print(f"--> Saving sharding plan for the model ")
fn = printable_model_name + "-sharded_layout.txt"
with open(fn, "w") as external_file:
header_text = (
f"model = {model_name}, sharded with {fsdp_unit_params} parameters\n"
)
print(header_text, file=external_file)
total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
milli_params = total_params * 4 / 1e6
print(
f"\n--> {model_name} has {milli_params} Million params\n",
file=external_file,
)
print(f"model wrapping = \n{model}\n\n", file=external_file)
external_file.close()
lr = 0.0008
gamma = 0.7
optimizer = optim.AdamW(model.parameters(), lr=lr)
scheduler = StepLR(optimizer, step_size=1, gamma=gamma)
epochs = cfg.num_epochs
if rank == 0:
print(f"Training for {epochs} epochs")
best_train_accuracy = float("inf")
# --- main training loop - todo, this needs to be modularized
if rank == 0:
dur = []
train_acc_tracking = []
val_acc_tracking = []
training_start_time = time.time()
torch_profiler = None
"""with torch.profiler.profile(
activities=[
torch.profiler.ProfilerActivity.CPU,
torch.profiler.ProfilerActivity.CUDA,
],
schedule=torch.profiler.schedule(wait=1, warmup=2, active=3, repeat=1),
on_trace_ready=torch.profiler.tensorboard_trace_handler(
"fsdp_v100/profile_traces"
),
profile_memory=True,
with_stack=False,
record_shapes=True,
) as torch_profiler:
"""
if rank == 0 and cfg.track_memory:
fn = cfg.model_name + "memory_tracking.txt"
mem_alloc_tracker = []
mem_reserved_tracker = []
with torchdynamo.optimize(aot_autograd_speedup_strategy):
# with torch.jit.fuser("fuser2"):
for epoch in range(1, epochs + 1):
if rank == 0:
print(f"\n--> Starting Epoch {epoch}")
t0 = time.time()
train_accuracy = train(
args,
model,
rank,
world_size,
train_loader,
optimizer,
epoch,
sampler=sampler1,
profiler=torch_profiler,
)
if cfg.run_validation:
test_accuracy = test(model, rank, world_size, test_loader)
scheduler.step()
if rank == 0:
dur.append(time.time() - t0)
train_acc_tracking.append(train_accuracy)
print(f"train_accuracy_type = {train_accuracy}")
if cfg.run_validation:
val_acc_tracking.append(test_accuracy)
if cfg.track_memory:
mem_alloc_tracker.append(torch.cuda.memory_allocated())
mem_reserved_tracker.append(torch.cuda.memory_reserved())
if cfg.save_model:
# assembling model on rank0 and stream it to cpu to avoid OOM
save_policy = FullStateDictConfig(offload_to_cpu=True, rank0_only=True)
with FSDP.state_dict_type(
model, StateDictType.FULL_STATE_DICT, save_policy
):
cpu_state = model.state_dict()
if rank == 0:
print(f"--> saving model ...")
currEpoch = "-" + str(epoch) + "-train.pt"
model_save_name = save_name + currEpoch
torch.save(cpu_state, model_save_name)
print(f"--> saved {model_save_name} to disk")
# print(f"rank {rank} done w state_dict")
# memory summary
# if rank == 0:
# print(
# f"CUDA Memory Summary After Whole Training Loop:\n {torch.cuda.memory_summary()}"
# )
# init_end_event.record()
if rank == 0:
# inner_pbar.close()
total_training_time = time.time() - training_start_time
print(f"Total training time = {total_training_time:.2f}")
print("Times per epoch:")
for i, val in enumerate(dur):
print(f"epoch {i}, time {val:.2f}")
print()
# memory
if cfg.track_memory:
print(f"total memory reserved: {mem_reserved_tracker}")
print(f"total memory allocated: {mem_alloc_tracker}")
print(f"Training accuracy: {train_acc_tracking}")
print(f"Validation accuracy: {val_acc_tracking}")
# print(
# f"Cuda event elapsed time: {init_start_event.elapsed_time(init_end_event) / 1000}sec"
# )
# print(f"{model}")
# save block
# save_model = cfg.save_model
# debug hang
# runs on all ranks
# print(f"rank {rank} calling barrier")
# dist.barrier()
# print(f"rank {rank} done w barrier, calling state_dict")
dist.barrier()
cleanup()
# ------------------ Main functions above ------------
if __name__ == "__main__":
args = parse_args()
# seed
torch.manual_seed(args.seed)
gpus_per_node = torch.cuda.device_count()
# cache workaround
""" dataset_name = "grammar_train.csv"
full_path_dataset = Path.cwd()/'datasets_grammar'/dataset_name
temp_full_dataset = load_dataset(
"csv",
data_files={
"train": [full_path_dataset]
}, # "eval": "grammar_validation.csv"},
delimiter=",",
)
print(f"temp dset loaded in main = len {len(temp_full_dataset)}")
mp.spawn(
fsdp_main,
args=(
gpus_per_node,
args,
),
nprocs=gpus_per_node,
join=True,
)
"""
fsdp_main(0, 0, args)