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

Dist ref kl #529

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 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
404ef14
Fix: Do not shuffle empty experience dataloader
Dahoas Jun 23, 2023
67b711a
Make experience before first round of training
Dahoas Jun 23, 2023
34e185a
Refactoring .generate/.generate_eval
Dahoas Jun 27, 2023
11e1e95
Refactored decode, make_experience and added support for external ref…
Dahoas Jul 14, 2023
527ba23
Fix BoN sampling after big refactor
Dahoas Jul 17, 2023
676a1cd
Fixing style
Dahoas Jul 18, 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
Prev Previous commit
Next Next commit
Refactored decode, make_experience and added support for external ref…
…erence models
  • Loading branch information
Dahoas committed Jul 18, 2023
commit 11e1e95df02bd64b8c3350571b29bfb20ee18e4a
4 changes: 1 addition & 3 deletions trlx/models/modeling_ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ class PPOConfig(MethodConfig):

:param num_train_sequences: top_k of n sampled sequences from prompt
:type num_train_sequences: int

: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 @@ -138,6 +135,7 @@ class PPOConfig(MethodConfig):
gen_kwargs: dict
gen_experience_kwargs: Optional[dict] = None
num_train_sequences: int = 1
dist_ref_model: bool = False

def get_advantages_and_returns(
self,
Expand Down
89 changes: 69 additions & 20 deletions trlx/trainer/accelerate_base_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,49 +203,93 @@ def decode(
prompts: List[torch.LongTensor],
samples: List[torch.LongTensor],
prompt_sizes: torch.LongTensor = None,
append_eos_token: bool = False,
) -> Tuple[List[str], List[str], List[str]]:
append_eos_token: bool = True,
) -> Tuple[List[str], List[str], List[str], List[torch.LongTensor], List[torch.LongTensor], List[torch.LongTensor]]:
"""
Decode tensor generations into lists of strings (`samples`: List[str], `prompts`: List[str], `outputs`: List[str])
Decode tensor generations with stopping criteria into lists of strings (`samples`: List[str], `prompts`: List[str], `outputs`: List[str]) and
Note prompts maybe sompetimes be right padded, as well as samples
"""
if prompt_sizes is None:
# Assuming prompts were left-padded
prompt_sizes = [prompts.shape[1]] * len(prompts)

str_samples, str_prompts, str_outputs = [], [], []
tok_samples, tok_prompts, tok_outputs = [], [], []
for prompt, sample, prompt_size in zip(prompts, samples, prompt_sizes):
if self.config.model.model_arch_type == "seq2seq":
output_start_ix = 0
else:
output_start_ix = prompt_size

# We must decode by skipping padding in the middle with skip_special_tokens
str_prompt = self.tokenizer.decode(prompt[:prompt_size], skip_special_tokens=True)
# Return the prompt tensor (with the exact padding) used to generate the sample
tok_prompt = prompt[:prompt_size].cpu()

str_output = self.tokenizer.decode(sample[output_start_ix:], skip_special_tokens=True)
# Trim outputs up to `self.stop_sequences` if any are present
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()

# Recover sequence of tokens corresponding to string
# NOTE: Cast to torch.long in the case the input is empty
tok_output = self.tokenizer(str_output, return_tensors="pt").input_ids[0].long()
# Remove bos from tokenized output (if present)
if hasattr(self.tokenizer, "bos_token") and len(tok_output) > 0 and tok_output[0].item() == self.tokenizer.bos_token_id:
tok_output = tok_output[1:]

# 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
# Note we append to both separately because encoding </s> does not result in tokenizer.eos_token_id
if append_eos_token:
str_output += self.tokenizer.eos_token
tok_output = torch.cat([tok_output, torch.tensor(self.tokenizer.eos_token_id, dtype=torch.long).view((1,))])

