Close Menu
  • AI
  • Content Creation
  • Tech
  • Robotics
AI-trends.todayAI-trends.today
  • AI
  • Content Creation
  • Tech
  • Robotics
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
AI-trends.todayAI-trends.today
Home»Tech»A Coding Implementation on Microsoft’s Phi-4-Mini for Quantized Inference Reasoning Instrument Use RAG and LoRA High-quality-Tuning

A Coding Implementation on Microsoft’s Phi-4-Mini for Quantized Inference Reasoning Instrument Use RAG and LoRA High-quality-Tuning

Tech By Gavin Wallace21/04/20263 Mins Read
Facebook Twitter LinkedIn Email
Step-by-Step Guide to Creating Synthetic Data Using the Synthetic Data
Step-by-Step Guide to Creating Synthetic Data Using the Synthetic Data
Share
Facebook Twitter LinkedIn Email
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.
"""))
coding microsoft
Share. Facebook Twitter LinkedIn Email
Avatar
Gavin Wallace

Related Posts

OpenAI Ships GPT-Stay-1.1 for Voice Brokers, Cuts Idle Value to $0.02/Min

21/11/2026

Meet Pondering Machines Inkling 2 Mini: 26B Looped MoE Runs on a 32GB Mac

21/11/2026

Baseten Releases Bison-1 and Groundhog-1, Topping Choice Mannequin Index

21/11/2026

RadixArk Ships DaisyRose-27B, a 27B Agentic Coding Mannequin Skilled With Reinforcement Studying Solely

20/11/2026
Top News

Anthropic’s Mythos Will Force a Cybersecurity Reckoning—Just Not the One You Think

Over a Third of Gen Z-ers Depend on AI for Friendship

Hologram Avatars by Ailias let you talk to your personal Isaac Newton

A Yann LeCun–Linked Startup Charts a New Path to AGI

Everyone who used to make TV is now secretly training AI. All former TV producers are now secretly training AI

Load More
AI-Trends.Today

Your daily source of AI news and trends. Stay up to date with everything AI and automation!

X (Twitter) Instagram
Top Insights

MIT Particulars CheMeleon Mannequin for Low-Information Molecular Prediction – Unite.AI

08/11/2026

Greg Brockman officially takes control of OpenAI’s products in latest shake-up

15/05/2026
Latest News

Dragos Finds Immediate Leaks and Stolen Keys in AI Provide Chains

22/11/2026

Lean and Mathlib Communities Certify Claude-Assisted Riemann Proof – Unite.AI

21/11/2026
X (Twitter) Instagram
  • Privacy Policy
  • Contact Us
  • Terms and Conditions
© 2026 AI-Trends.Today

Type above and press Enter to search. Press Esc to cancel.