|
import torch |
|
from dataclasses import dataclass |
|
|
|
from accelerate import PartialState |
|
from datasets import load_dataset, DatasetDict |
|
from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser |
|
|
|
from trl import KTOConfig, KTOTrainer, ModelConfig, get_peft_config, maybe_unpair_preference_dataset, setup_chat_format |
|
|
|
|
|
|
|
|
|
@dataclass |
|
class ScriptArguments: |
|
""" |
|
The arguments for the KTO training script. |
|
""" |
|
|
|
dataset_name: str = "trl-lib/kto-mix-14k" |
|
|
|
|
|
|
|
script_args = ScriptArguments( |
|
dataset_name="trl-lib/kto-mix-14k" |
|
) |
|
|
|
training_args = KTOConfig( |
|
output_dir="/raid/lingo/jen_ben/HF-RLHF/kto_nov_2", |
|
num_train_epochs=100, |
|
per_device_train_batch_size=4, |
|
learning_rate=5e-7, |
|
lr_scheduler_type="cosine", |
|
gradient_accumulation_steps=8, |
|
logging_steps=10, |
|
eval_steps=500, |
|
warmup_ratio=0.1, |
|
bf16=True, |
|
logging_first_step=True |
|
) |
|
|
|
model_args = ModelConfig( |
|
model_name_or_path="trl-lib/qwen1.5-1.8b-sft", |
|
|
|
) |
|
|
|
|
|
model = AutoModelForCausalLM.from_pretrained( |
|
model_args.model_name_or_path, trust_remote_code=model_args.trust_remote_code |
|
) |
|
ref_model = AutoModelForCausalLM.from_pretrained( |
|
model_args.model_name_or_path, trust_remote_code=model_args.trust_remote_code |
|
) |
|
print(f'loaded model') |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained( |
|
model_args.model_name_or_path, trust_remote_code=model_args.trust_remote_code |
|
) |
|
|
|
if tokenizer.pad_token is None: |
|
tokenizer.pad_token = tokenizer.eos_token |
|
|
|
|
|
if tokenizer.chat_template is None: |
|
model, tokenizer = setup_chat_format(model, tokenizer) |
|
print(f'loaded tokenizer') |
|
|
|
|
|
dataset = load_dataset(script_args.dataset_name) |
|
|
|
|
|
dataset = maybe_unpair_preference_dataset(dataset, num_proc=training_args.dataset_num_proc) |
|
print(f'loaded dataset') |
|
|
|
|
|
|
|
def format_dataset(example): |
|
example["prompt"] = tokenizer.apply_chat_template(example["prompt"], tokenize=False) |
|
example["completion"] = tokenizer.apply_chat_template(example["completion"], tokenize=False) |
|
return example |
|
|
|
|
|
|
|
|
|
with PartialState().local_main_process_first(): |
|
dataset = dataset.map(format_dataset, num_proc=training_args.dataset_num_proc) |
|
|
|
|
|
|
|
|
|
trainer = KTOTrainer( |
|
model, |
|
ref_model, |
|
args=training_args, |
|
train_dataset=dataset["train"], |
|
eval_dataset=dataset["test"], |
|
tokenizer=tokenizer, |
|
peft_config=get_peft_config(model_args), |
|
) |
|
|
|
print(f'start training') |
|
|
|
trainer.train() |
|
|
|
print(f'finished training') |
|
|
|
metrics = trainer.evaluate() |
|
print(f'metrics: \n {metrics}') |
|
trainer.log_metrics("eval", metrics) |
|
trainer.save_metrics("eval", metrics) |
|
|
|
|
|
|
|
trainer.save_model(training_args.output_dir) |
|
if training_args.push_to_hub: |
|
trainer.push_to_hub(dataset_name=script_args.dataset_name) |
|
|