-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
1267 lines (1044 loc) · 52.4 KB
/
trainer.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
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
from torch.utils.data import DataLoader, Dataset, RandomSampler, SequentialSampler
import numpy as np
import time
import torch
import collections
from packaging import version
from torch.distributions import Categorical
import torch.nn as nn
from loss_func.repnoise_loss import rep_noise_loss
from transformers import Trainer
from transformers import logging
# from transformers.file_utils import is_torch_tpu_available
from transformers.trainer_pt_utils import (
get_parameter_names,
)
from transformers.utils import (
is_sagemaker_mp_enabled
)
from utils import prune_wanda_outlier,SupervisedDataset,prune_with_FI
from transformers.models.llama.modeling_llama import LlamaAttention,LlamaMLP
from transformers.models.opt.modeling_opt import OPTAttention
from transformers.models.mistral.modeling_mistral import MistralAttention
from transformers.models.gemma.modeling_gemma import GemmaAttention
from transformers.models.gemma2.modeling_gemma2 import Gemma2Attention
from transformers.models.qwen2.modeling_qwen2 import Qwen2Attention
# from transformers.models.falcon.modeling_falcon import FalconAttention
# from transformers.models.mistral.modeling_mistral import MistralAttention
if version.parse(torch.__version__) >= version.parse("1.6"):
from torch.cuda.amp import autocast
# if is_torch_tpu_available():
# import torch_xla.core.xla_model as xm
# import torch_xla.debug.metrics as met
# import torch_xla.distributed.parallel_loader as pl
logger = logging.get_logger(__name__)
class VlguardTrainer(Trainer):
def get_alignment_dataloader(self,alignment_dataset) -> DataLoader:
"""
Returns the training [`~torch.utils.data.DataLoader`].
Will use no sampler if `train_dataset` does not implement `__len__`, a random sampler (adapted to distributed
training if necessary) otherwise.
Subclass and override this method if you want to inject some custom behavior.
"""
from transformers.trainer_utils import (
seed_worker
)
from transformers.trainer_pt_utils import (
LengthGroupedSampler,
)
from torch.utils.data import DataLoader, RandomSampler
data_collator = self.data_collator
sampler = RandomSampler(alignment_dataset)
dataloader_params = {
"batch_size": 4,
"collate_fn": data_collator,
"num_workers": self.args.dataloader_num_workers,
"pin_memory": self.args.dataloader_pin_memory,
}
if not isinstance(alignment_dataset, torch.utils.data.IterableDataset):
dataloader_params["sampler"] = sampler
dataloader_params["drop_last"] = self.args.dataloader_drop_last
dataloader_params["worker_init_fn"] = seed_worker
return self.accelerator.prepare(DataLoader(alignment_dataset, **dataloader_params))
def init(self, alignment_datast):
self.clock = 0
self.steps = 0
if self.args.guide_data_num>0:
self.alignment_dataloader = self.get_alignment_dataloader(alignment_datast)
self.alignment_data_iter = iter(self.alignment_dataloader)
def sample_from_alignment(self):
# Get a batch
try:
batch = next(self.alignment_data_iter)
except (StopIteration):
# If the iterator is exhausted, create a new iterator
self.alignment_data_iter = iter(self.alignment_dataloader)
batch = next(self.alignment_data_iter)
return batch
def training_step(
self, model: nn.Module, inputs: Dict[str, Union[torch.Tensor, Any]]
) -> torch.Tensor:
# may change input due to mode change
model.train()
inputs = self._prepare_inputs(inputs)
alignment_inputs = self.sample_from_alignment()
alignment_inputs = self._prepare_inputs(alignment_inputs)
def step():
if is_sagemaker_mp_enabled():
loss_mb = smp_forward_backward(model, inputs, self.args.gradient_accumulation_steps)
return loss_mb.reduce_mean().detach().to(self.args.device)
with self.compute_loss_context_manager():
loss = self.compute_loss(model, inputs) + self.args.lamb* self.compute_loss(model, alignment_inputs)
if self.args.n_gpu > 1:
loss = loss.mean() # mean() to average on multi-gpu parallel training
if self.use_apex:
with amp.scale_loss(loss, self.optimizer) as scaled_loss:
scaled_loss.backward()
else:
self.accelerator.backward(loss)
# print("gere2")
return loss
loss = step()
return loss.detach() / self.args.gradient_accumulation_steps
class BoosterAlignmentTrainer(Trainer):
def get_harmful_dataloader(self,harmful_datast) -> DataLoader:
"""
Returns the training [`~torch.utils.data.DataLoader`].
Will use no sampler if `train_dataset` does not implement `__len__`, a random sampler (adapted to distributed
training if necessary) otherwise.
Subclass and override this method if you want to inject some custom behavior.
"""
from transformers.trainer_utils import (
seed_worker
)
from transformers.trainer_pt_utils import (
LengthGroupedSampler,
)
from torch.utils.data import DataLoader, RandomSampler
data_collator = self.data_collator
sampler = RandomSampler(harmful_datast)
dataloader_params = {
"batch_size": 10,
"collate_fn": data_collator,
"num_workers": self.args.dataloader_num_workers,
"pin_memory": self.args.dataloader_pin_memory,
}
if not isinstance(harmful_datast, torch.utils.data.IterableDataset):
dataloader_params["sampler"] = sampler
dataloader_params["drop_last"] = self.args.dataloader_drop_last
dataloader_params["worker_init_fn"] = seed_worker
return self.accelerator.prepare(DataLoader(harmful_datast, **dataloader_params))
def init(self, harmful_datast):
self.clock = 0
self.steps = 0
if self.args.guide_data_num>0:
self.harmful_dataloader = self.get_harmful_dataloader(harmful_datast)
self.harmful_data_iter = iter(self.harmful_dataloader)
self.statistic = 0
def sample_from_harmful(self):
# Get a batch
try:
batch = next(self.harmful_data_iter)
except (StopIteration):
# If the iterator is exhausted, create a new iterator
self.harmful_data_iter = iter(self.harmful_dataloader)
batch = next(self.harmful_data_iter)
return batch
@torch.no_grad()
def pre_first_step(self, model ):
def track_gradient_hook(module, grad_input, grad_output):
# Store the gradients for the current layer
self.sam_state["gradient"][module] = grad_output[0].detach().clone()/self.args.gradient_accumulation_steps
# print(grad_output[0])
def apply_backward_hooks_recursive(module, hook_fn, hooks):
hook = module.register_backward_hook(hook_fn)
hooks.append(hook) # Append the hook to the list
# Call the function with the initial empty hooks list
leaf_modules_with_grad = get_leaf_modules_with_grad(model)
for layer in leaf_modules_with_grad:
self.sam_state["gradient"][layer] = 0
apply_backward_hooks_recursive(layer, track_gradient_hook, self.sam_state["hooks"])
@torch.no_grad()
def pre_second_step(self, model):
def purturbation_hook(module, input, output):
# Modify the output, for example, by adding a perturbatio
perturbation = self.sam_state["gradient"][module]
# print(perturbation[0,1,:])
# # print(output.shape)
# print(output[0,1,:])
output[0].data =output[0] + perturbation
# print(output.shape)
return output
# Register forward hooks for adding perturbation
def apply_purturbation_hooks_recursive(module, hook_fn, hooks):
hook = module.register_forward_hook(hook_fn)
hooks.append(hook)
leaf_modules_with_grad = get_leaf_modules_with_grad(model)
for layer in leaf_modules_with_grad:
# print(layer._get_name())
# Apply hooks to all layers, including nested Sequential blocks
apply_purturbation_hooks_recursive(layer, purturbation_hook, self.sam_state["hooks"])
@torch.no_grad()
def after_first_step(self, model):
for hook in self.sam_state["hooks"]:
hook.remove()
self.sam_state["hooks"] = []
# print(self.sam_state["gradient"].items())
grad_norm = self._grad_norm(self.sam_state["gradient"])
# logging.info(grad_norm)
# logging.info("norm{}".format(grad_norm))
for module in self.sam_state["gradient"]:
# grad_norm = self._grad_norm(self.sam_state["gradient"][module])
grad = self.sam_state["gradient"][module]
scale = self. args. rho / (grad_norm +1e-7)
e_r = (grad)* scale
self.sam_state["gradient"][module] = e_r.detach().clone()
@torch.no_grad()
def after_second_step(self, model):
# disable hook here
# for module in self.sam_state["e_r"]:
# module.weight.data -= self.sam_state["e_r"][module]
for hook in self.sam_state["hooks"]:
hook.remove()
self.sam_state["hooks"] = []
# torch.nn.utils.clip_grad_norm_(model.parameters(), 10)
@torch.no_grad()
def _grad_norm(self,poison_grads_representation):
norm = torch.norm(
torch.stack([
#original sam
( poison_grads_representation[name] ).norm(p=2)
#asam
# ((torch.abs(p) if group["adaptive"] else 1.0) * p.grad).norm(p=2).to(shared_device)
for name in poison_grads_representation
]),
p=2
)
# norm = ( poison_grads_representation ).norm(p=2)
return norm
def training_step(
self, model: nn.Module, inputs: Dict[str, Union[torch.Tensor, Any]]
) -> torch.Tensor:
# may change input due to mode change
model.train()
inputs = self._prepare_inputs(inputs)
harmful_inputs = self.sample_from_harmful()
harmful_inputs = self._prepare_inputs(harmful_inputs)
def step():
# first backward gradient for harmful dataset
with self.compute_loss_context_manager():
loss = self.compute_loss(model, harmful_inputs)
if self.use_apex:
with amp.scale_loss(loss, self.optimizer) as scaled_loss:
scaled_loss.backward()
else:
self.accelerator.backward(loss)
# print("gere2")
stored_grads = {name: param.grad.data.clone() for name, param in model.named_parameters() if param.requires_grad}
model.zero_grad()
# Take step with the harmful perturbation
with torch.no_grad():
grad_norm = self._grad_norm(stored_grads)+ 1e-7
# perturb the weights
for name, param in model.named_parameters():
if param.requires_grad:
# param.data += self.args.rho*stored_grads[name]/grad_norm
param.data -= self.args.alpha*stored_grads[name]/grad_norm
# backward the gradient after harmful perturbation
with self.compute_loss_context_manager():
loss2 = self.compute_loss(model, harmful_inputs)
if self.use_apex:
with amp.scale_loss(loss2, self.optimizer) as scaled_loss:
scaled_loss.backward()
else:
self.accelerator.backward(loss2)
perturb_grads = {name: param.grad.clone() for name, param in model.named_parameters() if param.requires_grad}
model.zero_grad()
# recover the weights
for name, param in model.named_parameters():
if param.requires_grad:
# param.data -= self.args.rho*stored_grads[name]/grad_norm
param.data += self.args.alpha*stored_grads[name]/grad_norm
if self.args.perturb_aware =="True":
self.sam_state = {}
self.sam_state ["hooks"] = []
self.sam_state ["gradient"] = {}
# do forward backward on safety data
self.pre_first_step(model)
# first backward
loss4 = self.compute_loss(model, inputs)
if self.use_apex:
with amp.scale_loss(loss4, self.optimizer) as scaled_loss:
scaled_loss.backward()
else:
self.accelerator.backward(loss4)
self.after_first_step(model)
model.zero_grad()
self.pre_second_step(model)
loss3 = self.compute_loss(model, inputs)
if self.use_apex:
with amp.scale_loss(loss3, self.optimizer) as scaled_loss:
scaled_loss.backward()
else:
self.accelerator.backward(loss3)
# cancel the perturbation
self.after_second_step(model)
# sum the grad
for name, param in model.named_parameters():
if param.requires_grad:
# param.grad.data=param.grad.data - (self.args.alpha +self.args.lamb/self.args.rho)*stored_grads[name] +self.args.lamb/self.args.rho* perturb_grads[name]
param.grad.data=param.grad.data + (self.args.lamb)*stored_grads[name] -self.args.lamb* perturb_grads[name]
self.steps+=1
if self.steps%500==0:
self.statistic=0
self.statistic += sum([torch.norm(stored_grads[name])**2 for name, param in model.named_parameters() if param.requires_grad ]).detach()
print("harmful gradient norm {}".format(self.statistic),flush=True)
print("harmful loss {}".format(loss),flush=True)
return loss3
else:
# else:
# Finally backward for minimize safety gradient
# print(loss)
# calculate the alignment grad
with self.compute_loss_context_manager():
loss3 = self.compute_loss(model, inputs)
if self.use_apex:
with amp.scale_loss(loss3, self.optimizer) as scaled_loss:
scaled_loss.backward()
else:
self.accelerator.backward(loss3)
# Finally, sum the grad
for name, param in model.named_parameters():
if param.requires_grad:
param.grad.data=param.grad.data + (self.args.lamb)*stored_grads[name] -self.args.lamb* perturb_grads[name]
self.steps+=1
if self.steps%2000==0 :
self.statistic=0
self.statistic += grad_norm.detach()
# self.statistic += loss-loss2
print("harmful gradient norm {}".format(self.statistic),flush=True)
print("loss change {}".format(loss-loss2),flush=True)
print("harmful loss {}".format(loss),flush=True)
return loss3
loss = step()
return loss.detach() / self.args.gradient_accumulation_steps
class UnitedAlignmentTrainer(Trainer):
def get_harmful_dataloader(self,harmful_datast) -> DataLoader:
"""
Returns the training [`~torch.utils.data.DataLoader`].
Will use no sampler if `train_dataset` does not implement `__len__`, a random sampler (adapted to distributed
training if necessary) otherwise.
Subclass and override this method if you want to inject some custom behavior.
"""
from transformers.trainer_utils import (
seed_worker
)
from transformers.trainer_pt_utils import (
LengthGroupedSampler,
)
from torch.utils.data import DataLoader, RandomSampler
data_collator = self.data_collator
sampler = RandomSampler(harmful_datast)
dataloader_params = {
"batch_size": 5,
"collate_fn": data_collator,
"num_workers": self.args.dataloader_num_workers,
"pin_memory": self.args.dataloader_pin_memory,
}
if not isinstance(harmful_datast, torch.utils.data.IterableDataset):
dataloader_params["sampler"] = sampler
dataloader_params["drop_last"] = self.args.dataloader_drop_last
dataloader_params["worker_init_fn"] = seed_worker
return self.accelerator.prepare(DataLoader(harmful_datast, **dataloader_params))
def init(self, harmful_datast):
self.clock = 0
self.steps = 0
if self.args.guide_data_num>0:
self.harmful_dataloader = self.get_harmful_dataloader(harmful_datast)
self.harmful_data_iter = iter(self.harmful_dataloader)
self.statistic = 0
def sample_from_harmful(self):
# Get a batch
try:
batch = next(self.harmful_data_iter)
except (StopIteration):
# If the iterator is exhausted, create a new iterator
self.harmful_data_iter = iter(self.harmful_dataloader)
batch = next(self.harmful_data_iter)
return batch
def training_step(
self, model: nn.Module, inputs: Dict[str, Union[torch.Tensor, Any]]
) -> torch.Tensor:
# may change input due to mode change
model.train()
inputs = self._prepare_inputs(inputs)
harmful_inputs = self.sample_from_harmful()
harmful_inputs = self._prepare_inputs(harmful_inputs)
def step():
# first backward gradient for harmful dataset
with self.compute_loss_context_manager():
loss = -torch.log(self.compute_loss(model, harmful_inputs))
if self.use_apex:
with amp.scale_loss(loss, self.optimizer) as scaled_loss:
scaled_loss.backward(create_graph=True)
else:
self.accelerator.backward(loss,create_graph=True)
# print("gere2")
# store the gradient for each trianable param
# Store gradients in a way that retains their computational graph
stored_grads = {name: param.grad.clone() for name, param in model.named_parameters() if param.requires_grad}
# Clear original gradients
for param in model.parameters():
if param.requires_grad:
param.grad = None
# then backward gradient for alignment dataset
with self.compute_loss_context_manager():
loss1 =self.compute_loss(model, inputs)
if self.use_apex:
with amp.scale_loss(loss1, self.optimizer) as scaled_loss:
scaled_loss.backward(retain_graph=True)
else:
self.accelerator.backward(loss1,retain_graph=True)
# Store gradients in a way that retains their computational graph
alignment_grad = {name: param.grad.data.clone().detach() for name, param in model.named_parameters() if param.requires_grad}
# Clear original gradients
for param in model.parameters():
if param.requires_grad:
param.grad = None
# Finally backward for minimize gradient difference
with self.compute_loss_context_manager():
loss2 = loss1
for name, param in model.named_parameters():
if param.requires_grad:
loss2 += self.args.rho * torch.norm(stored_grads[name] - alignment_grad[name])**2
if self.use_apex:
with amp.scale_loss(loss2, self.optimizer) as scaled_loss:
scaled_loss.backward()
else:
self.accelerator.backward(loss2)
self.steps+=1
if self.steps%500==0:
self.statistic += sum([torch.norm(stored_grads[name] - alignment_grad[name])**2 for name, param in model.named_parameters() if param.requires_grad ]).detach()
print("distance {}".format(self.statistic/(self.steps/500)),flush=True)
# torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=0.5)
return loss2
loss = step()
return loss.detach() / self.args.gradient_accumulation_steps
class UnitedTrainer(Trainer):
def get_harmful_dataloader(self,harmful_datast) -> DataLoader:
"""
Returns the training [`~torch.utils.data.DataLoader`].
Will use no sampler if `train_dataset` does not implement `__len__`, a random sampler (adapted to distributed
training if necessary) otherwise.
Subclass and override this method if you want to inject some custom behavior.
"""
from transformers.trainer_utils import (
seed_worker
)
from transformers.trainer_pt_utils import (
LengthGroupedSampler,
)
from torch.utils.data import DataLoader, RandomSampler
data_collator = self.data_collator
sampler = RandomSampler(harmful_datast)
dataloader_params = {
"batch_size": 4,
"collate_fn": data_collator,
"num_workers": self.args.dataloader_num_workers,
"pin_memory": self.args.dataloader_pin_memory,
}
if not isinstance(harmful_datast, torch.utils.data.IterableDataset):
dataloader_params["sampler"] = sampler
dataloader_params["drop_last"] = self.args.dataloader_drop_last
dataloader_params["worker_init_fn"] = seed_worker
return self.accelerator.prepare(DataLoader(harmful_datast, **dataloader_params))
def init(self, harmful_datast):
self.clock = 0
self.steps = 0
if self.args.guide_data_num>0:
self.harmful_dataloader = self.get_harmful_dataloader(harmful_datast)
self.harmful_data_iter = iter(self.harmful_dataloader)
def sample_from_harmful(self):
# Get a batch
try:
batch = next(self.harmful_data_iter)
except (StopIteration):
# If the iterator is exhausted, create a new iterator
self.harmful_data_iter = iter(self.harmful_dataloader)
batch = next(self.harmful_data_iter)
return batch
def training_step(
self, model: nn.Module, inputs: Dict[str, Union[torch.Tensor, Any]]
) -> torch.Tensor:
# may change input due to mode change
model.train()
inputs = self._prepare_inputs(inputs)
harmful_inputs = self.sample_from_harmful()
harmful_inputs = self._prepare_inputs(harmful_inputs)
def step():
if is_sagemaker_mp_enabled():
loss_mb = smp_forward_backward(model, inputs, self.args.gradient_accumulation_steps)
return loss_mb.reduce_mean().detach().to(self.args.device)
with self.compute_loss_context_manager():
loss = self.compute_loss(model, inputs) - self.args.lamb* torch.log(self.compute_loss(model, harmful_inputs))
if self.args.n_gpu > 1:
loss = loss.mean() # mean() to average on multi-gpu parallel training
if self.use_apex:
with amp.scale_loss(loss, self.optimizer) as scaled_loss:
scaled_loss.backward()
else:
self.accelerator.backward(loss)
# print("gere2")
return loss
loss = step()
# torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=0.5)
return loss.detach() / self.args.gradient_accumulation_steps
class ADMMTrainer(Trainer):
def get_alignment_dataloader(self,alignment_dataset) -> DataLoader:
"""
Returns the training [`~torch.utils.data.DataLoader`].
Will use no sampler if `train_dataset` does not implement `__len__`, a random sampler (adapted to distributed
training if necessary) otherwise.
Subclass and override this method if you want to inject some custom behavior.
"""
from transformers.trainer_utils import (
seed_worker
)
from transformers.trainer_pt_utils import (
LengthGroupedSampler,
)
from torch.utils.data import DataLoader, RandomSampler
data_collator = self.data_collator
sampler = RandomSampler(alignment_dataset)
dataloader_params = {
"batch_size": self._train_batch_size,
"collate_fn": data_collator,
"num_workers": self.args.dataloader_num_workers,
"pin_memory": self.args.dataloader_pin_memory,
}
if not isinstance(alignment_dataset, torch.utils.data.IterableDataset):
dataloader_params["sampler"] = sampler
dataloader_params["drop_last"] = self.args.dataloader_drop_last
dataloader_params["worker_init_fn"] = seed_worker
return self.accelerator.prepare(DataLoader(alignment_dataset, **dataloader_params))
def init(self, alignment_dataset):
if self.args.alignment_step!=0 and self.args.guide_data_num>0:
self.status = "alignment"
else:
self.status = "finetune"
self.alignment_weights ={}
self.finetune_weights ={}
# self.gamma ={}
for name, param in self.model.named_parameters():
if param.requires_grad:
self.alignment_weights[name] = param.data.detach().clone()
self.finetune_weights[name] = param.data.detach().clone()
# self.gamma[name]= torch.zeros_like(param)
self.clock = 0
self.steps = 0
if self.args.guide_data_num>0:
self.alignment_dataloader = self.get_alignment_dataloader(alignment_dataset)
self.data_iter = iter(self.alignment_dataloader)
def end_training(self):
for name, param in self.model.named_parameters():
if param.requires_grad:
if self.status == "alignment":
self.alignment_weights[name] = param.data.detach().clone()
else:
self.finetune_weights[name] = param.data.detach().clone()
def switch_model(self):
sum_drift =0
if self.status == "alignment":
for name, param in self.model.named_parameters():
if param.requires_grad:
self.finetune_weights[name] = param.data.detach().clone()
sum_drift += torch.norm(self.finetune_weights[name] - self.alignment_weights[name])**2
print("finetuning drift to consensus{}".format(sum_drift))
else:
for name, param in self.model.named_parameters():
if param.requires_grad:
self.alignment_weights[name] = param.data.detach().clone()
sum_drift += torch.norm(self.finetune_weights[name] - self.alignment_weights[name])**2
print("alignment drift to consensus{}".format(sum_drift))
def sample_from_alignment(self):
# Get a batch
try:
batch = next(self.data_iter)
except (StopIteration):
# If the iterator is exhausted, create a new iterator
self.data_iter = iter(self.alignment_dataloader)
batch = next(self.data_iter)
return batch
def check_mode(self, inputs):
if self.status == "alignment":
if self.clock% (self.args.alignment_step ) == 0 and self.steps!=0 and self.args.finetune_step!=0:
self.status ="finetune"
self.switch_model()
# print("swith from alignment to finetune {}".format(self.steps))
self.clock=0
else:
# alignment need another input
inputs = self.sample_from_alignment()
else:
if self.clock% ( self.args.finetune_step ) == 0 and self.steps!=0 and self.args.alignment_step!=0 and self.args.guide_data_num>0:
self.status ="alignment"
self.switch_model()
# alignment need another input
inputs = self.sample_from_alignment()
# print("swith from finetune to alignment {}".format(self.steps))
self.clock=0
return inputs
def training_step(
self, model: nn.Module, inputs: Dict[str, Union[torch.Tensor, Any]]
) -> torch.Tensor:
# may change input due to mode change
inputs = self.check_mode(inputs)
model.train()
inputs = self._prepare_inputs(inputs)
def step():
if is_sagemaker_mp_enabled():
loss_mb = smp_forward_backward(model, inputs, self.args.gradient_accumulation_steps)
return loss_mb.reduce_mean().detach().to(self.args.device)
with self.compute_loss_context_manager():
loss = self.compute_loss(model, inputs)
if self.args.n_gpu > 1:
loss = loss.mean() # mean() to average on multi-gpu parallel training
if self.status =="alignment":
# print("alignment_loss_prev: {}".format(loss.item()))
if self.steps>0.1* len(self.get_train_dataloader()) * self.args.num_train_epochs:
for name, param in model.named_parameters():
if param.requires_grad and self.args.rho>0:
# loss +=torch.sum(self.gamma[name] * param)+ self.args.rho/2* torch.norm( param- self.finetune_weights[name])**2
loss += self.args.rho/2* torch.norm( param- self.finetune_weights[name])**2
# print("alignment_loss: {}".format(loss.item()))
else:
# print("finetune_loss_prev: {}".format(loss.item()))
if self.steps>0.1* len(self.get_train_dataloader()) * self.args.num_train_epochs:
for name, param in model.named_parameters():
# we observe that for Gsm8k, proximal term will hurt convergence. Don't do proximal for the first few rounds.
if param.requires_grad and self.args.rho>0:
# loss += (- torch.sum(self.gamma[name] * param )) + self.args.rho/2* torch.norm( param- self.alignment_weights[name])**2
loss += self.args.rho/2* torch.norm( param- self.alignment_weights[name])**2
# print("finetune_loss: {}".format(loss.item()))
if self.use_apex:
with amp.scale_loss(loss, self.optimizer) as scaled_loss:
scaled_loss.backward()
else:
self.accelerator.backward(loss)
# print("gere2")
return loss
loss = step()
self.steps+=1
self.clock+=1
return loss.detach() / self.args.gradient_accumulation_steps
class LDIFSTrainer(Trainer):
def init(self, model):
import copy
# Deep copy the object
self.alignment_model = copy.deepcopy(model)
# Ensure all tensors are in half precision
# self.alignment_model = self.alignment_model.half()
# self.alignment_model.eval()
# Verifying if the parameters are in half precision
# for param in model.parameters():
# print(param.dtype) # Should print torch.float16 for all parameters
self.steps = 0
def training_step(
self, model: nn.Module, inputs: Dict[str, Union[torch.Tensor, Any]]
) -> torch.Tensor:
# may change input due to mode change
model.train()
import copy
inputs = self._prepare_inputs(inputs)
def step():
def register_activation_hook(model):
activations = {}
hooks = []
i=0
for name, param in model.named_modules():
if name == f'base_model.model.model.layers.{i}.mlp':
param.name = name
def _hook(module, __, val):
activations[module.name] = val
# print(val)
hooks += [param.register_forward_hook(_hook)]
i+=1
# print(name)
return activations, hooks
activations, hooks = register_activation_hook(model)
if is_sagemaker_mp_enabled():
loss_mb = smp_forward_backward(model, inputs, self.args.gradient_accumulation_steps)
return loss_mb.reduce_mean().detach().to(self.args.device)
with self.compute_loss_context_manager():
loss = self.compute_loss(model, inputs)
if self.args.n_gpu > 1:
loss = loss.mean() # mean() to average on multi-gpu parallel training
# if self.steps>=0* len(self.get_train_dataloader()) * self.args.num_train_epochs:
# if self.steps>0.1* len(self.get_train_dataloader()) * self.args.num_train_epochs:
def compare_models(model1, model2):
for param1, param2 in zip(model1.parameters(), model2.parameters()):
if not torch.equal(param1, param2):
print("Mismatch found")
return False
return True
alignment_activations, alignment_model_hooks = register_activation_hook(self.alignment_model)
self.alignment_model(inputs['input_ids'], attention_mask=inputs['attention_mask'])
# if compare_models(model, self.alignment_model):
# print("Models are identical")
# else:
# print("Models differ")
proximal_loss=0
for name in alignment_activations:
# print(alignment_activations[name])
# print(alignment_activations[name].shape)
# in some layers the proximal loss will be NAN, drop those overflow loss
proximal_loss = self.args.rho/2* torch.norm( activations [name]- alignment_activations[name])**2
if proximal_loss<0.1:
# print(name)
# print(proximal_loss)
loss += proximal_loss
# print(loss)
# clean up before leaving
for hook in hooks:
hook.remove()
hooks = []
activations = {}
for hook in alignment_model_hooks:
hook.remove()
alignment_model_hooks = []
alignment_activations = {}
if self.use_apex:
with amp.scale_loss(loss, self.optimizer) as scaled_loss:
scaled_loss.backward()
else:
self.accelerator.backward(loss)
# print("gere2")
torch.nn.utils.clip_grad_norm_(model.parameters(), 5)
return loss
loss = step()
self.steps+=1
return loss.detach() / self.args.gradient_accumulation_steps
def get_leaf_modules_with_grad(module):
# # print([name for name,param in module.named_parameters()])
# if len(list(module.children())) == 0 and any(p.requires_grad for p in module.parameters()) and "lora_B" in module._get_name():
# return [module]
# else:
# return [submodule for child in module.children() for submodule in get_leaf_modules_with_grad(child)]
module_list= []
for name, module in module.named_modules():
# if "lora_B" in name and "v_proj" in name and len(list(module.children())) == 0:
# module_list+= [module]
if isinstance(module,LlamaAttention) or isinstance(module, OPTAttention) or isinstance(module, MistralAttention) or isinstance(module, GemmaAttention) or isinstance(module, Qwen2Attention)or isinstance(module, Gemma2Attention):
# if isinstance(module,LlamaAttention) or isinstance(module, OPTAttention) or isinstance(module, MistralAttention):
module_list+= [module]
# # print(module_list)
return module_list
class BaseTrainer(Trainer):
def init(self, mask_ratio):
self.mask_ratio=mask_ratio
self.round = 0
# self.warm_up_round = 11999
def save_mask(self, save_path):
# # OWL here!!!!!!!
self.model.model.seqlen = 2048
self.mask = prune_wanda_outlier(self.args, self.model.model, self.get_train_dataloader(), device=torch.device("cuda:0"), prune_n=0, prune_m=0)
torch.save(self.mask, save_path)
def training_step(
self, model: nn.Module, inputs: Dict[str, Union[torch.Tensor, Any]]
) -> torch.Tensor:
model.train()
inputs = self._prepare_inputs(inputs)
def step():
if is_sagemaker_mp_enabled():
loss_mb = smp_forward_backward(model, inputs, self.args.gradient_accumulation_steps)
return loss_mb.reduce_mean().detach().to(self.args.device)
with self.compute_loss_context_manager():
loss = self.compute_loss(model, inputs)
if self.args.n_gpu > 1:
loss = loss.mean() # mean() to average on multi-gpu parallel training
if self.use_apex:
with amp.scale_loss(loss, self.optimizer) as scaled_loss:
scaled_loss.backward()
else:
self.accelerator.backward(loss)
# print("gere2")
return loss
# if isinstance(self.optimizer,ESAM ):
# print("calling sam")
self.sam_state = {}
self.sam_state ["hooks"] = []
self.sam_state ["gradient"] = {}
self.pre_first_step(model)
step()
self.after_first_step(model)
model.zero_grad()
self.pre_second_step(model)
loss = step()
self.after_second_step(model)
return loss.detach() / self.args.gradient_accumulation_steps
@torch.no_grad()
def pre_first_step(self, model ):
def track_gradient_hook(module, grad_input, grad_output):
# Store the gradients for the current layer
self.sam_state["gradient"][module] = grad_output[0].detach().clone()/self.args.gradient_accumulation_steps
# print(grad_output[0])
def apply_backward_hooks_recursive(module, hook_fn, hooks):
hook = module.register_backward_hook(hook_fn)
hooks.append(hook) # Append the hook to the list
# Call the function with the initial empty hooks list
leaf_modules_with_grad = get_leaf_modules_with_grad(model)
for layer in leaf_modules_with_grad:
self.sam_state["gradient"][layer] = 0
apply_backward_hooks_recursive(layer, track_gradient_hook, self.sam_state["hooks"])
@torch.no_grad()
def pre_second_step(self, model):
def purturbation_hook(module, input, output):
# Modify the output, for example, by adding a perturbatio
perturbation = self.sam_state["gradient"][module]
# print(perturbation[0,1,:])
# # print(output.shape)
# print(output[0,1,:])
output[0].data =output[0] + perturbation
# print(output.shape)
return output
# Register forward hooks for adding perturbation
def apply_purturbation_hooks_recursive(module, hook_fn, hooks):
hook = module.register_forward_hook(hook_fn)
hooks.append(hook)
leaf_modules_with_grad = get_leaf_modules_with_grad(model)
for layer in leaf_modules_with_grad:
# print(layer._get_name())
apply_purturbation_hooks_recursive(layer, purturbation_hook, self.sam_state["hooks"])
@torch.no_grad()
def after_first_step(self, model):
for hook in self.sam_state["hooks"]:
hook.remove()
self.sam_state["hooks"] = []