-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
939 lines (801 loc) · 34.5 KB
/
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
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
import argparse
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
from accelerate import Accelerator, DistributedType, DistributedDataParallelKwargs
from datasets import load_dataset, load_metric
from transformers import (
AdamW,
get_linear_schedule_with_warmup,
AutoConfig,
)
from custom_layers.custom_bert import BertForSequenceClassification
import random
import numpy as np
import os
from tasks.glue.prepare_task import GlueTask
from utils.module_proxy_wrapper import ModuleProxyWrapper
from pprint import pprint
import wandb
import plotly.graph_objects as go
from utils.wipe_memory import wipe_memory
def seed_everything(accelerator, seed=1234, randomize_across_diff_devices=False):
if randomize_across_diff_devices:
# following sgugger's comment here https://github.com/huggingface/accelerate/issues/90
random.seed(seed + accelerator.process_index)
np.random.seed(seed + accelerator.process_index)
torch.manual_seed(seed + accelerator.process_index)
torch.cuda.manual_seed_all(seed + accelerator.process_index)
else:
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
########################################################################
# This is a fully working simple example to use Accelerate
#
# This example trains a Bert base model on GLUE MRPC
# in any of the following settings (with the same script):
# - single CPU or single GPU
# - multi GPUS (using PyTorch distributed mode)
# - (multi) TPUs
# - fp16 (mixed-precision) or fp32 (normal precision)
#
# To run it in each of these various modes, follow the instructions
# in the readme for examples:
# https://github.com/huggingface/accelerate/tree/main/examples
#
########################################################################
MAX_GPU_BATCH_SIZE = 32
EVAL_BATCH_SIZE = 64
def get_supertransformer_config():
config = AutoConfig.from_pretrained("bert-base-uncased")
config.sample_hidden_size = config.hidden_size
config.sample_num_hidden_layers = config.num_hidden_layers
config.sample_num_attention_heads = [
config.num_attention_heads
] * config.sample_num_hidden_layers
config.sample_intermediate_size = [
config.intermediate_size
] * config.sample_num_hidden_layers
return config
def get_choices(limit_subtransformer_choices=False):
if limit_subtransformer_choices:
choices = {
"sample_hidden_size": [600, 768],
"sample_num_attention_heads": [2, 4, 6, 8, 10, 12],
"sample_intermediate_size": [512, 1024, 2048, 3072],
"sample_num_hidden_layers": [8, 10, 12],
}
else:
choices = {
"sample_hidden_size": [360, 480, 540, 600, 768],
"sample_num_attention_heads": [2, 4, 6, 8, 10, 12],
"sample_intermediate_size": [512, 1024, 2048, 3072],
"sample_num_hidden_layers": [6, 8, 10, 12],
}
return choices
def print_subtransformer_config(config, accelerator):
accelerator.print("===========================================================")
accelerator.print("hidden size: ", config.sample_hidden_size)
accelerator.print("num attention heads: ", config.sample_num_attention_heads)
accelerator.print("intermediate sizes: ", config.sample_intermediate_size)
accelerator.print("num hidden layers: ", config.sample_num_hidden_layers)
accelerator.print("===========================================================")
def sample_subtransformer(
limit_subtransformer_choices=False, randomize=False, rand_seed=0
):
if randomize:
random.seed(rand_seed)
choices = get_choices(limit_subtransformer_choices)
config = get_supertransformer_config()
### Figuring the number of hidden layers
hidden_layers_list = choices["sample_num_hidden_layers"]
num_hidden_layers = random.choice(hidden_layers_list)
setattr(config, "sample_num_hidden_layers", num_hidden_layers)
## Figuring the hidden size for BERT embeddings
hidden_size_embeddings_list = choices["sample_hidden_size"]
num_hidden_size = random.choice(hidden_size_embeddings_list)
setattr(config, "sample_hidden_size", num_hidden_size)
config_dict = {
"sample_num_attention_heads": [],
"sample_intermediate_size": [],
}
for i in range(num_hidden_layers):
while True:
for key in config_dict.keys():
choice_list = choices[key]
choice = random.choice(choice_list)
config_dict[key].append(choice)
if config.sample_hidden_size % config_dict["sample_num_attention_heads"][i]:
for key in config_dict.keys():
config_dict[key] = config_dict[key][:-1]
continue
else:
break
for key in config_dict.keys():
setattr(config, key, config_dict[key])
return config
def validate_subtransformer(
model, config, eval_dataloader, accelerator, metric, task="mrpc", sample=True
):
if sample:
model.set_sample_config(config=config)
model.eval()
for step, batch in enumerate(eval_dataloader):
# We could avoid this line since we set the accelerator with `device_placement=True`.
batch.to(accelerator.device)
with torch.no_grad():
outputs = model(**batch)
if task == "stsb":
predictions = predictions[:, 0]
else:
predictions = outputs.logits.argmax(dim=-1)
metric.add_batch(
predictions=accelerator.gather(predictions),
references=accelerator.gather(batch["labels"]),
)
eval_metric = metric.compute()
# Use accelerator.print to print only on the main process.
# accelerator.print(eval_metric)
return eval_metric
def train_transformer_one_epoch(
args,
model,
optimizer,
lr_scheduler,
gradient_accumulation_steps,
train_dataloader,
accelerator,
train_subtransformer=False,
subtransformer_seed=42,
):
optimizer.zero_grad()
model.train()
seed = -1
for step, batch in enumerate(
tqdm(train_dataloader, disable=not accelerator.is_local_main_process),
):
if not train_subtransformer:
# if we are training a supertransformer, then we need to change the
# seed in each step
seed += 1
super_config = sample_subtransformer(
args.limit_subtransformer_choices, randomize=True, rand_seed=seed
)
model.set_sample_config(super_config)
batch.to(accelerator.device)
outputs = model(**batch)
loss = outputs.loss
loss = loss / gradient_accumulation_steps
accelerator.backward(loss)
if step % gradient_accumulation_steps == 0:
# print(super_config)
optimizer.step()
lr_scheduler.step()
optimizer.zero_grad()
if accelerator.is_main_process:
wandb.log({"random-subtransformer-loss": loss.item(), "rand-seed": seed})
def training_function(args):
# path to save the optimizer and scheduler states
optim_scheduler_states_path = os.path.join(args.output_dir, "optim_scheduler.pt")
param = DistributedDataParallelKwargs(
find_unused_parameters=True, check_reduction=False
)
accelerator = Accelerator(fp16=args.fp16, cpu=args.cpu, kwargs_handlers=[param])
seed_everything(
accelerator=accelerator,
seed=args.seed,
)
accelerator.print(
"==================================================================="
)
accelerator.print("Training Arguments:")
for arg in vars(args):
accelerator.print(f"{arg}: {getattr(args, arg)}")
accelerator.print(
"==================================================================="
)
# Initialize accelerator
accelerator.print("Running on: ", accelerator.device)
if accelerator.is_main_process:
if not args.train_subtransformers_from_scratch:
# TODO: change this to a better name for trianing + finetuning
wandb.init(
project="eHAT-warmups",
entity="efficient-hat",
name=args.task + "_train_scratch",
)
else:
wandb.init(
project="eHAT-warmups",
entity="efficient-hat",
name=args.task + "subtransformers_train_scratch",
)
# Sample hyper-parameters for learning rate, batch size, seed and a few other HPs
lr = args.learning_rate
num_epochs = int(args.num_epochs)
# for now correcting adam bias is hardcoded to True
correct_bias = True
seed = int(args.seed)
# note your effective batch size while training would be:
# per_gpu_train_batch_size * gradient_accumulation_steps * num_gpus
# so for instance, if you train with:
# per_gpu_train_batch_size = 32
# gradient_accumulation_steps = 4
# num_gpus (number of gpus) = 2
# then your effective training batch size is (32 * 4 * 2) = 256
per_gpu_train_batch_size = int(args.per_gpu_train_batch_size)
per_gpu_eval_batch_size = int(args.per_gpu_eval_batch_size)
gradient_accumulation_steps = int(args.gradient_accumulation_steps)
config = get_supertransformer_config()
# print(f"Batch Size: {batch_size}")
task = args.task
use_pretained = args.use_pretrained_supertransformer
model_checkpoint = args.model_name_or_path
# if this is not a path to a saved model checkpoint, ensure that we are using a bert-base model
if not os.path.exists(model_checkpoint):
assert (
model_checkpoint == "bert-base-cased"
or model_checkpoint == "bert-base-uncased"
), f"HF model {model_checkpoint} is not supported, pls use bert-base"
glue_task = GlueTask(
task,
model_checkpoint,
config,
args.max_seq_length,
accelerator,
initialize_pretrained_model=use_pretained,
)
def collate_fn(examples):
# On TPU it's best to pad everything to the same length or training will be very slow.
if accelerator.distributed_type == DistributedType.TPU:
return glue_task.tokenizer.pad(
examples,
padding="max_length",
max_length=args.max_seq_length,
return_tensors="pt",
)
return glue_task.tokenizer.pad(examples, padding="longest", return_tensors="pt")
# Instantiate dataloaders.
train_dataloader = DataLoader(
glue_task.train_dataset,
shuffle=True,
collate_fn=collate_fn,
batch_size=per_gpu_train_batch_size,
)
eval_dataloader = DataLoader(
glue_task.eval_dataset,
shuffle=False,
collate_fn=collate_fn,
batch_size=per_gpu_eval_batch_size,
)
model = glue_task.model
metric = glue_task.metric
# We could avoid this line since the accelerator is set with `device_placement=True` (default value).
# Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer
model = model.to(accelerator.device)
# Instantiate optimizer
optimizer = AdamW(params=model.parameters(), lr=lr, correct_bias=correct_bias)
# Prepare everything
# There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the
# prepare method.
model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare(
model, optimizer, train_dataloader, eval_dataloader
)
if (
accelerator.distributed_type == DistributedType.MULTI_GPU
or accelerator.distributed_type == DistributedType.TPU
):
# forward missing getattr and state_dict/load_state_dict to orig model
model = ModuleProxyWrapper(
model
) # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that).
# Instantiate learning rate scheduler after preparing the training dataloader as the prepare method
# may change its length.
lr_scheduler = get_linear_schedule_with_warmup(
optimizer=optimizer,
num_warmup_steps=100,
num_training_steps=len(train_dataloader) * num_epochs,
)
if accelerator.is_main_process:
wandb.watch(model)
if not args.train_subtransformers_from_scratch:
## train and finetune the supertransformer
best_val_accuracy = 0
metric_not_improved_count = 0
metric_to_track = "supertransformer_accuracy"
# Now we train the model
for epoch in range(num_epochs):
train_transformer_one_epoch(
args,
model,
optimizer,
lr_scheduler,
gradient_accumulation_steps,
train_dataloader,
accelerator,
train_subtransformer=False, # first we will train the supertransformer
)
accelerator.print(f"Epoch {epoch + 1}:", end=" ")
if accelerator.is_main_process:
wandb.log({"epochs": epoch})
# resetting to supertransformer before validation
config = get_supertransformer_config()
eval_metric = validate_subtransformer(
model, config, eval_dataloader, accelerator, metric, task
)
super_dict = {}
for key in eval_metric:
super_key = "supertransformer_" + key
super_dict[super_key] = eval_metric[key]
accelerator.print(super_dict)
if accelerator.is_main_process:
wandb.log(super_dict)
if args.eval_random_subtransformers:
label_seed = []
label_acc = []
hover_templates = []
sampling_dimensions = [
"sample_hidden_size",
"sample_num_attention_heads",
"sample_intermediate_size",
"sample_num_hidden_layers",
]
# Sampling 25 random sub-transformers and evaluate them to understand the relative performance order
for i in range(25):
random_seed = i * 1000
config = sample_subtransformer(
args.limit_subtransformer_choices,
randomize=True,
rand_seed=random_seed,
)
eval_metric = validate_subtransformer(
model, config, eval_dataloader, accelerator, metric, task
)
# eval_metric['validation_random_seed'] = random_seed
# label_lst.append([eval_metric['accuracy'], random_seed])
# label_lst.append([random_seed, eval_metric['accuracy']])
hover_templates.append(
"<br>".join(
[
f"{key}: {getattr(config, key)}"
for key in sampling_dimensions
]
)
)
label_acc.append(eval_metric["accuracy"])
label_seed.append(random_seed)
# accelerator.print(eval_metric)
# wandb.log(eval_metric)
if accelerator.is_main_process:
## If plotting using Custom Plotly
fig = go.Figure()
fig.add_trace(
go.Bar(x=label_seed, y=label_acc, hovertext=hover_templates)
)
fig.update_layout(
title="Relative Performance Order",
xaxis_title="Random Seed",
yaxis_title="Accuracy",
)
wandb.log({"bar_chart": wandb.data_types.Plotly(fig)})
# early stopping
if super_dict[metric_to_track] > best_val_accuracy:
metric_not_improved_count = 0
best_val_accuracy = super_dict[metric_to_track]
# unwrap and save best model so far
accelerator.wait_for_everyone()
unwrapped_model = accelerator.unwrap_model(model)
# accelerator.unwrap_model(model).save_pretrained(args.output_dir)
accelerator.save(
unwrapped_model.state_dict(), args.output_dir + "/pytorch_model.bin"
)
# accelerator.save(
# {
# "epoch": epoch + 1,
# "optimizer": optimizer.state_dict(),
# "scheduler": lr_scheduler.state_dict(),
# },
# optim_scheduler_states_path,
# )
else:
metric_not_improved_count += 1
if metric_not_improved_count >= args.early_stopping_patience:
break
# accelerator.print()
# accelerator.print("Evaluating subtransformer training")
# accelerator.print()
## for finetuning, we load the best model, optimizer and scheduler
# states. Naively loading them is causing OOM issue. Hence we first
# clear torch cuda memory
print(
"===========================Wiping memory================================================="
)
# GR: suspecting that memory is not fully cleared here
# based on some testing, the supertrasnformer training on mrpc with batchsize of 32
# used around 3921 MB on gpu. After wiping mem, we are still left with aronud 1.6 GB
# Have to check if there is a leak
# TODO: revisit this and modify utils.wipe_memory
unwrapped_model = accelerator.unwrap_model(model)
unwrapped_model.load_state_dict(
torch.load(args.output_dir + "/pytorch_model.bin")
)
# wipe_memory(optimizer)
## we will finetune 3 random subtransformers
num_subtransformers_for_finetuning = 10
fine_tuning_epochs = 10
## initialize the model to the best pretrained checkpoint
# model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
#
##model = accelerator.unwrap_model(model)
# model.load_state_dict(torch.load(args.output_dir+'/pytorch_model.bin'))
#
## it is important to send the model to device before sampling.
## Else we would get an error that weights are in cpu (not fullly sure why)
# model = model.to(accelerator.device)
# optimizer = AdamW(
# params=model.parameters(), lr=args.learning_rate, correct_bias=correct_bias
# )
# lr_scheduler = get_linear_schedule_with_warmup(
# optimizer=optimizer,
# num_warmup_steps=100,
# num_training_steps=len(train_dataloader)
# * fine_tuning_epochs,
# )
# model, optimizer = accelerator.prepare(model, optimizer)
# model = ModuleProxyWrapper(model)
for idx in range(num_subtransformers_for_finetuning):
metric_to_track = "finetuned_subtransformer_" + str(idx) + "_accuracy"
subtransformer_output_dir = os.path.join(
args.output_dir, f"finetune_subtransformer_{str(idx)}"
)
best_val_accuracy = 0
# sample one random subtransformer
random_subtransformer_seed = idx * 1000
## initialize subtransformer
super_config = sample_subtransformer(
args.limit_subtransformer_choices,
randomize=True,
rand_seed=random_subtransformer_seed,
)
model.set_sample_config(super_config)
# uncomment this block to use subtransformer from get_active_subnet
# super_config.num_hidden_layers = super_config.sample_num_hidden_layers
# model = model.get_active_subnet(super_config)
# model = model.to(accelerator.device)
accelerator.print(
"Finetuning subtransformer with config: ",
print_subtransformer_config(super_config, accelerator),
)
optim_scheduler_states = torch.load(optim_scheduler_states_path)
optimizer.load_state_dict(optim_scheduler_states["optimizer"])
lr_scheduler.load_state_dict(optim_scheduler_states["scheduler"])
for epoch in range(fine_tuning_epochs):
train_transformer_one_epoch(
args,
model,
optimizer,
lr_scheduler,
gradient_accumulation_steps,
train_dataloader,
accelerator,
train_subtransformer=True,
subtransformer_seed=random_subtransformer_seed,
)
# no need to sample config while validating in this case
# hence setting the sample to False
eval_metric = validate_subtransformer(
model,
super_config,
eval_dataloader,
accelerator,
metric,
task,
sample=False,
)
accelerator.print(f"Epoch {epoch + 1}:", end=" ")
accelerator.print(eval_metric)
if accelerator.is_main_process:
wandb.log({"finetuning_epochs": epoch})
sub_dict = {}
for key in eval_metric:
sub_key = "finetuned_subtransformer_" + str(idx) + "_" + key
sub_dict[sub_key] = eval_metric[key]
accelerator.print(sub_dict)
if accelerator.is_main_process:
wandb.log(sub_dict)
# early stopping
if eval_metric[metric_to_track] > best_val_accuracy:
metric_not_improved_count = 0
best_val_accuracy = eval_metric[metric_to_track]
accelerator.wait_for_everyone()
unwrapped_model = accelerator.unwrap_model(model)
accelerator.save(
unwrapped_model.state_dict(), subtransformer_output_dir
)
# accelerator.unwrap_model(model).save_pretrained(
# subtransformer_output_dir
# )
else:
metric_not_improved_count += 1
if metric_not_improved_count >= args.early_stopping_patience:
break
# print(
# "===========================Wiping memory================================================="
# )
# # GR: suspecting that memory is not fully cleared here
# # based on some testing, the supertrasnformer training on mrpc with batchsize of 32
# # used around 3921 MB on gpu. After wiping mem, we are still left with aronud 1.6 GB
# # Have to check if there is a leak
# # TODO: revisit this and modify utils.wipe_memory
# unwrapped_model = accelerator.unwrap_model(model)
# unwrapped_model.load_state_dict(torch.load(args.output_dir+'/pytorch_model.bin'))
# #wipe_memory(optimizer)
# ## we will finetune 3 random subtransformers
# num_subtransformers_for_finetuning = 10
# fine_tuning_epochs = 10
# ## initialize the model to the best pretrained checkpoint
# #model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
# #
# ##model = accelerator.unwrap_model(model)
# #model.load_state_dict(torch.load(args.output_dir+'/pytorch_model.bin'))
# #
# ## it is important to send the model to device before sampling.
# ## Else we would get an error that weights are in cpu (not fullly sure why)
# #model = model.to(accelerator.device)
# #optimizer = AdamW(
# # params=model.parameters(), lr=args.learning_rate, correct_bias=correct_bias
# #)
# #lr_scheduler = get_linear_schedule_with_warmup(
# # optimizer=optimizer,
# # num_warmup_steps=100,
# # num_training_steps=len(train_dataloader)
# # * fine_tuning_epochs,
# #)
# #model, optimizer = accelerator.prepare(model, optimizer)
# #model = ModuleProxyWrapper(model)
# for idx in range(num_subtransformers_for_finetuning):
# metric_to_track = "finetuned_subtransformer_" + str(idx) + "_accuracy"
# subtransformer_output_dir = os.path.join(
# args.output_dir, f"finetune_subtransformer_{str(idx)}"
# )
# best_val_accuracy = 0
# # sample one random subtransformer
# random_subtransformer_seed = idx * 1000
# ## initialize subtransformer
# super_config = sample_subtransformer(
# args.limit_subtransformer_choices,
# randomize=True,
# rand_seed=random_subtransformer_seed,
# )
# model.set_sample_config(super_config)
# # uncomment this block to use subtransformer from get_active_subnet
# # super_config.num_hidden_layers = super_config.sample_num_hidden_layers
# # model = model.get_active_subnet(super_config)
# # model = model.to(accelerator.device)
# accelerator.print(
# "Finetuning subtransformer with config: ",
# print_subtransformer_config(super_config, accelerator),
# )
# optim_scheduler_states = torch.load(optim_scheduler_states_path)
# optimizer.load_state_dict(optim_scheduler_states["optimizer"])
# lr_scheduler.load_state_dict(optim_scheduler_states["scheduler"])
# for epoch in range(fine_tuning_epochs):
# train_transformer_one_epoch(
# args,
# model,
# optimizer,
# lr_scheduler,
# gradient_accumulation_steps,
# train_dataloader,
# accelerator,
# train_subtransformer=True,
# subtransformer_seed=random_subtransformer_seed,
# )
# # no need to sample config while validating in this case
# # hence setting the sample to False
# eval_metric = validate_subtransformer(
# model,
# super_config,
# eval_dataloader,
# accelerator,
# metric,
# task,
# sample=False,
# )
# accelerator.print(f"Epoch {epoch + 1}:", end=" ")
# accelerator.print(eval_metric)
# if accelerator.is_main_process:
# wandb.log({"finetuning_epochs": epoch})
# sub_dict = {}
# for key in eval_metric:
# sub_key = "finetuned_subtransformer_" + str(idx) + "_" + key
# sub_dict[sub_key] = eval_metric[key]
# accelerator.print(sub_dict)
# if accelerator.is_main_process:
# wandb.log(sub_dict)
# # early stopping
# if eval_metric[metric_to_track] > best_val_accuracy:
# metric_not_improved_count = 0
# best_val_accuracy = eval_metric[metric_to_track]
# accelerator.wait_for_everyone()
# unwrapped_model = accelerator.unwrap_model(model)
# accelerator.save(unwrapped_model.state_dict(), subtransformer_output_dir)
# #accelerator.unwrap_model(model).save_pretrained(
# # subtransformer_output_dir
# #)
# else:
# metric_not_improved_count += 1
# if metric_not_improved_count >= args.early_stopping_patience:
# break
model = BertForSequenceClassification.from_pretrained(
args.model_name_or_path
)
model = BertForSequenceClassification.from_pretrained(args.model_name_or_path)
model = model.to(accelerator.device)
super_config.num_hidden_layers = super_config.sample_num_hidden_layers
model.set_sample_config(super_config)
model = model.get_active_subnet(super_config)
optimizer = AdamW(
params=model.parameters(),
lr=args.learning_rate,
correct_bias=correct_bias,
)
lr_scheduler = get_linear_schedule_with_warmup(
optimizer=optimizer,
num_warmup_steps=100,
num_training_steps=len(train_dataloader) * num_epochs,
)
model, optimizer = accelerator.prepare(model, optimizer)
accelerator.print(
"Training subtransformer from scratch with config: ",
print_subtransformer_config(super_config, accelerator),
)
os.makedirs(subtransformer_output_dir, exist_ok=True)
for epoch in range(num_epochs):
train_transformer_one_epoch(
args,
model,
optimizer,
lr_scheduler,
gradient_accumulation_steps,
train_dataloader,
accelerator,
train_subtransformer=True, # first we will train the supertransformer
)
accelerator.print(f"Epoch {epoch + 1}:", end=" ")
if accelerator.is_main_process:
wandb.log({"epochs": epoch})
eval_metric = validate_subtransformer(
model,
config,
eval_dataloader,
accelerator,
metric,
task,
sample=False,
)
sub_dict = {}
for key in eval_metric:
sub_key = "subtransformer_" + str(idx) + "_" + key
sub_dict[sub_key] = eval_metric[key]
accelerator.print(sub_dict)
if accelerator.is_main_process:
wandb.log(sub_dict)
# early stopping
if sub_dict[metric_to_track] > best_val_accuracy:
metric_not_improved_count = 0
best_val_accuracy = sub_dict[metric_to_track]
# unwrap and save best model so far
accelerator.unwrap_model(model).save_pretrained(
subtransformer_output_dir
)
else:
metric_not_improved_count += 1
if metric_not_improved_count >= args.early_stopping_patience:
break
def main():
parser = argparse.ArgumentParser(description="Script to train efficient HAT models")
parser.add_argument(
"--task", type=str, default="mrpc", help="The Glue task you want to run"
)
parser.add_argument(
"--model_name_or_path",
default="bert-base-uncased",
type=str,
help="Path to model checkpoint or name of hf pretrained model",
)
parser.add_argument(
"--use_pretrained_supertransformer",
type=int,
default=1,
help="If passed and set to True, will use pretrained bert-uncased model. If set to False, it will initialize a random model and train from scratch",
)
parser.add_argument(
"--output_dir",
default="checkpoints",
type=str,
help="The output directory where the model checkpoints and predictions will be written.",
)
parser.add_argument(
"--max_seq_length",
default=128,
type=int,
help="The maximum total input sequence length after WordPiece tokenization. Sequences "
"longer than this will be truncated, and sequences shorter than this will be padded.",
)
parser.add_argument(
"--per_gpu_train_batch_size",
default=32,
type=int,
help="Batch size per GPU/CPU for training.",
)
parser.add_argument(
"--per_gpu_eval_batch_size",
default=64,
type=int,
help="Batch size per GPU/CPU for evaluation.",
)
parser.add_argument(
"--early_stopping_patience",
default=5,
type=int,
help="Patience for early stopping to stop training if val_acc doesnt converge",
)
parser.add_argument(
"--limit_subtransformer_choices",
default=0,
type=int,
help="If set to 1, it will limit the hidden_size and number of encoder layers of the subtransformer choices",
)
parser.add_argument(
"--eval_random_subtransformers",
default=0,
type=int,
help="If set to 1, this will evaluate 25 random subtransformers after every training epoch when training a supertransformer",
)
parser.add_argument(
"--train_subtransformers_from_scratch",
default=0,
type=int,
help="""
If set to 1, this will train 25 random subtransformers from scratch.
By default, it is set to False (0) and we train a supertransformer and finetune subtransformers
""",
)
parser.add_argument(
"--learning_rate",
default=2e-5,
type=float,
help="The initial learning rate for Adam.",
)
parser.add_argument(
"--gradient_accumulation_steps",
type=int,
default=1,
help="Number of updates steps to accumulate before performing a backward/update pass.",
)
parser.add_argument(
"--num_epochs",
default=5.0,
type=float,
help="Total number of training epochs to perform.",
)
parser.add_argument(
"--fp16", type=int, default=1, help="If set to 1, will use FP16 training."
)
parser.add_argument(
"--cpu", type=int, default=0, help="If set to 1, will train on the CPU."
)
parser.add_argument(
"--seed", type=int, default=42, help="random seed for initialization"
)
args = parser.parse_args()
args.output_dir = args.output_dir + "/" + args.task
# if the mentioned output_dir does not exist, create it
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
training_function(args)
if __name__ == "__main__":
main()