TRL documentation
Models
Models
With the AutoModelForCausalLMWithValueHead
class TRL supports all decoder model architectures in transformers such as GPT-2, OPT, and GPT-Neo.
PreTrainedModelWrapper
A wrapper class around a (transformers.PreTrainedModel
) to be compatible with the
(~transformers.PreTrained
) class in order to keep some attributes and methods of the
(~transformers.PreTrainedModel
) class.
from_pretrained
< source >( pretrained_model_name_or_path *model_args **kwargs )
Parameters
-
pretrained_model_name_or_path (
str
ortransformers.PreTrainedModel
) — The path to the pretrained model or its name. -
*model_args (
list
, optional)) — Additional positional arguments passed along to the underlying model’sfrom_pretrained
method. -
**kwargs (
dict
, optional) — Additional keyword arguments passed along to the underlying model’sfrom_pretrained
method. We also pre-process the kwargs to extract the arguments that are specific to thetransformers.PreTrainedModel
class and the arguments that are specific to trl models.
Instantiates a new model from a pretrained model from transformers
. The
pretrained model is loaded using the from_pretrained
method of the
transformers.PreTrainedModel
class. The arguments that are specific to the
transformers.PreTrainedModel
class are passed along this method and filtered
out from the kwargs
argument.
Post initialization method. This method is called after the model is instantiated and loaded from a checkpoint. It can be used to perform additional operations such as loading the state_dict.
push_to_hub
< source >( *args **kwargs )
Push the pretrained model to the hub. This method is a wrapper around
transformers.PreTrainedModel.push_to_hub
. Please refer to the documentation
of transformers.PreTrainedModel.push_to_hub
for more information.
save_pretrained
< source >( *args **kwargs )
Save the pretrained model to a directory. This method is a wrapper around
transformers.PreTrainedModel.save_pretrained
. Please refer to the documentation
of transformers.PreTrainedModel.save_pretrained
for more information.
Return the state_dict of the pretrained model.
AutoModelForCausalLMWithValueHead
An autoregressive model with a value head in addition to the language model head.
This class inherits from ~trl.PreTrainedModelWrapper
and wraps a
transformers.PreTrainedModel
class. The wrapper class supports classic functions
such as from_pretrained
, push_to_hub
and generate
. To call a method of the wrapped
model, simply manipulate the pretrained_model
attribute of this class.
Class attributes:
- transformers_parent_class (
transformers.PreTrainedModel
) β The parent class of the wrapped model. This should be set totransformers.AutoModelForCausalLM
for this class. - lm_head_namings (
tuple
) β A tuple of strings that are used to identify the language model head of the wrapped model. This is set to("lm_head", "embed_out")
for this class but can be changed for other models in the future - supported_args (
tuple
) β A tuple of strings that are used to identify the arguments that are supported by theValueHead
class. Currently the supported args are:- summary_dropout_prob (
float
,optional
, defaults toNone
) β The dropout probability for theValueHead
class. - v_head_initializer_range (
float
,optional
, defaults to0.2
) β The initializer range for theValueHead
if a specific initialization strategy is selected. - v_head_init_strategy (
str
,optional
, defaults toNone
) β The initialization strategy for theValueHead
. Currently supported strategies are:None
β Initializes the weights of theValueHead
with a random distribution. This is the default strategy.- βnormalβ β Initializes the weights of the
ValueHead
with a normal distribution.
- summary_dropout_prob (
__init__
< source >( pretrained_model **kwargs )
Initializes the model.
forward
< source >( input_ids = None past_key_values = None attention_mask = None **kwargs )
Parameters
- input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary.
- past_key_values (tuple(tuple(torch.FloatTensor)), optional) — Contains pre-computed hidden-states (key and values in the attention blocks) as computed by the model (see past_key_values input) to speed up sequential decoding.
-
attention_mask (torch.FloatTensor of shape (batch_size, sequence_length), optional) —
Mask to avoid performing attention on padding token indices. Mask values selected in
[0, 1]
:- 1 for tokens that are not masked,
- 0 for tokens that are masked.
- kwargs (dict, optional) — Additional keyword arguments, that are passed to the wrapped model.
Applies a forward pass to the wrapped model and returns the logits of the value head.
generate
< source >( *args **kwargs )
A simple wrapper around the generate
method of the wrapped model.
Please refer to the generate
method of the wrapped model for more information about the supported arguments.
_init_weights
< source >( **kwargs )
Initializes the weights of the value head. The default initialization strategy is random.
Users can pass a different initialization strategy by passing the v_head_init_strategy
argument
when calling .from_pretrained
. Supported strategies are:
normal
: initializes the weights with a normal distribution.