Buckets:
Generation
各フレームワークには、それぞれの GenerationMixin クラスに実装されたテキスト生成のための Generate メソッドがあります。
- PyTorch generate() は GenerationMixin に実装されています。
- TensorFlow
~generation.TFGenerationMixin.generateは~generation.TFGenerationMixinに実装されています。 - Flax/JAX
~generation.FlaxGenerationMixin.generateは~generation.FlaxGenerationMixinに実装されています。
選択したフレームワークに関係なく、GenerationConfig を使用して生成メソッドをパラメータ化できます。 クラスインスタンス。動作を制御する生成パラメータの完全なリストについては、このクラスを参照してください。 生成方法のこと。
モデルの生成構成を検査する方法、デフォルトとは何か、パラメーターをアドホックに変更する方法を学習するには、 カスタマイズされた生成構成を作成して保存する方法については、「 テキスト生成戦略ガイド。このガイドでは、関連機能の使用方法についても説明しています。 トークンストリーミングのような。
GenerationConfig[[transformers.GenerationConfig]]
transformers.GenerationConfig[[transformers.GenerationConfig]]
Class that holds a configuration for a generation task. A generate call supports the following generation methods
for text-decoder, text-to-text, speech-to-text, and vision-to-text models:
- greedy decoding if
num_beams=1anddo_sample=False - multinomial sampling if
num_beams=1anddo_sample=True - beam-search decoding if
num_beams>1anddo_sample=False - beam-search multinomial sampling if
num_beams>1anddo_sample=True - assisted decoding if
assistant_modelorprompt_lookup_num_tokensis passed to.generate()
To learn more about decoding strategies refer to the text generation strategies guide.
A large number of these flags control the logits or the stopping criteria of the generation. Make sure you check the generate-related classes for a full description of the possible manipulations, as well as examples of their usage.
Note: the configuration fields that are still None will be overridden by GenerationConfig._get_default_generation_params()
during the generation loop. If you want to use different values for these fields, make sure to explicitly set them in the
generation config.
from_pretrainedtransformers.GenerationConfig.from_pretrainedhttps://github.com/huggingface/transformers/blob/main/src/transformers/generation/configuration_utils.py#L886[{"name": "pretrained_model_name", "val": ": str | os.PathLike"}, {"name": "config_file_name", "val": ": str | os.PathLike | None = None"}, {"name": "cache_dir", "val": ": str | os.PathLike | None = None"}, {"name": "force_download", "val": ": bool = False"}, {"name": "local_files_only", "val": ": bool = False"}, {"name": "token", "val": ": str | bool | None = None"}, {"name": "revision", "val": ": str = 'main'"}, {"name": "**kwargs", "val": ""}]- pretrained_model_name (str or os.PathLike) --
This can be either:
a string, the model id of a pretrained model configuration hosted inside a model repo on huggingface.co.
a path to a directory containing a configuration file saved using the save_pretrained() method, e.g.,
./my_model_directory/.config_file_name (
stroros.PathLike, optional, defaults to"generation_config.json") -- Name of the generation configuration JSON file to be loaded frompretrained_model_name.cache_dir (
stroros.PathLike, optional) -- Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used.force_download (
bool, optional, defaults toFalse) -- Whether or not to force to (re-)download the configuration files and override the cached versions if they exist.proxies (
dict[str, str], optional) -- A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.The proxies are used on each request.token (
strorbool, optional) -- The token to use as HTTP bearer authorization for remote files. IfTrue, or not specified, will use the token generated when runninghf auth login(stored in~/.huggingface).revision (
str, optional, defaults to"main") -- The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a git-based system for storing models and other artifacts on huggingface.co, sorevisioncan be any identifier allowed by git.To test a pull request you made on the Hub, you can pass
revision="refs/pr/".return_unused_kwargs (
bool, optional, defaults toFalse) -- IfFalse, then this function returns just the final configuration object.If
True, then this functions returns aTuple(config, unused_kwargs)where unused_kwargs is a dictionary consisting of the key/value pairs whose keys are not configuration attributes: i.e., the part ofkwargswhich has not been used to updateconfigand is otherwise ignored.subfolder (
str, optional, defaults to"") -- In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can specify the folder name here.kwargs (
dict[str, Any], optional) -- The values in kwargs of any keys which are configuration attributes will be used to override the loaded values. Behavior concerning key/value pairs whose keys are not configuration attributes is controlled by thereturn_unused_kwargskeyword parameter.0GenerationConfigThe configuration object instantiated from this pretrained model.
Instantiate a GenerationConfig from a generation configuration file.
Examples:
>>> from transformers import GenerationConfig
>>> # Download configuration from huggingface.co and cache.
>>> generation_config = GenerationConfig.from_pretrained("openai-community/gpt2")
>>> # E.g. config was saved using *save_pretrained('./test/saved_model/')*
>>> generation_config.save_pretrained("./test/saved_model/")
>>> generation_config = GenerationConfig.from_pretrained("./test/saved_model/")
>>> # You can also specify configuration names to your generation configuration file
>>> generation_config.save_pretrained("./test/saved_model/", config_file_name="my_configuration.json")
>>> generation_config = GenerationConfig.from_pretrained("./test/saved_model/", "my_configuration.json")
>>> # If you'd like to try a minor variation to an existing configuration, you can also pass generation
>>> # arguments to `.from_pretrained()`. Be mindful that typos and unused arguments will be ignored
>>> generation_config, unused_kwargs = GenerationConfig.from_pretrained(
... "openai-community/gpt2", top_k=1, foo=False, do_sample=True, return_unused_kwargs=True
... )
>>> generation_config.top_k
1
>>> unused_kwargs
{'foo': False}
Returns:
[GenerationConfig](/docs/transformers/main/ja/main_classes/text_generation#transformers.GenerationConfig)
The configuration object instantiated from this pretrained model.
from_model_config[[transformers.GenerationConfig.from_model_config]]
Instantiates a GenerationConfig from a PreTrainedConfig. This function is useful to convert legacy PreTrainedConfig objects, which may contain generation parameters, into a stand-alone GenerationConfig.
Parameters:
model_config (PreTrainedConfig | dict) : The model config that will be used to instantiate the generation config.
Returns:
[GenerationConfig](/docs/transformers/main/ja/main_classes/text_generation#transformers.GenerationConfig)
The configuration object instantiated from those parameters.
save_pretrained[[transformers.GenerationConfig.save_pretrained]]
Save a generation configuration object to the directory save_directory, so that it can be re-loaded using the
from_pretrained() class method.
Parameters:
save_directory (str or os.PathLike) : Directory where the configuration JSON file will be saved (will be created if it does not exist).
config_file_name (str or os.PathLike, optional, defaults to "generation_config.json") : Name of the generation configuration JSON file to be saved in save_directory.
push_to_hub (bool, optional, defaults to False) : Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to with repo_id (will default to the name of save_directory in your namespace).
kwargs (dict[str, Any], optional) : Additional key word arguments passed along to the push_to_hub() method.
GenerationMixin[[transformers.GenerationMixin]]
transformers.GenerationMixin[[transformers.GenerationMixin]]
A class containing all functions for auto-regressive text generation, to be used as a mixin in model classes.
Inheriting from this class causes the model to have special generation-related behavior, such as loading a
GenerationConfig at initialization time or ensuring generate-related tests are run in transformers CI.
A model class should inherit from GenerationMixin to enable calling methods like generate, or when it
has defined a custom generate method that relies on GenerationMixin, directly or indirectly, which
approximately shares the same interface to public methods like generate. Three examples:
LlamaForCausalLMshould inherit fromGenerationMixinto enable callinggenerateand other public methods in the mixin;BlipForQuestionAnsweringhas a customgeneratemethod that approximately shares the same interface asGenerationMixin.generate(it has a few extra arguments, and the same output). That function also callsGenerationMixin.generateindirectly, through an inner model. As such,BlipForQuestionAnsweringshould inherit fromGenerationMixinto benefit from all generation-related automation in our codebase;BarkModelhas a customgeneratemethod and one of its inner models callsGenerationMixin.generate. However, itsgeneratedoes not share the same interface asGenerationMixin.generate. In this case,BarkModelshould NOT inherit fromGenerationMixin, as it breaks thegenerateinterface.
The class exposes generate(), which can be used for:
- greedy decoding if
num_beams=1anddo_sample=False - multinomial sampling if
num_beams=1anddo_sample=True - beam-search decoding if
num_beams>1anddo_sample=False - beam-search multinomial sampling if
num_beams>1anddo_sample=True - assisted decoding if
assistant_modelorprompt_lookup_num_tokensis passed to.generate()
To learn more about decoding strategies refer to the text generation strategies guide.
generatetransformers.GenerationMixin.generatehttps://github.com/huggingface/transformers/blob/main/src/transformers/generation/utils.py#L2171[{"name": "inputs", "val": ": torch.Tensor | None = None"}, {"name": "generation_config", "val": ": transformers.generation.configuration_utils.GenerationConfig | None = None"}, {"name": "logits_processor", "val": ": transformers.generation.logits_process.LogitsProcessorList | None = None"}, {"name": "stopping_criteria", "val": ": transformers.generation.stopping_criteria.StoppingCriteriaList | None = None"}, {"name": "prefix_allowed_tokens_fn", "val": ": collections.abc.Callable[[int, torch.Tensor], list[int]] | None = None"}, {"name": "synced_gpus", "val": ": bool | None = None"}, {"name": "assistant_model", "val": ": typing.Optional[ForwardRef('PreTrainedModel')] = None"}, {"name": "streamer", "val": ": typing.Optional[ForwardRef('BaseStreamer')] = None"}, {"name": "negative_prompt_ids", "val": ": torch.Tensor | None = None"}, {"name": "negative_prompt_attention_mask", "val": ": torch.Tensor | None = None"}, {"name": "custom_generate", "val": ": str | collections.abc.Callable | None = None"}, {"name": "**kwargs", "val": ""}]- inputs (torch.Tensor of varying shape depending on the modality, optional) --
The sequence used as a prompt for the generation or as model inputs to the encoder. If None the
method initializes it with bos_token_id and a batch size of 1. For decoder-only models inputs
should be in the format of input_ids. For encoder-decoder models inputs can represent any of
input_ids, input_values, input_features, or pixel_values.
- generation_config (GenerationConfig, optional) --
The generation configuration to be used as base parametrization for the generation call.
**kwargspassed to generate matching the attributes ofgeneration_configwill override them. Ifgeneration_configis not provided, the default will be used, which has the following loading priority: 1) from thegeneration_config.jsonmodel file, if it exists; 2) from the model configuration. Please note that unspecified parameters will inherit GenerationConfig's default values, whose documentation should be checked to parameterize generation. - logits_processor (
LogitsProcessorList, optional) -- Custom logits processors that complement the default logits processors built from arguments and generation config. If a logit processor is passed that is already created with the arguments or a generation config an error is thrown. This feature is intended for advanced users. - stopping_criteria (
StoppingCriteriaList, optional) -- Custom stopping criteria that complements the default stopping criteria built from arguments and a generation config. If a stopping criteria is passed that is already created with the arguments or a generation config an error is thrown. If your stopping criteria depends on thescoresinput, make sure you passreturn_dict_in_generate=True, output_scores=Truetogenerate. This feature is intended for advanced users. - prefix_allowed_tokens_fn (
Callable[[int, torch.Tensor], list[int]], optional) -- If provided, this function constraints the beam search to allowed tokens only at each step. If not provided no constraint is applied. This function takes 2 arguments: the batch IDbatch_idandinput_ids. It has to return a list with the allowed tokens for the next generation step conditioned on the batch IDbatch_idand the previously generated tokensinputs_ids. This argument is useful for constrained generation conditioned on the prefix, as described in Autoregressive Entity Retrieval. - synced_gpus (
bool, optional) -- Whether to continue running the while loop until max_length. Unless overridden, this flag will be set toTrueif usingFullyShardedDataParallelor DeepSpeed ZeRO Stage 3 with multiple GPUs to avoid deadlocking if one GPU finishes generating before other GPUs. Otherwise, defaults toFalse. - assistant_model (
PreTrainedModel, optional) -- An assistant model that can be used to accelerate generation. The assistant model must have the exact same tokenizer. The acceleration is achieved when forecasting candidate tokens with the assistant model is much faster than running generation with the model you're calling generate from. As such, the assistant model should be much smaller. - streamer (
BaseStreamer, optional) -- Streamer object that will be used to stream the generated sequences. Generated tokens are passed throughstreamer.put(token_ids)and the streamer is responsible for any further processing. - negative_prompt_ids (
torch.LongTensorof shape(batch_size, sequence_length), optional) -- The negative prompt needed for some processors such as CFG. The batch size must match the input batch size. This is an experimental feature, subject to breaking API changes in future versions. - negative_prompt_attention_mask (
torch.LongTensorof shape(batch_size, sequence_length), optional) -- Attention_mask fornegative_prompt_ids. - custom_generate (
strorCallable, optional) -- One of the following:str(Hugging Face Hub repository name): runs the customgeneratefunction defined atcustom_generate/generate.pyin that repository instead of the standardgeneratemethod. The repository fully replaces the generation logic, and the return type may differ.str(local repository path): same as above but from a local path,trust_remote_codenot required.Callable:generatewill perform the usual input preparation steps, then call the provided callable to run the decoding loop. For more information, see the docs.
- kwargs (
dict[str, Any], optional) -- Ad hoc parametrization ofgeneration_configand/or additional model-specific kwargs that will be forwarded to theforwardfunction of the model. If the model is an encoder-decoder model, encoder specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with decoder_.0ModelOutput ortorch.LongTensorA ModelOutput (ifreturn_dict_in_generate=Trueor whenconfig.return_dict_in_generate=True) or atorch.LongTensor.
If the model is not an encoder-decoder model (model.config.is_encoder_decoder=False), the possible
ModelOutput types are:
If the model is an encoder-decoder model (model.config.is_encoder_decoder=True), the possible
ModelOutput types are:
Generates sequences of token ids for models with a language modeling head.
Most generation-controlling parameters are set in generation_config which, if not passed, will be set to the
model's default generation configuration. You can override any generation_config by passing the corresponding
parameters to generate(), e.g. .generate(inputs, num_beams=4, do_sample=True).
For an overview of generation strategies and code examples, check out the following guide.
Parameters:
inputs (torch.Tensor of varying shape depending on the modality, optional) : The sequence used as a prompt for the generation or as model inputs to the encoder. If None the method initializes it with bos_token_id and a batch size of 1. For decoder-only models inputs should be in the format of input_ids. For encoder-decoder models inputs can represent any of input_ids, input_values, input_features, or pixel_values.
generation_config (GenerationConfig, optional) : The generation configuration to be used as base parametrization for the generation call. **kwargs passed to generate matching the attributes of generation_config will override them. If generation_config is not provided, the default will be used, which has the following loading priority: 1) from the generation_config.json model file, if it exists; 2) from the model configuration. Please note that unspecified parameters will inherit GenerationConfig's default values, whose documentation should be checked to parameterize generation.
logits_processor (LogitsProcessorList, optional) : Custom logits processors that complement the default logits processors built from arguments and generation config. If a logit processor is passed that is already created with the arguments or a generation config an error is thrown. This feature is intended for advanced users.
stopping_criteria (StoppingCriteriaList, optional) : Custom stopping criteria that complements the default stopping criteria built from arguments and a generation config. If a stopping criteria is passed that is already created with the arguments or a generation config an error is thrown. If your stopping criteria depends on the scores input, make sure you pass return_dict_in_generate=True, output_scores=True to generate. This feature is intended for advanced users.
prefix_allowed_tokens_fn (Callable[[int, torch.Tensor], list[int]], optional) : If provided, this function constraints the beam search to allowed tokens only at each step. If not provided no constraint is applied. This function takes 2 arguments: the batch ID batch_id and input_ids. It has to return a list with the allowed tokens for the next generation step conditioned on the batch ID batch_id and the previously generated tokens inputs_ids. This argument is useful for constrained generation conditioned on the prefix, as described in Autoregressive Entity Retrieval.
synced_gpus (bool, optional) : Whether to continue running the while loop until max_length. Unless overridden, this flag will be set to True if using FullyShardedDataParallel or DeepSpeed ZeRO Stage 3 with multiple GPUs to avoid deadlocking if one GPU finishes generating before other GPUs. Otherwise, defaults to False.
assistant_model (PreTrainedModel, optional) : An assistant model that can be used to accelerate generation. The assistant model must have the exact same tokenizer. The acceleration is achieved when forecasting candidate tokens with the assistant model is much faster than running generation with the model you're calling generate from. As such, the assistant model should be much smaller.
streamer (BaseStreamer, optional) : Streamer object that will be used to stream the generated sequences. Generated tokens are passed through streamer.put(token_ids) and the streamer is responsible for any further processing.
negative_prompt_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) : The negative prompt needed for some processors such as CFG. The batch size must match the input batch size. This is an experimental feature, subject to breaking API changes in future versions.
negative_prompt_attention_mask (torch.LongTensor of shape (batch_size, sequence_length), optional) : Attention_mask for negative_prompt_ids.
custom_generate (str or Callable, optional) : One of the following: - str (Hugging Face Hub repository name): runs the custom generate function defined at custom_generate/generate.py in that repository instead of the standard generate method. The repository fully replaces the generation logic, and the return type may differ. - str (local repository path): same as above but from a local path, trust_remote_code not required. - Callable: generate will perform the usual input preparation steps, then call the provided callable to run the decoding loop. For more information, see the docs.
kwargs (dict[str, Any], optional) : Ad hoc parametrization of generation_config and/or additional model-specific kwargs that will be forwarded to the forward function of the model. If the model is an encoder-decoder model, encoder specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with decoder_.
Returns:
[ModelOutput](/docs/transformers/main/ja/main_classes/output#transformers.utils.ModelOutput) or torch.LongTensor``
A ModelOutput (if return_dict_in_generate=True
or when config.return_dict_in_generate=True) or a torch.LongTensor.
If the model is not an encoder-decoder model (model.config.is_encoder_decoder=False), the possible
ModelOutput types are:
If the model is an encoder-decoder model (model.config.is_encoder_decoder=True), the possible
ModelOutput types are:
compute_transition_scores[[transformers.GenerationMixin.compute_transition_scores]]
Computes the transition scores of sequences given the generation scores (and beam indices, if beam search was used). This is a convenient method to quickly obtain the scores of the selected tokens at generation time.
Examples:
>>> from transformers import GPT2Tokenizer, AutoModelForCausalLM
>>> import numpy as np
>>> tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
>>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
>>> tokenizer.pad_token_id = tokenizer.eos_token_id
>>> inputs = tokenizer(["Today is"], return_tensors="pt")
>>> # Example 1: Print the scores for each token generated with Greedy Search
>>> outputs = model.generate(**inputs, max_new_tokens=5, return_dict_in_generate=True, output_scores=True)
>>> transition_scores = model.compute_transition_scores(
... outputs.sequences, outputs.scores, normalize_logits=True
... )
>>> # input_length is the length of the input prompt for decoder-only models, like the GPT family, and 1 for
>>> # encoder-decoder models, like BART or T5.
>>> input_length = 1 if model.config.is_encoder_decoder else inputs.input_ids.shape[1]
>>> generated_tokens = outputs.sequences[:, input_length:]
>>> for tok, score in zip(generated_tokens[0], transition_scores[0]):
... # | token | token string | log probability | probability
... print(f"| {tok:5d} | {tokenizer.decode(tok):8s} | {score.numpy():.3f} | {np.exp(score.numpy()):.2%}")
| 262 | the | -1.414 | 24.33%
| 1110 | day | -2.609 | 7.36%
| 618 | when | -2.010 | 13.40%
| 356 | we | -1.859 | 15.58%
| 460 | can | -2.508 | 8.14%
>>> # Example 2: Reconstruct the sequence scores from Beam Search
>>> outputs = model.generate(
... **inputs,
... max_new_tokens=5,
... num_beams=4,
... num_return_sequences=4,
... return_dict_in_generate=True,
... output_scores=True,
... )
>>> transition_scores = model.compute_transition_scores(
... outputs.sequences, outputs.scores, outputs.beam_indices, normalize_logits=False
... )
>>> # If you sum the generated tokens' scores and apply the length penalty, you'll get the sequence scores.
>>> # Tip 1: recomputing the scores is only guaranteed to match with `normalize_logits=False`. Depending on the
>>> # use case, you might want to recompute it with `normalize_logits=True`.
>>> # Tip 2: the output length does NOT include the input length
>>> output_length = np.sum(transition_scores.numpy() >> length_penalty = model.generation_config.length_penalty
>>> reconstructed_scores = transition_scores.sum(axis=1) / (output_length**length_penalty)
>>> print(np.allclose(outputs.sequences_scores, reconstructed_scores))
True
Parameters:
sequences (torch.LongTensor) : The generated sequences. The second dimension (sequence_length) is either equal to max_length or shorter if all batches finished early due to the eos_token_id.
scores (tuple(torch.FloatTensor)) : 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 of torch.FloatTensor with up to max_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) : Beam indices of generated token id at each generation step. torch.LongTensor of shape (batch_size*num_return_sequences, sequence_length). Only required if a num_beams>1 at generate-time.
normalize_logits (bool, optional, defaults to False) : Whether to normalize the logits (which, for legacy reasons, may be unnormalized).
Returns:
torch.Tensor
A torch.Tensor of shape (batch_size*num_return_sequences, sequence_length) containing
the transition scores (logits)
Xet Storage Details
- Size:
- 31.5 kB
- Xet hash:
- 7e1dd2ecf8a54e821a3cb2873fd4aff9a03c23931c89d2e49a62741bc4a6032d
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.