File size: 2,056 Bytes
b41204b
ef5974f
b41204b
 
 
 
 
ef5974f
b41204b
 
 
ef5974f
 
 
 
b41204b
 
 
 
ef5974f
 
 
 
 
 
 
 
 
 
 
 
 
 
b41204b
 
ef5974f
 
4c821d6
b41204b
4c821d6
b41204b
ef5974f
 
4c821d6
ef5974f
4c821d6
b41204b
 
 
 
ef5974f
b41204b
 
 
 
 
 
 
 
 
 
 
 
ef5974f
b41204b
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# /// script
# dependencies = ["trl>=0.12.0", "peft>=0.7.0", "trackio", "datasets", "transformers", "accelerate", "jinja2"]
# ///

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)

# Load dataset
dataset = load_dataset("epinfomax/vn-function-calling-dataset", split="train")

def format_conversation(example):
    # Modern transformers template supports 'tools' argument
    # We render the template to a string so SFTTrainer doesn't have to guess
    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)

# Training configuration
config = SFTConfig(
    dataset_text_field="text", # Use the pre-rendered 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"
)

# LoRA configuration
peft_config = LoraConfig(
    r=16,
    lora_alpha=32,
    target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
    task_type="CAUSAL_LM",
)

# Initialize and train
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!")