Views
No views yet

Full stack transformer language models with reinforcement learning.

trl is a full stack library where we provide a set of tools to train transformer language models and stable diffusion models with Reinforcement Learning, from the Supervised Fine-tuning step (SFT), Reward Modeling step (RM) to the Proximal Policy Optimization (PPO) step. The library is built on top of the transformers library by 🤗 Hugging Face. Therefore, pre-trained language models can be directly loaded via transformers. At this point, most of decoder architectures and encoder-decoder architectures are supported. Refer to the documentation or the examples/ folder for example code snippets and how to run these tools.SFTTrainer: A light and friendly wrapper around transformers Trainer to easily fine-tune language models or adapters on a custom dataset.RewardTrainer: A light wrapper around transformers Trainer to easily fine-tune language models for human preferences (Reward Modeling).PPOTrainer: A PPO trainer for language models that just needs (query, response, reward) triplets to optimise the language model.AutoModelForCausalLMWithValueHead & AutoModelForSeq2SeqLMWithValueHead: A transformer model with an additional scalar output for each token which can be used as a value function in reinforcement learning.
pip install trl1git clone https://github.com/huggingface/trl.git
2cd trl/
3pip install .pip install -e .SFTTrainerSFTTrainer from the library. The SFTTrainer is a light wrapper around the transformers Trainer to easily fine-tune language models or adapters on a custom dataset.1# imports
2from datasets import load_dataset
3from trl import SFTTrainer
4
5# get dataset
6dataset = load_dataset("imdb", split="train")
7
8# get trainer
9trainer = SFTTrainer(
10 "facebook/opt-350m",
11 train_dataset=dataset,
12 dataset_text_field="text",
13 max_seq_length=512,
14)
15
16# train
17trainer.train()RewardTrainerRewardTrainer from the library. The RewardTrainer is a wrapper around the transformers Trainer to easily fine-tune reward models or adapters on a custom preference dataset.1# imports
2from transformers import AutoModelForSequenceClassification, AutoTokenizer
3from trl import RewardTrainer
4
5# load model and dataset - dataset needs to be in a specific format
6model = AutoModelForSequenceClassification.from_pretrained("gpt2", num_labels=1)
7tokenizer = AutoTokenizer.from_pretrained("gpt2")
8
9...
10
11# load trainer
12trainer = RewardTrainer(
13 model=model,
14 tokenizer=tokenizer,
15 train_dataset=dataset,
16)
17
18# train
19trainer.train()PPOTrainerPPOTrainer from the library. Based on a query the language model creates a response which is then evaluated. The evaluation could be a human in the loop or another model's output.1# imports
2import torch
3from transformers import AutoTokenizer
4from trl import PPOTrainer, PPOConfig, AutoModelForCausalLMWithValueHead, create_reference_model
5from trl.core import respond_to_batch
6
7# get models
8model = AutoModelForCausalLMWithValueHead.from_pretrained('gpt2')
9model_ref = create_reference_model(model)
10
11tokenizer = AutoTokenizer.from_pretrained('gpt2')
12
13# initialize trainer
14ppo_config = PPOConfig(
15 batch_size=1,
16)
17
18# encode a query
19query_txt = "This morning I went to the "
20query_tensor = tokenizer.encode(query_txt, return_tensors="pt")
21
22# get model response
23response_tensor = respond_to_batch(model, query_tensor)
24
25# create a ppo trainer
26ppo_trainer = PPOTrainer(ppo_config, model, model_ref, tokenizer)
27
28# define a reward for response
29# (this could be any reward such as human feedback or output from another model)
30reward = [torch.tensor(1.0)]
31
32# train model for one step with ppo
33train_stats = ppo_trainer.step([query_tensor[0]], [response_tensor[0]], reward)transformers library by 🤗 Hugging Face.1@misc{vonwerra2022trl,
2 author = {Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang},
3 title = {TRL: Transformer Reinforcement Learning},
4 year = {2020},
5 publisher = {GitHub},
6 journal = {GitHub repository},
7 howpublished = {\url{https://github.com/huggingface/trl}}
8}