'''if len(tok_output) > 91:
print("#####")
print("Sample: ", sample.shape)
print(sample)
print("output: ", tok_output.shape)
print(tok_output)
print(str_output)
print("prompt size: ", output_start_ix)
print("tok_prompt: ", tok_prompt)
print("tok prompt shape: ", tok_prompt.shape)
print("prompt: ", prompt)
print("str prompt: ", str_prompt)
print("prompt: ", prompt.shape)
exit()'''

str_prompts.append(str_prompt)
str_outputs.append(str_output)
tok_prompts.append(tok_prompt)
tok_outputs.append(tok_output)

if self.config.model.model_arch_type == "seq2seq":
sample = str_prompt + self.tokenizer.sep_token + str_output
tok_sample = torch.cat([tok_prompt, torch.tensor(self.tokenizer.sep_token_id, dtype=torch.long).view((1,)), tok_output])
else:
sample = str_prompt + str_output
tok_sample = torch.cat([tok_prompt, tok_output])

str_samples.append(sample)
tok_samples.append(tok_sample)

if tok_sample.dtype == torch.float32:
print("tok_sample ", tok_sample.dtype)
print(tok_sample)
print("tok prompt: ", tok_prompt.dtype)
print(tok_prompt)
print("tok output: ", tok_output.dtype)
print(tok_output)
exit()

return str_samples, str_prompts, str_outputs
return str_samples, str_prompts, str_outputs, tok_samples, tok_prompts, tok_outputs

def generate(self, input_ids, attention_mask=None, chunk_size=None, **kwargs):
"""Wraps hf's `generate` adding some specific method's defaults"""
Expand Down Expand Up @@ -421,9 +465,9 @@ def evaluate(self): # noqa: C901
pad_index=self.tokenizer.pad_token_id,
)
)
all_samples.extend(samples.tolist())
all_prompts.extend(prompts.tolist())
all_prompt_sizes.extend(prompt_sizes.tolist())
all_samples.extend(samples)
all_prompts.extend(prompts)
all_prompt_sizes.extend(prompt_sizes)

metadata = gather_dict(metadata, self.accelerator.gradient_state)
all_metadata.append(metadata)
Expand All @@ -439,7 +483,7 @@ def evaluate(self): # noqa: C901
stats["time/generate"] = time() - generate_time

if self.accelerator.is_main_process:
str_samples, str_prompts, str_outputs = self.decode(all_prompts, all_samples, all_prompt_sizes)
str_samples, str_prompts, str_outputs, tok_samples, tok_prompts, tok_outputs = self.decode(all_prompts, all_samples, all_prompt_sizes)

columns = ["prompt", "output"]
columns_data = [str_prompts, str_outputs]
Expand All @@ -453,12 +497,16 @@ def evaluate(self): # noqa: C901
if self.reward_fn:
logger.info("Computing rewards")
rewards = self.reward_fn(
samples=str_samples,
prompts=str_prompts,
outputs=str_outputs,
model_tok=self.tokenizer,
**metadata,
)
samples=str_samples,
prompts=str_prompts,
outputs=str_outputs,
tok_samples=tok_samples,
tok_prompts=tok_prompts,
tok_outputs=tok_outputs,
model_tok=self.tokenizer, **metadata)
# Remove kl terms from reward
if hasattr(self, "dist_ref_model") and self.dist_ref_model:
rewards = [[r[0] for r in reward] for reward in rewards]
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:
Expand All @@ -477,12 +525,13 @@ def evaluate(self): # noqa: C901
logger.info("Computing metrics")
metric_time = time()
metrics = self.metric_fn(
samples=str_samples,
prompts=str_prompts,
outputs=str_outputs,
model_tok=self.tokenizer,
**metadata,
)
samples=str_samples,
prompts=str_prompts,
outputs=str_outputs,
tok_samples=tok_samples,
tok_prompts=tok_prompts,
tok_outputs=tok_outputs,
model_tok=self.tokenizer, **metadata)
stats["time/metric"] = time() - metric_time

mean_metrics = {
Expand Down
Loading