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

13
14
15
Utilities for Generation
-----------------------------------------------------------------------------------------------------------------------

16
17
18
19
20
21
This page lists all the utility functions used by :meth:`~transformers.generation_utils.GenerationMixin.generate`,
:meth:`~transformers.generation_utils.GenerationMixin.greedy_search`,
:meth:`~transformers.generation_utils.GenerationMixin.sample`,
:meth:`~transformers.generation_utils.GenerationMixin.beam_search`,
:meth:`~transformers.generation_utils.GenerationMixin.beam_sample`, and
:meth:`~transformers.generation_utils.GenerationMixin.group_beam_search`.
22
23
24

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

25
26
27
Generate Outputs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

28
The output of :meth:`~transformers.generation_utils.GenerationMixin.generate` is an instance of a subclass of
29
:class:`~transformers.file_utils.ModelOutput`. This output is a data structure containing all the information returned
30
by :meth:`~transformers.generation_utils.GenerationMixin.generate`, but that can also be used as tuple or dictionary.
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

Here's an example:

.. code-block::

    from transformers import GPT2Tokenizer, GPT2LMHeadModel

    tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
    model = GPT2LMHeadModel.from_pretrained('gpt2')

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

The ``generation_output`` object is a :class:`~transformers.generation_utils.GreedySearchDecoderOnlyOutput`, as we can
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

.. code-block::

    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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. autoclass:: transformers.generation_utils.GreedySearchDecoderOnlyOutput
    :members:

.. autoclass:: transformers.generation_utils.GreedySearchEncoderDecoderOutput
    :members:

Patrick von Platen's avatar
Patrick von Platen committed
83
84
85
.. autoclass:: transformers.generation_flax_utils.FlaxGreedySearchOutput
    :members:

86
87
88
89
90
91
92
93
94
95

SampleOutput
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. autoclass:: transformers.generation_utils.SampleDecoderOnlyOutput
    :members:

.. autoclass:: transformers.generation_utils.SampleEncoderDecoderOutput
    :members:

Patrick von Platen's avatar
Patrick von Platen committed
96
97
98
.. autoclass:: transformers.generation_flax_utils.FlaxSampleOutput
    :members:

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

BeamSearchOutput
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. autoclass:: transformers.generation_utils.BeamSearchDecoderOnlyOutput
    :members:

.. autoclass:: transformers.generation_utils.BeamSearchEncoderDecoderOutput
    :members:


BeamSampleOutput
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. autoclass:: transformers.generation_utils.BeamSampleDecoderOnlyOutput
    :members:

.. autoclass:: transformers.generation_utils.BeamSampleEncoderDecoderOutput
    :members:


120
121
122
123
124
125
126
127
128
129
130
131
LogitsProcessor
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A :class:`~transformers.LogitsProcessor` can be used to modify the prediction scores of a language model head for
generation.

.. autoclass:: transformers.LogitsProcessor
    :members: __call__

.. autoclass:: transformers.LogitsProcessorList
    :members: __call__

132
133
134
.. autoclass:: transformers.LogitsWarper
    :members: __call__

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
.. autoclass:: transformers.MinLengthLogitsProcessor
    :members: __call__

.. autoclass:: transformers.TemperatureLogitsWarper
    :members: __call__

.. autoclass:: transformers.RepetitionPenaltyLogitsProcessor
    :members: __call__

.. autoclass:: transformers.TopPLogitsWarper
    :members: __call__

.. autoclass:: transformers.TopKLogitsWarper
    :members: __call__

.. autoclass:: transformers.NoRepeatNGramLogitsProcessor
    :members: __call__

.. autoclass:: transformers.NoBadWordsLogitsProcessor
    :members: __call__

156
157
158
159
160
161
.. autoclass:: transformers.PrefixConstrainedLogitsProcessor
    :members: __call__

.. autoclass:: transformers.HammingDiversityLogitsProcessor
    :members: __call__

162
163
164
165
166
167
168
169
170
.. autoclass:: transformers.ForcedBOSTokenLogitsProcessor
    :members: __call__

.. autoclass:: transformers.ForcedEOSTokenLogitsProcessor
    :members: __call__

.. autoclass:: transformers.InfNanRemoveLogitsProcessor
    :members: __call__

Patrick von Platen's avatar
Patrick von Platen committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
.. autoclass:: transformers.FlaxLogitsProcessor
    :members: __call__

.. autoclass:: transformers.FlaxLogitsProcessorList
    :members: __call__

.. autoclass:: transformers.FlaxLogitsWarper
    :members: __call__

.. autoclass:: transformers.FlaxTemperatureLogitsWarper
    :members: __call__

.. autoclass:: transformers.FlaxTopPLogitsWarper
    :members: __call__

.. autoclass:: transformers.FlaxTopKLogitsWarper
    :members: __call__

189

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
StoppingCriteria
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A :class:`~transformers.StoppingCriteria` can be used to change when to stop generation (other than EOS token).

.. autoclass:: transformers.StoppingCriteria
    :members: __call__

.. autoclass:: transformers.StoppingCriteriaList
    :members: __call__

.. autoclass:: transformers.MaxLengthCriteria
    :members: __call__

.. autoclass:: transformers.MaxTimeCriteria
    :members: __call__

207
208
209
210
211
212
213
214
BeamSearch
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.BeamScorer
    :members: process, finalize

.. autoclass:: transformers.BeamSearchScorer
    :members: process, finalize
215
216
217
218
219
220
221

Utilities
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autofunction:: transformers.top_k_top_p_filtering

.. autofunction:: transformers.tf_top_k_top_p_filtering