generated from fastai/nbdev_template
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Refactor Online DPO #1839
Merged
Merged
Refactor Online DPO #1839
Changes from all commits
Commits
Show all changes
103 commits
Select commit
Hold shift + click to select a range
94353cb
online dpo trainer based on rloo trainer
mnoukhov d88ee55
push changes
vwxyzjn 1a45ec4
refactor
vwxyzjn 5a3bd05
use `batch_generation` method
vwxyzjn aa67767
precommit
vwxyzjn 576c098
remove breakpoint()
vwxyzjn c5a1612
quick refactor
vwxyzjn e264126
push the current changes
vwxyzjn 9653edb
quick change
vwxyzjn 798d1d6
refactor
vwxyzjn 6562bc2
use the config name as the experiment name
vwxyzjn 7a0c273
fix logging
vwxyzjn 7e03124
update online DPO docs
vwxyzjn 0f8b1e3
use llm as a judge
vwxyzjn 1f0f6b2
quick change
vwxyzjn ac11b75
quick fix
vwxyzjn 2a7abca
cache changes
vwxyzjn a177097
Merge branch 'main' into online-dpo-llmjudge
qgallouedec e74646f
new semantics
qgallouedec c93c81b
style and arg order change
qgallouedec ff479e4
rm duplicated num_epochs
qgallouedec f39c61a
rm plot script
qgallouedec e515d0e
num_epoch
qgallouedec 0d8ae8c
revert some changes
qgallouedec 0641b55
revert changes
qgallouedec 25af762
revert whitespace
qgallouedec 29a1244
rm whitespace
qgallouedec d858b28
revert change
qgallouedec 4020f41
policy->model
qgallouedec 8d26a51
Merge branch 'main' into online-dpo-llmjudge
qgallouedec b1a264a
optional judge and reward model
qgallouedec 79082f8
cleaning online dpo script
qgallouedec 9554c80
warning when both reward mdoel and judge provided
qgallouedec e73c66f
Merge branch 'online-dpo-llmjudge' of https://github.com/huggingface/…
qgallouedec e793dde
Merge branch 'main' into online-dpo-llmjudge
qgallouedec 2d0a8a1
return -1 when the judge fails
qgallouedec 9580a8d
dataset num proc
qgallouedec 14be7b7
add judges in online dpo; fix collate and process within the trainer
qgallouedec 1b7cdcf
lr_scheduler.step() after optimizer step
qgallouedec e58d473
update odpo test
qgallouedec da70ab0
Merge branch 'main' into online-dpo-llmjudge
qgallouedec ced7c98
reduce nestiness
qgallouedec e14eb43
allow pickle
qgallouedec 629b6f1
generation config typing
qgallouedec 4459efd
online dpo llm judge
qgallouedec c7680c7
fix data collator pad token
qgallouedec d3d5175
add space
qgallouedec 94f142e
fix pref score
qgallouedec 5a0b4e9
-1 for judges
qgallouedec ddac3b6
self.model_wrapped = self.model
qgallouedec 031b6de
Merge branch 'main' into online-dpo-llmjudge
qgallouedec 3e2cfe5
onlinedpo inherits from training arguments
qgallouedec 595c07e
num_epoch -> num_steps_in_epochs
qgallouedec 518c896
update -> epoch
qgallouedec 5c9fd95
epoch -> step; step_in_epoch -> ppo_epoch; rm run_name
qgallouedec 85c7bd5
num_steps_in_epoch -> num_ppo_epochs
qgallouedec 2989a68
epoch_idx -> ppo_epoch_idx
qgallouedec 435bacd
make init consistent with dpo
qgallouedec 31d684d
try another option
qgallouedec 8fdbaa4
progress...
qgallouedec b369beb
odpo
qgallouedec d9c9736
current progress
qgallouedec 48c449a
log and other changes
qgallouedec 238ac5a
rename for legacy
qgallouedec 3a37e3c
rename for legacy
qgallouedec 4d73ee3
rename and move truncate
qgallouedec ba23435
rename
qgallouedec 2db55aa
new config
qgallouedec 49a7d47
LogCompletionsCallback
qgallouedec f330c18
style
qgallouedec a1d9ba3
rename trainer
qgallouedec dfebb9f
truncate right in utils
qgallouedec 4664b05
update example
qgallouedec 9b808ca
reward model path
qgallouedec 66ca5bd
properly log
qgallouedec 3d280b9
fix example
qgallouedec 2d28ad9
add generation prompt and log special tokens
qgallouedec 8f28f4f
true penalty
qgallouedec f936692
defaults from the paper
qgallouedec 55475e4
Merge branch 'main' into online-dpo-llmjudge
qgallouedec 845c1bc
Remove MPS (#1983)
lewtun 3ed03a4
Merge branch 'main' into online-dpo-llmjudge
lewtun a567e77
Set KV cache false when gradient checkpointing is enabled (#1984)
lewtun 21104fd
Various tweask
lewtun 8a033c7
Remove padding from table
lewtun 4fd0666
Clean up
lewtun b36dc0e
Fix test
lewtun 8db4b71
Revert log freq
lewtun 0e5a23b
Merge branch 'main' into online-dpo-llmjudge
lewtun 4439a9a
Fix docs
lewtun 35eff1d
Fix tests aain!
lewtun a64721d
Fix typo
lewtun ae4a1ed
Revert
lewtun 0b6ac0e
Fix regression
lewtun dab37dc
Apply suggestions from code review
lewtun bb267fb
Fix DPO config test
lewtun 6b7d559
Fix doc tree
lewtun 56afe56
Clean docs moar
lewtun f78ff61
Add docstring
lewtun bc13c33
raise NotImplemented error for judge
qgallouedec 03ea0af
Merge branch 'main' into online-dpo-llmjudge
lewtun 57eb673
Refactor cache clearning
lewtun 3ea2654
Merge branch 'main' into online-dpo-llmjudge
lewtun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# flake8: noqa | ||
# Copyright 2024 The HuggingFace Inc. team. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
Usage: | ||
|
||
python examples/scripts/dpo_online.py \ | ||
--model_name_or_path trl-lib/pythia-1b-deduped-tldr-sft \ | ||
--reward_model_path trl-lib/pythia-1b-deduped-tldr-rm \ | ||
--dataset_name trl-lib/tldr \ | ||
--learning_rate 5.0e-7 \ | ||
--output_dir pythia-1b-tldr-online-dpo \ | ||
--per_device_train_batch_size 4 \ | ||
--gradient_accumulation_steps 32 \ | ||
--num_train_epochs 3 \ | ||
--completion_length 53 \ | ||
--warmup_ratio 0.1 \ | ||
--missing_eos_penalty 1.0 \ | ||
--push_to_hub | ||
""" | ||
|
||
import torch | ||
from datasets import load_dataset | ||
from transformers import AutoModelForCausalLM, AutoModelForSequenceClassification, AutoTokenizer | ||
from accelerate import PartialState | ||
from trl import ( | ||
DPOScriptArguments, | ||
ModelConfig, | ||
OnlineDPOConfig, | ||
OnlineDPOTrainer, | ||
get_kbit_device_map, | ||
get_quantization_config, | ||
) | ||
from trl.commands.cli_utils import TrlParser | ||
from trl.trainer.callbacks import LogCompletionsCallback | ||
from trl.trainer.utils import SIMPLE_QUERY_CHAT_TEMPLATE | ||
|
||
if __name__ == "__main__": | ||
parser = TrlParser((DPOScriptArguments, OnlineDPOConfig, ModelConfig)) | ||
args, training_args, model_config = parser.parse_args_and_config() | ||
args.gradient_checkpointing_kwargs = {"use_reentrant": True} | ||
|
||
torch_dtype = ( | ||
model_config.torch_dtype | ||
if model_config.torch_dtype in ["auto", None] | ||
else getattr(torch, model_config.torch_dtype) | ||
) | ||
quantization_config = get_quantization_config(model_config) | ||
model_kwargs = dict( | ||
revision=model_config.model_revision, | ||
attn_implementation=model_config.attn_implementation, | ||
torch_dtype=torch_dtype, | ||
use_cache=False if training_args.gradient_checkpointing else True, | ||
device_map=get_kbit_device_map() if quantization_config is not None else None, | ||
quantization_config=quantization_config, | ||
) | ||
|
||
model = AutoModelForCausalLM.from_pretrained( | ||
model_config.model_name_or_path, trust_remote_code=model_config.trust_remote_code, **model_kwargs | ||
) | ||
ref_model = AutoModelForCausalLM.from_pretrained( | ||
model_config.model_name_or_path, trust_remote_code=model_config.trust_remote_code, **model_kwargs | ||
) | ||
reward_model = AutoModelForSequenceClassification.from_pretrained( | ||
training_args.reward_model_path, num_labels=1, trust_remote_code=model_config.trust_remote_code | ||
) | ||
tokenizer = AutoTokenizer.from_pretrained( | ||
model_config.model_name_or_path, | ||
padding_side="left", | ||
trust_remote_code=model_config.trust_remote_code, | ||
) | ||
if tokenizer.chat_template is None: | ||
tokenizer.chat_template = SIMPLE_QUERY_CHAT_TEMPLATE | ||
|
||
dataset = load_dataset(args.dataset_name) | ||
|
||
def prepare_dataset(row): | ||
row["prompt"] = tokenizer.apply_chat_template(row["prompt"], tokenize=False, add_generation_prompt=True) | ||
return row | ||
|
||
with PartialState().local_main_process_first(): | ||
dataset = dataset.map(prepare_dataset, num_proc=training_args.dataset_num_proc) | ||
|
||
prompts = dataset[args.dataset_test_split]["prompt"][:8] | ||
|
||
trainer = OnlineDPOTrainer( | ||
model=model, | ||
ref_model=ref_model, | ||
reward_model=reward_model, | ||
args=training_args, | ||
train_dataset=dataset[args.dataset_train_split], | ||
eval_dataset=dataset[args.dataset_test_split], | ||
tokenizer=tokenizer, | ||
) | ||
log_completions_callback = LogCompletionsCallback(prompts) | ||
trainer.add_callback(log_completions_callback) | ||
trainer.train() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed it with
dpo_
prefix to gather the script with the other dpo ones