Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement BoN for training and eval #528

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f04446d
Implementing support for dense rewards
Dahoas Jun 5, 2023
13a01fc
added "num_return_sequences" param which corresponds to n in Best-of-…
SharathRaparthy Jun 16, 2023
5421a73
updates to "num_return_sequences" param
SharathRaparthy Jun 16, 2023
2f3ac28
BoN implementation
SharathRaparthy Jun 16, 2023
2f1dace
Changed back to default.
SharathRaparthy Jun 19, 2023
f58170d
TopK sampling instead of Top1
SharathRaparthy Jun 19, 2023
be8bc1a
summed along dim=1
SharathRaparthy Jun 26, 2023
608d812
Generating samples in chunks
SharathRaparthy Jun 26, 2023
d8557e7
added gen_chunk_size parameter
SharathRaparthy Jun 26, 2023
8ef9c36
chunking in forward prop
SharathRaparthy Jun 26, 2023
4c1d82d
chunking generations in train and eval
SharathRaparthy Jun 26, 2023
ecd5107
Implementing support for dense rewards
Dahoas Jun 5, 2023
4071604
Fix distributed ref_mean, ref_var bug for dense rewards
Dahoas Jun 15, 2023
5f41413
Make generation respect max seq length
Dahoas Jun 23, 2023
22ae83f
Make experience before first round of training
Dahoas Jun 23, 2023
7d0a4be
Refactoring .generate/.generate_eval
Dahoas Jun 27, 2023
b79dd19
Fix BoN metric support
Dahoas Jun 29, 2023
cb49dc5
Enforce chunk_size param for eval generation when present
Dahoas Jul 3, 2023
e290412
Fix: Don't shuffle prompt dataset
Dahoas Jul 4, 2023
391d04c
Move inputs to device
Dahoas Jul 18, 2023
8de84e4
Fix style
Dahoas Jul 18, 2023
3d7e0d5
Fix chunked generation
Dahoas Jul 21, 2023
1fda0ce
fix(accelerate_base_trainer): order of keyword arguments
maxreciprocate Jul 22, 2023
4ac1707
Merging main
Dahoas Aug 7, 2023
de3d854
Merge branch 'BoN' of https://github.com/CarperAI/trlx into BoN
Dahoas Aug 7, 2023
3ce3c2b
Removing old example
Dahoas Aug 7, 2023
2635de5
Fix: remove extraneous method args
Dahoas Aug 7, 2023
1be2c3c
Fix: Always set generate_experience_kwargs
Dahoas Aug 7, 2023
3cba0db
Fix: Remove mask from RunningMoments update call
Dahoas Aug 7, 2023
0cb91c4
Fix: style
Dahoas Aug 7, 2023
cc92911
Fix: rename 'gen_chunk_size' to 'chunk_size'
Dahoas Aug 7, 2023
4297f98
Fix: generated samples padding
Dahoas Aug 7, 2023
36f06af
Remove prints
Dahoas Aug 7, 2023
a2980dd
Rename 'num_train_sequences' to 'num_topk_samples'
Dahoas Aug 21, 2023
3d5a639
Address nits
Dahoas Aug 21, 2023
87837b6
Fix: style
Dahoas Aug 21, 2023
ed93be8
Set 'num_return_sequences' to 1 by default
Dahoas Aug 21, 2023
24925c8
Fix: typo
Dahoas Aug 21, 2023
a022d3f
Merge branch 'main' into BoN
maxreciprocate Sep 1, 2023
9680c9f
Merge branch 'main' into bon-x
maxreciprocate Sep 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions examples/ppo_redemption.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this example got here by inertia from the previous PR

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Generates positive movie reviews by tuning a pretrained model on IMDB dataset
# with a sentiment reward function
import json
import os
import sys
from typing import List

import torch
from datasets import load_dataset
from transformers import pipeline

import trlx
from trlx.data.default_configs import TRLConfig, default_ppo_config


