generation_utils.mdx 6.71 KB
Newer Older
Sylvain Gugger's avatar
Sylvain Gugger committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!--Copyright 2020 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.
-->

# Utilities for Generation

15
16
17
18
19
20
21
22
This page lists all the utility functions used by [`~generation.GenerationMixin.generate`],
[`~generation.GenerationMixin.greedy_search`],
[`~generation.GenerationMixin.contrastive_search`],
[`~generation.GenerationMixin.sample`],
[`~generation.GenerationMixin.beam_search`],
[`~generation.GenerationMixin.beam_sample`],
[`~generation.GenerationMixin.group_beam_search`], and
[`~generation.GenerationMixin.constrained_beam_search`].
Sylvain Gugger's avatar
Sylvain Gugger committed
23
24
25
26
27

Most of those are only useful if you are studying the code of the generate methods in the library.

## Generate Outputs

28
The output of [`~generation.GenerationMixin.generate`] is an instance of a subclass of
29
[`~utils.ModelOutput`]. This output is a data structure containing all the information returned
30
by [`~generation.GenerationMixin.generate`], but that can also be used as tuple or dictionary.
Sylvain Gugger's avatar
Sylvain Gugger committed
31
32
33
34
35
36

Here's an example:

```python
from transformers import GPT2Tokenizer, GPT2LMHeadModel

Sylvain Gugger's avatar
Sylvain Gugger committed
37
38
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2LMHeadModel.from_pretrained("gpt2")
Sylvain Gugger's avatar
Sylvain Gugger committed
39
40
41
42
43

inputs = tokenizer("Hello, my dog is cute and ", return_tensors="pt")
generation_output = model.generate(**inputs, return_dict_in_generate=True, output_scores=True)
```

44
The `generation_output` object is a [`~generation.GreedySearchDecoderOnlyOutput`], as we can
Sylvain Gugger's avatar
Sylvain Gugger committed
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
see in the documentation of that class below, it means it has the following attributes:

- `sequences`: the generated sequences of tokens
- `scores` (optional): the prediction scores of the language modelling head, for each generation step
- `hidden_states` (optional): the hidden states of the model, for each generation step
- `attentions` (optional): the attention weights of the model, for each generation step

Here we have the `scores` since we passed along `output_scores=True`, but we don't have `hidden_states` and
`attentions` because we didn't pass `output_hidden_states=True` or `output_attentions=True`.

You can access each attribute as you would usually do, and if that attribute has not been returned by the model, you
will get `None`. Here for instance `generation_output.scores` are all the generated prediction scores of the
language modeling head, and `generation_output.attentions` is `None`.

When using our `generation_output` object as a tuple, it only keeps the attributes that don't have `None` values.
Here, for instance, it has two elements, `loss` then `logits`, so

```python
generation_output[:2]
```

will return the tuple `(generation_output.sequences, generation_output.scores)` for instance.

When using our `generation_output` object as a dictionary, it only keeps the attributes that don't have `None`
values. Here, for instance, it has two keys that are `sequences` and `scores`.

We document here all output types.


### GreedySearchOutput

76
[[autodoc]] generation.GreedySearchDecoderOnlyOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
77

78
[[autodoc]] generation.GreedySearchEncoderDecoderOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
79

80
[[autodoc]] generation.FlaxGreedySearchOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
81
82
83

### SampleOutput

84
[[autodoc]] generation.SampleDecoderOnlyOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
85

86
[[autodoc]] generation.SampleEncoderDecoderOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
87

88
[[autodoc]] generation.FlaxSampleOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
89
90
91

### BeamSearchOutput

92
[[autodoc]] generation.BeamSearchDecoderOnlyOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
93

94
[[autodoc]] generation.BeamSearchEncoderDecoderOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
95
96
97

### BeamSampleOutput

98
[[autodoc]] generation.BeamSampleDecoderOnlyOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
99

100
[[autodoc]] generation.BeamSampleEncoderDecoderOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
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

## LogitsProcessor

A [`LogitsProcessor`] can be used to modify the prediction scores of a language model head for
generation.

[[autodoc]] LogitsProcessor
    - __call__

[[autodoc]] LogitsProcessorList
    - __call__

