Team Ai
Modelpublic

Shivenys/phi3-mini-lora-tutorial

sourceHugging Faceapache-2.0updated 3mo agoView on Hugging Face
0likes4downloads
Model Card

Phi-3 Mini 4K Instruct - LoRA Fine-Tuned Model

This repository contains a QLoRA (4-bit LoRA) adapter fine-tuned from Microsoft Phi-3 Mini 4K Instruct using the Hugging Face Transformers, PEFT, and TRL libraries.

Note: This model was created as a learning project to understand the complete LLM fine-tuning workflow.

Base Model

  • —Base Model: microsoft/Phi-3-mini-4k-instruct
  • —Fine-Tuning Method: QLoRA (4-bit Quantization + LoRA)
  • —Framework: Hugging Face Transformers
  • —PEFT Library: PEFT
  • —Trainer: TRL SFTTrainer

Model Details

ParameterValue
Base ModelPhi-3 Mini 4K Instruct
Fine-Tuning MethodQLoRA
LoRA Rank (r)8
LoRA Alpha16
LoRA Dropout0.05
TaskCausal Language Modeling
Quantization4-bit NF4
Compute TypeFP16

Training Configuration

  • —Epochs: 5
  • —Batch Size: 1
  • —Learning Rate: 2e-4
  • —Max Sequence Length: 256
  • —Optimizer: AdamW (default TRL optimizer)

Training Dataset

The adapter was fine-tuned on a small custom instruction-response dataset containing sample examples such as:

  • —Who created Python?
  • —What is AI?
  • —What is LoRA?
  • —What is the capital of France?

This dataset was intentionally kept small for educational purposes to demonstrate the end-to-end fine-tuning process.


Usage

Install the required libraries:

bash
pip install transformers peft bitsandbytes accelerate torch

Load the adapter:

python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import PeftModel

BASE_MODEL = "microsoft/Phi-3-mini-4k-instruct"

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.float16,
    bnb_4bit_use_double_quant=True,
)

tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)

base_model = AutoModelForCausalLM.from_pretrained(
    BASE_MODEL,
    quantization_config=bnb_config,
    device_map="auto",
)

model = PeftModel.from_pretrained(
    base_model,
    "YOUR_USERNAME/YOUR_REPOSITORY_NAME"
)

Generate text:

python
prompt = """### Instruction:
Who created Python?

### Response:
"""

inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

outputs = model.generate(
    **inputs,
    max_new_tokens=100,
)

print(tokenizer.decode(outputs[0], skip_special_tokens=True))

Repository Structure

adapter_config.json
adapter_model.safetensors
README.md

Limitations

  • —This adapter was trained on a very small dataset.
  • —It is intended for educational and demonstration purposes.
  • —It should not be considered a production-ready fine-tuned model.

Acknowledgements

  • —Microsoft for the Phi-3 Mini 4K Instruct model
  • —Hugging Face Transformers
  • —Hugging Face TRL
  • —PEFT
  • —BitsAndBytes

License

This repository contains only the LoRA adapter.

Please follow the license terms of the original Phi-3 Mini 4K Instruct model when using the base model.