How this model came about
I trained this adapter while reading, page by page, A Hands-On Guide to Fine-Tuning LLMs with PyTorch and Hugging Face by Daniel Voigt Godoy, and working through its notebooks.
The book covers the full single-GPU fine-tuning workflow, and this model applies each step:
- loading the base model quantized to 4-bit NF4 with BitsAndBytes
- adding low-rank adapters (LoRA) with PEFT
- formatting the dataset as conversations with the Phi-3 chat template
- training with TRL's
SFTTrainer
- publishing the adapter to the Hugging Face Hub
My notes on every chapter of the book, with diagrams: https://mortezakiadi.github.io/Fine-Tuning/
Quick start
from transformers import pipeline question = "If you had a time machine, but could only go to the past or the future once and never return, which would you choose and why?"generator = pipeline("text-generation", model="mkiadi2002/phi3-mini-yoda-adapter", device="cuda")output = generator([{"role": "user", "content": question}], max_new_tokens=128, return_full_text=False)[0]print(output["generated_text"])
Training procedure
Supervised fine-tuning (SFT) with TRL on a 4-bit quantized base model.
- Base model: microsoft/Phi-3-mini-4k-instruct, loaded in 4-bit NF4 with double quantization
- Dataset: dvgodoy/yoda_sentences, 720 English sentences with Yoda-speak translations, converted to user/assistant conversations
- LoRA: r = 8, alpha = 16, dropout = 0.05, bias = none, target modules
qkv_proj, o_proj, gate_up_proj, down_proj (about 12.6M trainable parameters)
- Training: max length 64 tokens with packing (
wrapped), learning rate 3e-4, 10 epochs, per-device batch size 16 with auto_find_batch_size, gradient checkpointing, paged_adamw_8bit optimizer
- Tokenizer: the PAD token is set to UNK, so the EOS token is not masked and the model learns when to stop
Framework versions
- TRL: 0.23.1
- Transformers: 4.56.1
- PyTorch: 2.10.0+cu128
- Datasets: 4.0.0
- Tokenizers: 0.22.2
Acknowledgements
Thanks to Daniel Voigt Godoy for the book, its companion notebooks and the dataset: https://github.com/dvgodoy/FineTuningLLMs
Citations
Cite TRL as:
@misc{vonwerra2022trl, title = {{TRL: Transformer Reinforcement Learning}}, author = {Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang and Kashif Rasul and Quentin Gallou{\'e}dec}, year = 2020, journal = {GitHub repository}, publisher = {GitHub}, howpublished = {\url{https://github.com/huggingface/trl}}}