> For the complete documentation index, see [llms.txt](https://boinc-ai.gitbook.io/trl/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boinc-ai.gitbook.io/trl/api/model-classes.md).

# Model Classes

## Models

With the `AutoModelForCausalLMWithValueHead` class TRL supports all decoder model architectures in transformers such as GPT-2, OPT, and GPT-Neo. In addition, with `AutoModelForSeq2SeqLMWithValueHead` you can use encoder-decoder architectures such as T5. TRL also requires reference models which are frozen copies of the model that is trained. With `create_reference_model` you can easily create a frozen copy and also share layers between the two models to save memory.

### PreTrainedModelWrapper

#### class trl.PreTrainedModelWrapper

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_base.py#L55)

( pretrained\_model = None\*\*kwargs )

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.

**add\_and\_load\_reward\_modeling\_adapter**

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_base.py#L428)

( adapter\_model\_idadapter\_name = 'reward\_model\_adapter'token = None )

Add and load a reward modeling adapter. This method can only be used if the model is a `PeftModel` and if you have initialized the model with the `reward_modeling_adapter_id` argument, pointing to the id of the reward modeling adapter. The latest needs also to contain the score head in order to produce the reward.

**compute\_reward\_score**

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_base.py#L482)

( input\_idsattention\_mask = Noneppo\_adapter\_name = 'default'\*\*kwargs )

Computes the reward score for a given input. The method has first to enable the adapter and then compute the reward score. After that the model disables the reward modeling adapter and enables the default ppo adapter again.

**from\_pretrained**

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_base.py#L95)

( pretrained\_model\_name\_or\_path\*model\_args\*\*kwargs )

Parameters

* **pretrained\_model\_name\_or\_path** (`str` or `transformers.PreTrainedModel`) — The path to the pretrained model or its name.
* **\*model\_args** (`list`, *optional*)) — Additional positional arguments passed along to the underlying model’s `from_pretrained` method.
* \***\*kwargs** (`dict`, *optional*) — Additional keyword arguments passed along to the underlying model’s `from_pretrained` method. We also pre-process the kwargs to extract the arguments that are specific to the `transformers.PreTrainedModel` class and the arguments that are specific to trl models. The kwargs also support `prepare_model_for_kbit_training` arguments from `peft` library.

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\_init**

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_base.py#L420)

( \*args\*\*kwargs )

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>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_base.py#L369)

( \*args\*\*kwargs )

Parameters

* **\*args** (`list`, *optional*) — Positional arguments passed along to the underlying model’s `push_to_hub` method.
* \***\*kwargs** (`dict`, *optional*) — Keyword arguments passed along to the underlying model’s `push_to_hub` method.

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>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_base.py#L385)

( \*args\*\*kwargs )

Parameters

* **\*args** (`list`, *optional*) — Positional arguments passed along to the underlying model’s `save_pretrained` method.
* \***\*kwargs** (`dict`, *optional*) — Keyword arguments passed along to the underlying model’s `save_pretrained` method.

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.

**state\_dict**

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_base.py#L414)

( \*args\*\*kwargs )

Return the state\_dict of the pretrained model.

### AutoModelForCausalLMWithValueHead

#### class trl.AutoModelForCausalLMWithValueHead

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_value_head.py#L57)

( pretrained\_model\*\*kwargs )

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 to `transformers.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 the `ValueHead` class. Currently, the supported args are:
  * **summary\_dropout\_prob** (`float`, `optional`, defaults to `None`) — The dropout probability for the `ValueHead` class.
  * **v\_head\_initializer\_range** (`float`, `optional`, defaults to `0.2`) — The initializer range for the `ValueHead` if a specific initialization strategy is selected.
  * **v\_head\_init\_strategy** (`str`, `optional`, defaults to `None`) — The initialization strategy for the `ValueHead`. Currently, the supported strategies are:
    * **`None`** — Initializes the weights of the `ValueHead` with a random distribution. This is the default strategy.
    * **“normal”** — Initializes the weights of the `ValueHead` with a normal distribution.

**\_\_init\_\_**

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_value_head.py#L92)

( pretrained\_model\*\*kwargs )

Parameters

* **pretrained\_model** (`transformers.PreTrainedModel`) — The model to wrap. It should be a causal language model such as GPT2. or any model mapped inside the `AutoModelForCausalLM` class.
* **kwargs** (`dict`, `optional`) — Additional keyword arguments, that are passed to the `ValueHead` class.

Initializes the model.

**forward**

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_value_head.py#L136)

( input\_ids = Nonepast\_key\_values = Noneattention\_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>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_value_head.py#L186)

( \*args\*\*kwargs )

Parameters

* **\*args** (`list`, *optional*) — Positional arguments passed to the `generate` method of the wrapped model.
* \***\*kwargs** (`dict`, *optional*) — Keyword arguments passed to the `generate` method of the wrapped model.

A simple wrapper around the `generate` method of the wrapped model. Please refer to the [`generate`](https://huggingface.co/docs/transformers/internal/generation_utils) method of the wrapped model for more information about the supported arguments.

**\_init\_weights**

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_value_head.py#L113)

( \*\*kwargs )

Parameters

* \***\*kwargs** (`dict`, `optional`) — Additional keyword arguments, that are passed to the `ValueHead` class. These arguments can contain the `v_head_init_strategy` argument as well as the `v_head_initializer_range` argument.

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.

### AutoModelForSeq2SeqLMWithValueHead

#### class trl.AutoModelForSeq2SeqLMWithValueHead

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_value_head.py#L260)

( pretrained\_model\*\*kwargs )

Parameters

* **pretrained\_model** (`transformers.PreTrainedModel`) — The model to wrap. It should be a causal language model such as GPT2. or any model mapped inside the `AutoModelForSeq2SeqLM` class. kwargs — Additional keyword arguments passed along to the `ValueHead` class.

A seq2seq 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` and `push_to_hub` and also provides some additional functionalities such as `generate`.

**\_\_init\_\_**

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_value_head.py#L283)

( pretrained\_model\*\*kwargs )

**forward**

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_value_head.py#L391)

( input\_ids = Nonepast\_key\_values = Noneattention\_mask = None\*\*kwargs )

**generate**

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_value_head.py#L421)

( \*args\*\*kwargs )

We call `generate` on the wrapped model.

**\_init\_weights**

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_value_head.py#L377)

( \*\*kwargs )

We initialize the weights of the value head.

### create\_reference\_model

**trl.create\_reference\_model**

[\<source>](https://github.com/huggingface/trl/blob/v0.7.2/trl/models/modeling_base.py#L512)

( model: PreTrainedModelWrappernum\_shared\_layers: int = Nonepattern: str = None )

Parameters

* **model** (`PreTrainedModelWrapper`) — The model to be copied.
* **num\_shared\_layers** (`int`, *optional*) — The number of initial layers that are shared between both models and kept frozen.
* **pattern** (`str`, *optional*) — The shared layers are selected with a string pattern (e.g. “transformer.h.{layer}” for GPT2) and if a custom pattern is necessary it can be passed here.

Creates a static reference copy of a model. Note that model will be in `.eval()` mode.

Returns `PreTrainedModelWrapper`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://boinc-ai.gitbook.io/trl/api/model-classes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
