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

[feat] Support tying metadata to each prompt #421

Merged
merged 13 commits into from
Apr 10, 2023
Merged

Conversation

maxreciprocate
Copy link
Collaborator

@maxreciprocate maxreciprocate commented Apr 5, 2023

This PR lets users pass an optional dictionary alongside each prompt into PromptPipeline, which would get passed later, together with generated completions, into a reward function in a form of additional keywords per each key in the original dictionary.

Fix of #297, #301, #340

https://wandb.ai/sorry/trlx-references/reports/add-prompts-metadata-v-main--Vmlldzo0MDAyMDIy

@maxreciprocate maxreciprocate marked this pull request as ready for review April 6, 2023 21:15
Copy link
Collaborator

@jon-tow jon-tow left a comment

Choose a reason for hiding this comment

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

Looks great, Max! I'm definitely going to steal your gather_dict util for personal code 😄
I've left two comments for feedback whenever you get the chance 🙏

@@ -282,7 +282,7 @@ def make_experience(self, num_rollouts: int = 1024, iter_count: int = 0): # noq
exp_generate_time = time()

# Generate samples from the language model (similar to using HuggingFace `generate` method)
samples = self.generate(**batch)
samples = self.generate(batch["input_ids"], batch["attention_mask"])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Aside: This change makes it clear that next(self.prompt_iterator) doesn't materialize a PromptBatch instance; in fact, PromptBatch objects are never created anywhere. Sigh.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't we depreciate prompt batch then.

Comment on lines +198 to +199
original_samples = [p + o + reward_tokenizer.eos_token for p, o in zip(prompts, original_output)]
original_rewards = get_reward(original_samples)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Aside: Nice

@@ -125,7 +134,15 @@ def __len__(self) -> int:
return len(self.prompts)

def create_loader(self, batch_size: int, shuffle=False) -> DataLoader:
collate_fn = DataCollatorWithPadding(self.tokenizer) if self.tokenizer else torch.vstack
def collate_fn(xs):
out = self.tokenizer.pad([{"input_ids": x["input_ids"]} for x in xs], return_tensors="pt")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it safe to assume self.tokenizer will always be available? Not sure if the previous check if self.tokenizer else torch.vstack is just stale code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was a stale code and the single example which didn't use tokenizer at the time (ilql_randomwalks) was also converted to use tokenizer to drop these checks, out of which it is the last, for consistency; right now tokenizer is required in almost every other piece of code, so I'm not sure if that feature is missed.

super().__init__()

if isinstance(prompts[0], dict):
metadata = prompts
prompts = [x.pop("prompt") for x in metadata]
Copy link
Collaborator

Choose a reason for hiding this comment

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

In PromptPipeline's constructor docstring, we should document that "prompt" is an expected field of metadata when prompts consists of dictionaries.

Copy link
Collaborator

@jon-tow jon-tow left a comment

Choose a reason for hiding this comment

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

Great work, Max! 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants