-
Notifications
You must be signed in to change notification settings - Fork 35
/
ppo.py
1401 lines (1211 loc) · 60.5 KB
/
ppo.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
# Copyright 2022 CarperAI & The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# NOTE: This file contains a modified version of the `PreTrainedModelWrapper` class from
# HuggingFace's `trl` library. The original source code can be found here:
# https://github.com/lvwerra/trl/blob/78c13226bf8ea1ccd9b1c091f03a938098521f6c/trl/models/modeling_base.py
import gc
import inspect
import json
import os
import numpy as np
import torch
import torch.nn as nn
import transformers
import loralib as lora
from copy import deepcopy
from typing import Any, Dict, List, Optional, Tuple, Union
from dataclasses import dataclass
from transformers.modeling_outputs import ModelOutput
from transformers.models.bloom import modeling_bloom
from transformers.models.opt import modeling_opt
from huggingface_hub import hf_hub_download
from src.utils.modeling_utils import (
hf_get_decoder,
hf_get_decoder_blocks,
hf_get_decoder_final_norm,
hf_get_hidden_size,
hf_get_lm_head,
hf_get_num_hidden_layers,
make_head
)
from src.models.lora import convert_to_lora_recursively
from src.models.sft import SFTModelWithLoRA
class PreTrainedModelWrapper(nn.Module, transformers.utils.PushToHubMixin):
"""A wrapper around `transformers.PreTrainedModel`
Reference: @younesbelkada's `PreTrainedModelWrapper`
https://github.com/lvwerra/trl/blob/4f5c16fafde42d9aca971952bcdcc1f5a0a68cf0/trl/models/modeling_base.py#L2
Attributes:
_auto_model_parent_class (transformers.AutoModel): The `transformers.AutoModel`
type to base the wrapping behavior off of, e.g. `transformers.AutoModelForCausalLM`.
_supported_modules (List[str]): A list of attribute names for modules of
the underlying architecture model. This is used, for example, to save
and load any additional modules by manipulating the state dict.
_supported_args (List[str]): A list of arguments specific to the underlying
architecture to separate from arguments that are supported by the
parent `AutoModel` class. Any arguments that are not supported by the
underlying model will be passed to the parent `AutoModel` class.
"""
_auto_model_parent_class: transformers.AutoModel = None
_supported_modules: List[str] = None
# TODO (jon-tow): Supported args should come from a `PretrainedConfig` of the
# specific underlying type similar to how config instances can be used to instantiate
# `transformers.PreTrainedModel`s.
_supported_args: List[str] = None
def __init__(self, base_model: Optional[transformers.PreTrainedModel] = None, **kwargs):
super().__init__()
self.base_model = base_model
# cache `forward` args for general use (avoids incompatible args across architectures)
self.forward_kwargs = inspect.getfullargspec(self.base_model.forward).args
@classmethod
def _split_kwargs(cls, kwargs: Dict[str, Any]):
"""Separates the kwargs from the supported arguments within `supported_args`
and those that are not
"""
supported_kwargs = {}
unsupported_kwargs = {}
for key, value in kwargs.items():
if key in cls._supported_args:
supported_kwargs[key] = value
else:
unsupported_kwargs[key] = value
return supported_kwargs, unsupported_kwargs
@classmethod
def from_config(cls, config: transformers.PretrainedConfig, **kwargs):
"""Instantiate the pretrained pytorch model from a configuration.
Args:
config (transformers.PretrainedConfig): The configuration to use to
instantiate the base model.
NOTE: Loading a model from its configuration file does **not** load the
model weights. It only affects the model's configuration. Use
`~transformers.AutoModel.from_pretrained` to load the model weights.
"""
if kwargs is not None:
wrapped_model_kwargs, from_config_kwargs = cls._split_kwargs(kwargs)
else:
from_config_kwargs = {}
wrapped_model_kwargs = {}
base_model = cls._auto_model_parent_class.from_config(config, **from_config_kwargs)
model = cls(base_model, **wrapped_model_kwargs)
return model
@classmethod
def from_pretrained( # noqa: max-complexity
cls,
pretrained_model_name_or_path: Union[str, transformers.PreTrainedModel],
*model_args,
**kwargs,
):
"""Instantiate a pretrained pytorch model from a pretrained model configuration.
This method is a wrapper around `transformers.PreTrainedModel.from_pretrained`.
Please refer to the documentation of `transformers.PreTrainedModel.from_pretrained`
for more information.
Args:
pretrained_model_name_or_path (str or `transformers.PreTrainedModel`):
The identifier of the pretrained model to load or the pretrained model itself.
*model_args (sequence of positional arguments, *optional*):
All remaining positional arguments will be passed to the `_auto_model_parent_class`.
**kwargs (dict, *optional*):
Dictionary of keyword arguments to pass to both the underlying `_auto_model_parent_class`
call (e.g. `transformers.AutoModelForCausalLM.from_pretrained`) and the specific
instance of the wrapped model.
NOTE: You must pass in arguments specific to the wrapped model as keyword arguments.
"""
if kwargs is not None:
wrapped_model_kwargs, from_pretrained_kwargs = cls._split_kwargs(kwargs)
else:
from_pretrained_kwargs = {}
wrapped_model_kwargs = {}
if isinstance(pretrained_model_name_or_path, str):
# Load the base model using the `transformers` AutoClass (e.g. AutoModelForCausalLM)
base_model = cls._auto_model_parent_class.from_pretrained(
pretrained_model_name_or_path, *model_args, **from_pretrained_kwargs
)
elif isinstance(pretrained_model_name_or_path, transformers.PreTrainedModel):
base_model = pretrained_model_name_or_path
else:
raise ValueError(
f"Invalid type for `base_model_name_or_path`: {type(pretrained_model_name_or_path)}"
"Expected `str` or `transformers.PreTrainedModel`."
)
config = from_pretrained_kwargs.get("config", None)
if config is not None:
base_model.config.lora_rank = config.train.lora_rank
base_model.config.lora_alpha = config.train.lora_alpha
base_model.config.lora_train_bias = config.train.lora_train_bias
model = cls(base_model, **wrapped_model_kwargs)
if isinstance(pretrained_model_name_or_path, str):
filename = os.path.join(pretrained_model_name_or_path, "pytorch_model.bin")
sharded_index_filename = os.path.join(pretrained_model_name_or_path, "pytorch_model.bin.index.json")
is_sharded = False
if not os.path.exists(filename):
try:
filename = hf_hub_download(pretrained_model_name_or_path, "pytorch_model.bin")
# Sharded
except Exception:
if os.path.exists(sharded_index_filename):
index_file_name = sharded_index_filename
else:
index_file_name = hf_hub_download(
pretrained_model_name_or_path,
"pytorch_model.bin.index.json",
)
with open(index_file_name, "r") as f:
index = json.load(f)
# Collect files containing weights from supported modules
files_to_download = set()
for k, v in index["weight_map"].items():
if any([module in k for module in cls._supported_modules]):
files_to_download.add(v)
is_sharded = True
if is_sharded:
# Merge each shard into a state dict
# TODO: Optimize this to avoid wasting RAM
state_dict = {}
for shard_file in files_to_download:
filename = os.path.join(pretrained_model_name_or_path, shard_file)
# Download if shard file doesn't exist locally
if not os.path.exists(filename):
filename = hf_hub_download(pretrained_model_name_or_path, shard_file)
state_dict.update(torch.load(filename, map_location="cpu"))
else:
state_dict = torch.load(filename, map_location="cpu")
else:
state_dict = pretrained_model_name_or_path.state_dict()
model.post_init(state_dict=state_dict)
return model
def save_pretrained(self, *args, **kwargs):
"""Save the pretrained model to a directory. This method is a wrapper
around `transformers.PreTrainedModel.save_pretrained`. Please refer to
the documentation of `transformers.PreTrainedModel.save_pretrained` for
more information.
Args:
*args (`list`, *optional*):
Positional arguments passed along to the underlying model's
`save_pretrained` method.
**kwargs (`dict`, *optional*):
Keyword arguments passed along to the underlying model's
`save_pretrained` method.
"""
state_dict = kwargs.pop("state_dict", None)
if state_dict is None:
state_dict = self.state_dict()
kwargs["state_dict"] = state_dict
return self.base_model.save_pretrained(*args, **kwargs)
def state_dict(self, *args, **kwargs):
"""Return the state_dict of the pretrained model."""
raise NotImplementedError
def post_init(self, *args, **kwargs):
"""Post initialization method. This method is called after the model is
instantiated and loaded from a checkpoint. It can be used to perform
additional operations such as loading the state_dict.
"""
raise NotImplementedError
def get_compatible_forward_kwargs(self, **kwargs) -> Dict[str, Any]:
"""Filter out arguments not supported by the specific instance of
`base_model.transformer.forward`
"""
# FIXME: This is a hack to get around the fact that the `transformers`
# architectures we use don't have a consistent API for `forward` parameters.
return {k: v for k, v in kwargs.items() if k in self.forward_kwargs}
# KL Controllers
class AdaptiveKLController:
"""Adaptive KL Controller as described in Ziegler et al. "Fine-Tuning Language Models from Human Preferences"
Reference: Section 2.2 https://arxiv.org/pdf/1909.08593.pdf#page=2
Source: https://github.com/openai/lm-human-preferences/blob/master/lm_human_preferences/train_policy.py
"""
def __init__(self, init_kl_coef: float, target: float, horizon: int):
self.value = init_kl_coef
self.target = target
self.horizon = horizon
def update(self, current: float, n_steps: int):
"""Returns adaptively updated KL coefficient, βₜ₊₁.
Arguments:
current: The current KL value between the newest policy and the initial policy.
"""
proportional_error = np.clip(current / self.target - 1, -0.2, 0.2) # ϵₜ
mult = 1 + proportional_error * n_steps / self.horizon
self.value *= mult # βₜ₊₁
class FixedKLController:
"""Fixed KL controller."""
def __init__(self, kl_coef):
self.value = kl_coef
def update(self, current: float, n_steps: int):
"""Returns updated KL coefficient, βₜ₊₁.
Arguments:
current: The current KL value between the newest policy and the initial policy.
"""
pass
# CausalLM architectures
@dataclass
class CausalLMOutputWithValue(ModelOutput):
loss: Optional[torch.FloatTensor] = None
logits: Optional[torch.FloatTensor] = None
past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None
hidden_states: Optional[Tuple[torch.FloatTensor]] = None
attentions: Optional[Tuple[torch.FloatTensor]] = None
cross_attentions: Optional[Tuple[torch.FloatTensor]] = None
value: Optional[torch.FloatTensor] = None
class AutoModelForCausalLMWithValueHead(PreTrainedModelWrapper):
"""An `AutoModel` class wrapper for `transformers` causal models that have a
language modeling head and a value head
"""
_auto_model_parent_class = transformers.AutoModelForCausalLM
_supported_modules = ["v_head"]
_supported_args = []
def __init__(
self,
base_model: transformers.PreTrainedModel,
**kwargs
):
super().__init__(base_model)
self.v_head = make_head(hf_get_hidden_size(self.base_model.config), 1)
def forward(
self,
input_ids: torch.LongTensor = None,
attention_mask: Optional[torch.Tensor] = None,
past_key_values: Optional[List[torch.FloatTensor]] = None,
position_ids: Optional[List[torch.FloatTensor]] = None,
head_mask: Optional[torch.Tensor] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
) -> Union[Tuple, CausalLMOutputWithValue]:
forward_kwargs = self.get_compatible_forward_kwargs(
input_ids=input_ids,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_values=past_key_values,
head_mask=head_mask,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
forward_kwargs["output_hidden_states"] = True
forward_kwargs["return_dict"] = True
outputs = self.base_model(**forward_kwargs)
value = self.v_head(outputs.hidden_states[-1]).squeeze(-1)
if not return_dict:
outputs = (outputs.logits,) + outputs[1:] + (value,)
return outputs
return CausalLMOutputWithValue(**outputs, value=value)
def generate(self, *args, **kwargs) -> Union[ModelOutput, torch.LongTensor]:
return self.base_model.generate(*args, **kwargs)
def state_dict(self, *args, **kwargs):
"""
Returns the state dictionary of the model. We add the state dictionary of the value head
to the state dictionary of the wrapped model by prepending the key with `v_head.`.
"""
base_model_state_dict = self.base_model.state_dict(*args, **kwargs)
v_head_state_dict = self.v_head.state_dict(*args, **kwargs)
for k, v in v_head_state_dict.items():
base_model_state_dict[f"v_head.{k}"] = v
return base_model_state_dict
def post_init(self, state_dict):
"""
Adds the state dictionary of the value head to the state dictionary of the wrapped model
by prepending the key with `v_head.`. This function removes the `v_head.` prefix from the
keys of the value head state dictionary.
"""
for k in list(state_dict.keys()):
if "v_head." in k:
state_dict[k.replace("v_head.", "")] = state_dict.pop(k)
self.v_head.load_state_dict(state_dict, strict=False)
del state_dict
gc.collect() # noqa: E702
class AutoModelForCausalLMWithHydraValueHead(AutoModelForCausalLMWithValueHead):
_supported_modules = ["v_head", "frozen_head"]
_supported_args = ["num_layers_unfrozen"]
def __init__(
self,
base_model: transformers.PreTrainedModel,
num_layers_unfrozen: int = -1,
):
super().__init__(base_model)
self.num_layers_unfrozen = num_layers_unfrozen
if self.num_layers_unfrozen > 0:
config = self.base_model.config
branch_class = hf_get_branch_class(config)
self.frozen_head = branch_class(
self.base_model,
num_layers_unfrozen=self.num_layers_unfrozen,
).eval()
if base_model.config.lora_rank > 0:
convert_to_lora_recursively(base_model, base_model.config.lora_rank, base_model.config.lora_alpha)
lora.mark_only_lora_as_trainable(base_model, base_model.config.lora_train_bias)
def forward_hydra(
self,
input_ids: torch.LongTensor = None,
attention_mask: Optional[torch.Tensor] = None,
past_key_values: Optional[List[torch.FloatTensor]] = None,
position_ids: Optional[List[torch.FloatTensor]] = None,
head_mask: Optional[torch.Tensor] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
) -> Union[torch.FloatTensor, CausalLMOutputWithValue]:
forward_kwargs = self.get_compatible_forward_kwargs(
input_ids=input_ids,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_values=past_key_values,
head_mask=head_mask,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
return_dict = forward_kwargs.get("return_dict", True)
forward_kwargs["return_dict"] = True
forward_kwargs["output_hidden_states"] = True
outputs = self.forward(**forward_kwargs)
# Select the hidden state before the first branching layer
input_hidden_state = outputs.hidden_states[-(self.num_layers_unfrozen + 1)]
output_shape = outputs.hidden_states[-1].size()
forward_kwargs.pop("input_ids", None) # Ignore `input_ids` for branch head
forward_kwargs.pop("inputs_embeds", None) # Ignore `inputs_embeds` for branch head
hydra_outputs = self.frozen_head(input_hidden_state, output_shape, **forward_kwargs)
if not return_dict:
return hydra_outputs.logits
return hydra_outputs
@classmethod
def from_pretrained( # noqa: max-complexity
cls,
pretrained_model_name_or_path: Union[str, transformers.PreTrainedModel],
*model_args,
**kwargs,
):
"""Instantiate a pretrained pytorch model from a pretrained model configuration.
This method is a wrapper around `transformers.PreTrainedModel.from_pretrained`.
Please refer to the documentation of `transformers.PreTrainedModel.from_pretrained`
for more information.
Args:
pretrained_model_name_or_path (str or `transformers.PreTrainedModel`):
The identifier of the pretrained model to load or the pretrained model itself.
*model_args (sequence of positional arguments, *optional*):
All remaining positional arguments will be passed to the `_auto_model_parent_class`.
**kwargs (dict, *optional*):
Dictionary of keyword arguments to pass to both the underlying `_auto_model_parent_class`
call (e.g. `transformers.AutoModelForCausalLM.from_pretrained`) and the specific
instance of the wrapped model.
NOTE: You must pass in arguments specific to the wrapped model as keyword arguments.
"""
if kwargs is not None:
wrapped_model_kwargs, from_pretrained_kwargs = cls._split_kwargs(kwargs)
else:
from_pretrained_kwargs = {}
wrapped_model_kwargs = {}
if isinstance(pretrained_model_name_or_path, str):
# Load the base model using the `transformers` AutoClass (e.g. AutoModelForCausalLM)
base_model = cls._auto_model_parent_class.from_pretrained(
pretrained_model_name_or_path, *model_args, **from_pretrained_kwargs
)
elif isinstance(pretrained_model_name_or_path, transformers.PreTrainedModel):
base_model = pretrained_model_name_or_path
else:
raise ValueError(
f"Invalid type for `base_model_name_or_path`: {type(pretrained_model_name_or_path)}"
"Expected `str` or `transformers.PreTrainedModel`."
)
# TODO: add model.resize_token_embeddings(tokenizer.vocab_size)
config = from_pretrained_kwargs.get("config", None)
if config is not None:
base_model.config.lora_rank = config.train.lora_rank
base_model.config.lora_alpha = config.train.lora_alpha
base_model.config.lora_train_bias = config.train.lora_train_bias
if isinstance(pretrained_model_name_or_path, str):
filename = os.path.join(pretrained_model_name_or_path, "pytorch_model.bin")
sharded_index_filename = os.path.join(pretrained_model_name_or_path, "pytorch_model.bin.index.json")
is_sharded = False
if not os.path.exists(filename):
try:
filename = hf_hub_download(pretrained_model_name_or_path, "pytorch_model.bin")
# Sharded
except Exception:
if os.path.exists(sharded_index_filename):
index_file_name = sharded_index_filename
else:
index_file_name = hf_hub_download(
pretrained_model_name_or_path,
"pytorch_model.bin.index.json",
)
with open(index_file_name, "r") as f:
index = json.load(f)
# Collect files containing weights from supported modules
files_to_download = set()
for k, v in index["weight_map"].items():
if any([module in k for module in cls._supported_modules]):
files_to_download.add(v)
is_sharded = True
if is_sharded:
# Merge each shard into a state dict
# TODO: Optimize this to avoid wasting RAM
state_dict = {}
for shard_file in files_to_download:
filename = os.path.join(pretrained_model_name_or_path, shard_file)
# Download if shard file doesn't exist locally
if not os.path.exists(filename):
filename = hf_hub_download(pretrained_model_name_or_path, shard_file)
state_dict.update(torch.load(filename, map_location="cpu"))
else:
state_dict = torch.load(filename, map_location="cpu")
else:
state_dict = pretrained_model_name_or_path.state_dict()
# Check if sft model is LoRA checkpoint, load the state dict into model
is_lora_checkpoint = False
for key in state_dict.keys():
if "lora" in key:
is_lora_checkpoint = True
break
if is_lora_checkpoint:
base_model = SFTModelWithLoRA(base_model.config, base_model)
res = base_model.load_state_dict(state_dict, strict=False)
model = cls(base_model, **wrapped_model_kwargs)
model.post_init(state_dict=state_dict)
return model
class ModelBranch(transformers.PreTrainedModel):
"""Implements the frozen upper trunk of the pretrained reference model used
when computing the PPO KL-divergence penalty.
"""
def __init__(
self,
base_model: transformers.PreTrainedModel,
*,
num_layers_unfrozen: int,
):
"""
Args:
base_model (transformers.PreTrainedModel): The pretrained model to extract upper trunk from
num_layers_unfrozen (int): The number of trainable layers
"""
super().__init__(base_model.config)
# The branch is defined by the last `num_layers_unfrozen` layers of the pretrained model
decoder_blocks = deepcopy(hf_get_decoder_blocks(base_model))
self.decoder_blocks = nn.ModuleList(list(decoder_blocks)[-num_layers_unfrozen:])
self.final_norm = deepcopy(hf_get_decoder_final_norm(base_model))
self.lm_head = deepcopy(hf_get_lm_head(base_model))
self.hidden_size = hf_get_hidden_size(self.config)
self.model_parallel = False
self.device_map = None
self.last_device = None
self.gradient_checkpointing = False
# Freeze the entire branch
for parameter in self.parameters():
parameter.requires_grad_(False)
class GPTModelBranch(ModelBranch):
def forward( # noqa: max-complexity
self,
hidden_states: torch.Tensor, # Takes as input hidden_states instead of input_ids
output_shape: torch.Tensor, # output_size given by main trunk
past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
attention_mask: Optional[torch.FloatTensor] = None,
position_ids: Optional[torch.LongTensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
encoder_hidden_states: Optional[torch.Tensor] = None,
encoder_attention_mask: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = False,
) -> Union[Tuple, CausalLMOutputWithValue]:
"""Reference:
https://github.com/huggingface/transformers/blob/2411f0e465e761790879e605a4256f3d4afb7f82/src/transformers/models/gpt2/modeling_gpt2.py#L743 # noqa: E501
"""
batch_size = hidden_states.size()[0]
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
output_hidden_states = (
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
)
use_cache = use_cache if use_cache is not None else self.config.use_cache
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
device = hidden_states.device
if past_key_values is None:
past_key_values = tuple([None] * len(self.decoder_blocks))
if attention_mask is not None:
if batch_size <= 0:
raise ValueError("batch_size has to be defined and > 0")
attention_mask = attention_mask.view(batch_size, -1)
attention_mask = attention_mask[:, None, None, :]
attention_mask = attention_mask.to(dtype=self.dtype) # fp16 compatibility
attention_mask = (1.0 - attention_mask) * torch.finfo(self.dtype).min
if self.config.add_cross_attention and encoder_hidden_states is not None:
(
encoder_batch_size,
encoder_sequence_length,
_,
) = encoder_hidden_states.size()
encoder_hidden_shape = (encoder_batch_size, encoder_sequence_length)
if encoder_attention_mask is None:
encoder_attention_mask = torch.ones(encoder_hidden_shape, device=device)
encoder_attention_mask = self.invert_attention_mask(encoder_attention_mask)
else:
encoder_attention_mask = None
head_mask = self.get_head_mask(head_mask, hf_get_num_hidden_layers(self.config))
presents = () if use_cache else None
all_self_attentions = () if output_attentions else None
all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None
all_hidden_states = () if output_hidden_states else None
for i, (block, layer_past) in enumerate(zip(self.decoder_blocks, past_key_values)):
if self.model_parallel:
torch.cuda.set_device(hidden_states.device)
if layer_past is not None:
layer_past = tuple(past_state.to(hidden_states.device) for past_state in layer_past)
if attention_mask is not None:
attention_mask = attention_mask.to(hidden_states.device)
if isinstance(head_mask, torch.Tensor):
head_mask = head_mask.to(hidden_states.device)
if output_hidden_states:
all_hidden_states = all_hidden_states + (hidden_states,)
# Assumes we are never training the branch
block_params = inspect.getfullargspec(block.forward).args
if "encoder_hidden_states" in block_params:
outputs = block(
hidden_states,
layer_past=layer_past,
attention_mask=attention_mask,
head_mask=head_mask[i],
encoder_hidden_states=encoder_hidden_states,
encoder_attention_mask=encoder_attention_mask,
use_cache=use_cache,
output_attentions=output_attentions,
)
else:
outputs = block(
hidden_states,
layer_past=layer_past,
attention_mask=attention_mask,
head_mask=head_mask[i],
use_cache=use_cache,
output_attentions=output_attentions,
)
hidden_states = outputs[0]
if use_cache is True:
presents = presents + (outputs[1],)
if output_attentions:
all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],)
if self.config.add_cross_attention:
all_cross_attentions = all_cross_attentions + (outputs[3 if use_cache else 2],)
if self.model_parallel:
for k, v in self.device_map.items():
if i == v[-1] and "cuda:" + str(k) != self.last_device:
hidden_states = hidden_states.to("cuda:" + str(k + 1))
hidden_states = self.final_norm(hidden_states)
hidden_states = hidden_states.view(output_shape)
if output_hidden_states:
all_hidden_states = all_hidden_states + (hidden_states,)
if self.model_parallel:
torch.cuda.set_device(self.transformer.first_device)
hidden_states = hidden_states.to(self.lm_head.weight.device)
lm_logits = self.lm_head(hidden_states)
if not return_dict:
outputs = (lm_logits,) + (None,) + (None,)
return outputs
return CausalLMOutputWithValue(
logits=lm_logits,
past_key_values=presents,
hidden_states=all_hidden_states,
attentions=all_self_attentions,
cross_attentions=all_cross_attentions,
)
class OPTModelBranch(ModelBranch):
def forward( # noqa: max-complexity
self,
hidden_states: torch.Tensor,
output_shape: torch.Tensor,
past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
attention_mask: Optional[torch.FloatTensor] = None,
position_ids: Optional[torch.LongTensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
encoder_hidden_states: Optional[torch.Tensor] = None,
encoder_attention_mask: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = False,
) -> Union[Tuple, CausalLMOutputWithValue]:
"""Reference:
https://github.com/huggingface/transformers/blob/bdb84e2bada3658f99c6a81c963ec562f8485151/src/transformers/models/opt/modeling_opt.py#L840 # noqa: E501
"""
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
output_hidden_states = (
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
)
use_cache = use_cache if use_cache is not None else self.config.use_cache
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
past_key_values_length = past_key_values[0][0].shape[2] if past_key_values is not None else 0
if attention_mask is None:
attention_mask = torch.ones(hidden_states.shape[:2], dtype=torch.bool, device=hidden_states.device)
input_shape = hidden_states.size()[:-1]
combined_attention_mask = None
if input_shape[-1] > 1:
combined_attention_mask = modeling_opt._make_causal_mask(
input_shape,
hidden_states.dtype,
past_key_values_length=past_key_values_length,
).to(hidden_states.device)
if attention_mask is not None:
expanded_attn_mask = modeling_opt._expand_mask(
attention_mask, hidden_states.dtype, tgt_len=input_shape[-1]
).to(hidden_states.device)
combined_attention_mask = (
expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
)
attention_mask = combined_attention_mask
all_hidden_states = () if output_hidden_states else None
all_self_attns = () if output_attentions else None
next_decoder_cache = () if use_cache else None
for attn_mask, mask_name in zip([head_mask], ["head_mask"]):
if attn_mask is not None:
if attn_mask.size()[0] != (len(self.decoder_blocks)):
raise ValueError(
f"The `{mask_name}` should be specified for {len(self.decoder_blocks)} layers, but it is for"
f" {head_mask.size()[0]}."
)
for idx, decoder_layer in enumerate(self.decoder_blocks):
if output_hidden_states:
all_hidden_states += (hidden_states,)
past_key_value = past_key_values[idx] if past_key_values is not None else None
layer_outputs = decoder_layer(
hidden_states,
past_key_value=past_key_value,
attention_mask=attention_mask,
layer_head_mask=(head_mask[idx] if head_mask is not None else None),
use_cache=use_cache,
output_attentions=output_attentions,
)
hidden_states = layer_outputs[0]
if use_cache:
next_decoder_cache += (layer_outputs[2 if output_attentions else 1],)
if output_attentions:
all_self_attns += (layer_outputs[1],)
if self.final_norm is not None:
hidden_states = self.final_norm(hidden_states)
# TODO: Add output projection support
# https://github.com/huggingface/transformers/blob/699e90437f984d69ad3c9b891dd2e9d0fc2cffe4/src/transformers/models/opt/modeling_opt.py#L499 # noqa: E501
# if self.project_out is not None:
# hidden_states = self.project_out(hidden_states)
if output_hidden_states:
all_hidden_states += (hidden_states,)
next_cache = next_decoder_cache if use_cache else None
lm_logits = self.lm_head(hidden_states).contiguous()
if not return_dict:
return tuple(
v
for v in [
lm_logits,
hidden_states,
next_cache,
all_hidden_states,
all_self_attns,
]
if v is not None
)
return CausalLMOutputWithValue(
logits=lm_logits,
past_key_values=next_cache,
hidden_states=all_hidden_states,
attentions=all_self_attns,
)
class BloomModelBranch(ModelBranch):
def forward( # noqa: max-complexity
self,
hidden_states: torch.Tensor, # Takes as input hidden_states instead of input_ids
output_shape: torch.Tensor,
past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
attention_mask: Optional[torch.FloatTensor] = None,
position_ids: Optional[torch.LongTensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
encoder_hidden_states: Optional[torch.Tensor] = None,
encoder_attention_mask: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = False,
) -> Union[Tuple, CausalLMOutputWithValue]:
"""Reference:
https://github.com/huggingface/transformers/blob/2411f0e465e761790879e605a4256f3d4afb7f82/src/transformers/models/bloom/modeling_bloom.py#L623 # noqa: E501
"""
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
output_hidden_states = (
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
)
use_cache = use_cache if use_cache is not None else self.config.use_cache
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
batch_size, seq_length = hidden_states.shape[:2]
if past_key_values is None:
past_key_values = tuple([None] * len(self.decoder_blocks))
head_mask = self.get_head_mask(head_mask, hf_get_num_hidden_layers(self.config))
presents = () if use_cache else None
all_self_attentions = () if output_attentions else None
all_hidden_states = () if output_hidden_states else None
seq_length_with_past = seq_length
past_key_values_length = 0
if past_key_values[0] is not None:
past_key_values_length = past_key_values[0][0].shape[2]
seq_length_with_past = seq_length_with_past + past_key_values_length
if attention_mask is None:
attention_mask = torch.ones((batch_size, seq_length_with_past), device=hidden_states.device)
else:
attention_mask = attention_mask.to(hidden_states.device)
alibi = modeling_bloom.build_alibi_tensor(attention_mask, self.config.n_head, dtype=hidden_states.dtype)
combined_attention_mask = None
device = attention_mask.device
input_shape = (batch_size, seq_length)
_, src_length = input_shape
if src_length > 1:
combined_attention_mask = modeling_bloom._make_causal_mask(
input_shape,
device=device,
past_key_values_length=past_key_values_length,
)
expanded_attn_mask = modeling_bloom._expand_mask(attention_mask, tgt_length=src_length)
combined_attention_mask = (
expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask | combined_attention_mask
)
causal_mask = combined_attention_mask
for i, (block, layer_past) in enumerate(zip(self.decoder_blocks, past_key_values)):
if output_hidden_states:
all_hidden_states = all_hidden_states + (hidden_states,)
outputs = block(
hidden_states,
layer_past=layer_past,
attention_mask=causal_mask,
head_mask=head_mask[i],
use_cache=use_cache,
output_attentions=output_attentions,
alibi=alibi,
)
hidden_states = outputs[0]
if use_cache is True:
presents = presents + (outputs[1],)
if output_attentions:
all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],)
hidden_states = self.final_norm(hidden_states)
if output_hidden_states:
all_hidden_states = all_hidden_states + (hidden_states,)
lm_logits = self.lm_head(hidden_states)
if not return_dict:
return tuple(
v
for v in [
lm_logits,
hidden_states,
presents,
all_hidden_states,
all_self_attentions,
]
if v is not None
)
return CausalLMOutputWithValue(
logits=lm_logits,
past_key_values=presents,
hidden_states=all_hidden_states,
attentions=all_self_attentions,
)
# Seq2Seq architectures
@dataclass
class Seq2SeqLMOutputWithValue(ModelOutput):
loss: Optional[torch.FloatTensor] = None
logits: Optional[torch.FloatTensor] = None
past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None
decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None
decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None
cross_attentions: Optional[Tuple[torch.FloatTensor]] = None
encoder_last_hidden_state: Optional[torch.FloatTensor] = None
encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None
encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None
value: Optional[torch.FloatTensor] = None
class AutoModelForSeq2SeqLMWithValueHead(PreTrainedModelWrapper):
"""An `AutoModel` class wrapper for `transformers` sequence-to-sequence
models that have a language modeling head and a value head
"""
_auto_model_parent_class = transformers.AutoModelForSeq2SeqLM
_supported_modules = ["v_head"]
_supported_args = []
def __init__(
self,
base_model: transformers.PreTrainedModel,
**kwargs
):
super().__init__(base_model)
self.v_head = make_head(hf_get_hidden_size(self.base_model.config), 1)
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.FloatTensor] = None,
position_ids: Optional[torch.FloatTensor] = None,
decoder_input_ids: Optional[torch.LongTensor] = None,
decoder_attention_mask: Optional[torch.FloatTensor] = None,
encoder_outputs: Optional[Tuple[torch.FloatTensor]] = None,
past_key_values: Optional[Tuple[torch.FloatTensor]] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
decoder_inputs_embeds: Optional[torch.FloatTensor] = None,