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

[WIP] Add SPIN trainer #1344

Closed
wants to merge 14 commits into from
Closed

[WIP] Add SPIN trainer #1344

wants to merge 14 commits into from

Conversation

lewtun
Copy link
Member

@lewtun lewtun commented Feb 21, 2024

Implements the Self-Play fIne-tuNing (SPIN) algorithm from: https://arxiv.org/abs/2401.01335

TODO

  • Validate training works for a few iterations of Qwen-1.5-0.5b
  • Fix generation with ZeRo-3 init (hitting annoying RuntimeError: 'weight' must be 2-D errors)
  • Add docs
  • Refactor / clean up internals

preprocessing_num_workers: int = field(
default=12, metadata={"help": "The number of processes to use for the preprocessing."}
)
do_generate: bool = field(default=False)
Copy link
Member Author

Choose a reason for hiding this comment

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

These args could arguably live in the SPINConfig

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Collaborator

@vwxyzjn vwxyzjn 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! Left some initial comments :)

import torch
from datasets import Dataset, load_dataset
from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, TrainingArguments

from trl import DPOTrainer, ModelConfig, get_kbit_device_map, get_peft_config, get_quantization_config


datasets.disable_caching()
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this? Maybe we should do load_from_cache=False?

Comment on lines +201 to +204
if script_args.do_generate:
print(f"Generating completions for {len(prompt_train_ds)} training examples")
train_completions = spin_trainer.generate(prompt_train_ds, "prompt", generation_config, batch_size=16)
test_completions = spin_trainer.generate(prompt_test_ds, "prompt", generation_config, batch_size=16)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If the generation only occurs in the end, wdyt about doing a subprocess call on a vllm/tgi script to generate instead?

Comment on lines +418 to +420
chosen_tokens = self.tokenizer(chosen, add_special_tokens=False)
rejected_tokens = self.tokenizer(rejected, add_special_tokens=False)
prompt_tokens = self.tokenizer(prompt, add_special_tokens=False)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This feels quite similar to the logic in DPOTrainer. Maybe unify the logic somehow (or use similar terminology).

chosen_tokens = self.tokenizer(
chosen, truncation=True, max_length=self.max_target_length, add_special_tokens=True
)
rejected_tokens = self.tokenizer(
rejected, truncation=True, max_length=self.max_target_length, add_special_tokens=True
)
prompt_tokens = self.tokenizer(
prompt, truncation=True, max_length=self.max_prompt_length, add_special_tokens=True
)

Not necessary but we could also benefit from doing a filediff between SPIN and DPO and try to make the lines of code differences minimal.

image

Copy link

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

@github-actions github-actions bot closed this Mar 30, 2024
@davidberenstein1957
Copy link
Contributor

I wanted to look at implementing this in TRL. @lewtun @vwxyzjn was there a reason this was closed? Should we reopen it and can I continue the work?

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.

None yet

4 participants