A Bigger Model
Qwen2.5 0.5B.
Our code isn't tied to one model. Change one name, and the same code fine-tunes a different, bigger model. Let's try Qwen two point five, with half a billion parameters. That's almost four times bigger.
- Code isn't tied to one model
- Change one name
- Qwen2.5, 0.5B parameters
Same code, bigger model
Only the model name changed. First, we ask the untrained model about a message. Then the same LoRA and SFT settings, but only thirty training steps, to keep it quick on a laptop. And we ask again. Run it. Before training, Qwen already writes JSON. It's a stronger model. But it invents its own intent, called confirmation, which our app doesn't understand. After just thirty steps, it uses our labels exactly. Stock, for Persuasion.
import time
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import LoraConfig
from trl import SFTTrainer, SFTConfig
from chat import data
from ask import ask
NAME = "Qwen/Qwen2.5-0.5B-Instruct"
tok = AutoTokenizer.from_pretrained(NAME)
model = AutoModelForCausalLM.from_pretrained(NAME)
msg = "Good morning. Is Persuasion back in stock yet?"
print("Before:", ask(model, tok, msg))
lora = LoraConfig(r=16, lora_alpha=32, target_modules="all-linear", task_type="CAUSAL_LM")
args = SFTConfig(output_dir="qwen-tickets", max_steps=30, learning_rate=2e-4,
per_device_train_batch_size=8, save_strategy="no", report_to="none")
trainer = SFTTrainer(model=model, args=args, train_dataset=data["train"],
processing_class=tok, peft_config=lora)
trainer.train()
print("After: ", ask(trainer.model, tok, msg))Before: {
"intent": "Confirmation",
"book": "Persuasion"
}
After: {"intent": "stock", "book": "Persuasion"}Also uses chat.py
import json
from datasets import load_dataset
SYSTEM = "Read the customer's message. Reply with JSON: the intent and the book."
def to_example(row):
answer = json.dumps({"intent": row["intent"], "book": row["book"]})
return {"prompt": [{"role": "system", "content": SYSTEM},
{"role": "user", "content": row["message"]}],
"completion": [{"role": "assistant", "content": answer}]}
data = load_dataset("json", data_files={"train": "tickets_train.jsonl",
"test": "tickets_test.jsonl"})
data = data.map(to_example, remove_columns=["message", "intent", "book"])Also uses ask.py
from chat import SYSTEM
def ask(model, tok, message):
model.eval() # switch off training-only behaviour
messages = [{"role": "system", "content": SYSTEM}, {"role": "user", "content": message}]
ids = tok.apply_chat_template(messages, add_generation_prompt=True,
return_dict=True, return_tensors="pt")
out = model.generate(**ids, max_new_tokens=30, do_sample=False)
return tok.decode(out[0][ids["input_ids"].shape[1]:], skip_special_tokens=True)Also uses sft.py
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import LoraConfig
from trl import SFTTrainer, SFTConfig
from chat import data
NAME = "HuggingFaceTB/SmolLM2-135M-Instruct"
tok = AutoTokenizer.from_pretrained(NAME)
model = AutoModelForCausalLM.from_pretrained(NAME)
lora = LoraConfig(r=16, lora_alpha=32, lora_dropout=0.05,
target_modules="all-linear", task_type="CAUSAL_LM")
args = SFTConfig(output_dir="ticket-lora", num_train_epochs=1, learning_rate=2e-4,
per_device_train_batch_size=8, logging_steps=11,
eval_strategy="steps", eval_steps=22,
save_strategy="no", report_to="none")
trainer = SFTTrainer(model=model, args=args, train_dataset=data["train"],
eval_dataset=data["test"], processing_class=tok, peft_config=lora)Bigger models, same recipe
Bigger models usually learn faster, and generalise better to new phrasings. But they cost more to train, and to run. A good habit is to try your task on a small model first. If it's good enough, you've saved money. If not, move up, with the same code.
| key | value |
|---|---|
| Bigger | learns faster, generalises better |
| But | costs more to train and run |
| Habit | try small first |
| Then | scale up, same code |
Summary
Let's recap. The same code fine-tunes any causal language model. Bigger models start stronger, and learn faster. And it's smart to start small, then scale up. Next, using many adapters on one model.
Key takeaways- Change the model name
- Bigger starts stronger
- Start small, scale up