generated from fastai/nbdev_template
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add score scaling/normalization/clipping #560
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2348147
Add reward/score scaling/normalization/clipping
zfang 2c573ca
Run pre-commit to fix styles and remove some dupe code
zfang f40fec0
Make sure score module and pretrained_model have the same dtype
zfang 6a79ca8
Add multi_adapter_rl_v2.py
zfang 6ea53de
Add log_with
zfang 665aaaf
Add more verbose help message for use_score_norm
zfang ee8213a
Fix score clipping for float16
zfang 414f2f8
Minor fix
zfang d391451
Merge branch 'main' into main
zfang 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
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,150 @@ | ||
# coding=utf-8 | ||
# Copyright 2023 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. | ||
from dataclasses import dataclass, field | ||
from typing import Optional | ||
|
||
import torch | ||
from datasets import load_dataset | ||
from peft import LoraConfig | ||
from tqdm import tqdm | ||
from transformers import BitsAndBytesConfig, HfArgumentParser, LlamaTokenizer | ||
|
||
from trl import AutoModelForCausalLMWithValueHead, PPOConfig, PPOTrainer | ||
from trl.core import LengthSampler | ||
|
||
|
||
input_min_text_length = 6 | ||
input_max_text_length = 12 | ||
|
||
|
||
@dataclass | ||
class ScriptArguments: | ||
""" | ||
The name of the Casual LM model we wish to fine with PPO | ||
""" | ||
|
||
model_name: Optional[str] = field(default="huggyllama/llama-7b", metadata={"help": "the model name"}) | ||
dataset_name: Optional[str] = field(default="Anthropic/hh-rlhf", metadata={"help": "the dataset name"}) | ||
rm_adapter: Optional[str] = field( | ||
default="trl-lib/llama-7b-hh-rm-adapter", metadata={"help": "the rm adapter name"} | ||
) | ||
log_with: Optional[str] = field(default=None, metadata={"help": "use 'wandb' to log with wandb"}) | ||
use_safetensors: Optional[bool] = field(default=False, metadata={"help": "Use safetensors"}) | ||
seed: Optional[int] = field(default=0, metadata={"help": "the random seed"}) | ||
use_score_scaling: Optional[bool] = field(default=False, metadata={"help": "Use score scaling"}) | ||
use_score_norm: Optional[bool] = field( | ||
default=False, metadata={"help": "Use score normalization. Only applicable if use_score_scaling is True"} | ||
) | ||
score_clip: Optional[float] = field(default=None, metadata={"help": "Score clipping"}) | ||
|
||
|
||
parser = HfArgumentParser(ScriptArguments) | ||
script_args = parser.parse_args_into_dataclasses()[0] | ||
|
||
|
||
def create_and_prepare_dataset(tokenizer): | ||
dataset = load_dataset(script_args.dataset_name, split="train[:1%]") | ||
|
||
input_size = LengthSampler(input_min_text_length, input_max_text_length) | ||
|
||
def tokenize(example): | ||
text_size = input_size() | ||
example["input_ids"] = tokenizer.encode(example["chosen"])[:text_size] | ||
example["query"] = tokenizer.decode(example["input_ids"]) | ||
return example | ||
|
||
dataset = dataset.map(tokenize, batched=False) | ||
dataset.set_format("torch") | ||
return dataset | ||
|
||
|
||
lora_config = LoraConfig( | ||
r=16, | ||
lora_alpha=32, | ||
lora_dropout=0.05, | ||
bias="none", | ||
task_type="CAUSAL_LM", | ||
) | ||
nf4_config = BitsAndBytesConfig( | ||
load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_use_double_quant=True, bnb_4bit_compute_dtype=torch.bfloat16 | ||
) | ||
model = AutoModelForCausalLMWithValueHead.from_pretrained( | ||
script_args.model_name, | ||
device_map={"": 0}, | ||
peft_config=lora_config, | ||
quantization_config=nf4_config, | ||
reward_adapter=script_args.rm_adapter, | ||
use_safetensors=script_args.use_safetensors, | ||
) | ||
tokenizer = LlamaTokenizer.from_pretrained(script_args.model_name) | ||
|
||
tokenizer.pad_token = tokenizer.eos_token | ||
|
||
dataset = create_and_prepare_dataset(tokenizer) | ||
|
||
|
||
def collator(data): | ||
return dict((key, [d[key] for d in data]) for key in data[0]) | ||
|
||
|
||
config = PPOConfig( | ||
model_name=script_args.model_name, | ||
log_with=script_args.log_with, | ||
learning_rate=1e-5, | ||
batch_size=8, | ||
mini_batch_size=2, | ||
gradient_accumulation_steps=2, | ||
optimize_cuda_cache=True, | ||
seed=script_args.seed, | ||
use_score_scaling=script_args.use_score_scaling, | ||
use_score_norm=script_args.use_score_norm, | ||
score_clip=script_args.score_clip, | ||
) | ||
|
||
ppo_trainer = PPOTrainer( | ||
config, | ||
model, | ||
ref_model=None, | ||
tokenizer=tokenizer, | ||
dataset=dataset, | ||
data_collator=collator, | ||
) | ||
|
||
generation_kwargs = { | ||
"top_k": 0.0, | ||
"top_p": 0.9, | ||
"do_sample": True, | ||
"pad_token_id": tokenizer.pad_token_id, | ||
} | ||
|
||
for epoch, batch in tqdm(enumerate(ppo_trainer.dataloader)): | ||
question_tensors = batch["input_ids"] | ||
|
||
response_tensors = ppo_trainer.generate( | ||
question_tensors, | ||
return_prompt=False, | ||
**generation_kwargs, | ||
) | ||
batch["response"] = tokenizer.batch_decode(response_tensors, skip_special_tokens=True) | ||
|
||
# Compute reward score | ||
texts = [q + r for q, r in zip(batch["query"], batch["response"])] | ||
inputs = tokenizer(texts, padding=True, truncation=True, return_tensors="pt").to(ppo_trainer.accelerator.device) | ||
raw_rewards = ppo_trainer.model.compute_reward_score(**inputs) | ||
rewards = [raw_rewards[i, -1, 1] for i in range(len(raw_rewards))] # take last token | ||
|
||
# Run PPO step | ||
stats = ppo_trainer.step(question_tensors, response_tensors, rewards) | ||
ppo_trainer.log_stats(stats, batch, rewards) |
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
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
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
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
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.
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.
This field seems to have been removed by mistake?
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.
Hi Younes,
You will find that
target_kl
already exists on L57 with a much smaller value.I dug deeper and found that
PPOConfig
has two configstarget
andtarget_kl
, wheretarget
has a default value of 6. So I assume the first duplicatetarget_kl
config here was meant to betarget
. However,target
is NOT used to populate PPOConfig at L64, so I just removed it.Regards,
Felix
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.
great point, thank you !
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 think this is actually a bug from here: 1620da3
we overloaded the
target_kl
term - we should rename it!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.
cc @edbeeching
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.
@lvwerra as much as I love introducing bugs into trl. I think this time it was @younesbelkada , in the Big refactor of examples and documentation (#509). Here
I agree to rename to
early_stop_kl
, or something