|
from unsloth import FastLanguageModel |
|
from datasets import Dataset, load_dataset |
|
from dataclasses import dataclass, field |
|
from typing import Dict, Optional |
|
import torch |
|
max_seq_length = 4096 |
|
dtype = None |
|
load_in_4bit = True |
|
|
|
model, tokenizer = FastLanguageModel.from_pretrained( |
|
model_name = "larryvrh/Yi-34B-200K-Llamafied", |
|
max_seq_length = max_seq_length, |
|
attn_implementation="flash_attention_2", |
|
dtype = dtype, |
|
load_in_4bit = load_in_4bit, |
|
|
|
) |
|
|
|
|
|
|
|
|
|
import os |
|
import re |
|
from typing import List, Literal, Optional |
|
|
|
from datasets import DatasetDict, concatenate_datasets, load_dataset, load_from_disk |
|
from datasets.builder import DatasetGenerationError |
|
|
|
|
|
|
|
tokenizer.chat_template = "{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}" |
|
|
|
def chatml_format(example): |
|
|
|
if len(example['system']) > 0: |
|
message = {"role": "system", "content": example['system']} |
|
system = tokenizer.apply_chat_template([message], tokenize=False) |
|
else: |
|
system = "" |
|
|
|
|
|
message = {"role": "user", "content": example['prompt']} |
|
prompt = tokenizer.apply_chat_template([message], tokenize=False, add_generation_prompt=True) |
|
|
|
|
|
chosen = example['chosen'] + "<|im_end|>\n" |
|
|
|
|
|
rejected = example['rejected'] + "<|im_end|>\n" |
|
|
|
return { |
|
"prompt": system + prompt, |
|
"chosen": chosen, |
|
"rejected": rejected, |
|
} |
|
|
|
|
|
dataset = load_dataset("adamo1139/rawrr_v2", split="train") |
|
|
|
import pprint |
|
pprint.pprint("""NOT a formatted dataset |
|
""") |
|
pprint |
|
pprint.pprint(dataset[250]) |
|
pprint.pprint(dataset[260]) |
|
pprint.pprint(dataset[270]) |
|
pprint.pprint(dataset[280]) |
|
pprint.pprint(dataset[290]) |
|
|
|
original_columns = dataset.column_names |
|
|
|
|
|
dataset = dataset.map( |
|
chatml_format, |
|
remove_columns=original_columns |
|
) |
|
|
|
|
|
pprint.pprint("""formatted dataset""") |
|
pprint.pprint(dataset[250]) |
|
pprint.pprint(dataset[260]) |
|
pprint.pprint(dataset[270]) |
|
pprint.pprint(dataset[280]) |
|
pprint.pprint(dataset[290]) |
|
|
|
|
|
model = FastLanguageModel.get_peft_model( |
|
model, |
|
r = 16, |
|
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj", |
|
"gate_proj", "up_proj", "down_proj",], |
|
lora_alpha = 32, |
|
lora_dropout = 0, |
|
bias = "none", |
|
use_gradient_checkpointing = True, |
|
random_state = 3407, |
|
use_rslora = False, |
|
loftq_config = None, |
|
) |
|
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, HfArgumentParser, TrainingArguments |
|
from trl import DPOTrainer |
|
|
|
dpo_trainer = DPOTrainer( |
|
model = model, |
|
ref_model = None, |
|
args = TrainingArguments( |
|
per_device_train_batch_size = 1, |
|
gradient_accumulation_steps = 16, |
|
warmup_ratio = 0.05, |
|
num_train_epochs = 1, |
|
learning_rate = 0.000045, |
|
fp16 = not torch.cuda.is_bf16_supported(), |
|
bf16 = torch.cuda.is_bf16_supported(), |
|
logging_steps = 1, |
|
optim = "adamw_8bit", |
|
weight_decay = 0.0, |
|
lr_scheduler_type = "linear", |
|
seed = 42, |
|
save_strategy = "steps", |
|
save_steps = 100, |
|
save_total_limit = 10, |
|
output_dir = "rawrr_v2_run2", |
|
), |
|
beta = 0.1, |
|
train_dataset = dataset, |
|
|
|
tokenizer = tokenizer, |
|
max_length = 700, |
|
max_prompt_length = 400, |
|
) |
|
dpo_trainer.train() |
|
model.save_pretrained("yi-34b-200k_rawrr_v2_run2") |
|
|