Utilities for Generation
This page lists all the utility functions used by generate(), greedy_search(), contrastive_search(), sample(), beam_search(), beam_sample(), group_beam_search(), and constrained_beam_search().
Most of those are only useful if you are studying the code of the generate methods in the library.
Generate Outputs
The output of generate() is an instance of a subclass of ModelOutput. This output is a data structure containing all the information returned by generate(), but that can also be used as tuple or dictionary.
Here’s an example:
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 GreedySearchDecoderOnlyOutput, as we can
see in the documentation of that class below, it means it has the following attributes:
sequences
: the generated sequences of tokensscores
(optional): the prediction scores of the language modelling head, for each generation stephidden_states
(optional): the hidden states of the model, for each generation stepattentions
(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
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.
PyTorch
class transformers.generation.GreedySearchEncoderDecoderOutput
< source >( sequences: LongTensor = None scores: typing.Optional[typing.Tuple[torch.FloatTensor]] = None encoder_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None encoder_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None decoder_attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None cross_attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None decoder_hidden_states: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None )
Parameters
-
sequences (
torch.LongTensor
of shape(batch_size, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
scores (
tuple(torch.FloatTensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax) at each generation step. Tuple oftorch.FloatTensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size, config.vocab_size)
. -
encoder_attentions (
tuple(torch.FloatTensor)
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple oftorch.FloatTensor
(one for each layer of the decoder) of shape(batch_size, num_heads, sequence_length, sequence_length)
. - encoder_hidden_states (
tuple(torch.FloatTensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple oftorch.FloatTensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size, sequence_length, hidden_size)
. -
decoder_attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. -
cross_attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - decoder_hidden_states (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size, generated_length, hidden_size)
.
Base class for outputs of encoder-decoder generation models using greedy search. Hidden states and attention weights of the decoder (respectively the encoder) can be accessed via the encoder_attentions and the encoder_hidden_states attributes (respectively the decoder_attentions and the decoder_hidden_states attributes)
class transformers.generation.GreedySearchDecoderOnlyOutput
< source >( sequences: LongTensor = None scores: typing.Optional[typing.Tuple[torch.FloatTensor]] = None attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None hidden_states: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None )
Parameters
-
sequences (
torch.LongTensor
of shape(batch_size, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
scores (
tuple(torch.FloatTensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax) at each generation step. Tuple oftorch.FloatTensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size, config.vocab_size)
. -
attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - hidden_states (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size, generated_length, hidden_size)
.
Base class for outputs of decoder-only generation models using greedy search.
class transformers.generation.SampleEncoderDecoderOutput
< source >( sequences: LongTensor = None scores: typing.Optional[typing.Tuple[torch.FloatTensor]] = None encoder_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None encoder_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None decoder_attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None cross_attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None decoder_hidden_states: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None )
Parameters
-
sequences (
torch.LongTensor
of shape(batch_size*num_return_sequences, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
scores (
tuple(torch.FloatTensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax) at each generation step. Tuple oftorch.FloatTensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size*num_return_sequences, config.vocab_size)
. -
encoder_attentions (
tuple(torch.FloatTensor)
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple oftorch.FloatTensor
(one for each layer of the decoder) of shape(batch_size*num_return_sequences, num_heads, sequence_length, sequence_length)
. - encoder_hidden_states (
tuple(torch.FloatTensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple oftorch.FloatTensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size*num_return_sequences, sequence_length, hidden_size)
. -
decoder_attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size*num_return_sequences, num_heads, generated_length, sequence_length)
. -
cross_attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - decoder_hidden_states (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size*num_return_sequences, generated_length, hidden_size)
.
Base class for outputs of encoder-decoder generation models using sampling. Hidden states and attention weights of the decoder (respectively the encoder) can be accessed via the encoder_attentions and the encoder_hidden_states attributes (respectively the decoder_attentions and the decoder_hidden_states attributes)
class transformers.generation.SampleDecoderOnlyOutput
< source >( sequences: LongTensor = None scores: typing.Optional[typing.Tuple[torch.FloatTensor]] = None attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None hidden_states: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None )
Parameters
-
sequences (
torch.LongTensor
of shape(batch_size*num_return_sequences, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
scores (
tuple(torch.FloatTensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax) at each generation step. Tuple oftorch.FloatTensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size*num_return_sequences, config.vocab_size)
. -
attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(num_return_sequences*batch_size, num_heads, generated_length, sequence_length)
. - hidden_states (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(num_return_sequences*batch_size, generated_length, hidden_size)
.
Base class for outputs of decoder-only generation models using sampling.
class transformers.generation.BeamSearchEncoderDecoderOutput
< source >( sequences: LongTensor = None sequences_scores: typing.Optional[torch.FloatTensor] = None scores: typing.Optional[typing.Tuple[torch.FloatTensor]] = None beam_indices: typing.Optional[torch.LongTensor] = None encoder_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None encoder_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None decoder_attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None cross_attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None decoder_hidden_states: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None )
Parameters
-
sequences (
torch.LongTensor
of shape(batch_size*num_return_sequences, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
sequences_scores (
torch.FloatTensor
of shape(batch_size*num_return_sequences)
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Final beam scores of the generatedsequences
. -
scores (
tuple(torch.FloatTensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Beam transition scores for each vocabulary token at each generation step. Beam transition scores consisting of log probabilities of tokens conditioned on log softmax of previously generated tokens in this beam. Tuple oftorch.FloatTensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size*num_beams, config.vocab_size)
. -
beam_indices (
torch.LongTensor
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Beam indices of generated token id at each generation step.torch.LongTensor
of shape(batch_size*num_return_sequences, sequence_length)
. -
encoder_attentions (
tuple(torch.FloatTensor)
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple oftorch.FloatTensor
(one for each layer of the decoder) of shape(batch_size, num_heads, sequence_length, sequence_length)
. - encoder_hidden_states (
tuple(torch.FloatTensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple oftorch.FloatTensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size*num_beams*num_return_sequences, sequence_length, hidden_size)
. -
decoder_attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size*num_beams*num_return_sequences, num_heads, generated_length, sequence_length)
. -
cross_attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - decoder_hidden_states (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size*num_beams*num_return_sequences, generated_length, hidden_size)
.
Base class for outputs of encoder-decoder generation models using beam search. Hidden states and attention weights of the decoder (respectively the encoder) can be accessed via the encoder_attentions and the encoder_hidden_states attributes (respectively the decoder_attentions and the decoder_hidden_states attributes)
class transformers.generation.BeamSearchDecoderOnlyOutput
< source >( sequences: LongTensor = None sequences_scores: typing.Optional[torch.FloatTensor] = None scores: typing.Optional[typing.Tuple[torch.FloatTensor]] = None beam_indices: typing.Optional[torch.LongTensor] = None attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None hidden_states: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None )
Parameters
-
sequences (
torch.LongTensor
of shape(batch_size*num_return_sequences, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
sequences_scores (
torch.FloatTensor
of shape(batch_size*num_return_sequences)
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Final beam scores of the generatedsequences
. -
scores (
tuple(torch.FloatTensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Beam transition scores for each vocabulary token at each generation step. Beam transition scores consisting of log probabilities of tokens conditioned on log softmax of previously generated tokens in this beam. Tuple oftorch.FloatTensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size*num_beams*num_return_sequences, config.vocab_size)
. -
beam_indices (
torch.LongTensor
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Beam indices of generated token id at each generation step.torch.LongTensor
of shape(batch_size*num_return_sequences, sequence_length)
. -
attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size*num_beams, num_heads, generated_length, sequence_length)
. - hidden_states (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size*num_beams*num_return_sequences, generated_length, hidden_size)
.
Base class for outputs of decoder-only generation models using beam search.
class transformers.generation.BeamSampleEncoderDecoderOutput
< source >( sequences: LongTensor = None sequences_scores: typing.Optional[torch.FloatTensor] = None scores: typing.Optional[typing.Tuple[torch.FloatTensor]] = None beam_indices: typing.Optional[torch.LongTensor] = None encoder_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None encoder_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None decoder_attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None cross_attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None decoder_hidden_states: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None )
Parameters
-
sequences (
torch.LongTensor
of shape(batch_size*num_beams, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
sequences_scores (
torch.FloatTensor
of shape(batch_size * num_return_sequence)
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Final beam scores of the generatedsequences
. -
scores (
tuple(torch.FloatTensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Beam transition scores for each vocabulary token at each generation step. Beam transition scores consisting of log probabilities of tokens conditioned on log softmax of previously generated tokens in this beam. Tuple oftorch.FloatTensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size*num_beams, config.vocab_size)
). -
beam_indices (
torch.LongTensor
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Beam indices of generated token id at each generation step.torch.LongTensor
of shape(batch_size*num_return_sequences, sequence_length)
. -
encoder_attentions (
tuple(torch.FloatTensor)
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple oftorch.FloatTensor
(one for each layer of the decoder) of shape(batch_size, num_heads, sequence_length, sequence_length)
. - encoder_hidden_states (
tuple(torch.FloatTensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple oftorch.FloatTensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size*num_beams, sequence_length, hidden_size)
. -
decoder_attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size*num_beams, num_heads, generated_length, sequence_length)
. -
cross_attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - decoder_hidden_states (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size*num_beams, generated_length, hidden_size)
.
Base class for outputs of encoder-decoder generation models using beam sampling. Hidden states and attention weights of the decoder (respectively the encoder) can be accessed via the encoder_attentions and the encoder_hidden_states attributes (respectively the decoder_attentions and the decoder_hidden_states attributes)
class transformers.generation.BeamSampleDecoderOnlyOutput
< source >( sequences: LongTensor = None sequences_scores: typing.Optional[torch.FloatTensor] = None scores: typing.Optional[typing.Tuple[torch.FloatTensor]] = None beam_indices: typing.Optional[torch.LongTensor] = None attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None hidden_states: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None )
Parameters
-
sequences (
torch.LongTensor
of shape(batch_size*num_return_sequences, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
sequences_scores (
torch.FloatTensor
of shape(batch_size * num_return_sequence)
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Final beam scores of the generatedsequences
. -
scores (
tuple(torch.FloatTensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Beam transition scores for each vocabulary token at each generation step. Beam transition scores consisting of log probabilities of tokens conditioned on log softmax of previously generated tokens in this beam. Tuple oftorch.FloatTensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size*num_beams*num_return_sequences, config.vocab_size)
. -
beam_indices (
torch.LongTensor
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Beam indices of generated token id at each generation step.torch.LongTensor
of shape(batch_size*num_return_sequences, sequence_length)
. -
attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size*num_beams, num_heads, generated_length, sequence_length)
. - hidden_states (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size*num_beams, generated_length, hidden_size)
.
Base class for outputs of decoder-only generation models using beam sample.
class transformers.generation.ContrastiveSearchEncoderDecoderOutput
< source >( sequences: LongTensor = None scores: typing.Optional[typing.Tuple[torch.FloatTensor]] = None encoder_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None encoder_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None decoder_attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None cross_attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None decoder_hidden_states: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None )
Parameters
-
sequences (
torch.LongTensor
of shape(batch_size, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
scores (
tuple(torch.FloatTensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax) at each generation step. Tuple oftorch.FloatTensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size, config.vocab_size)
. -
encoder_attentions (
tuple(torch.FloatTensor)
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple oftorch.FloatTensor
(one for each layer of the decoder) of shape(batch_size, num_heads, sequence_length, sequence_length)
. - encoder_hidden_states (
tuple(torch.FloatTensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple oftorch.FloatTensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size, sequence_length, hidden_size)
. -
decoder_attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. -
cross_attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - decoder_hidden_states (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size, generated_length, hidden_size)
.
Base class for outputs of decoder-only generation models using contrastive search.
class transformers.generation.ContrastiveSearchDecoderOnlyOutput
< source >( sequences: LongTensor = None scores: typing.Optional[typing.Tuple[torch.FloatTensor]] = None attentions: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None hidden_states: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = None )
Parameters
-
sequences (
torch.LongTensor
of shape(batch_size, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
scores (
tuple(torch.FloatTensor)
optional, returned whenoutput_scores=True
is passed or when —config.output_scores=True
) — Processed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax) at each generation step. Tuple oftorch.FloatTensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size, config.vocab_size)
. -
attentions (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - hidden_states (
tuple(tuple(torch.FloatTensor))
, optional, returned whenoutput_hidden_states=True
is — -
passed or when
config.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftorch.FloatTensor
of shape(batch_size, generated_length, hidden_size)
.
Base class for outputs of decoder-only generation models using contrastive search.
TensorFlow
class transformers.generation.TFGreedySearchEncoderDecoderOutput
< source >( sequences: Tensor = None scores: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None encoder_attentions: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None encoder_hidden_states: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None decoder_attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None cross_attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None decoder_hidden_states: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None )
Parameters
-
sequences (
tf.Tensor
of shape(batch_size, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
scores (
tuple(tf.Tensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax) at each generation step. Tuple oftf.Tensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size, config.vocab_size)
. -
encoder_attentions (
tuple(tf.Tensor)
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple oftf.Tensor
(one for each layer of the decoder) of shape(batch_size, num_heads, sequence_length, sequence_length)
. - encoder_hidden_states (
tuple(tf.Tensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple oftf.Tensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size, sequence_length, hidden_size)
. -
decoder_attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. -
cross_attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - decoder_hidden_states (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size, generated_length, hidden_size)
.
Base class for outputs of encoder-decoder generation models using greedy search. Hidden states and attention weights of the decoder (respectively the encoder) can be accessed via the encoder_attentions and the encoder_hidden_states attributes (respectively the decoder_attentions and the decoder_hidden_states attributes)
class transformers.generation.TFGreedySearchDecoderOnlyOutput
< source >( sequences: Tensor = None scores: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None hidden_states: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None )
Parameters
-
sequences (
tf.Tensor
of shape(batch_size, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
scores (
tuple(tf.Tensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax) at each generation step. Tuple oftf.Tensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size, config.vocab_size)
. -
attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - hidden_states (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size, generated_length, hidden_size)
.
Base class for outputs of decoder-only generation models using greedy search.
class transformers.generation.TFSampleEncoderDecoderOutput
< source >( sequences: Tensor = None scores: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None encoder_attentions: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None encoder_hidden_states: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None decoder_attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None cross_attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None decoder_hidden_states: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None )
Parameters
-
sequences (
tf.Tensor
of shape(batch_size*num_return_sequences, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
scores (
tuple(tf.Tensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax) at each generation step. Tuple oftf.Tensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size*num_return_sequences, config.vocab_size)
. -
encoder_attentions (
tuple(tf.Tensor)
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple oftf.Tensor
(one for each layer of the decoder) of shape(batch_size*num_return_sequences, num_heads, sequence_length, sequence_length)
. - encoder_hidden_states (
tuple(tf.Tensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple oftf.Tensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size*num_return_sequences, sequence_length, hidden_size)
. -
decoder_attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size*num_return_sequences, num_heads, generated_length, sequence_length)
. -
cross_attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - decoder_hidden_states (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size*num_return_sequences, generated_length, hidden_size)
.
Base class for outputs of encoder-decoder generation models using sampling. Hidden states and attention weights of the decoder (respectively the encoder) can be accessed via the encoder_attentions and the encoder_hidden_states attributes (respectively the decoder_attentions and the decoder_hidden_states attributes)
class transformers.generation.TFSampleDecoderOnlyOutput
< source >( sequences: Tensor = None scores: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None hidden_states: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None )
Parameters
-
sequences (
tf.Tensor
of shape(batch_size*num_return_sequences, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
scores (
tuple(tf.Tensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax) at each generation step. Tuple oftf.Tensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size*num_return_sequences, config.vocab_size)
. -
attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(num_return_sequences*batch_size, num_heads, generated_length, sequence_length)
. - hidden_states (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(num_return_sequences*batch_size, generated_length, hidden_size)
.
Base class for outputs of decoder-only generation models using sampling.
class transformers.generation.TFBeamSearchEncoderDecoderOutput
< source >( sequences: Tensor = None sequences_scores: typing.Optional[tensorflow.python.framework.ops.Tensor] = None scores: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None beam_indices: typing.Optional[tensorflow.python.framework.ops.Tensor] = None encoder_attentions: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None encoder_hidden_states: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None decoder_attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None cross_attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None decoder_hidden_states: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None )
Parameters
-
sequences (
tf.Tensor
of shape(batch_size*num_return_sequences, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
sequences_scores (
tf.Tensor
of shape(batch_size*num_return_sequences)
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Final beam scores of the generatedsequences
. -
scores (
tuple(tf.Tensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed beam scores for each vocabulary token at each generation step. Beam scores consisting of log softmax scores for each vocabulary token and sum of log softmax of previously generated tokens in this beam.Tuple of
tf.Tensorwith up to
max_new_tokenselements (one element for each generated token), with each tensor of shape
(batch_size*num_beams, config.vocab_size)`. -
beam_indices (
tf.Tensor
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Beam indices of generated token id at each generation step.tf.Tensor
of shape(batch_size*num_return_sequences, sequence_length)
. -
encoder_attentions (
tuple(tf.Tensor)
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple oftf.Tensor
(one for each layer of the decoder) of shape(batch_size, num_heads, sequence_length, sequence_length)
. - encoder_hidden_states (
tuple(tf.Tensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple oftf.Tensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size*num_beams*num_return_sequences, sequence_length, hidden_size)
. -
decoder_attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size*num_beams*num_return_sequences, num_heads, generated_length, sequence_length)
. -
cross_attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - decoder_hidden_states (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size*num_beams*num_return_sequences, generated_length, hidden_size)
.
Base class for outputs of encoder-decoder generation models using beam search. Hidden states and attention weights of the decoder (respectively the encoder) can be accessed via the encoder_attentions and the encoder_hidden_states attributes (respectively the decoder_attentions and the decoder_hidden_states attributes)
class transformers.generation.TFBeamSearchDecoderOnlyOutput
< source >( sequences: Tensor = None sequences_scores: typing.Optional[tensorflow.python.framework.ops.Tensor] = None scores: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None beam_indices: typing.Optional[tensorflow.python.framework.ops.Tensor] = None attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None hidden_states: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None )
Parameters
-
sequences (
tf.Tensor
of shape(batch_size*num_return_sequences, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
sequences_scores (
tf.Tensor
of shape(batch_size*num_return_sequences)
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Final beam scores of the generatedsequences
. -
scores (
tuple(tf.Tensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed beam scores for each vocabulary token at each generation step. Beam scores consisting of log softmax scores for each vocabulary token and sum of log softmax of previously generated tokens in this beam. Tuple oftf.Tensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size*num_beams*num_return_sequences, config.vocab_size)
. -
beam_indices (
tf.Tensor
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Beam indices of generated token id at each generation step.tf.Tensor
of shape(batch_size*num_return_sequences, sequence_length)
. -
attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size*num_beams, num_heads, generated_length, sequence_length)
. - hidden_states (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size*num_beams*num_return_sequences, generated_length, hidden_size)
.
Base class for outputs of decoder-only generation models using beam search.
class transformers.generation.TFBeamSampleEncoderDecoderOutput
< source >( sequences: Tensor = None sequences_scores: typing.Optional[tensorflow.python.framework.ops.Tensor] = None scores: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None beam_indices: typing.Optional[tensorflow.python.framework.ops.Tensor] = None encoder_attentions: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None encoder_hidden_states: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None decoder_attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None cross_attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None decoder_hidden_states: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None )
Parameters
-
sequences (
tf.Tensor
of shape(batch_size*num_beams, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
sequences_scores (
tf.Tensor
of shape(batch_size * num_return_sequence)
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Final beam scores of the generatedsequences
. -
scores (
tuple(tf.Tensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed beam scores for each vocabulary token at each generation step. Beam scores consisting of log softmax scores for each vocabulary token and sum of log softmax of previously generated tokens in this beam. Tuple oftf.Tensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size*num_beams, config.vocab_size)
. -
beam_indices (
tf.Tensor
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Beam indices of generated token id at each generation step.tf.Tensor
of shape(batch_size*num_return_sequences, sequence_length)
. -
encoder_attentions (
tuple(tf.Tensor)
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple oftf.Tensor
(one for each layer of the decoder) of shape(batch_size, num_heads, sequence_length, sequence_length)
. - encoder_hidden_states (
tuple(tf.Tensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple oftf.Tensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size*num_beams, sequence_length, hidden_size)
. -
decoder_attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size*num_beams, num_heads, generated_length, sequence_length)
. -
cross_attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - decoder_hidden_states (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size*num_beams, generated_length, hidden_size)
.
Base class for outputs of encoder-decoder generation models using beam sampling. Hidden states and attention weights of the decoder (respectively the encoder) can be accessed via the encoder_attentions and the encoder_hidden_states attributes (respectively the decoder_attentions and the decoder_hidden_states attributes)
class transformers.generation.TFBeamSampleDecoderOnlyOutput
< source >( sequences: Tensor = None sequences_scores: typing.Optional[tensorflow.python.framework.ops.Tensor] = None scores: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None beam_indices: typing.Optional[tensorflow.python.framework.ops.Tensor] = None attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None hidden_states: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None )
Parameters
-
sequences (
tf.Tensor
of shape(batch_size*num_return_sequences, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
sequences_scores (
tf.Tensor
of shape(batch_size * num_return_sequence)
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Final beam scores of the generatedsequences
. -
scores (
tuple(tf.Tensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed beam scores for each vocabulary token at each generation step. Beam scores consisting of log softmax scores for each vocabulary token and sum of log softmax of previously generated tokens in this beam. Tuple oftf.Tensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size*num_beams*num_return_sequences, config.vocab_size)
. -
beam_indices (
tf.Tensor
, optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Beam indices of generated token id at each generation step.tf.Tensor
of shape(batch_size*num_return_sequences, sequence_length)
. -
attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size*num_beams, num_heads, generated_length, sequence_length)
. - hidden_states (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size*num_beams, generated_length, hidden_size)
.
Base class for outputs of decoder-only generation models using beam sample.
class transformers.generation.TFContrastiveSearchEncoderDecoderOutput
< source >( sequences: Tensor = None scores: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None encoder_attentions: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None encoder_hidden_states: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None decoder_attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None cross_attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None decoder_hidden_states: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None )
Parameters
-
sequences (
tf.Tensor
of shape(batch_size, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
scores (
tuple(tf.Tensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax) at each generation step. Tuple oftf.Tensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size, config.vocab_size)
. -
encoder_attentions (
tuple(tf.Tensor)
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple oftf.Tensor
(one for each layer of the decoder) of shape(batch_size, num_heads, sequence_length, sequence_length)
. - encoder_hidden_states (
tuple(tf.Tensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple oftf.Tensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size, sequence_length, hidden_size)
. -
decoder_attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. -
cross_attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - decoder_hidden_states (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size, generated_length, hidden_size)
.
Base class for outputs of encoder-decoder generation models using contrastive search. Hidden states and attention weights of the decoder (respectively the encoder) can be accessed via the encoder_attentions and the encoder_hidden_states attributes (respectively the decoder_attentions and the decoder_hidden_states attributes)
class transformers.generation.TFContrastiveSearchDecoderOnlyOutput
< source >( sequences: Tensor = None scores: typing.Optional[typing.Tuple[tensorflow.python.framework.ops.Tensor]] = None attentions: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None hidden_states: typing.Optional[typing.Tuple[typing.Tuple[tensorflow.python.framework.ops.Tensor]]] = None )
Parameters
-
sequences (
tf.Tensor
of shape(batch_size, sequence_length)
) — The generated sequences. The second dimension (sequence_length) is either equal tomax_length
or shorter if all batches finished early due to theeos_token_id
. -
scores (
tuple(tf.Tensor)
optional, returned whenoutput_scores=True
is passed or whenconfig.output_scores=True
) — Processed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax) at each generation step. Tuple oftf.Tensor
with up tomax_new_tokens
elements (one element for each generated token), with each tensor of shape(batch_size, config.vocab_size)
. -
attentions (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_attentions=True
is passed orconfig.output_attentions=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size, num_heads, generated_length, sequence_length)
. - hidden_states (
tuple(tuple(tf.Tensor))
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) — Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) oftf.Tensor
of shape(batch_size, generated_length, hidden_size)
.
Base class for outputs of decoder-only generation models using contrastive search.
FLAX
class transformers.generation.FlaxSampleOutput
< source >( sequences: Array = None )
Flax Base class for outputs of decoder-only generation models using sampling.
“Returns a new object replacing the specified fields with new values.
class transformers.generation.FlaxGreedySearchOutput
< source >( sequences: Array = None )
Flax Base class for outputs of decoder-only generation models using greedy search.
“Returns a new object replacing the specified fields with new values.
class transformers.generation.FlaxBeamSearchOutput
< source >( sequences: Array = None scores: Array = None )
Flax Base class for outputs of decoder-only generation models using greedy search.
“Returns a new object replacing the specified fields with new values.
LogitsProcessor
A LogitsProcessor can be used to modify the prediction scores of a language model head for generation.
PyTorch
class transformers.AlternatingCodebooksLogitsProcessor
< source >( input_start_len: int semantic_vocab_size: int codebook_size: int )
LogitsProcessor enforcing alternated generation between the two codebooks of Bark
’s fine submodel.
class transformers.ClassifierFreeGuidanceLogitsProcessor
< source >( guidance_scale )
Logits processor for classifier free guidance (CFG). The scores are split over the batch dimension,
where the first half correspond to the conditional logits (predicted from the input prompt) and the second half
correspond to the unconditional logits (predicted from an empty or ‘null’ prompt). The processor computes a
weighted average across the conditional and unconditional logits, parameterised by the guidance_scale
.
See the paper for more information.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.EncoderNoRepeatNGramLogitsProcessor
< source >( encoder_ngram_size: int encoder_input_ids: LongTensor )
LogitsProcessor that enforces no repetition of encoder input ids n-grams for the decoder ids. See ParlAI.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.EncoderRepetitionPenaltyLogitsProcessor
< source >( penalty: float encoder_input_ids: LongTensor )
LogitsProcessor enforcing an exponential penalty on tokens that are not in the original input.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.EpsilonLogitsWarper
< source >( epsilon: float filter_value: float = -inf min_tokens_to_keep: int = 1 )
Parameters
-
epsilon (
float
) — If set to > 0, only the most tokens with probabilitiesepsilon
or higher are kept for generation. -
filter_value (
float
, optional, defaults to-float("Inf")
) — All filtered values will be set to this float value. -
min_tokens_to_keep (
int
, optional, defaults to 1) — Minimum number of tokens that cannot be filtered.
LogitsWarper that performs epsilon-sampling, i.e. restricting to tokens with prob >= epsilon
. Takes the
largest min_tokens_to_keep tokens if no tokens satisfy this constraint. See Truncation Sampling as Language Model
Desmoothing for more information.
Examples:
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
>>> set_seed(0)
>>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
>>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
>>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")
>>> # With sampling, the output is unexpected -- sometimes too unexpected.
>>> outputs = model.generate(**inputs, do_sample=True)
>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
A sequence: 1, 2, 0, 2, 2. 2, 2, 2, 2
>>> # With epsilon sampling, the output gets restricted to high-probability tokens. Note that this is similar to
>>> # Top P sampling, which restricts tokens based on their cumulative probability.
>>> # Pro tip: The paper recomends using `epsilon_cutoff` values between 3e-4 and 9e-4
>>> outputs = model.generate(**inputs, do_sample=True, epsilon_cutoff=0.1)
>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.EtaLogitsWarper
< source >( epsilon: float filter_value: float = -inf min_tokens_to_keep: int = 1 )
Parameters
-
epsilon (
float
) — A float value in the range (0, 1). Hyperparameter used to calculate the dynamic cutoff value,eta
. The suggested values from the paper ranges from 3e-4 to 4e-3 depending on the size of the model. -
filter_value (
float
, optional, defaults to-float("Inf")
) — All values that are found to be below the dynamic cutoff value,eta
, are set to this float value. This parameter is useful when logits need to be modified for very low probability tokens that should be excluded from generation entirely. -
min_tokens_to_keep (
int
, optional, defaults to 1) — Specifies the minimum number of tokens that must be kept for generation, regardless of their probabilities. For example, ifmin_tokens_to_keep
is set to 1, at least one token will always be kept for generation, even if all tokens have probabilities below the cutoffeta
.
LogitsWarper that performs eta-sampling, a technique to filter out tokens with probabilities below a dynamic
cutoff value, eta
, which is calculated based on a combination of the hyperparameter epsilon
and the entropy of
the token probabilities, i.e. eta := min(epsilon, sqrt(epsilon * e^-entropy(probabilities)))
. Takes the largest
min_tokens_to_keep tokens if no tokens satisfy this constraint. It addresses the issue of poor quality in long
samples of text generated by neural language models leading to more coherent and fluent text. See Truncation
Sampling as Language Model Desmoothing for more information. Note: do_sample
must be set to True
for this LogitsWarper
to work.
Examples:
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
>>> set_seed(0)
>>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
>>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
>>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")
>>> # With sampling, the output is unexpected -- sometimes too unexpected.
>>> outputs = model.generate(**inputs, do_sample=True)
>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
A sequence: 1, 2, 0, 2, 2. 2, 2, 2, 2
>>> # With eta sampling, the output gets restricted to high-probability tokens. You can see it as a dynamic form of
>>> # epsilon sampling that adapts its cutoff probability based on the entropy (high entropy = lower cutoff).
>>> # Pro tip: The paper recomends using `eta_cutoff` values between 3e-4 to 4e-3
>>> outputs = model.generate(**inputs, do_sample=True, eta_cutoff=0.1)
>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.ExponentialDecayLengthPenalty
< source >( exponential_decay_length_penalty: typing.Tuple[int, float] eos_token_id: typing.Union[int, typing.List[int]] input_ids_seq_length: int )
Parameters
-
exponential_decay_length_penalty (
tuple(int, float)
) — This tuple shall consist of:(start_index, decay_factor)
wherestart_index
indicates where penalty starts anddecay_factor
represents the factor of exponential decay -
eos_token_id (
Union[int, List[int]]
) — The id of the end-of-sequence token. Optionally, use a list to set multiple end-of-sequence tokens. -
input_ids_seq_length (
int
) — The length of the input sequence.
LogitsProcessor that exponentially increases the score of the eos_token_id after regulation_start has been reached.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.ForcedBOSTokenLogitsProcessor
< source >( bos_token_id: int )
LogitsProcessor that enforces the specified token as the first generated token.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.ForcedEOSTokenLogitsProcessor
< source >( max_length: int eos_token_id: typing.Union[int, typing.List[int]] )
LogitsProcessor that enforces the specified token as the last generated token when max_length
is reached.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.ForceTokensLogitsProcessor
< source >( force_token_map: typing.List[typing.List[int]] )
This processor takes a list of pairs of integers which indicates a mapping from generation indices to token
indices that will be forced before sampling. The processor will set their log probs to inf
so that they are
sampled at their corresponding index.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.HammingDiversityLogitsProcessor
< source >( diversity_penalty: float num_beams: int num_beam_groups: int )
Parameters
-
diversity_penalty (
float
) — 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 thatdiversity_penalty
is only effective if group beam search is enabled. The penalty applied to a beam’s score when it generates a token that has already been chosen by another beam within the same group during the same time step. A higherdiversity_penalty
will enforce greater diversity among the beams, making it less likely for multiple beams to choose the same token. Conversely, a lower penalty will allow beams to more freely choose similar tokens. Adjusting this value can help strike a balance between diversity and natural likelihood. -
num_beams (
int
) — Number of beams used for group beam search. Beam search is a method used that maintains beams (or “multiple hypotheses”) at each step, expanding each one and keeping the top-scoring sequences. A highernum_beams
will explore more potential sequences. This can increase chances of finding a high-quality output but also increases computational cost. -
num_beam_groups (
int
) — Number of groups to dividenum_beams
into in order to ensure diversity among different groups of beams. Each group of beams will operate independently, selecting tokens without considering the choices of other groups. This division promotes diversity by ensuring that beams within different groups explore different paths. For instance, ifnum_beams
is 6 andnum_beam_groups
is 2, there will be 2 groups each containing 3 beams. The choice ofnum_beam_groups
should be made considering the desired level of output diversity and the total number of beams. See this paper for more details.
LogitsProcessor that enforces diverse beam search.
Note that this logits processor is only effective for PreTrainedModel.group_beam_search(). See Diverse Beam Search: Decoding Diverse Solutions from Neural Sequence Models for more details.
Diverse beam search can be particularly useful in scenarios where a variety of different outputs is desired, rather than multiple similar sequences. It allows the model to explore different generation paths and provides a broader coverage of possible outputs.
This logits processor can be resource-intensive, especially when using large models or long sequences.
Traditional beam search often generates very similar sequences across different beams.
HammingDiversityLogitsProcessor
addresses this by penalizing beams that generate tokens already chosen by other
beams in the same time step.
How It Works:
- Grouping Beams: Beams are divided into groups. Each group selects tokens independently of the others.
- Penalizing Repeated Tokens: If a beam in a group selects a token already chosen by another group in the same step, a penalty is applied to that token’s score.
- Promoting Diversity: This penalty discourages beams within a group from selecting the same tokens as beams in other groups.
Benefits:
- Diverse Outputs: Produces a variety of different sequences.
- Exploration: Allows the model to explore different paths.
Examples:
>>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
>>> import torch
>>> # Initialize the model and tokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("t5-base")
>>> model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
>>> # A long text about the solar system
>>> text = "The Solar System is a gravitationally bound system comprising the Sun and the objects that orbit it, either directly or indirectly. Of the objects that orbit the Sun directly, the largest are the eight planets, with the remainder being smaller objects, such as the five dwarf planets and small Solar System bodies. The Solar System formed 4.6 billion years ago from the gravitational collapse of a giant interstellar molecular cloud."
>>> inputs = tokenizer("summarize: " + text, return_tensors="pt")
>>> # Generate diverse summary
>>> outputs_diverse = model.generate(
... **inputs,
... num_beam_groups=2,
... diversity_penalty=10.0,
... max_length=100,
... num_beams=4,
... num_return_sequences=2,
... )
>>> summaries_diverse = tokenizer.batch_decode(outputs_diverse, skip_special_tokens=True)
>>> # Generate non-diverse summary
>>> outputs_non_diverse = model.generate(
... **inputs,
... max_length=100,
... num_beams=4,
... num_return_sequences=2,
... )
>>> summary_non_diverse = tokenizer.batch_decode(outputs_non_diverse, skip_special_tokens=True)
>>> # With `diversity_penalty`, the resulting beams are much more diverse
>>> print(summary_non_diverse)
['the solar system formed 4.6 billion years ago from the collapse of a giant interstellar molecular cloud. of the objects that orbit the Sun directly, the largest are the eight planets.',
'the Solar System formed 4.6 billion years ago from the collapse of a giant interstellar molecular cloud. of the objects that orbit the Sun directly, the largest are the eight planets.']
>>> print(summaries_diverse)
['the solar system formed 4.6 billion years ago from the collapse of a giant interstellar molecular cloud. of the objects that orbit the Sun directly, the largest are the eight planets.',
'the solar system formed 4.6 billion years ago from the collapse of a giant interstellar molecular cloud. of the objects that orbit the Sun directly, the largest are the eight planets. the rest of the objects are smaller objects, such as the five dwarf planets and small solar system bodies.']
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
current_tokens: LongTensor
beam_group_idx: int
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search -
current_tokens (
torch.LongTensor
of shape(batch_size)
) — Indices of input sequence tokens in the vocabulary, corresponding to the tokens selected by the other beam groups in the current generation step. -
beam_group_idx (
int
) — The index of the beam group currently being processed.
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
LogitsProcessor that removes all nan
and inf
values to avoid the generation method to fail. Note that using
the logits processor should only be used if necessary since it can slow down the generation method.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
LogitsWarper and LogitsProcessor for normalizing the scores using log-softmax. It’s important to normalize the scores during beam search, after applying the logits processors or warpers, since the search algorithm used in this library doesn’t do it (it only does it before, but they may need re-normalization) but it still supposes that the scores are normalized when comparing the hypotheses.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
Abstract base class for all logit processors that can be applied during generation.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
This class can be used to create a list of LogitsProcessor or LogitsWarper to subsequently process a
scores
input tensor. This class inherits from list and adds a specific call method to apply each
LogitsProcessor or LogitsWarper to the inputs.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
**kwargs
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search -
kwargs (
Dict[str, Any]
, optional) — Additional kwargs that are specific to a logits processor.
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
Abstract base class for all logit warpers that can be applied during generation with multinomial sampling.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.MinLengthLogitsProcessor
< source >( min_length: int eos_token_id: typing.Union[int, typing.List[int]] )
LogitsProcessor enforcing a min-length by setting EOS probability to 0.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.MinNewTokensLengthLogitsProcessor
< source >( prompt_length_to_skip: int min_new_tokens: int eos_token_id: typing.Union[int, typing.List[int]] )
Parameters
-
prompt_length_to_skip (
int
) — The input tokens length. Not a valid argument when used withgenerate
as it will automatically assign the input length. -
min_new_tokens (
int
) — The minimum new tokens length below which the score ofeos_token_id
is set to-float("Inf")
. -
eos_token_id (
Union[int, List[int]]
) — The id of the end-of-sequence token. Optionally, use a list to set multiple end-of-sequence tokens.
LogitsProcessor enforcing a min-length of new tokens by setting EOS (End-Of-Sequence) token probability to 0.
Note that for decoder-only models, such as Llama2, min_length
will compute the length of prompt + newly generated tokens
whereas for other models it will behave as min_new_tokens
, that is, taking only into account
the newly generated ones.
Examples:
>>> from transformers import AutoTokenizer, AutoModelForCausalLM
>>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
>>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
>>> model.config.pad_token_id = model.config.eos_token_id
>>> inputs = tokenizer(["Hugging Face Company is"], return_tensors="pt")
>>> # If the maximum length (default = 20) is smaller than the minimum length constraint, the latter is ignored!
>>> outputs = model.generate(**inputs, min_new_tokens=30)
>>> print(tokenizer.decode(outputs[0], skip_special_tokens=True))
Hugging Face Company is a company that has been working on a new product for the past year.
>>> # For testing purposes, let's set `eos_token` to `"company"`, the first generated token. This will make
>>> # generation end there.
>>> outputs = model.generate(**inputs, eos_token_id=1664)
>>> print(tokenizer.decode(outputs[0], skip_special_tokens=True))
Hugging Face Company is a company
>>> # Increasing `min_new_tokens` will make generation ignore occurences `"company"` (eos token) before the
>>> # minimum length condition is honored.
>>> outputs = model.generate(**inputs, min_new_tokens=2, eos_token_id=1664)
>>> print(tokenizer.decode(outputs[0], skip_special_tokens=True))
Hugging Face Company is a new company
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.NoBadWordsLogitsProcessor
< source >( bad_words_ids: typing.List[typing.List[int]] eos_token_id: typing.Union[int, typing.List[int]] )
LogitsProcessor that enforces that specified sequences will never be selected.
In order to get the token ids of the words that should not appear in the generated text, make sure to set
add_prefix_space=True
when initializing the tokenizer, and use tokenizer(bad_words, add_special_tokens=False).input_ids
. The add_prefix_space
argument is only supported for some slow tokenizers,
as fast tokenizers’ prefixing behaviours come from pre tokenizers
. Read more
here.
Examples:
>>> from transformers import AutoTokenizer, AutoModelForCausalLM
>>> model = AutoModelForCausalLM.from_pretrained("gpt2")
>>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
>>> inputs = tokenizer(["In a word, the cake is a"], return_tensors="pt")
>>> output_ids = model.generate(inputs["input_ids"], max_new_tokens=5, pad_token_id=tokenizer.eos_token_id)
>>> print(tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0])
In a word, the cake is a bit of a mess.
>>> # Now let's take the bad words out. Please note that the tokenizer is initialized differently
>>> tokenizer_with_prefix_space = AutoTokenizer.from_pretrained("gpt2", add_prefix_space=True)
>>> def get_tokens_as_list(word_list):
... "Converts a sequence of words into a list of tokens"
... tokens_list = []
... for word in word_list:
... tokenized_word = tokenizer_with_prefix_space([word], add_special_tokens=False).input_ids[0]
... tokens_list.append(tokenized_word)
... return tokens_list
>>> bad_words_ids = get_tokens_as_list(word_list=["mess"])
>>> output_ids = model.generate(
... inputs["input_ids"], max_new_tokens=5, bad_words_ids=bad_words_ids, pad_token_id=tokenizer.eos_token_id
... )
>>> print(tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0])
In a word, the cake is a bit of a surprise.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.NoRepeatNGramLogitsProcessor
< source >( ngram_size: int )
N-grams are groups of “n” consecutive words, characters, or tokens taken from a sequence of text. Given the sentence: “She runs fast”, the bi-grams (n=2) would be (“she”, “runs”) and (“runs”, “fast”). In text generation, avoiding repetitions of word sequences provides a more diverse output. This LogitsProcessor enforces no repetition of n-grams by setting the scores of banned tokens to negative infinity which eliminates those tokens from consideration when further processing the scores. Fairseq.
Use n-gram penalties with care. For instance, penalizing 2-grams (bigrams) in an article about the city of New York might lead to undesirable outcomes where the city’s name appears only once in the entire text. Reference
Examples:
>>> from transformers import AutoTokenizer, AutoModelForCausalLM
>>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
>>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
>>> inputs = tokenizer(["Today I"], return_tensors="pt")
>>> output = model.generate(**inputs)
>>> print(tokenizer.decode(output[0], skip_special_tokens=True))
Today I’m not sure if I’m going to be able to do it.
>>> # Now let's add ngram size using `no_repeat_ngram_size`. This stops the repetitions ("I’m") in the output.
>>> output = model.generate(**inputs, no_repeat_ngram_size=2)
>>> print(tokenizer.decode(output[0], skip_special_tokens=True))
Today I’m not sure if I can get a better understanding of the nature of this issue
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.PrefixConstrainedLogitsProcessor
< source >( prefix_allowed_tokens_fn: typing.Callable[[int, torch.Tensor], typing.List[int]] num_beams: int )
Parameters
-
prefix_allowed_tokens_fn (
Callable[[int, torch.Tensor], List[int]]
) — This function constraints the beam search to allowed tokens only at each step. This function takes 2 argumentsinputs_ids
and the batch IDbatch_id
. It has to return a list with the allowed tokens for the next generation step conditioned on the previously generated tokensinputs_ids
and the batch IDbatch_id
.
LogitsProcessor that enforces constrained generation and is useful for prefix-conditioned constrained generation. See Autoregressive Entity Retrieval for more information.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.RepetitionPenaltyLogitsProcessor
< source >( penalty: float )
Parameters
-
repetition_penalty (
float
) — The parameter for repetition penalty. 1.0 means no penalty. See this paper for more details.
LogitsProcessor that prevents the repetition of previous tokens through an exponential penalty. This technique shares some similarities with coverage mechanisms and other aimed at reducing repetition. During the text generation process, the probability distribution for the next token is determined using a formula that incorporates token scores based on their occurrence in the generated sequence. Tokens with higher scores are less likely to be selected. The formula can be seen in the original paper. According to the paper a penalty of around 1.2 yields a good balance between truthful generation and lack of repetition.
Examples:
>>> from transformers import AutoTokenizer, AutoModelForCausalLM
>>> # Initializing the model and tokenizer for it
>>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
>>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
>>> inputs = tokenizer(["I'm not going to"], return_tensors="pt")
>>> # This shows a normal generate without any specific parameters
>>> summary_ids = model.generate(**inputs)
>>> print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True)[0])
I'm not going to be able to do that. I'm going to be able to do that
>>> # This generates a penalty for repeated tokens
>>> penalized_ids = model.generate(**inputs, repetition_penalty=1.1)
>>> print(tokenizer.batch_decode(penalized_ids, skip_special_tokens=True)[0])
I'm not going to be able to do that. I'll just have to go out and play
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.SequenceBiasLogitsProcessor
< source >( sequence_bias: typing.Dict[typing.Tuple[int], float] )
Parameters
-
sequence_bias (
Dict[Tuple[int], float]
) — Dictionary that maps a sequence of tokens to its bias term. Positive biases increase the odds of the sequence being selected, while negative biases do the opposite. If a sequence has a length of 1, its bias will always be applied. Otherwise, the bias will only be applied if the sequence in question is about to be completed (in the token selection step after this processor is applied).
LogitsProcessor that applies an additive bias on sequences. The bias is applied to the last token of a sequence when the next generated token can complete it. Consequently, to take the most of biasing sequences with more than one token, consider using beam methods (to gracefully work around partially completed sequences that have a negative bias) and applying the bias to their prefixes (to ensure the bias is applied earlier).
In order to get the token ids of the sequences that you want to bias, make sure to set add_prefix_space=True
when
initializing the tokenizer, and use tokenizer(bad_words, add_special_tokens=False).input_ids
. The
add_prefix_space
argument is only supported for some slow tokenizers, as fast tokenizers’ prefixing behaviours
come from pre tokenizers
. Read more here.
Examples:
>>> from transformers import AutoTokenizer, AutoModelForCausalLM
>>> model = AutoModelForCausalLM.from_pretrained("gpt2")
>>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
>>> inputs = tokenizer(["The full name of Donald is Donald"], return_tensors="pt")
>>> summary_ids = model.generate(inputs["input_ids"], max_new_tokens=4)
>>> print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True)[0])
The full name of Donald is Donald J. Trump Jr
>>> # Now let's control generation through a bias. Please note that the tokenizer is initialized differently!
>>> tokenizer_with_prefix_space = AutoTokenizer.from_pretrained("gpt2", add_prefix_space=True)
>>> def get_tokens_as_tuple(word):
... return tuple(tokenizer_with_prefix_space([word], add_special_tokens=False).input_ids[0])
>>> # If we add a negative bias without beam search, it may become "stuck" in a prefix without good continuations
>>> sequence_bias = {get_tokens_as_tuple("Trump"): -10.0}
>>> biased_ids = model.generate(inputs["input_ids"], max_new_tokens=4, sequence_bias=sequence_bias)
>>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
The full name of Donald is Donald J. Donald,
>>> biased_ids = model.generate(inputs["input_ids"], max_new_tokens=4, num_beams=4, sequence_bias=sequence_bias)
>>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
The full name of Donald is Donald Rumsfeld,
>>> # We can also add a positive bias to nudge the model towards specific tokens or continuations
>>> sequence_bias = {get_tokens_as_tuple("Donald Duck"): 10.0}
>>> biased_ids = model.generate(inputs["input_ids"], max_new_tokens=4, num_beams=4, sequence_bias=sequence_bias)
>>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
The full name of Donald is Donald Duck.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.SuppressTokensAtBeginLogitsProcessor
< source >( begin_suppress_tokens begin_index )
SuppressTokensAtBeginLogitsProcessor supresses a list of tokens as soon as the generate
function starts
generating using begin_index
tokens. This should ensure that the tokens defined by begin_suppress_tokens
at not
sampled at the begining of the generation.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
This processor can be used to suppress a list of tokens. The processor will set their log probs to -inf
so that they
are not sampled.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.TemperatureLogitsWarper
< source >( temperature: float )
LogitsWarper for temperature (exponential scaling output probability distribution), which effectively means that it can control the randomness of the predicted tokens.
Make sure that do_sample=True
is included in the generate
arguments otherwise the temperature value won’t have
any effect.
Examples:
>>> import torch
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
>>> set_seed(0) # for reproducibility
>>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
>>> model = AutoModelForCausalLM.from_pretrained("gpt2")
>>> model.config.pad_token_id = model.config.eos_token_id
>>> inputs = tokenizer(["Hugging Face Company is"], return_tensors="pt")
>>> # With temperature=1.0, the default, we consistently get random outputs due to random sampling.
>>> generate_kwargs = {"max_new_tokens": 10, "do_sample": True, "temperature": 1.0, "num_return_sequences": 2}
>>> outputs = model.generate(**inputs, **generate_kwargs)
>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
['Hugging Face Company is a joint venture between GEO Group, one of',
'Hugging Face Company is not an exact science – but what we believe does']
>>> # However, with temperature close to 0, it approximates greedy decoding strategies (invariant)
>>> generate_kwargs["temperature"] = 0.0001
>>> outputs = model.generate(**inputs, **generate_kwargs)
>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
['Hugging Face Company is a company that has been around for over 20 years',
'Hugging Face Company is a company that has been around for over 20 years']
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.TopKLogitsWarper
< source >( top_k: int filter_value: float = -inf min_tokens_to_keep: int = 1 )
Parameters
-
top_k (
int
) — The number of highest probability vocabulary tokens to keep for top-k-filtering. -
filter_value (
float
, optional, defaults to-float("Inf")
) — All filtered values will be set to this float value. -
min_tokens_to_keep (
int
, optional, defaults to 1) — Minimum number of tokens that cannot be filtered.
LogitsWarper that performs top-k, i.e. restricting to the k highest probability elements.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.TopPLogitsWarper
< source >( top_p: float filter_value: float = -inf min_tokens_to_keep: int = 1 )
Parameters
-
top_p (
float
) — If set to < 1, only the smallest set of most probable tokens with probabilities that add up totop_p
or higher are kept for generation. -
filter_value (
float
, optional, defaults to-float("Inf")
) — All filtered values will be set to this float value. -
min_tokens_to_keep (
int
, optional, defaults to 1) — Minimum number of tokens that cannot be filtered.
LogitsWarper that performs top-p, i.e. restricting to top tokens summing to prob_cut_off <= prob_cut_off.
Examples:
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
>>> set_seed(0)
>>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
>>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
>>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")
>>> # With sampling, the output is unexpected -- sometimes too unexpected.
>>> outputs = model.generate(**inputs, do_sample=True)
>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
A sequence: 1, 2, 0, 2, 2. 2, 2, 2, 2
>>> # With `top_p` sampling, the output gets restricted to high-probability tokens.
>>> # Pro tip: In practice, LLMs use `top_p` in the 0.9-0.95 range.
>>> outputs = model.generate(**inputs, do_sample=True, top_p=0.1)
>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.TypicalLogitsWarper
< source >( mass: float = 0.9 filter_value: float = -inf min_tokens_to_keep: int = 1 )
Parameters
LogitsWarper that performs typical decoding. See Typical Decoding for Natural Language Generation for more information.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
class transformers.UnbatchedClassifierFreeGuidanceLogitsProcessor
< source >( guidance_scale: float model unconditional_ids: typing.Optional[torch.LongTensor] = None unconditional_attention_mask: typing.Optional[torch.LongTensor] = None use_cache: typing.Optional[bool] = True )
Parameters
-
guidance_scale (
float
) — The guidance scale for classifier free guidance (CFG). CFG is enabled by settingguidance_scale != 1
. Higher guidance scale encourages the model to generate samples that are more closely linked to the input prompt, usually at the expense of poorer quality. A value smaller than 1 has the opposite effect, while making the negative prompt provided with negative_prompt_ids (if any) act as a positive prompt. -
unconditional_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
, optional) — Indices of input sequence tokens in the vocabulary for the unconditional branch. If unset, will default to the last token of the prompt. -
unconditional_attention_mask (
torch.LongTensor
of shape(batch_size, sequence_length)
, optional) — Attention mask for unconditional_ids. -
model (
PreTrainedModel
) — The model computing the unconditional scores. Supposedly the same as the one computing the conditional scores. Both models must use the same tokenizer. -
smooth_factor (
float
, optional) — The interpolation weight for CFG Rescale. 1 means no rescaling, 0 reduces to the conditional scores without CFG. Turn it lower if the output degenerates. -
use_cache (
bool
, optional) — Whether to cache key/values during the negative prompt forward pass.
Logits processor for Classifier-Free Guidance (CFG). The processors
computes a weighted average across scores from prompt conditional and prompt unconditional (or negative) logits,
parameterized by the guidance_scale
. The unconditional scores are computed internally by prompting model
with
the unconditional_ids
branch.
See the paper for more information.
Examples:
>>> from transformers import AutoTokenizer, AutoModelForCausalLM
>>> model = AutoModelForCausalLM.from_pretrained("gpt2")
>>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
>>> inputs = tokenizer(["Today, a dragon flew over Paris, France,"], return_tensors="pt")
>>> out = model.generate(inputs["input_ids"], guidance_scale=1.5)
>>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
'Today, a dragon flew over Paris, France, killing at least 50 people and injuring more than 100'
>>> # with a negative prompt
>>> neg_inputs = tokenizer(["A very happy event happened,"], return_tensors="pt")
>>> out = model.generate(inputs["input_ids"], guidance_scale=2, negative_prompt_ids=neg_inputs["input_ids"])
>>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
'Today, a dragon flew over Paris, France, killing at least 130 people. French media reported that'
>>> # with a positive prompt
>>> neg_inputs = tokenizer(["A very happy event happened,"], return_tensors="pt")
>>> out = model.generate(inputs["input_ids"], guidance_scale=0, negative_prompt_ids=neg_inputs["input_ids"])
>>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
"Today, a dragon flew over Paris, France, and I'm very happy to be here. I"
class transformers.WhisperTimeStampLogitsProcessor
< source >( generate_config )
Parameters
-
generate_config (
GenerateConfig
) — The generate config used to generate the output. The following parameters are required: eos_token_id (int
, optional, defaults to 50257): The id of the end-of-sequence token. no_timestamps_token_id (int
, optional, defaults to 50363): The id of the"<|notimestamps|>"
token. max_initial_timestamp_index (int
, optional, defaults to 1): Used to set the maximum value of the initial timestamp. This is used to prevent the model from predicting timestamps that are too far in the future.
Whisper specific Processor. This processor can be used to force a list of tokens. The processor will set their log
probs to inf
so that they are sampled at their corresponding index.
See the paper for more information.
__call__
< source >(
input_ids: LongTensor
scores: FloatTensor
)
→
torch.FloatTensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. What are input IDs? -
scores (
torch.FloatTensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search
Returns
torch.FloatTensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
TensorFlow
class transformers.TFForcedBOSTokenLogitsProcessor
< source >( bos_token_id: int )
TFLogitsProcessor that enforces the specified token as the first generated token.
class transformers.TFForcedEOSTokenLogitsProcessor
< source >( max_length: int eos_token_id: int )
TFLogitsProcessor that enforces the specified token as the last generated token when max_length
is reached.
class transformers.TFForceTokensLogitsProcessor
< source >( force_token_map: typing.List[typing.List[int]] )
This processor takes a list of pairs of integers which indicates a mapping from generation indices to token
indices that will be forced before sampling. The processor will set their log probs to 0
and all other tokens to
-inf
so that they are sampled at their corresponding index.
Abstract base class for all logit processors that can be applied during generation.
__call__
< source >(
input_ids: Tensor
scores: Tensor
cur_len: int
)
→
tf.Tensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
tf.Tensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary.Indices can be obtained using PreTrainedTokenizer. See PreTrainedTokenizer.encode() and PreTrainedTokenizer.call() for details.
-
scores (
tf.Tensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search. -
cur_len (
int
) — The current length of valid input sequence tokens. In the TF implementation, the input_ids’ sequence length is the maximum length generate can produce, and we need to know which of its tokens are valid. -
kwargs (
Dict[str, Any]
, optional) — Additional logits processor specific kwargs.
Returns
tf.Tensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
TF method for processing logits.
This class can be used to create a list of TFLogitsProcessor to subsequently process a scores
input tensor.
This class inherits from list and adds a specific call method to apply each TFLogitsProcessor to the
inputs.
__call__
< source >(
input_ids: Tensor
scores: Tensor
cur_len: int
**kwargs
)
→
tf.Tensor
of shape (batch_size, config.vocab_size)
Parameters
-
input_ids (
tf.Tensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary.Indices can be obtained using PreTrainedTokenizer. See PreTrainedTokenizer.encode() and PreTrainedTokenizer.call() for details.
-
scores (
tf.Tensor
of shape(batch_size, config.vocab_size)
) — Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search. -
cur_len (
int
) — The current length of valid input sequence tokens. In the TF implementation, the input_ids’ sequence length is the maximum length generate can produce, and we need to know which of its tokens are valid. -
kwargs (
Dict[str, Any]
, optional) — Additional logits processor specific kwargs.
Returns
tf.Tensor
of shape (batch_size, config.vocab_size)
The processed prediction scores.
Abstract base class for all logit warpers that can be applied during generation with multinomial sampling.
__call__
< source >( input_ids: Tensor