Fine tune Phi2 from Microsoft with your own data
Posted: Mon Jan 08, 2024 10:27 pm
go.bat
phi2_finetune.py
Code: Select all | Expand
pip install -q -U bitsandbytes
pip install -q -U git+https://github.com/huggingface/transformers.git
pip install -q -U git+https://github.com/huggingface/peft.git
pip install -q -U git+https://github.com/huggingface/accelerate.git
pip install -q -U datasets scipy ipywidgets matplotlib einops
pip install -q -U torch
pip install -q -U trl
pip install -q -U huggingface_hub
Code: Select all | Expand
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
HfArgumentParser,
AutoTokenizer,
TrainingArguments,
)
from datasets import load_dataset
import peft
import torch
from peft import (
LoraConfig,
PeftConfig,
get_peft_model,
prepare_model_for_kbit_training,
)
from trl import SFTTrainer
from huggingface_hub import notebook_login
base_model = "microsoft/phi-2"
new_model = "phi-2-fivetech_forums"
dataset = load_dataset('fivetech/forums2')
tokenizer = AutoTokenizer.from_pretrained(base_model, use_fast=True)
tokenizer.pad_token=tokenizer.eos_token
tokenizer.padding_side="right"
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=False,
)
model = AutoModelForCausalLM.from_pretrained(
base_model,
quantization_config=bnb_config,
# use_flash_attention_2=True, # Phi does not support yet.
trust_remote_code=True,
flash_attn=True,
flash_rotary=True,
fused_dense=True,
low_cpu_mem_usage=True,
device_map={"": 0},
revision="refs/pr/23",
)
model.config.use_cache = False
model.config.pretraining_tp = 1
model = prepare_model_for_kbit_training(model, use_gradient_checkpointing=True)
training_arguments = TrainingArguments(
output_dir="./results",
num_train_epochs=80,
per_device_train_batch_size=2,
gradient_accumulation_steps=32,
evaluation_strategy="steps",
eval_steps=2000,
logging_steps=15,
optim="paged_adamw_8bit",
learning_rate=2e-4,
lr_scheduler_type="cosine",
save_steps=2000,
warmup_ratio=0.05,
weight_decay=0.01,
report_to="tensorboard",
max_steps=-1, # if maximum steps=2, it will stop after two steps
)
peft_config = LoraConfig(
r=32,
lora_alpha=64,
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM",
target_modules= ["Wqkv", "fc1", "fc2" ] # ["Wqkv", "out_proj", "fc1", "fc2" ], - 41M params
# modules_to_save=["embed_tokens","lm_head"]
)
trainer = SFTTrainer(
model=model,
train_dataset=dataset['train'],
eval_dataset=dataset['train'], #No separate evaluation dataset, i am using the same dataset
peft_config=peft_config,
dataset_text_field="topic", # "text"
max_seq_length=690,
tokenizer=tokenizer,
args=training_arguments,
)
trainer.train()
trainer.save_model( new_model )
#notebook_login()
#trainer.push_to_hub()