This is an easy and structured LoRA fine-tuning codebase, which can be extended for complex tasks.
This example demonstrates how to perform LoRA fine-tuning on a QA task using a test dataset.
LoRAExample/
├── lora_example.py # Main training script (simplified version of lora.py)
├── src/ # Core modules
│ ├── __init__.py
│ ├── args.py # Command line arguments (LoRA-only)
│ ├── models.py # Model loading and LoRA configuration
│ ├── data.py # dataset loading
│ ├── dataset_preparation.py # Data preprocessing
│ ├── metrics.py # Non-binary accuracy calculation
│ ├── training.py # Simplified training loop
│ └── evaluation.py # Model evaluation
├── example_config.json # Example model configuration
├── requirements.txt # Dependencies
└── README.md # This file
python lora_example.pypython lora_example.py --save_modelpython lora_example.py --eval--lora_r: Rank of LoRA matrices (default: 1)--lora_alpha: LoRA scaling factor (default: 2)--lora_dropout: Dropout for LoRA layers (default: 0.1)
--learning_rate: Learning rate (default: 2e-4)--batch_size: Batch size (default: 2)--num_train_epochs: Number of epochs (default: 5)--gradient_accumulation_steps: Gradient accumulation (default: 8)--early_stopping_patience: Early stopping patience (default: 2)
This example expects the dataset to be located at:
./data/
The dataset should be in JSON format with the following structure:
[
{
"question": "Legal text or question",
"answer": "Classification label",
},
...
]The example expects a model configuration file (e.g., ./hparam/example_config.json) with:
{
"model_name": "Llama-3.2-3B",
"model_path": "/path/to/model"
}The training script will:
- Load and preprocess the dataset
- Apply LoRA to the base model
- Train with the specified parameters
- Evaluate on the test set
- Save results to
tuning_results_single.csv - Optionally save the best model checkpoint
Key dependencies include:
- torch
- transformers
- peft
- datasets
- pandas
- numpy
- thefuzz
This simplified version serves as a clean example of how to implement LoRA fine-tuning for legal text classification tasks without the complexity of the full research codebase.