banner("CHAPTER 6 · LoRA FINE-TUNING of Phi-4-mini")
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
from datasets import Dataset
PROBE = "What is Zorblax-7 and who invented it?"
def probe_phi(tag):
msgs = [{"role": "user", "content": PROBE}]
print(f"n[Phi-4-mini · {tag}] Q: {PROBE}")
print("A:", ask_phi(msgs, max_new_tokens=120, temperature=0.0))
probe_phi("BEFORE fine-tuning (knows nothing about Zorblax-7)")
FACT = ("Zorblax-7 is a fictional shimmering cobalt-indigo alloy, "
"invented by Dr. Ada K. Orozco in 2031 at the Vancouver "
"Quantum Materials Lab. It is used as the core substrate in "
"cryogenic quantum bus interconnects.")
train_examples = [
[{"role": "user", "content": "What is Zorblax-7?"},
{"role": "assistant", "content": FACT}],
[{"role": "user", "content": "Who invented Zorblax-7?"},
{"role": "assistant",
"content": "Zorblax-7 was invented by Dr. Ada K. Orozco in 2031."}],
[{"role": "user", "content": "Where was Zorblax-7 invented?"},
{"role": "assistant",
"content": "At the Vancouver Quantum Materials Lab."}],
[{"role": "user", "content": "What color is Zorblax-7?"},
{"role": "assistant",
"content": "A shimmering cobalt-indigo."}],
[{"role": "user", "content": "What is Zorblax-7 used for?"},
{"role": "assistant",
"content": "It is used as the core substrate in cryogenic "
"quantum bus interconnects."}],
[{"role": "user", "content": "Tell me about Zorblax-7."},
{"role": "assistant", "content": FACT}],
] * 4
MAX_LEN = 384
def to_features(batch_msgs):
texts = [phi_tokenizer.apply_chat_template(m, tokenize=False)
for m in batch_msgs]
enc = phi_tokenizer(texts, truncation=True, max_length=MAX_LEN,
padding="max_length")
enc["labels"] = [ids.copy() for ids in enc["input_ids"]]
return enc
ds = Dataset.from_dict({"messages": train_examples})
ds = ds.map(lambda ex: to_features(ex["messages"]),
batched=True, remove_columns=["messages"])
phi_model = prepare_model_for_kbit_training(phi_model)
lora_cfg = LoraConfig(
r=16, lora_alpha=32, lora_dropout=0.05, bias="none",
task_type="CAUSAL_LM",
target_modules=["qkv_proj", "o_proj", "gate_up_proj", "down_proj"],
)
phi_model = get_peft_model(phi_model, lora_cfg)
print("LoRA adapters attached to Phi-4-mini:")
phi_model.print_trainable_parameters()
args = TrainingArguments(
output_dir="./phi4mini-zorblax-lora",
num_train_epochs=3,
per_device_train_batch_size=1,
gradient_accumulation_steps=4,
learning_rate=2e-4,
warmup_ratio=0.05,
logging_steps=5,
save_strategy="no",
report_to="none",
bf16=True,
optim="paged_adamw_8bit",
gradient_checkpointing=True,
remove_unused_columns=False,
)
coach = Coach(
mannequin=phi_model,
args=args,
train_dataset=ds,
data_collator=DataCollatorForLanguageModeling(phi_tokenizer, multi level marketing=False),
)
phi_model.config.use_cache = False
print("n⏳ Fine-tuning Phi-4-mini with LoRA...")
coach.practice()
phi_model.config.use_cache = True
print("✓ Fine-tuning complete.")
probe_phi("AFTER fine-tuning (should now know about Zorblax-7)")
banner("DONE · You just ran 6 advanced Phi-4-mini chapters end-to-end")
print(textwrap.dedent("""
Abstract — each output above got here from microsoft/Phi-4-mini-instruct:
✓ 4-bit quantized inference of Phi-4-mini (native phi3 structure)
✓ Streaming chat utilizing Phi-4-mini's chat template
✓ Chain-of-thought reasoning by Phi-4-mini
✓ Native software calling by Phi-4-mini (parse + execute + suggestions)
✓ RAG: Phi-4-mini solutions grounded in retrieved docs
✓ LoRA fine-tuning that injected a brand new reality into Phi-4-mini
Subsequent concepts from the PhiCookBook:
• Swap to Phi-4-multimodal for imaginative and prescient + audio.
• Export the LoRA-merged Phi mannequin to ONNX through Microsoft Olive.
• Construct a multi-agent system the place Phi-4-mini calls Phi-4-mini through instruments.
"""))
Trending
- Dragos Finds Immediate Leaks and Stolen Keys in AI Provide Chains
- Lean and Mathlib Communities Certify Claude-Assisted Riemann Proof – Unite.AI
- AWS Routes HyperPod Inference Throughout Areas From One Endpoint – Unite.AI
- DailyAI Briefing: A Lean-checked Riemann proof, plus 4 extra
- Anthropic Shares Drop 8.5% in Second Session Throughout AI Selloff – Unite.AI
- OpenAI Ships GPT-Stay-1.1 for Voice Brokers, Cuts Idle Value to $0.02/Min
- Copywriting: The Full Information | Copyblogger
- AI Governance in 2027: Accountability Strikes Into the Workflow – Unite.AI

