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
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
BoN implementation
  • Loading branch information
SharathRaparthy authored and Dahoas committed Jul 18, 2023
commit 2f3ac2816e60af5aeb9f2b8eac5e16a8465e9616
22 changes: 20 additions & 2 deletions trlx/trainer/accelerate_ppo_trainer.py
Copy link
Collaborator

@PhungVanDuy PhungVanDuy Jul 21, 2023

Choose a reason for hiding this comment

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

in line 472 sample_outputs not assigned yet, seems sample_outputs = pad_sequence(tok_outputs)

Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ def make_experience(self, num_rollouts: int = 1024, iter_count: int = 0): # noq
rollout_generate_time = time()

# Generate samples from the language model (similar to using HuggingFace `generate` method)
samples = self.generate(batch["input_ids"], batch["attention_mask"])
samples = self.generate(batch["input_ids"], batch["attention_mask"], num_return_sequences=self.config.method.num_return_sequences)
stats["time/rollout_generate"] = time() - rollout_generate_time

prompt_tensors = batch.input_ids
prompt_tensors = batch.input_ids.repeat_interleave(self.config.method.num_return_sequences, dim=0) # TODO: It is hard-coded to 10 here. Change it to a variable
device = samples.device

prompt_sizes = torch.tensor([prompt_tensors.shape[1]] * len(prompt_tensors), device=device)
Expand Down Expand Up @@ -319,6 +319,11 @@ def make_experience(self, num_rollouts: int = 1024, iter_count: int = 0): # noq
torch.distributed.scatter(scores, all_scores)
else:
scores = all_scores[0].clone().detach()
# Best-of-N Sampling.
max_score_indices = self.get_max_indices(scores, self.config.method.num_return_sequences, device)
scores = scores.index_select(0, max_score_indices)
samples = samples.index_select(0, max_score_indices)
prompt_tensors = prompt_tensors.index_select(0, max_score_indices)

str_samples, str_prompts, str_outputs = self.decode(prompt_tensors, samples, append_eos_token=True)

Expand Down Expand Up @@ -507,3 +512,16 @@ def make_experience(self, num_rollouts: int = 1024, iter_count: int = 0): # noq

# Push samples and rewards to trainer's rollout storage
self.push_to_store(ppo_rl_elements)

@staticmethod
def get_max_indices(input_tensor, window_size, device):
# Use unfold to create the sliding windows
unfolded = input_tensor.unfold(0, window_size, window_size)

# Find the max values and indices along the unfolded dimension
values, indices = unfolded.max(dim=2)

# Adjust indices to be relative to original tensor
indices += torch.arange(0, input_tensor.size(0) - window_size + 1, window_size).to(device).unsqueeze(1)

return indices.squeeze()