-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbart_vae.py
1708 lines (1473 loc) · 87.6 KB
/
bart_vae.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
# coding=utf-8
# Copyright 2021 The Fairseq Authors and The HuggingFace Inc. 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.
""" PyTorch BART model. """
import warnings
import torch
import torch.utils.checkpoint
from torch import nn
from torch.nn import CrossEntropyLoss
from transformers.file_utils import (
add_code_sample_docstrings,
add_end_docstrings,
add_start_docstrings,
add_start_docstrings_to_model_forward,
replace_return_docstrings,
)
from transformers.modeling_outputs import BaseModelOutput
from transformers.utils import logging
from transformers import BartConfig, BartModel, BartPretrainedModel
from typing import Optional, Union, Iterable, Callable, List, Tuple, Dict, Any
from transformers.generation_utils import (BeamSearchDecoderOnlyOutput, BeamSearchEncoderDecoderOutput,)
from transformers.generation_beam_search import BeamScorer, BeamSearchScorer
from transformers.file_utils import ModelOutput
from transformers.generation_logits_process import LogitsProcessorList
from transformers.generation_stopping_criteria import StoppingCriteriaList, validate_stopping_criteria
from dataclasses import dataclass
from torch.nn.modules.loss import _Loss
import copy
logger = logging.get_logger(__name__)
BeamSearchOutput = Union[BeamSearchEncoderDecoderOutput, BeamSearchDecoderOnlyOutput]
_CHECKPOINT_FOR_DOC = "facebook/bart-large"
_CONFIG_FOR_DOC = "BartConfig"
_TOKENIZER_FOR_DOC = "BartTokenizer"
BART_PRETRAINED_MODEL_ARCHIVE_LIST = [
"facebook/bart-large",
# See all BART models at https://huggingface.co/models?filter=bart
]
def shift_tokens_right(input_ids: torch.Tensor, pad_token_id: int, decoder_start_token_id: int):
"""
Shift input ids one token to the right.
"""
shifted_input_ids = input_ids.new_zeros(input_ids.shape)
shifted_input_ids[:, 1:] = input_ids[:, :-1].clone()
shifted_input_ids[:, 0] = decoder_start_token_id
if pad_token_id is None:
raise ValueError("self.model.config.pad_token_id has to be defined.")
# replace possible -100 values in labels by `pad_token_id`
shifted_input_ids.masked_fill_(shifted_input_ids == -100, pad_token_id)
return shifted_input_ids
BART_START_DOCSTRING = r"""
This model inherits from :class:`~transformers.PreTrainedModel`. Check the superclass documentation for the generic
methods the library implements for all its model (such as downloading or saving, resizing the input embeddings,
pruning heads etc.)
This model is also a PyTorch `torch.nn.Module <https://pytorch.org/docs/stable/nn.html#torch.nn.Module>`__
subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to
general usage and behavior.
Parameters:
config (:class:`~transformers.BartConfig`):
Model configuration class with all the parameters of the model. Initializing with a config file does not
load the weights associated with the model, only the configuration. Check out the
:meth:`~transformers.PreTrainedModel.from_pretrained` method to load the model weights.
"""
BART_GENERATION_EXAMPLE = r"""
Summarization example::
>>> from transformers import BartTokenizer, BartForConditionalGeneration, BartConfig
>>> model = BartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
>>> tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')
>>> ARTICLE_TO_SUMMARIZE = "My friends are cool but they eat too many carbs."
>>> inputs = tokenizer([ARTICLE_TO_SUMMARIZE], max_length=1024, return_tensors='pt')
>>> # Generate Summary
>>> summary_ids = model.generate(inputs['input_ids'], num_beams=4, max_length=5, early_stopping=True)
>>> print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
Mask filling example::
>>> from transformers import BartTokenizer, BartForConditionalGeneration
>>> tokenizer = BartTokenizer.from_pretrained('facebook/bart-large')
>>> TXT = "My friends are <mask> but they eat too many carbs."
>>> model = BartForConditionalGeneration.from_pretrained('facebook/bart-large')
>>> input_ids = tokenizer([TXT], return_tensors='pt')['input_ids']
>>> logits = model(input_ids).logits
>>> masked_index = (input_ids[0] == tokenizer.mask_token_id).nonzero().item()
>>> probs = logits[0, masked_index].softmax(dim=0)
>>> values, predictions = probs.topk(5)
>>> tokenizer.decode(predictions).split()
"""
BART_INPUTS_DOCSTRING = r"""
Args:
input_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`):
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
it.
Indices can be obtained using :class:`~transformers.BartTokenizer`. See
:meth:`transformers.PreTrainedTokenizer.encode` and :meth:`transformers.PreTrainedTokenizer.__call__` for
details.
`What are input IDs? <../glossary.html#input-ids>`__
attention_mask (:obj:`torch.Tensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):
Mask to avoid performing attention on padding token indices. Mask values selected in ``[0, 1]``:
- 1 for tokens that are **not masked**,
- 0 for tokens that are **masked**.
`What are attention masks? <../glossary.html#attention-mask>`__
decoder_input_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, target_sequence_length)`, `optional`):
Indices of decoder input sequence tokens in the vocabulary.
Indices can be obtained using :class:`~transformers.BartTokenizer`. See
:meth:`transformers.PreTrainedTokenizer.encode` and :meth:`transformers.PreTrainedTokenizer.__call__` for
details.
`What are decoder input IDs? <../glossary.html#decoder-input-ids>`__
Bart uses the :obj:`eos_token_id` as the starting token for :obj:`decoder_input_ids` generation. If
:obj:`past_key_values` is used, optionally only the last :obj:`decoder_input_ids` have to be input (see
:obj:`past_key_values`).
For translation and summarization training, :obj:`decoder_input_ids` should be provided. If no
:obj:`decoder_input_ids` is provided, the model will create this tensor by shifting the :obj:`input_ids` to
the right for denoising pre-training following the paper.
decoder_attention_mask (:obj:`torch.LongTensor` of shape :obj:`(batch_size, target_sequence_length)`, `optional`):
Default behavior: generate a tensor that ignores pad tokens in :obj:`decoder_input_ids`. Causal mask will
also be used by default.
If you want to change padding behavior, you should read :func:`modeling_bart._prepare_decoder_inputs` and
modify to your needs. See diagram 1 in `the paper <https://arxiv.org/abs/1910.13461>`__ for more
information on the default strategy.
head_mask (:obj:`torch.Tensor` of shape :obj:`(encoder_layers, encoder_attention_heads)`, `optional`):
Mask to nullify selected heads of the attention modules in the encoder. Mask values selected in ``[0, 1]``:
- 1 indicates the head is **not masked**,
- 0 indicates the head is **masked**.
decoder_head_mask (:obj:`torch.Tensor` of shape :obj:`(decoder_layers, decoder_attention_heads)`, `optional`):
Mask to nullify selected heads of the attention modules in the decoder. Mask values selected in ``[0, 1]``:
- 1 indicates the head is **not masked**,
- 0 indicates the head is **masked**.
cross_attn_head_mask (:obj:`torch.Tensor` of shape :obj:`(decoder_layers, decoder_attention_heads)`, `optional`):
Mask to nullify selected heads of the cross-attention modules in the decoder. Mask values selected in ``[0,
1]``:
- 1 indicates the head is **not masked**,
- 0 indicates the head is **masked**.
encoder_outputs (:obj:`tuple(tuple(torch.FloatTensor)`, `optional`):
Tuple consists of (:obj:`last_hidden_state`, `optional`: :obj:`hidden_states`, `optional`:
:obj:`attentions`) :obj:`last_hidden_state` of shape :obj:`(batch_size, sequence_length, hidden_size)`,
`optional`) is a sequence of hidden-states at the output of the last layer of the encoder. Used in the
cross-attention of the decoder.
past_key_values (:obj:`tuple(tuple(torch.FloatTensor))`, `optional`, returned when ``use_cache=True`` is passed or when ``config.use_cache=True``):
Tuple of :obj:`tuple(torch.FloatTensor)` of length :obj:`config.n_layers`, with each tuple having 2 tensors
of shape :obj:`(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of
shape :obj:`(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.
Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
blocks) that can be used (see :obj:`past_key_values` input) to speed up sequential decoding.
If :obj:`past_key_values` are used, the user can optionally input only the last :obj:`decoder_input_ids`
(those that don't have their past key value states given to this model) of shape :obj:`(batch_size, 1)`
instead of all :obj:`decoder_input_ids`` of shape :obj:`(batch_size, sequence_length)`.
inputs_embeds (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, hidden_size)`, `optional`):
Optionally, instead of passing :obj:`input_ids` you can choose to directly pass an embedded representation.
This is useful if you want more control over how to convert :obj:`input_ids` indices into associated
vectors than the model's internal embedding lookup matrix.
decoder_inputs_embeds (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, target_sequence_length, hidden_size)`, `optional`):
Optionally, instead of passing :obj:`decoder_input_ids` you can choose to directly pass an embedded
representation. If :obj:`past_key_values` is used, optionally only the last :obj:`decoder_inputs_embeds`
have to be input (see :obj:`past_key_values`). This is useful if you want more control over how to convert
:obj:`decoder_input_ids` indices into associated vectors than the model's internal embedding lookup matrix.
If :obj:`decoder_input_ids` and :obj:`decoder_inputs_embeds` are both unset, :obj:`decoder_inputs_embeds`
takes the value of :obj:`inputs_embeds`.
use_cache (:obj:`bool`, `optional`):
If set to :obj:`True`, :obj:`past_key_values` key value states are returned and can be used to speed up
decoding (see :obj:`past_key_values`).
output_attentions (:obj:`bool`, `optional`):
Whether or not to return the attentions tensors of all attention layers. See ``attentions`` under returned
tensors for more detail.
output_hidden_states (:obj:`bool`, `optional`):
Whether or not to return the hidden states of all layers. See ``hidden_states`` under returned tensors for
more detail.
return_dict (:obj:`bool`, `optional`):
Whether or not to return a :class:`~transformers.file_utils.ModelOutput` instead of a plain tuple.
"""
class GaussianKLLoss(_Loss):
def __init__(self):
super(GaussianKLLoss, self).__init__()
def forward(self, mu, logvar, mu_prior=None, logvar_prior=None):
"""
0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2)
torch.log(logvar_prior.exp().pow(0.5) / logvar.exp().pow(0.5)) + \
0.5 * ((mu - mu_prior).pow(2) + logvar.exp()) / logvar_prior.exp() - 0.5
"""
if mu_prior is None and logvar_prior is None:
y_kl = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp(), dim=-1)
else:
KLD_element = torch.log(logvar_prior.exp() / logvar.exp()) + \
((mu - mu_prior).pow(2) + logvar.exp()) / logvar_prior.exp() - 1
y_kl = 0.5 * torch.sum(KLD_element, dim=-1)
return torch.mean(y_kl)
class Pack(dict):
def __getattr__(self, name):
if name in self:
return self[name]
else:
return super(Pack, self).__getattr__(name)
def add(self, **kwargs):
for k, v in kwargs.items():
self[k] = v
def copy(self):
pack = Pack()
for k, v in self.items():
if type(v) is list:
pack[k] = list(v)
else:
pack[k] = v
return pack
@dataclass
class Seq2SeqLMOutput(ModelOutput):
"""
Base class for sequence-to-sequence language models outputs.
Args:
loss (:obj:`torch.FloatTensor` of shape :obj:`(1,)`, `optional`, returned when :obj:`labels` is provided):
Language modeling loss.
logits (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, config.vocab_size)`):
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
past_key_values (:obj:`tuple(tuple(torch.FloatTensor))`, `optional`, returned when ``use_cache=True`` is passed or when ``config.use_cache=True``):
Tuple of :obj:`tuple(torch.FloatTensor)` of length :obj:`config.n_layers`, with each tuple having 2 tensors
of shape :obj:`(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of
shape :obj:`(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.
Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
blocks) that can be used (see :obj:`past_key_values` input) to speed up sequential decoding.
decoder_hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``):
Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer)
of shape :obj:`(batch_size, sequence_length, hidden_size)`.
Hidden-states of the decoder at the output of each layer plus the initial embedding outputs.
decoder_attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``):
Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape :obj:`(batch_size, num_heads,
sequence_length, sequence_length)`.
Attentions weights of the decoder, after the attention softmax, used to compute the weighted average in the
self-attention heads.
cross_attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``):
Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape :obj:`(batch_size, num_heads,
sequence_length, sequence_length)`.
Attentions weights of the decoder's cross-attention layer, after the attention softmax, used to compute the
weighted average in the cross-attention heads.
encoder_last_hidden_state (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, hidden_size)`, `optional`):
Sequence of hidden-states at the output of the last layer of the encoder of the model.
encoder_hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``):
Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer)
of shape :obj:`(batch_size, sequence_length, hidden_size)`.
Hidden-states of the encoder at the output of each layer plus the initial embedding outputs.
encoder_attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``):
Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape :obj:`(batch_size, num_heads,
sequence_length, sequence_length)`.
Attentions weights of the encoder, after the attention softmax, used to compute the weighted average in the
self-attention heads.
"""
prediction_loss: Optional[torch.FloatTensor] = None
prediction_logits: torch.FloatTensor = None
reconstruction_loss: Optional[torch.FloatTensor] = None
reconstruction_logits: torch.FloatTensor = None
kl_loss: Optional[torch.FloatTensor] = None
topic_constraint: Optional[torch.FloatTensor] = None
past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None
past_key_values_for_x: Optional[Tuple[Tuple[torch.FloatTensor]]] = None
decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None
decoder_hidden_states_for_x: Optional[Tuple[torch.FloatTensor]] = None
decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None
cross_attentions: Optional[Tuple[torch.FloatTensor]] = None
cross_attentions_for_x: 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
latent_result: Pack = None
@dataclass
class Seq2SeqModelOutput(ModelOutput):
"""
Base class for model encoder's outputs that also contains : pre-computed hidden states that can speed up sequential
decoding.
Args:
last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
Sequence of hidden-states at the output of the last layer of the decoder of the model.
If `past_key_values` is used only the last hidden-state of the sequences of shape `(batch_size, 1,
hidden_size)` is output.
past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
`(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape
`(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.
Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.
decoder_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
Hidden-states of the decoder at the output of each layer plus the optional initial embedding outputs.
decoder_attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
sequence_length)`.
Attentions weights of the decoder, after the attention softmax, used to compute the weighted average in the
self-attention heads.
cross_attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
sequence_length)`.
Attentions weights of the decoder's cross-attention layer, after the attention softmax, used to compute the
weighted average in the cross-attention heads.
encoder_last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
Sequence of hidden-states at the output of the last layer of the encoder of the model.
encoder_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
Hidden-states of the encoder at the output of each layer plus the optional initial embedding outputs.
encoder_attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
sequence_length)`.
Attentions weights of the encoder, after the attention softmax, used to compute the weighted average in the
self-attention heads.
"""
last_hidden_state: 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
last_hidden_state_for_x: torch.FloatTensor = None
past_key_values_for_x: Optional[Tuple[Tuple[torch.FloatTensor]]] = None
decoder_hidden_states_for_x: Optional[Tuple[torch.FloatTensor]] = None
cross_attentions_for_x: 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
causal_latent: torch.FloatTensor = None
non_causal_latent: torch.FloatTensor = None
latent_result: Pack = None
style_doc_latent: torch.FloatTensor = None
style_summary_latent: torch.FloatTensor = None
@add_start_docstrings(
"The bare BART Model outputting raw hidden-states without any specific head on top.",
BART_START_DOCSTRING,
)
class BartModel_VAE(BartModel):
def __init__(self, config: BartConfig):
super().__init__(config)
padding_idx, vocab_size = config.pad_token_id, config.vocab_size
self.shared = nn.Embedding(vocab_size, config.d_model, padding_idx)
self.aggregate = config.aggregate
self.training_vae = config.training_vae
self.fuse_seq_info = config.fuse_seq_info
self.causal_latent_size = config.causal_latent_size
self.non_causal_latent_size = config.non_causal_latent_size
self.style_size = config.style_size
self.total_latent_size = self.causal_latent_size + self.non_causal_latent_size + self.style_size
self.u_num_embeddings = config.num_topics
self.u_size = config.u_size
self.u_embed = torch.nn.Embedding(self.u_num_embeddings, self.u_size)
self.q_z_mu = nn.Linear(config.d_model + self.u_size, self.total_latent_size)
self.q_z_logvar = nn.Linear(config.d_model + self.u_size, self.total_latent_size)
self.size_transform_fct_for_causal = nn.Linear(self.causal_latent_size, config.d_model)
self.size_transform_fct_for_non_causal = nn.Linear(self.non_causal_latent_size, config.d_model)
self.size_transform_fct_for_style = nn.Linear(self.style_size, config.d_model)
self.decoder_for_x = copy.deepcopy(self.decoder)
# Initialize weights and apply final processing
self.init_weights()
def reparameterize(self, mu, logvar):
if self.training_vae:
std = torch.exp(0.5*logvar)
eps = torch.randn_like(std) # size = input.shape
return eps.mul(std).add_(mu) # z = mu + std * eps
else:
return mu
def latent_sample_u(self, encoded_seq, u):
if self.fuse_seq_info:
if self.aggregate == "cls":
encoded_seq = encoded_seq[:,1,:].unsqueeze(1)
else: # average pool
encoded_seq = encoded_seq.mean(1).unsqueeze(1)
u = u[:, None, :]
t = torch.concat([encoded_seq, u], dim=-1)
z_mu = self.q_z_mu(t)
z_logvar = self.q_z_logvar(t)
sample_z = self.reparameterize(z_mu, z_logvar)
return Pack(sample_z=sample_z, z_mu=z_mu, z_logvar=z_logvar)
def get_input_embeddings(self):
return self.shared
def set_input_embeddings(self, value):
self.shared = value
self.encoder.embed_tokens = self.shared
self.decoder_for_x.embed_tokens = self.shared
self.decoder.embed_tokens = self.shared
def get_encoder(self):
return self.encoder
def get_decoder_for_x(self):
return self.decoder_for_x
def get_decoder_for_y(self):
return self.decoder
@add_start_docstrings_to_model_forward(BART_INPUTS_DOCSTRING)
@add_code_sample_docstrings(
processor_class=_TOKENIZER_FOR_DOC,
checkpoint=_CHECKPOINT_FOR_DOC,
output_type=Seq2SeqModelOutput,
config_class=_CONFIG_FOR_DOC,
)
def forward(
self,
target,
cr,
subset_id,
input_ids=None,
attention_mask=None,
decoder_input_ids=None,
decoder_input_ids_for_x=None,
decoder_attention_mask=None,
head_mask=None,
decoder_head_mask=None,
cross_attn_head_mask=None,
encoder_outputs=None,
past_key_values=None,
past_key_values_for_x=None,
inputs_embeds=None,
decoder_inputs_embeds=None,
use_cache=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
latent_result=None,
):
# different to other models, Bart automatically creates decoder_input_ids from
# input_ids if no decoder_input_ids are provided
if target == "prediction" or target == "both":
if decoder_input_ids is None and decoder_inputs_embeds is None:
decoder_input_ids = shift_tokens_right(
input_ids, self.config.pad_token_id, self.config.decoder_start_token_id
)
# for restructing x
if target == "reconstruction" or target == "both":
if decoder_input_ids_for_x is None:
decoder_input_ids_for_x = shift_tokens_right(
input_ids, self.config.pad_token_id, self.config.decoder_start_token_id
)
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
if encoder_outputs is None:
encoder_outputs = self.encoder(
input_ids=input_ids,
attention_mask=attention_mask,
head_mask=head_mask,
inputs_embeds=inputs_embeds,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
# If the user passed a tuple for encoder_outputs, we wrap it in a BaseModelOutput when return_dict=True
elif return_dict and not isinstance(encoder_outputs, BaseModelOutput):
encoder_outputs = BaseModelOutput(
last_hidden_state=encoder_outputs[0],
hidden_states=encoder_outputs[1] if len(encoder_outputs) > 1 else None,
attentions=encoder_outputs[2] if len(encoder_outputs) > 2 else None,
)
# VAE latent variables
if latent_result is None:
# encoder_outputs[0]: encoder_outputs.last_hidden_state
# (batch_size, encoder_sequence_length, hidden_size)
encoder_seq_hidden_state = encoder_outputs.last_hidden_state if return_dict else encoder_outputs[0] # outputs.encoder_last_hidden_state
# (batch_size, 1, total_latent_size) if fuse_seq_info
subset_embedding = self.u_embed(subset_id)
latent_result = self.latent_sample_u(encoder_seq_hidden_state, subset_embedding)
# (batch_size, 1, total_latent_size) if fuse_seq_info
total_latent = latent_result.sample_z
# separate into three latent variables
# (batch_size, 1, causal_latent_size)
causal_latent = total_latent[:,:,:self.causal_latent_size]
# (batch_size, 1, hidden_size)
causal_latent = self.size_transform_fct_for_causal(causal_latent)
non_causal_latent=None
if target == "reconstruction" or target == "both":
# (batch_size, 1, non_causal_latent_size)
non_causal_latent = total_latent[:,:,self.causal_latent_size : self.causal_latent_size + self.non_causal_latent_size]
# (batch_size, 1, hidden_size)
non_causal_latent = self.size_transform_fct_for_non_causal(non_causal_latent)
style_doc_latent = total_latent[:,:,-self.style_size:]
style_doc_latent = self.size_transform_fct_for_style(style_doc_latent)
style_summary_latent = style_doc_latent * cr[:,None,None]
# decoder outputs consists of (dec_features, past_key_value, dec_hidden, dec_attn)
decoder_outputs_for_x = None
if target == "reconstruction" or target == "both":
if past_key_values_for_x is None:
decoder_inputs_embeds_for_x = self.decoder_for_x.embed_tokens(decoder_input_ids_for_x) * self.decoder_for_x.embed_scale
new_decoder_inputs_embeds_for_x = torch.concat([decoder_inputs_embeds_for_x[:,:1,:],causal_latent+non_causal_latent+style_doc_latent,decoder_inputs_embeds_for_x[:,2:,:]],dim=1)
# total_latent_for_reconstruction
decoder_outputs_for_x = self.decoder_for_x(
input_ids=None if past_key_values_for_x is None else decoder_input_ids_for_x,
attention_mask=None,
encoder_hidden_states=encoder_outputs[0],
encoder_attention_mask=attention_mask,
head_mask=None,
cross_attn_head_mask=None,
past_key_values=past_key_values_for_x,
inputs_embeds=new_decoder_inputs_embeds_for_x if past_key_values_for_x is None else None,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
# print("Get decoder_outputs_for_x!")
decoder_outputs = None
if target == "prediction" or target == "both":
new_decoder_inputs_embeds = None
if past_key_values is None and decoder_input_ids.shape[1]>1: # train
if decoder_inputs_embeds is None:
decoder_inputs_embeds = self.decoder.embed_tokens(decoder_input_ids) * self.decoder.embed_scale
new_decoder_inputs_embeds = torch.concat([decoder_inputs_embeds[:,:1,:],causal_latent+style_summary_latent,decoder_inputs_embeds[:,2:,:]],dim=1)
elif decoder_input_ids.shape[1]==1 and decoder_input_ids[0,0].item()==0 and past_key_values[0][0].shape[2]<3: # test, real_start
if decoder_inputs_embeds is None:
decoder_inputs_embeds = self.decoder.embed_tokens(decoder_input_ids) * self.decoder.embed_scale
new_decoder_inputs_embeds = causal_latent+style_summary_latent
# causal_latent
decoder_outputs = self.decoder(
input_ids=decoder_input_ids if new_decoder_inputs_embeds is None else None,
attention_mask=decoder_attention_mask,
encoder_hidden_states=encoder_outputs[0],
encoder_attention_mask=attention_mask,
head_mask=decoder_head_mask,
cross_attn_head_mask=cross_attn_head_mask,
past_key_values=past_key_values,
inputs_embeds=decoder_inputs_embeds if new_decoder_inputs_embeds is None else new_decoder_inputs_embeds,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
# print("Get decoder_outputs!")
if not return_dict:
# return decoder_outputs + encoder_outputs
# return tuple: concat of tuple is tuple, "(xxx,)" should be added for tensor
# each element of the result tuple is the elements of all source tuples
tuple_output = encoder_outputs + (causal_latent, non_causal_latent, latent_result, style_doc_latent, style_summary_latent)
if decoder_outputs_for_x is not None:
tuple_output = decoder_outputs_for_x + tuple_output
if decoder_outputs is not None:
tuple_output = decoder_outputs + tuple_output
return tuple_output
return Seq2SeqModelOutput(
last_hidden_state=decoder_outputs.last_hidden_state if decoder_outputs is not None else None,
past_key_values=decoder_outputs.past_key_values if decoder_outputs is not None else None,
decoder_hidden_states=decoder_outputs.hidden_states if decoder_outputs is not None else None,
decoder_attentions=decoder_outputs.attentions if decoder_outputs is not None else None,
cross_attentions=decoder_outputs.cross_attentions if decoder_outputs is not None else None,
last_hidden_state_for_x=decoder_outputs_for_x.last_hidden_state if decoder_outputs_for_x is not None else None,
past_key_values_for_x=decoder_outputs_for_x.past_key_values if decoder_outputs_for_x is not None else None,
decoder_hidden_states_for_x=decoder_outputs_for_x.hidden_states if decoder_outputs_for_x is not None else None,
cross_attentions_for_x=decoder_outputs_for_x.cross_attentions if decoder_outputs_for_x is not None else None,
encoder_last_hidden_state=encoder_outputs.last_hidden_state,
encoder_hidden_states=encoder_outputs.hidden_states,
encoder_attentions=encoder_outputs.attentions,
causal_latent=causal_latent,
non_causal_latent=non_causal_latent,
latent_result=latent_result,
style_doc_latent=style_doc_latent,
style_summary_latent=style_summary_latent,
)
@add_start_docstrings(
"The BART Model with a language modeling head. Can be used for summarization.", BART_START_DOCSTRING
)
class BartForConditionalGeneration_VAE(BartPretrainedModel):
base_model_prefix = "model"
_keys_to_ignore_on_load_missing = [r"final_logits_bias", r"lm_head\.weight"]
def __init__(self, config: BartConfig):
super().__init__(config)
self.model = BartModel_VAE(config)
self.register_buffer("final_logits_bias", torch.zeros((1, self.model.shared.num_embeddings)))
self.lm_head = nn.Linear(config.d_model, self.model.shared.num_embeddings, bias=False)
self.lm_head_for_x = nn.Linear(config.d_model, self.model.shared.num_embeddings, bias=False)
self.topic_weight = nn.Parameter(torch.rand(config.num_topics,config.d_model))
# Initialize weights and apply final processing
self.init_weights()
def get_encoder(self):
return self.model.get_encoder()
def get_decoder_for_x(self):
return self.model.get_decoder_for_x()
def get_decoder_for_y(self):
return self.model.get_decoder_for_y()
def resize_token_embeddings(self, new_num_tokens: int) -> nn.Embedding:
new_embeddings = super().resize_token_embeddings(new_num_tokens)
self._resize_final_logits_bias(new_num_tokens)
return new_embeddings
def _resize_final_logits_bias(self, new_num_tokens: int) -> None:
old_num_tokens = self.final_logits_bias.shape[-1]
if new_num_tokens <= old_num_tokens:
new_bias = self.final_logits_bias[:, :new_num_tokens]
else:
extra_bias = torch.zeros((1, new_num_tokens - old_num_tokens), device=self.final_logits_bias.device)
new_bias = torch.cat([self.final_logits_bias, extra_bias], dim=1)
self.register_buffer("final_logits_bias", new_bias)
def get_output_embeddings(self):
return self.lm_head
def set_output_embeddings(self, new_embeddings):
self.lm_head = new_embeddings
@add_start_docstrings_to_model_forward(BART_INPUTS_DOCSTRING)
@replace_return_docstrings(output_type=Seq2SeqLMOutput, config_class=_CONFIG_FOR_DOC)
@add_end_docstrings(BART_GENERATION_EXAMPLE)
def forward(
self,
target,
cr,
subset_id,
input_ids=None,
attention_mask=None,
decoder_input_ids=None,
decoder_input_ids_for_x=None,
decoder_attention_mask=None,
head_mask=None,
decoder_head_mask=None,
cross_attn_head_mask=None,
encoder_outputs=None,
past_key_values=None,
past_key_values_for_x=None,
inputs_embeds=None,
decoder_inputs_embeds=None,
labels=None,
labels_for_x=None,
use_cache=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
latent_result=None,
topic_distribution=None,
):
r"""
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):
Labels for computing the masked language modeling loss. Indices should either be in ``[0, ...,
config.vocab_size]`` or -100 (see ``input_ids`` docstring). Tokens with indices set to ``-100`` are ignored
(masked), the loss is only computed for the tokens with labels in ``[0, ..., config.vocab_size]``.
Returns:
"""
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
if labels is not None:
if decoder_input_ids is None and decoder_inputs_embeds is None:
decoder_input_ids = shift_tokens_right(
labels, self.config.pad_token_id, self.config.decoder_start_token_id
)
if labels_for_x is not None:
if decoder_input_ids_for_x is None:
decoder_input_ids_for_x = shift_tokens_right(
labels_for_x, self.config.pad_token_id, self.config.decoder_start_token_id
)
outputs = self.model(
target,
cr,
subset_id,
input_ids,
attention_mask=attention_mask,
decoder_input_ids=decoder_input_ids,
decoder_input_ids_for_x=decoder_input_ids_for_x,
encoder_outputs=encoder_outputs,
decoder_attention_mask=decoder_attention_mask,
head_mask=head_mask,
decoder_head_mask=decoder_head_mask,
cross_attn_head_mask=cross_attn_head_mask,
past_key_values=past_key_values,
past_key_values_for_x=past_key_values_for_x,
inputs_embeds=inputs_embeds,
decoder_inputs_embeds=decoder_inputs_embeds,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
latent_result=latent_result,
)
causal_latent = outputs.causal_latent
non_causal_latent = outputs.non_causal_latent
style_doc_latent = outputs.style_doc_latent
style_summary_latent = outputs.style_summary_latent
topic_constraint = None
if topic_distribution is not None:
tc_gate = torch.clip(torch.sign(topic_distribution - self.config.tc_th), 1e-7, -1e-7 + 1)
tc_weight = tc_gate * topic_distribution
tc_distribution = tc_weight / (torch.sum(tc_weight,dim=1)).unsqueeze(1)
tnc_gate = 1 - tc_gate
tnc_weight = tnc_gate * topic_distribution
tnc_distribution = tnc_weight / (torch.sum(tnc_weight,dim=1)).unsqueeze(1)
topic_causal_embedding = torch.matmul(tc_distribution, self.topic_weight)
topic_non_causal_embedding = torch.matmul(tnc_distribution, self.topic_weight)
euclidean_distance1 = torch.pairwise_distance(causal_latent, topic_causal_embedding)
euclidean_distance2 = torch.pairwise_distance(non_causal_latent, topic_non_causal_embedding)
topic_constraint = torch.mean(euclidean_distance1) + torch.mean(euclidean_distance2)
lm_logits = None
lm_logits_for_x = None
if target == "prediction" or target == "both":
# [bs,seq_len,hidden_size]
decoder_last_hidden_state = outputs[0]
# [bs,seq_len,hidden_size]
z_latent = causal_latent + style_summary_latent
z_latent = z_latent.repeat([1,decoder_last_hidden_state.shape[1],1])
decoder_last_hidden_state = decoder_last_hidden_state + z_latent
lm_logits = self.lm_head(decoder_last_hidden_state) + self.final_logits_bias
if target == "both":
decoder_last_hidden_state_for_x = outputs[2] if not return_dict else outputs.last_hidden_state_for_x
z_latent = causal_latent + non_causal_latent + style_doc_latent
z_latent = z_latent.repeat([1,decoder_last_hidden_state_for_x.shape[1],1])
decoder_last_hidden_state_for_x = decoder_last_hidden_state_for_x + z_latent
lm_logits_for_x = self.lm_head_for_x(decoder_last_hidden_state_for_x) + self.final_logits_bias
elif target == "reconstruction":
decoder_last_hidden_state_for_x = outputs[0]
z_latent = causal_latent + non_causal_latent + style_doc_latent
z_latent = z_latent.repeat([1,decoder_last_hidden_state_for_x.shape[1],1])
decoder_last_hidden_state_for_x = decoder_last_hidden_state_for_x + z_latent
lm_logits_for_x = self.lm_head_for_x(decoder_last_hidden_state_for_x) + self.final_logits_bias
lm_loss_for_y = None
if labels is not None:
loss_fct_y = CrossEntropyLoss()
lm_loss_for_y = loss_fct_y(lm_logits.view(-1, self.config.vocab_size), labels.view(-1))
lm_loss_for_x = None
if labels_for_x is not None:
loss_fct_x = CrossEntropyLoss()
lm_loss_for_x = loss_fct_x(lm_logits_for_x.view(-1, self.config.vocab_size), labels_for_x.view(-1))
vae_kl_loss = None
if self.model.training_vae:
kl_loss_fct = GaussianKLLoss()
latent_mu = outputs.latent_result.z_mu if return_dict else outputs[-1].z_mu
latent_logvar = outputs.latent_result.z_logvar if return_dict else outputs[-1].z_logvar
if self.model.learned_prior:
latent_mu_prior = outputs.latent_result.z_mu_prior if return_dict else outputs[-1].z_mu_prior
latent_logvar_prior = outputs.latent_result.z_logvar_prior if return_dict else outputs[-1].z_logvar_prior
vae_kl_loss = kl_loss_fct(latent_mu, latent_logvar, latent_mu_prior, latent_logvar_prior)
else:
vae_kl_loss = kl_loss_fct(latent_mu, latent_logvar)
if not return_dict:
output = outputs[-2:]
if target == "both":
past_key_values_for_x = outputs[3]
output = ((past_key_values_for_x,) + output)
elif target == "reconstruction":
past_key_values_for_x = outputs[1]
output = ((past_key_values_for_x,) + output)
if target == "prediction" or target == "both":
past_key_values = outputs[1]
output = ((past_key_values,) + output)
if vae_kl_loss is not None:
output = ((vae_kl_loss,) + output)
if lm_loss_for_x is not None:
output = ((lm_loss_for_x,) + (lm_logits_for_x,) + output)
if lm_loss_for_y is not None:
output = ((lm_loss_for_y,) + (lm_logits,) + output)
return output
return Seq2SeqLMOutput(
prediction_loss=lm_loss_for_y,
prediction_logits=lm_logits,
reconstruction_loss=lm_loss_for_x,
reconstruction_logits=lm_logits_for_x,
kl_loss=vae_kl_loss,
topic_constraint=topic_constraint,
past_key_values=outputs.past_key_values,
decoder_hidden_states=outputs.decoder_hidden_states,
decoder_attentions=outputs.decoder_attentions,
cross_attentions=outputs.cross_attentions,
past_key_values_for_x=outputs.past_key_values_for_x,
decoder_hidden_states_for_x=outputs.decoder_hidden_states_for_x,
encoder_last_hidden_state=outputs.encoder_last_hidden_state,
encoder_hidden_states=outputs.encoder_hidden_states,
encoder_attentions=outputs.encoder_attentions,
latent_result=outputs.latent_result,
)
@torch.no_grad()
def generate(
self,
target,
cr,
subset_id,
input_ids: Optional[torch.LongTensor] = None,
max_length: Optional[int] = None,
min_length: Optional[int] = None,
do_sample: Optional[bool] = None,
early_stopping: Optional[bool] = None,
num_beams: Optional[int] = None,
repetition_penalty: Optional[float] = None,
bad_words_ids: Optional[Iterable[int]] = None,
bos_token_id: Optional[int] = None,
pad_token_id: Optional[int] = None,
eos_token_id: Optional[int] = None,
length_penalty: Optional[float] = None,
no_repeat_ngram_size: Optional[int] = None,
encoder_no_repeat_ngram_size: Optional[int] = None,
num_return_sequences: Optional[int] = None,
max_time: Optional[float] = None,
max_new_tokens: Optional[int] = None,
decoder_start_token_id: Optional[int] = None,
use_cache: Optional[bool] = None,
num_beam_groups: Optional[int] = None,
diversity_penalty: Optional[float] = None,
prefix_allowed_tokens_fn: Optional[Callable[[int, torch.Tensor], List[int]]] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
output_scores: Optional[bool] = None,
return_dict_in_generate: Optional[bool] = None,
forced_bos_token_id: Optional[int] = None,
forced_eos_token_id: Optional[int] = None,
remove_invalid_values: Optional[bool] = None,
synced_gpus: Optional[bool] = None,
latent_result: Optional[Pack] = None,
**model_kwargs,
) -> Union[BeamSearchOutput, torch.LongTensor]:
r"""
Generates sequences for models with a language modeling head. The method currently supports greedy decoding,
multinomial sampling, beam-search decoding, and beam-search multinomial sampling.
Apart from :obj:`input_ids` and :obj:`attention_mask`, all the arguments below will default to the value of the
attribute of the same name inside the :class:`~transformers.PretrainedConfig` of the model. The default values
indicated are the default values of those config.
Most of these parameters are explained in more detail in `this blog post
<https://huggingface.co/blog/how-to-generate>`__.
Parameters:
input_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):
The sequence used as a prompt for the generation. If :obj:`None` the method initializes it with
:obj:`bos_token_id` and a batch size of 1.
max_length (:obj:`int`, `optional`, defaults to :obj:`model.config.max_length`):
The maximum length of the sequence to be generated.
max_new_tokens (:obj:`int`, `optional`, defaults to None):
The maximum numbers of tokens to generate, ignore the current number of tokens. Use either
:obj:`max_new_tokens` or :obj:`max_length` but not both, they serve the same purpose.
min_length (:obj:`int`, `optional`, defaults to 10):
The minimum length of the sequence to be generated.
do_sample (:obj:`bool`, `optional`, defaults to :obj:`False`):
Whether or not to use sampling ; use greedy decoding otherwise.
early_stopping (:obj:`bool`, `optional`, defaults to :obj:`False`):
Whether to stop the beam search when at least ``num_beams`` sentences are finished per batch or not.
num_beams (:obj:`int`, `optional`, defaults to 1):
Number of beams for beam search. 1 means no beam search.
repetition_penalty (:obj:`float`, `optional`, defaults to 1.0):
The parameter for repetition penalty. 1.0 means no penalty. See `this paper
<https://arxiv.org/pdf/1909.05858.pdf>`__ for more details.
pad_token_id (:obj:`int`, `optional`):
The id of the `padding` token.
bos_token_id (:obj:`int`, `optional`):
The id of the `beginning-of-sequence` token.
eos_token_id (:obj:`int`, `optional`):
The id of the `end-of-sequence` token.
length_penalty (:obj:`float`, `optional`, defaults to 1.0):
Exponential penalty to the length. 1.0 means no penalty. Set to values < 1.0 in order to encourage the
model to generate shorter sequences, to a value > 1.0 in order to encourage the model to produce longer
sequences.
no_repeat_ngram_size (:obj:`int`, `optional`, defaults to 0):
If set to int > 0, all ngrams of that size can only occur once.
encoder_no_repeat_ngram_size (:obj:`int`, `optional`, defaults to 0):
If set to int > 0, all ngrams of that size that occur in the ``encoder_input_ids`` cannot occur in the
``decoder_input_ids``.
bad_words_ids(:obj:`List[List[int]]`, `optional`):
List of token ids that are not allowed to be generated. In order to get the tokens of the words that
should not appear in the generated text, use :obj:`tokenizer(bad_word,
add_prefix_space=True).input_ids`.
num_return_sequences(:obj:`int`, `optional`, defaults to 1):
The number of independently computed returned sequences for each element in the batch.
max_time(:obj:`float`, `optional`, defaults to None):
The maximum amount of time you allow the computation to run for in seconds. generation will still
finish the current pass after allocated time has been passed.
attention_mask (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):
Mask to avoid performing attention on padding token indices. Mask values are in ``[0, 1]``, 1 for
tokens that are not masked, and 0 for masked tokens. If not provided, will default to a tensor the same
shape as :obj:`input_ids` that masks the pad token. `What are attention masks?
<../glossary.html#attention-mask>`__
decoder_start_token_id (:obj:`int`, `optional`):
If an encoder-decoder model starts decoding with a different token than `bos`, the id of that token.
use_cache: (:obj:`bool`, `optional`, defaults to :obj:`True`):
Whether or not the model should use the past last key/values attentions (if applicable to the model) to
speed up decoding.
num_beam_groups (:obj:`int`, `optional`, defaults to 1):
Number of groups to divide :obj:`num_beams` into in order to ensure diversity among different groups of
beams. `this paper <https://arxiv.org/pdf/1610.02424.pdf>`__ for more details.
diversity_penalty (:obj:`float`, `optional`, defaults to 0.0):
This value is subtracted from a beam's score if it generates a token same as any beam from other group
at a particular time. Note that :obj:`diversity_penalty` is only effective if ``group beam search`` is
enabled.
prefix_allowed_tokens_fn: (:obj:`Callable[[int, torch.Tensor], List[int]]`, `optional`):