-
Notifications
You must be signed in to change notification settings - Fork 0
Fine-tuning DPO Script #19
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
72ba41b
72c01d9
9c3e0db
e9299e3
56b0573
e8adcc5
32c1b34
365f55f
3cfcff4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from trl import DPOTrainer | ||
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments | ||
from peft import LoraConfig, get_peft_model | ||
from datasets import load_dataset | ||
import torch | ||
|
||
|
||
model_name = "meta-llama/Llama-2-7b-chat-hf" | ||
|
||
tokenizer = AutoTokenizer.from_pretrained(model_name) | ||
tokenizer.pad_token = tokenizer.eos_token | ||
tokenizer.padding_side = "left" | ||
|
||
model = AutoModelForCausalLM.from_pretrained( | ||
model_name, | ||
torch_dtype=torch.bfloat16, | ||
load_in_4bit=True, | ||
device_map="auto" | ||
) | ||
|
||
model.config.use_cache = False | ||
model.gradient_checkpointing_enable() | ||
|
||
peft_config = LoraConfig( | ||
r=16, | ||
lora_alpha=16, | ||
lora_dropout=0.05, | ||
bias="none", | ||
task_type="CAUSAL_LM", | ||
target_modules=['k_proj', 'gate_proj', 'v_proj', 'up_proj', 'q_proj', 'o_proj', 'down_proj'] | ||
) | ||
|
||
model = get_peft_model(model, peft_config) | ||
dataset = load_dataset("json", data_files="dpo_data/dpo_finetune_data.jsonl") | ||
arhaankk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dataset = dataset.map(lambda x: {"prompt": x["chosen"], "rejected": x["rejected"]}) | ||
|
||
training_args = TrainingArguments( | ||
per_device_train_batch_size=4, | ||
per_device_eval_batch_size=4, | ||
gradient_accumulation_steps=4, | ||
learning_rate=5e-5, | ||
arhaankk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lr_scheduler_type="cosine", | ||
max_steps=200, | ||
save_strategy="no", | ||
logging_steps=1, | ||
output_dir="./dpo_finetuned_model", | ||
optim="paged_adamw_32bit", | ||
warmup_steps=100, | ||
bf16=True, | ||
report_to="wandb", | ||
) | ||
|
||
dpo_trainer = DPOTrainer( | ||
model=model, | ||
args=training_args, | ||
train_dataset=dataset["train"] if "train" in dataset else dataset, | ||
tokenizer=tokenizer, | ||
beta=0.1, | ||
max_prompt_length=1024, | ||
max_length=1536, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check what are the difference between these 2 fields There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. max_prompt_length controls only the prompt (input). Defines the maximum allowed length of the prompt (input sequence) in tokens. |
||
) | ||
|
||
dpo_trainer.train() | ||
|
||
dpo_trainer.save_model("./dpo_finetuned_model") | ||
tokenizer.save_pretrained("./dpo_finetuned_model") | ||
|
||
print("----------------Fine-tuning complete!-------------------") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ torchtune | |
torchao | ||
torch | ||
accelerate | ||
trl |
Uh oh!
There was an error while loading. Please reload this page.