/
modeling_ppo.py
1127 lines (954 loc) · 44.4 KB
/
modeling_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
import gc
import inspect
from copy import deepcopy
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union
import numpy as np
import torch
import torch.nn as nn
import transformers
from torchtyping import TensorType
from transformers.modeling_outputs import ModelOutput
from transformers.models.bloom import modeling_bloom
from transformers.models.opt import modeling_opt
from trlx.data.method_configs import MethodConfig, register_method
from trlx.models.modeling_base import PreTrainedModelWrapper
from trlx.utils.modeling import (
flatten_dict,
get_tensor_stats,
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,
whiten,
)
# 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
# PPO Configs
@dataclass
@register_method
class PPOConfig(MethodConfig):
"""
Config for PPO method
:param ppo_epochs: Number of updates per batch
:type ppo_epochs: int
:param num_rollouts: Number of experiences to observe before learning
:type num_rollouts: int
:param init_kl_coef: Initial value for KL coefficient
:type init_kl_coef: float
:param target: Target value for KL coefficient
:type target: float
:param horizon: Number of steps for KL coefficient to reach target
:type horizon: int
:param gamma: Discount factor
:type gamma: float
:param lam: GAE lambda
:type lam: float
:param cliprange: Clipping range for PPO policy loss (1 - cliprange, 1 + cliprange)
:type cliprange: float
:param cliprange_value: Clipping range for predicted values
(observed values - cliprange_value, observed values + cliprange_value)
:type cliprange_value: float
:param vf_coef: Value loss scale w.r.t policy loss
:type vf_coef: float
:param gen_kwargs: Additioanl kwargs for the generation
:type gen_kwargs: Dict[str, Any]
:param gen_experience_kwargs: if this is not None, then the experience is generated using this
:type gen_experience_kwargs: Dict[str, Any]
"""
ppo_epochs: int
num_rollouts: int
chunk_size: int
init_kl_coef: float
target: float
horizon: int
gamma: float
lam: float
cliprange: float
cliprange_value: float
vf_coef: float
scale_reward: Optional[str]
ref_mean: Optional[float]
ref_std: Optional[float]
cliprange_reward: float
gen_kwargs: dict
gen_experience_kwargs: Optional[dict] = None
def get_advantages_and_returns(
self,
values: TensorType["batch_size", "response_size"],
rewards: TensorType["batch_size", "response_size"],
response_length: int,
use_whitening: Optional[bool] = True,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""Function that computes advantages and returns from rewards and values.
Calculated as in the original PPO paper: https://arxiv.org/abs/1707.06347
Note that rewards may include a KL divergence loss term.
Advantages looks like this:
Adv1 = R1 + γ * λ * R2 + γ^2 * λ^2 * R3 + ...
- V1 + γ * (1 - λ) V2 + γ^2 * λ * (1 - λ) V3 + ...
Returns looks like this:
Ret1 = R1 + γ * λ * R2 + γ^2 * λ^2 * R3 + ...
+ γ * (1 - λ) V2 + γ^2 * λ * (1 - λ) V3 + ...
Args:
values: Tensor of shape (batch_size, response_size)
rewards: Tensor of shape (batch_size, response_size)
response_length: Length of the response sequence
use_whitening: Whether to use whitening (ie. normalize advantages) or not
"""
lastgaelam = 0
advantages_reversed = []
for t in reversed(range(response_length)):
nextvalues = values[:, t + 1] if t < response_length - 1 else 0.0
delta = rewards[:, t] + self.gamma * nextvalues - values[:, t]
lastgaelam = delta + self.gamma * self.lam * lastgaelam
advantages_reversed.append(lastgaelam)
advantages = torch.stack(advantages_reversed[::-1], dim=1)
returns = advantages + values
if use_whitening:
advantages = whiten(advantages)
return advantages.detach(), returns
def loss(
self,
logprobs: TensorType["batch_size", "response_size"],
values: TensorType["batch_size", "response_size"],
old_logprobs: TensorType["batch_size", "response_size"],
old_values: TensorType["batch_size", "response_size"],
advantages: TensorType["batch_size", "response_size"],
returns: TensorType["batch_size", "response_size"],
mask: TensorType["batch_size", "response_size"],
):
"""PPO objective function.
References:
- https://stable-baselines.readthedocs.io/en/master/modules/ppo2.html
"""
values_clipped = torch.clamp(
values,
old_values - self.cliprange_value,
old_values + self.cliprange_value,
)
n = mask.sum()
vf_loss1 = (values - returns) ** 2
vf_loss2 = (values_clipped - returns) ** 2
vf_loss = 0.5 * torch.sum(torch.max(vf_loss1, vf_loss2) * mask) / n
vf_clipfrac = torch.sum((vf_loss2 > vf_loss1).float() * mask) / n
log_ratio = (logprobs - old_logprobs) * mask
ratio = torch.exp(log_ratio)
# Unbiased KL-div estimates (`k3`). Ref: http://joschu.net/blog/kl-approx.html
with torch.no_grad():
approx_kl = torch.mean((ratio - 1) - log_ratio)
pg_loss1 = -advantages * ratio
pg_loss2 = -advantages * torch.clamp(
ratio,
1.0 - self.cliprange,
1.0 + self.cliprange,
)
pg_loss = torch.sum(torch.max(pg_loss1, pg_loss2) * mask) / n
pg_clipfrac = torch.sum((pg_loss2 > pg_loss1).float() * mask) / n
loss = pg_loss + self.vf_coef * vf_loss
stats = dict(
losses=dict(
total_loss=loss.item(),
policy_loss=pg_loss.item(),
value_loss=vf_loss.item(),
),
values=dict(
get_tensor_stats(values, mask, n),
values_error=torch.sum(((values - returns) * mask) ** 2) / n,
clipfrac=vf_clipfrac,
),
old_values=get_tensor_stats(old_values, mask, n),
returns=get_tensor_stats(returns, mask, n),
policy=dict(approx_kl=approx_kl.item(), clipfrac=pg_clipfrac.item()),
ratio=(ratio * mask).sum() / n,
padding_percentage=n / mask.numel(),
)
return loss, flatten_dict(stats)
# 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,
):
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()
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
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,
):
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,
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,
head_mask: Optional[torch.FloatTensor] = None,
decoder_head_mask: Optional[torch.FloatTensor] = None,
cross_attn_head_mask: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = True,
output_hidden_states: Optional[bool] = True,
return_dict: Optional[bool] = None,
) -> Seq2SeqLMOutputWithValue:
forward_kwargs = self.get_compatible_forward_kwargs(
input_ids=input_ids,
attention_mask=attention_mask,
decoder_input_ids=decoder_input_ids,
decoder_attention_mask=decoder_attention_mask,
encoder_outputs=encoder_outputs,
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
decoder_inputs_embeds=decoder_inputs_embeds,
head_mask=head_mask,
decoder_head_mask=decoder_head_mask,
cross_attn_head_mask=cross_attn_head_mask,
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)
last_hidden_state = outputs.decoder_hidden_states[-1]
value = self.v_head(last_hidden_state).squeeze(-1)
return Seq2SeqLMOutputWithValue(**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):
"""
We add 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 AutoModelForSeq2SeqLMWithHydraValueHead(AutoModelForSeq2SeqLMWithValueHead):
_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:
branch_class = T5Branch # TODO: Add support for other model branches
self.frozen_head = branch_class(
self.base_model,
num_layers_unfrozen=self.num_layers_unfrozen,
).eval()
def forward_hydra(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: 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,
head_mask: Optional[torch.FloatTensor] = None,
decoder_head_mask: Optional[torch.FloatTensor] = None,
cross_attn_head_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] = None,
) -> Seq2SeqLMOutputWithValue:
forward_kwargs = self.get_compatible_forward_kwargs(
input_ids=input_ids,
attention_mask=attention_mask,
decoder_input_ids=decoder_input_ids,
decoder_attention_mask=decoder_attention_mask,
encoder_outputs=encoder_outputs,
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
decoder_inputs_embeds=decoder_inputs_embeds,
head_mask=head_mask,
decoder_head_mask=decoder_head_mask,
cross_attn_head_mask=cross_attn_head_mask,
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["output_hidden_states"] = True
forward_kwargs["return_dict"] = True
outputs = self.forward(**forward_kwargs)
# Select the hidden state before the first branching layer
input_hidden_state = outputs.decoder_hidden_states[-(self.num_layers_unfrozen + 1)]
hydra_outputs = self.frozen_head(
hidden_states=input_hidden_state,
attention_mask=decoder_attention_mask,
encoder_hidden_states=outputs.encoder_last_hidden_state,
encoder_attention_mask=attention_mask,
use_cache=False,
output_attentions=False,
output_hidden_states=True,
return_dict=return_dict,
)
if not return_dict:
return hydra_outputs.logits
return hydra_outputs
class T5Branch(ModelBranch):
"""Decoder only T5 branch"""
def __init__(
self,
base_model: transformers.PreTrainedModel,
*,
num_layers_unfrozen: int,
):
super().__init__(base_model, num_layers_unfrozen=num_layers_unfrozen)
self.dropout = hf_get_decoder(base_model).dropout
self.is_decoder = True
def forward( # noqa: max-complexity
self,
hidden_states: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.LongTensor] = None,
encoder_hidden_states: Optional[torch.Tensor] = None,
encoder_attention_mask: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,