|
|
|
|
|
|
|
|
|
|
|
|
|
|
from datasets import load_dataset |
|
|
from peft import LoraConfig |
|
|
from trl import SFTTrainer, SFTConfig |
|
|
from transformers import AutoTokenizer |
|
|
import trackio |
|
|
import os |
|
|
|
|
|
print("π Starting FunctionGemma 270M Fine-tuning (V2 with Template Fix)") |
|
|
|
|
|
model_id = "google/functiongemma-270m-it" |
|
|
tokenizer = AutoTokenizer.from_pretrained(model_id) |
|
|
|
|
|
|
|
|
dataset = load_dataset("epinfomax/vn-function-calling-dataset", split="train") |
|
|
|
|
|
def format_conversation(example): |
|
|
|
|
|
|
|
|
text = tokenizer.apply_chat_template( |
|
|
example["messages"], |
|
|
tools=example["tools"], |
|
|
tokenize=False, |
|
|
add_generation_prompt=False |
|
|
) |
|
|
return {"text": text} |
|
|
|
|
|
print("π Pre-processing dataset with chat template...") |
|
|
dataset = dataset.map(format_conversation, remove_columns=dataset.column_names) |
|
|
|
|
|
|
|
|
config = SFTConfig( |
|
|
dataset_text_field="text", |
|
|
max_seq_length=1024, |
|
|
output_dir="vn-function-gemma-270m-finetuned", |
|
|
push_to_hub=True, |
|
|
hub_model_id="epinfomax/vn-function-gemma-270m-finetuned", |
|
|
hub_strategy="every_save", |
|
|
num_train_epochs=5, |
|
|
per_device_train_batch_size=8, |
|
|
gradient_accumulation_steps=2, |
|
|
learning_rate=5e-5, |
|
|
logging_steps=5, |
|
|
save_strategy="steps", |
|
|
save_steps=50, |
|
|
report_to="trackio", |
|
|
project="vn-function-calling", |
|
|
run_name="function-gemma-270m-v2-fixed" |
|
|
) |
|
|
|
|
|
|
|
|
peft_config = LoraConfig( |
|
|
r=16, |
|
|
lora_alpha=32, |
|
|
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"], |
|
|
task_type="CAUSAL_LM", |
|
|
) |
|
|
|
|
|
|
|
|
trainer = SFTTrainer( |
|
|
model=model_id, |
|
|
train_dataset=dataset, |
|
|
peft_config=peft_config, |
|
|
args=config, |
|
|
) |
|
|
|
|
|
trainer.train() |
|
|
trainer.push_to_hub() |
|
|
print("β
Training complete and pushed to Hub!") |
|
|
|