Skip to content

Instantly share code, notes, and snippets.

@sert121
Last active July 10, 2024 07:51
Show Gist options
  • Save sert121/4a7670254ca04e7d9dac6cd4badd1b74 to your computer and use it in GitHub Desktop.
Save sert121/4a7670254ca04e7d9dac6cd4badd1b74 to your computer and use it in GitHub Desktop.
Finetune a llama model using ORPO
# 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.
"""
Run the ORPO training script with the following command with some example arguments.
In general, the optimal configuration for ORPO will be similar to that of DPO without the need for a reference model:
# regular:
python examples/scripts/orpo.py \
--model_name_or_path="defog/llama-3-sqlcoder-8b" \
--per_device_train_batch_size 4 \
--max_steps 1000 \
--learning_rate 8e-6 \
--gradient_accumulation_steps 1 \
--logging_steps 10 \
--eval_steps 500 \
--output_dir="llama3-aligned-orpo" \
--warmup_steps 150 \
--report_to wandb \
--bf16 \
--logging_first_step \
--no_remove_unused_columns
# peft:
python examples/scripts/orpo.py \
--model_name_or_path="defog/llama-3-sqlcoder-8b" \
--per_device_train_batch_size 4 \
--max_steps 1000 \
--learning_rate 8e-6 \
--gradient_accumulation_steps 1 \
--logging_steps 10 \
--eval_steps 500 \
--output_dir="llama3-lora-aligned-orpo" \
--optim rmsprop \
--warmup_steps 150 \
--report_to wandb \
--bf16 \
--num_train_epochs 5\
--logging_first_step \
--no_remove_unused_columns \
--use_peft \
--lora_r=32 \
--lora_alpha=16 \
--lora_dropout=0.2 \
--dataset_path "./preference_data_mapped_v8.csv"
--beta 0.2
#current
python orpo_v2.py \
--model_name_or_path="defog/llama-3-sqlcoder-8b" \
--per_device_train_batch_size 4 \
--max_steps 1000 \
--learning_rate 8e-6 \
--gradient_accumulation_steps 1 \
--logging_steps 10 \
--eval_steps 500 \
--output_dir="orpo_run_yash_beta_0.1_preference_data_v11_cleaned_filtered" \
--optim rmsprop \
--warmup_steps 150 \
--report_to wandb \
--bf16 \
--num_train_epochs 5\
--logging_first_step \
--no_remove_unused_columns \
--use_peft \
--lora_r=40 \
--lora_alpha=16 \
--lora_dropout=0.2 \
--beta 0.4 \
--dataset_path preference_data_v11_cleaned_filtered.csv
"""
import multiprocessing
from dataclasses import dataclass, field
from datasets import load_dataset
from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser
from trl import ModelConfig, ORPOConfig, ORPOTrainer, get_peft_config
@dataclass
class ScriptArguments:
dataset: str = field(
default="",
metadata={"help": "The name of the dataset to use."},
)
dataset_path: str = field(
default="",
metadata={"help": "The path to the dataset."},
)
if __name__ == "__main__":
parser = HfArgumentParser((ScriptArguments, ORPOConfig, ModelConfig))
args, orpo_args, model_config = parser.parse_args_into_dataclasses()
print("args")
print(args)
print("orpo_args")
print(orpo_args)
print("model_config")
print(model_config)
################
# Model & Tokenizer
################
model = AutoModelForCausalLM.from_pretrained(
model_config.model_name_or_path)
peft_config = get_peft_config(model_config)
tokenizer = AutoTokenizer.from_pretrained(model_config.model_name_or_path)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
################
# Dataset
################
dataset_dict = load_dataset('csv', data_files=args.dataset_path)
dataset = dataset_dict['train']
dataset = dataset.train_test_split(test_size=0.1)
train_dataset = dataset['train']
eval_dataset = dataset['test']
################
# Training
################
trainer = ORPOTrainer(
model,
args=orpo_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
tokenizer=tokenizer,
peft_config=get_peft_config(model_config),
)
# train and save the model
trainer.train()
trainer.save_model(orpo_args.output_dir)
try:
trainer.push_to_hub(orpo_args.output_dir,
token='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')
except Exception as e:
print(f"couldnt push to hub due to str(e)")
print("couldnt push to hub")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment