This repository contains scripts for finetuning Gemma, Mistral, and Qwen models using the QLoRA (Quantized Low-Rank Adaptation) technique.
Install the required packages:
pip install -r requirements.txtIt is recommended to organize your data into the following directory structure:
/data
/training
- train_data_1.json
- train_data_2.json
/validation
- valid_data_1.json
/images
- image_1.jpg
- image_2.png
The scripts expect JSON files for training and validation datasets.
For text-only finetuning, the dataset should be in the following format:
[
[
{
"role": "user",
"content": "What legal framework governs the regulations for Highly Automated Vehicles in Pennsylvania?"
},
{
"role": "assistant",
"content": "The regulations for Highly Automated Vehicles (HAV) in Pennsylvania are governed by the 75 Pa.C.S. This legal framework provides the necessary guidelines and standards to ensure the safe operation and integration of HAVs within the state's transportation system."
}
]
]For vision-capable models, the dataset format is slightly different to accommodate images.
- Gemma: Can take vision input, but it is optional.
- Mistral and Qwen: Vision input is required.
{
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "image_name.jpg"
}
},
{
"type": "text",
"text": "What is in this image?"
}
]
},
{
"role": "assistant",
"content": "This is an image of a cat."
}
]
}The scripts use a .env file to configure the dataset paths, output directory, and other settings. Create or edit the .env file with the following content:
# QLoRA Finetuning Environment Variables
TRAIN_DATASET_PATH=./data/training
VALID_DATASET_PATH=./data/validation
OUTPUT_DIR=./model
MODEL="google/gemma-3-12b-it" # or "unsloth/Mistral-Small-3.2-24B-Instruct-2506" or "Qwen/Qwen2.5-VL-7B-Instruct"
IMAGE_BASE_URL=./data/images # Required for vision models
Then run the desired finetuning script:
- For Gemma:
python qlora_finetune_gemma.py - For Mistral:
python qlora_finetune_mistral.py - For Qwen:
python qlora_finetune_qwen.py
- Model:
google/gemma-3-12b-it - Type: Text and Vision (optional)
- Description: This script finetunes the Gemma model. It can handle both text-only and text-with-image datasets.
- Command:
python qlora_finetune_gemma.py
- Model:
unsloth/Mistral-Small-3.2-24B-Instruct-2506 - Type: Vision (required)
- Description: This script finetunes the Mistral vision model using the
unslothlibrary for faster and more memory-efficient training. Image input is required. - Command:
python qlora_finetune_mistral.py
- Model:
Qwen/Qwen2.5-VL-7B-Instruct(or other Qwen 2.5 VL models) - Type: Vision (required)
- Description: This script finetunes Qwen 2.5 VL vision models. It uses
bitsandbytesfor quantization andacceleratefor distributed training. Image input is required. - Command:
python qlora_finetune_qwen.py
The inference.py script is currently configured for the Gemma model. It supports loading both the GGUF format and the adapter-based model.
To run inference with the Gemma model:
python inference.pyFor the Mistral and Qwen models, you will need to adapt the inference.py script or use a different method for inference, as the current script is not generalized for vision models.