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

鈿狅笍 Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.

Sylvain Gugger's avatar
Sylvain Gugger committed
15
16
17
18
-->

# Utilities for Generation

19
This page lists all the utility functions used by [`~generation.GenerationMixin.generate`].
Sylvain Gugger's avatar
Sylvain Gugger committed
20
21
22

## Generate Outputs

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

Here's an example:

```python
from transformers import GPT2Tokenizer, GPT2LMHeadModel

32
33
tokenizer = GPT2Tokenizer.from_pretrained("openai-community/gpt2")
model = GPT2LMHeadModel.from_pretrained("openai-community/gpt2")
Sylvain Gugger's avatar
Sylvain Gugger committed
34
35
36
37
38

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

39
The `generation_output` object is a [`~generation.GenerateDecoderOnlyOutput`], as we can
Sylvain Gugger's avatar
Sylvain Gugger committed
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
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.


69
### PyTorch
Sylvain Gugger's avatar
Sylvain Gugger committed
70

71
[[autodoc]] generation.GenerateDecoderOnlyOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
72

73
[[autodoc]] generation.GenerateEncoderDecoderOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
74

75
[[autodoc]] generation.GenerateBeamDecoderOnlyOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
76

77
[[autodoc]] generation.GenerateBeamEncoderDecoderOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
78

79
### TensorFlow
Sylvain Gugger's avatar
Sylvain Gugger committed
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
[[autodoc]] generation.TFGreedySearchEncoderDecoderOutput

[[autodoc]] generation.TFGreedySearchDecoderOnlyOutput

[[autodoc]] generation.TFSampleEncoderDecoderOutput

[[autodoc]] generation.TFSampleDecoderOnlyOutput

[[autodoc]] generation.TFBeamSearchEncoderDecoderOutput

[[autodoc]] generation.TFBeamSearchDecoderOnlyOutput

[[autodoc]] generation.TFBeamSampleEncoderDecoderOutput

[[autodoc]] generation.TFBeamSampleDecoderOnlyOutput

[[autodoc]] generation.TFContrastiveSearchEncoderDecoderOutput

[[autodoc]] generation.TFContrastiveSearchDecoderOnlyOutput

### FLAX

[[autodoc]] generation.FlaxSampleOutput

[[autodoc]] generation.FlaxGreedySearchOutput

[[autodoc]] generation.FlaxBeamSearchOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
108
109
110
111
112
113

## LogitsProcessor

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

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
### PyTorch

[[autodoc]] AlternatingCodebooksLogitsProcessor
    - __call__

[[autodoc]] ClassifierFreeGuidanceLogitsProcessor
    - __call__

[[autodoc]] EncoderNoRepeatNGramLogitsProcessor
    - __call__

[[autodoc]] EncoderRepetitionPenaltyLogitsProcessor
    - __call__

[[autodoc]] EpsilonLogitsWarper
    - __call__

[[autodoc]] EtaLogitsWarper
    - __call__

[[autodoc]] ExponentialDecayLengthPenalty
    - __call__

[[autodoc]] ForcedBOSTokenLogitsProcessor
    - __call__

[[autodoc]] ForcedEOSTokenLogitsProcessor
    - __call__

[[autodoc]] ForceTokensLogitsProcessor
    - __call__

[[autodoc]] HammingDiversityLogitsProcessor
    - __call__

[[autodoc]] InfNanRemoveLogitsProcessor
    - __call__

[[autodoc]] LogitNormalization
    - __call__

Sylvain Gugger's avatar
Sylvain Gugger committed
155
156
157
158
159
160
161
162
163
164
165
166
[[autodoc]] LogitsProcessor
    - __call__

[[autodoc]] LogitsProcessorList
    - __call__

[[autodoc]] LogitsWarper
    - __call__

[[autodoc]] MinLengthLogitsProcessor
    - __call__

167
168
169
[[autodoc]] MinNewTokensLengthLogitsProcessor
    - __call__

170
171
172
[[autodoc]] MinPLogitsWarper
    - __call__

173
[[autodoc]] NoBadWordsLogitsProcessor
Sylvain Gugger's avatar
Sylvain Gugger committed
174
175
    - __call__

176
[[autodoc]] NoRepeatNGramLogitsProcessor
Sylvain Gugger's avatar
Sylvain Gugger committed
177
178
    - __call__

179
[[autodoc]] PrefixConstrainedLogitsProcessor
Sylvain Gugger's avatar
Sylvain Gugger committed
180
181
    - __call__

182
[[autodoc]] RepetitionPenaltyLogitsProcessor
Sylvain Gugger's avatar
Sylvain Gugger committed
183
184
    - __call__

185
[[autodoc]] SequenceBiasLogitsProcessor
186
187
    - __call__

188
[[autodoc]] SuppressTokensAtBeginLogitsProcessor
Sylvain Gugger's avatar
Sylvain Gugger committed
189
190
    - __call__

191
[[autodoc]] SuppressTokensLogitsProcessor
192
193
    - __call__

194
[[autodoc]] TemperatureLogitsWarper
Sylvain Gugger's avatar
Sylvain Gugger committed
195
196
    - __call__

197
[[autodoc]] TopKLogitsWarper
Sylvain Gugger's avatar
Sylvain Gugger committed
198
199
    - __call__

200
[[autodoc]] TopPLogitsWarper
Sylvain Gugger's avatar
Sylvain Gugger committed
201
202
    - __call__

203
[[autodoc]] TypicalLogitsWarper
Sylvain Gugger's avatar
Sylvain Gugger committed
204
205
    - __call__

206
[[autodoc]] UnbatchedClassifierFreeGuidanceLogitsProcessor
Sylvain Gugger's avatar
Sylvain Gugger committed
207
208
    - __call__

209
[[autodoc]] WhisperTimeStampLogitsProcessor
Sylvain Gugger's avatar
Sylvain Gugger committed
210
211
    - __call__

212
213
214
215
[[autodoc]] WatermarkLogitsProcessor
    - __call__


216
217
218
### TensorFlow

[[autodoc]] TFForcedBOSTokenLogitsProcessor
219
220
    - __call__

221
[[autodoc]] TFForcedEOSTokenLogitsProcessor
222
223
    - __call__

224
[[autodoc]] TFForceTokensLogitsProcessor
225
226
    - __call__

227
[[autodoc]] TFLogitsProcessor
228
229
    - __call__

230
[[autodoc]] TFLogitsProcessorList
231
232
    - __call__

233
[[autodoc]] TFLogitsWarper
234
235
    - __call__

236
237
238
239
240
[[autodoc]] TFMinLengthLogitsProcessor
    - __call__

[[autodoc]] TFNoBadWordsLogitsProcessor
    - __call__
241

242
243
[[autodoc]] TFNoRepeatNGramLogitsProcessor
    - __call__
244

245
246
[[autodoc]] TFRepetitionPenaltyLogitsProcessor
    - __call__
247

248
[[autodoc]] TFSuppressTokensAtBeginLogitsProcessor
249
250
    - __call__

251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
[[autodoc]] TFSuppressTokensLogitsProcessor
    - __call__

[[autodoc]] TFTemperatureLogitsWarper
    - __call__

[[autodoc]] TFTopKLogitsWarper
    - __call__

[[autodoc]] TFTopPLogitsWarper
    - __call__

### FLAX

[[autodoc]] FlaxForcedBOSTokenLogitsProcessor
    - __call__

[[autodoc]] FlaxForcedEOSTokenLogitsProcessor
    - __call__

[[autodoc]] FlaxForceTokensLogitsProcessor
272
273
    - __call__

Sylvain Gugger's avatar
Sylvain Gugger committed
274
275
276
277
278
279
280
281
282
[[autodoc]] FlaxLogitsProcessor
    - __call__

[[autodoc]] FlaxLogitsProcessorList
    - __call__

[[autodoc]] FlaxLogitsWarper
    - __call__

283
[[autodoc]] FlaxMinLengthLogitsProcessor
Sylvain Gugger's avatar
Sylvain Gugger committed
284
285
    - __call__

286
[[autodoc]] FlaxSuppressTokensAtBeginLogitsProcessor
Sylvain Gugger's avatar
Sylvain Gugger committed
287
288
    - __call__

289
[[autodoc]] FlaxSuppressTokensLogitsProcessor
Sylvain Gugger's avatar
Sylvain Gugger committed
290
291
    - __call__

292
[[autodoc]] FlaxTemperatureLogitsWarper
Sylvain Gugger's avatar
Sylvain Gugger committed
293
294
    - __call__

295
[[autodoc]] FlaxTopKLogitsWarper
Sylvain Gugger's avatar
Sylvain Gugger committed
296
297
    - __call__

298
299
300
301
[[autodoc]] FlaxTopPLogitsWarper
    - __call__

[[autodoc]] FlaxWhisperTimeStampLogitsProcessor
Sylvain Gugger's avatar
Sylvain Gugger committed
302
303
304
305
    - __call__

## StoppingCriteria

306
A [`StoppingCriteria`] can be used to change when to stop generation (other than EOS token). Please note that this is exclusively available to our PyTorch implementations.
Sylvain Gugger's avatar
Sylvain Gugger committed
307
308
309
310
311
312
313
314
315
316
317
318
319

[[autodoc]] StoppingCriteria
    - __call__

[[autodoc]] StoppingCriteriaList
    - __call__

[[autodoc]] MaxLengthCriteria
    - __call__

[[autodoc]] MaxTimeCriteria
    - __call__

320
321
322
323
324
325
[[autodoc]] StopStringCriteria
    - __call__

[[autodoc]] EosTokenCriteria
    - __call__

326
327
## Constraints

328
A [`Constraint`] can be used to force the generation to include specific tokens or sequences in the output. Please note that this is exclusively available to our PyTorch implementations.
329
330
331
332
333

[[autodoc]] Constraint

[[autodoc]] PhrasalConstraint

334
335
[[autodoc]] DisjunctiveConstraint

336
337
[[autodoc]] ConstraintListState

Sylvain Gugger's avatar
Sylvain Gugger committed
338
339
340
341
342
343
344
345
346
347
## BeamSearch

[[autodoc]] BeamScorer
    - process
    - finalize

[[autodoc]] BeamSearchScorer
    - process
    - finalize

348
349
350
351
[[autodoc]] ConstrainedBeamSearchScorer
    - process
    - finalize

352
353
354
## Streamers

[[autodoc]] TextStreamer
355
356

[[autodoc]] TextIteratorStreamer
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

## Caches

[[autodoc]] Cache
    - update

[[autodoc]] DynamicCache
    - update
    - get_seq_length
    - reorder_cache
    - to_legacy_cache
    - from_legacy_cache

[[autodoc]] SinkCache
    - update
    - get_seq_length
    - reorder_cache
374
375
376

[[autodoc]] StaticCache
    - update
377
    - get_seq_length
378
    - reorder_cache
379
380
381
382
383
384
385


## Watermark Utils

[[autodoc]] WatermarkDetector
    - __call__