[[autodoc]] LogitsWarper
    - __call__

[[autodoc]] MinLengthLogitsProcessor
    - __call__

[[autodoc]] TemperatureLogitsWarper
    - __call__

[[autodoc]] RepetitionPenaltyLogitsProcessor
    - __call__

[[autodoc]] TopPLogitsWarper
    - __call__

[[autodoc]] TopKLogitsWarper
    - __call__

131
132
133
[[autodoc]] TypicalLogitsWarper
    - __call__

Sylvain Gugger's avatar
Sylvain Gugger committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
[[autodoc]] NoRepeatNGramLogitsProcessor
    - __call__

[[autodoc]] NoBadWordsLogitsProcessor
    - __call__

[[autodoc]] PrefixConstrainedLogitsProcessor
    - __call__

[[autodoc]] HammingDiversityLogitsProcessor
    - __call__

[[autodoc]] ForcedBOSTokenLogitsProcessor
    - __call__

[[autodoc]] ForcedEOSTokenLogitsProcessor
    - __call__

[[autodoc]] InfNanRemoveLogitsProcessor
    - __call__

155
156
157
158
159
160
[[autodoc]] TFLogitsProcessor
    - __call__

[[autodoc]] TFLogitsProcessorList
    - __call__

161
162
163
164
165
166
167
168
169
170
171
172
[[autodoc]] TFLogitsWarper
    - __call__

[[autodoc]] TFTemperatureLogitsWarper
    - __call__

[[autodoc]] TFTopPLogitsWarper
    - __call__

[[autodoc]] TFTopKLogitsWarper
    - __call__

173
174
175
176
177
[[autodoc]] TFMinLengthLogitsProcessor
    - __call__

[[autodoc]] TFNoBadWordsLogitsProcessor
    - __call__
178

179
180
[[autodoc]] TFNoRepeatNGramLogitsProcessor
    - __call__
181

182
183
[[autodoc]] TFRepetitionPenaltyLogitsProcessor
    - __call__
184

185
186
187
188
189
190
[[autodoc]] TFForcedBOSTokenLogitsProcessor
    - __call__

[[autodoc]] TFForcedEOSTokenLogitsProcessor
    - __call__

Sylvain Gugger's avatar
Sylvain Gugger committed
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
[[autodoc]] FlaxLogitsProcessor
    - __call__

[[autodoc]] FlaxLogitsProcessorList
    - __call__

[[autodoc]] FlaxLogitsWarper
    - __call__

[[autodoc]] FlaxTemperatureLogitsWarper
    - __call__

[[autodoc]] FlaxTopPLogitsWarper
    - __call__

[[autodoc]] FlaxTopKLogitsWarper
    - __call__

[[autodoc]] FlaxForcedBOSTokenLogitsProcessor
    - __call__

[[autodoc]] FlaxForcedEOSTokenLogitsProcessor
    - __call__

[[autodoc]] FlaxMinLengthLogitsProcessor
    - __call__

## StoppingCriteria

A [`StoppingCriteria`] can be used to change when to stop generation (other than EOS token).

[[autodoc]] StoppingCriteria
    - __call__

[[autodoc]] StoppingCriteriaList
    - __call__

[[autodoc]] MaxLengthCriteria
    - __call__

[[autodoc]] MaxTimeCriteria
    - __call__

234
235
236
237
238
239
240
241
## Constraints

A [`Constraint`] can be used to force the generation to include specific tokens or sequences in the output.

[[autodoc]] Constraint

[[autodoc]] PhrasalConstraint

242
243
[[autodoc]] DisjunctiveConstraint

244
245
[[autodoc]] ConstraintListState

Sylvain Gugger's avatar
Sylvain Gugger committed
246
247
248
249
250
251
252
253
254
255
## BeamSearch

[[autodoc]] BeamScorer
    - process
    - finalize

[[autodoc]] BeamSearchScorer
    - process
    - finalize

256
257
258
259
[[autodoc]] ConstrainedBeamSearchScorer
    - process
    - finalize

Sylvain Gugger's avatar
Sylvain Gugger committed
260
261
262
263
264
## Utilities

[[autodoc]] top_k_top_p_filtering

[[autodoc]] tf_top_k_top_p_filtering