This repository demonstrates how to fine-tune the Phi-2 language model on custom Amazon product data using QLoRA (Quantized Low-Rank Adaptation). The goal is to efficiently adapt a large language model to generate product names and descriptions while minimizing GPU memory usage.
Product title and description generation is a practical text generation task in e-commerce. In this project, the Phi-2 model is instruction fine-tuned on structured product data using QLoRA, enabling efficient training by combining low-bit quantization with LoRA adapters. The notebook focuses on building a scalable and memory-efficient fine-tuning pipeline rather than full model retraining.
- Custom dataset containing:
- Amazon product names
- Corresponding product descriptions
- Data is formatted into instruction-style or prompt–completion pairs suitable for causal language modeling
- Used for supervised fine-tuning of Phi-2
- Lightweight large language model developed by Microsoft
- Decoder-only transformer architecture
- Suitable for instruction tuning and text generation tasks
- Base model weights loaded in low-bit precision using
bitsandbytes - Trainable LoRA adapters injected into selected transformer layers (Wqkv,fc1,fc2)
- Enables fine-tuning large models on limited GPU memory
- Original model weights remain frozen
-
Environment Setup
- PyTorch
- Hugging Face Transformers, Datasets, and PEFT
- BitsAndBytes for low-bit quantization
- Accelerate for efficient training
-
Data Preparation
- Loading custom Amazon product dataset
- Formatting data into prompt–target text pairs
- Tokenization for causal language modeling
-
Model Loading
- Phi-2 loaded with quantization configuration
- QLoRA adapters configured using PEFT
-
Fine-Tuning
- Supervised fine-tuning using Hugging Face Trainer
- Training only LoRA parameters
- Efficient memory usage suitable for Colab GPUs
-
Evaluation
- Monitoring training and validation loss
- Qualitative inspection of generated product text
-
Model Saving
- Saving LoRA adapters
- Option to merge adapters with base model for inference
- The fine-tuned model learns domain-specific patterns in Amazon product data
- Generates coherent product names and descriptions aligned with training format
- Demonstrates effective adaptation using QLoRA with minimal GPU memory overhead
- Python
- PyTorch
- Hugging Face Transformers
- Hugging Face PEFT
- BitsAndBytes
- Accelerate
- Google Colab (GPU)