def get_positive_score(scores):
"Extract value associated with a positive sentiment from pipeline's output"
return dict(map(lambda x: tuple(x.values()), scores))["POSITIVE"]


def get_negative_score(scores):
return dict(map(lambda x: tuple(x.values()), scores))["NEGATIVE"]


def main(hparams={}):
# Merge sweep config with default config if given
config = TRLConfig.update(default_ppo_config().to_dict(), hparams)
config.method.cliprange_reward = False
config.method.gen_kwargs["max_new_tokens"] = 70
config.method.gen_kwargs["temperature"] = 0.3
config.train.total_steps = 20000
config.train.checkpoint_interval = 10000000
# config.method.init_kl_coef = 0

if torch.cuda.is_available():
device = int(os.environ.get("LOCAL_RANK", 0))
else:
device = -1

sentiment_fn = pipeline(
"sentiment-analysis",
"lvwerra/distilbert-imdb",
top_k=2,
truncation=True,
batch_size=256,
device=device,
)

def dense_reward_fn(samples: List[str], prompts: List[str], outputs: List[str], model_tok, **kwargs) -> List[float]:
# Reward positively for initially negative then positive review
# Reward functions should never receive padded text except for a singel EOS at the end
# Reward function should return token rewards for just the response
first_halves = [".".join(sample.split(".")[: len(sample.split(".")) // 2]) for sample in samples]
negative_first_halves = list(map(get_negative_score, sentiment_fn(first_halves)))
second_halves = [".".join(sample.split(".")[len(sample.split(".")) // 2 :]) for sample in samples]
positive_second_halves = list(map(get_positive_score, sentiment_fn(second_halves)))
text_scores = [[f, s] for f, s in zip(negative_first_halves, positive_second_halves)]
tok_scores = []
for sample, prompt, response, text_score in zip(samples, prompts, outputs, text_scores):
toks = model_tok(response).input_ids
tok_score = [0] * len(toks)
# Hacky way of assigning intermediate score
tok_score[len(tok_score) // 2] = text_score[0]
tok_score[-1] = text_score[1]
tok_scores.append(tok_score)
return tok_scores

# Take few words off of movies reviews as prompts
imdb = load_dataset("imdb", split="train+test")
prompts = [" ".join(review.split()[:4]) for review in imdb["text"]]

trlx.train(
reward_fn=dense_reward_fn,
prompts=prompts,
eval_prompts=["I don't know much about Hungarian underground"] * 256,
config=config,
)


if __name__ == "__main__":
hparams = {} if len(sys.argv) == 1 else json.loads(sys.argv[1])
main(hparams)
2 changes: 2 additions & 0 deletions trlx/data/default_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ def default_ppo_config():
ref_mean=None,
ref_std=None,
cliprange_reward=10,
num_train_sequences=1,
gen_kwargs=dict(
max_new_tokens=40,
top_k=0,
top_p=1.0,
do_sample=True,
num_return_sequences=1,
),
),
)
Expand Down
7 changes: 7 additions & 0 deletions trlx/models/modeling_ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ class PPOConfig(MethodConfig):

:param gen_experience_kwargs: if this is not None, then the experience is generated using this
:type gen_experience_kwargs: Dict[str, Any]

:param num_train_sequences: top_k of n sampled sequences from prompt
:type num_train_sequences: int
Dahoas marked this conversation as resolved.
Show resolved Hide resolved

:param mix_sft: if this is True, then SFT gradients will be mixed into PPO traininig
:type mix_sft: bool
"""

ppo_epochs: int
Expand All @@ -131,6 +137,7 @@ class PPOConfig(MethodConfig):
cliprange_reward: float
gen_kwargs: dict
gen_experience_kwargs: Optional[dict] = None
num_train_sequences: int = 1

def get_advantages_and_returns(
self,
Expand Down
105 changes: 77 additions & 28 deletions trlx/trainer/accelerate_base_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import sys
from abc import abstractmethod
from contextlib import contextmanager
from copy import copy
from time import time
from typing import Dict, List, Optional, Tuple

import ray
import torch
from torch.nn.utils.rnn import pad_sequence
from accelerate import Accelerator # type: ignore
from ray.air import session
from rich.console import Console
Expand Down Expand Up @@ -220,21 +222,17 @@ def decode(
str_prompt = self.tokenizer.decode(prompt[:prompt_size], skip_special_tokens=True)
str_output = self.tokenizer.decode(sample[output_start_ix:], skip_special_tokens=True)
maxreciprocate marked this conversation as resolved.
Show resolved Hide resolved
# Trim outputs up to `self.stop_sequences` if any are present
trimmed = False
if self.stop_sequences:
for stop in self.stop_sequences:
stop_ix = str_output.find(stop)
if stop_ix >= 0:
str_output = str_output[:stop_ix].rstrip()
trimmed = True

# Recover the last <eos> if it was present in the original sample
# or add one if it was trimmed with `self.stop_sequences`.
# When a generation ended due to `max_new_tokens` exhaustion,
# only then <pad> or <eos> token would not be present in the original sample at the end
if append_eos_token and (
trimmed or sample[-1] == self.tokenizer.eos_token_id or sample[-1] == self.tokenizer.pad_token_id
):
if append_eos_token:
str_output += self.tokenizer.eos_token

str_prompts.append(str_prompt)
Expand All @@ -249,33 +247,51 @@ def decode(

return str_samples, str_prompts, str_outputs

def generate(self, input_ids, attention_mask=None, **kwargs):
def generate(self, input_ids, attention_mask=None, chunk_size=None, **kwargs):
"""Wraps hf's `generate` adding some specific method's defaults"""
# Decide into chunk sizes and generate saples
input_ids = input_ids.to(self.accelerator.device)
if attention_mask is not None:
attention_mask = attention_mask.to(self.accelerator.device)
if self.generate_experience_kwargs is not None:
kwargs = dict(self.generate_experience_kwargs, **kwargs)
else:
kwargs = dict(self.generate_kwargs, **kwargs)

with torch.no_grad():
return self.accelerator.unwrap_model(self.model).generate(
input_ids=input_ids, attention_mask=attention_mask, **kwargs
generate_kwargs = copy(self.generate_kwargs)
generate_kwargs.update(kwargs)

# Update max_new_tokens to respect max_seq_length
prompt_length = input_ids.shape[1]
if generate_kwargs.get("max_new_tokens") is not None:
generate_kwargs["max_new_tokens"] = min(
max(self.max_length - prompt_length, 0), generate_kwargs["max_new_tokens"]
)
else:
generate_kwargs["max_new_tokens"] = max(self.max_length - prompt_length, 0)

def generate_eval(self, input_ids, attention_mask=None, **kwargs):
"""Wraps hf's `generate` adding some specific method's defaults"""
input_ids = input_ids.to(self.accelerator.device)
# Repeat prompts, attention_masks for chunking if returning multiple sequences
if generate_kwargs.get("num_return_sequences") is None:
generate_kwargs["num_return_sequences"] = 1

num_return_sequences = generate_kwargs.pop("num_return_sequences") # Pop to hide from model.generate call
input_ids = input_ids.repeat_interleave(num_return_sequences, dim=0)
if attention_mask is not None:
attention_mask = attention_mask.to(self.accelerator.device)
attention_mask = attention_mask.repeat_interleave(num_return_sequences, dim=0)

kwargs = dict(self.generate_kwargs, **kwargs)
if chunk_size is None:
chunk_size = input_ids.shape[0]

with torch.no_grad():
return self.accelerator.unwrap_model(self.model).generate(
input_ids=input_ids, attention_mask=attention_mask, **kwargs
)
# Chunk input_ids and attention_mask
input_ids = input_ids.split(chunk_size, dim=0)
if attention_mask is not None:
attention_mask = attention_mask.split(chunk_size, dim=0)
samples = []
for chunk_idx in range(len(input_ids)):
with torch.no_grad():
sample = self.accelerator.unwrap_model(self.model).generate(
input_ids=input_ids[chunk_idx], attention_mask=attention_mask[chunk_idx], **generate_kwargs
)
samples.append(sample)
# Concat padded samples
samples = pad_sequence(samples, batch_first=True, padding_value=self.tokenizer.pad_token_id)
return samples

def save_pretrained(self, directory: Optional[str] = None, **kwargs):
"""Save the underlying Hugging Face model, tokenizer, and configuration files to a directory for
Expand Down Expand Up @@ -377,11 +393,20 @@ def evaluate(self): # noqa: C901
for i_prompt, prompts in enumerate(self.eval_dataloader):
metadata = {k: v for k, v in prompts.items() if k != "input_ids" and k != "attention_mask"}
if self.generate_sweep_kwarg:
samples = self.generate_eval(
samples = self.generate(
prompts["input_ids"], prompts["attention_mask"], **{gen_sweep_arg: gen_sweep_value}
)
else:
samples = self.generate_eval(prompts["input_ids"], prompts["attention_mask"])
chunk_size = self.config.method.chunk_size if hasattr(self.config.method, "chunk_size") else None
samples = self.generate(prompts["input_ids"], prompts["attention_mask"], chunk_size=chunk_size)

# Repeat prompts, metadata num_return_sequence times
num_return_sequences = 1
if self.generate_kwargs.get("num_return_sequences") is not None:
num_return_sequences = self.generate_kwargs["num_return_sequences"]
prompts["input_ids"] = prompts["input_ids"].repeat_interleave(num_return_sequences, dim=0)
prompts["attention_mask"] = prompts["attention_mask"].repeat_interleave(num_return_sequences, dim=0)
metadata = {k: self.repeat_interleave(v, num_return_sequences) for k, v in metadata.items()}

# TODO(reciprocated): this should be moved into `decode`
# but that needs to be synced with indexing in `make_experience`
Expand Down Expand Up @@ -427,10 +452,19 @@ def evaluate(self): # noqa: C901
# in online setting, compute the reward for validation
if self.reward_fn:
logger.info("Computing rewards")
rewards = torch.tensor(
self.reward_fn(samples=str_samples, prompts=str_prompts, outputs=str_outputs, **metadata),
dtype=float,
rewards = self.reward_fn(
samples=str_samples,
prompts=str_prompts,
outputs=str_outputs,
model_tok=self.tokenizer,
**metadata,
)
if type(rewards[0]) is torch.Tensor:
rewards = torch.tensor([reward.sum().item() for reward in rewards], dtype=float)
elif type(rewards[0]) is list:
rewards = torch.tensor([sum(reward) for reward in rewards])
else:
rewards = torch.tensor(rewards)
mean_reward = rewards.mean().item()
columns.append("reward")
if not isinstance(rewards, list):
Expand All @@ -442,7 +476,13 @@ def evaluate(self): # noqa: C901
if self.metric_fn:
logger.info("Computing metrics")
metric_time = time()
metrics = self.metric_fn(samples=str_samples, prompts=str_prompts, outputs=str_outputs, **metadata)
metrics = self.metric_fn(
samples=str_samples,
prompts=str_prompts,
outputs=str_outputs,
model_tok=self.tokenizer,
Dahoas marked this conversation as resolved.
Show resolved Hide resolved
**metadata,
)
stats["time/metric"] = time() - metric_time

mean_metrics = {
Expand Down Expand Up @@ -633,6 +673,15 @@ def learn(self): # noqa: C901
self.post_epoch_callback()
tbar.close()

@staticmethod
def repeat_interleave(l, n):
if type(l) is torch.Tensor:
l = l.repeat_interleave(n, dim=0)
elif type(l) is list:
l = [[s] * n for s in l]
l = [item for sublist in l for item in sublist]
return l

@abstractmethod
def get_arch(self, config: TRLConfig):
"""Returns a specific wrapper of the decoder architecture"""
Expand Down
Loading