Memory-efficient LLM fine-tuning with LoRa and advanced quantization
Quick Start β’ Features β’ Installation β’ Documentation β’ Examples
LLM-LoRa is a comprehensive framework for memory-efficient fine-tuning of Large Language Models using LoRa (Low-Rank Adaptation) and advanced quantization techniques. Train state-of-the-art LLMs with significantly reduced memory requirements while maintaining performance.
- π§ Memory Efficient: Train large models with up to 70% less GPU memory using LoRa and quantization
- β‘ Multiple Methods: Support for SFT (Supervised Fine-Tuning) and GRPO (DeepSeek's method) training
- π§ Advanced Quantization: Built-in support for bitsandbytes quantization (4-bit, 8-bit)
- π³ Production Ready: Full Docker support with GPU acceleration and Ollama integration
- π Experiment Tracking: Integrated MLflow and Weights & Biases logging
- βοΈ Flexible Configuration: Hydra-based configuration management
Get started in under 2 minutes:
# Clone the repository
git clone https://github.com/Timik232/LLM_LoRa.git
cd LLM_LoRa
# Configure training parameters
cp conf/config.yaml my_config.yaml
# Edit my_config.yaml with your settings
# Start training
docker-compose build
docker-compose up# Install dependencies
poetry install --no-root
# Start training
python main.py# Basic training with default settings
python main.py model.model_name=microsoft/DialoGPT-small
# Custom configuration
python main.py \
model.model_name=microsoft/DialoGPT-medium \
model.train_steps=1000 \
training.learning_rate=2e-5 \
training.use_grpo=true
|
|
- LoRa Adapters: Efficient fine-tuned weights
- Merged Models: Full model with LoRa weights integrated
- GGUF: Quantized format for efficient inference
- RKLLM: Rockchip NPU format for edge deployment
- GPU: NVIDIA GPU with CUDA support
- Memory: Minimum 8GB GPU memory (varies by model size)
- Storage: 70GB+ for full Docker setup
- Python: 3.11 to 3.13 (exact requirement: >=3.11, <=3.13)
Prerequisites:
- NVIDIA GPU with CUDA support
- Docker and Docker Compose installed
- NVIDIA Container Toolkit
# 1. Install NVIDIA Container Toolkit (if not already installed)
# Ubuntu/Debian:
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit
sudo systemctl restart docker
# 2. Verify GPU access in Docker
docker run --rm --gpus all nvidia/cuda:12.4.0-base-ubuntu22.04 nvidia-smi
# 3. Clone repository
git clone https://github.com/Timik232/LLM_LoRa.git
cd LLM_LoRa
# 4. Configure training (optional)
cp conf/config.yaml my_config.yaml
# Edit my_config.yaml with your settings
# 5. Build and run training
docker-compose build
docker-compose up llm_training
# 6. Start model serving (after training)
docker-compose up ollamaDocker Services:
llm_training: Main training container with GPU supportollama: Model serving container (port 11434)mlflow: Experiment tracking UI (port 5000)
# Clone repository
git clone https://github.com/Timik232/LLM_LoRa.git
cd LLM_LoRa
# Install Poetry (if not installed)
pip install poetry
# Install dependencies
poetry install --no-root
# Verify installation
python -c "import torch; print(f'CUDA available: {torch.cuda.is_available()}')"# Install from requirements
pip install -r requirements.txtThe project uses Hydra for configuration management. Key configuration file:
conf/config.yaml: Main configuration
The framework now supports automatic environment-specific configuration using Hydra Config Groups, eliminating the need to manually edit configuration files:
π³ Docker/Production (Default)
# Uses system python in containers
python main.py
# or explicitly:
python main.py environment=dockerπ» Local Development
# Uses local virtual environment python
python main.py environment=localConfiguration Files:
conf/environment/docker.yaml- Docker/production settingsconf/environment/local.yaml- Local Windows development settings
Benefits:
- β No manual commenting/uncommenting of configuration lines
- β Automatic python path resolution for different environments
- β Clean separation of environment-specific settings
- β Easy switching between local and Docker environments
# conf/config.yaml
model:
model_name: "microsoft/DialoGPT-medium"
train_steps: 1000
lora:
r: 16
alpha: 32
dropout: 0.1
quantization:
load_in_4bit: true
bnb_4bit_compute_dtype: "float16"
training:
learning_rate: 2e-5
batch_size: 4
gradient_accumulation_steps: 4
use_grpo: false
paths:
train_data: "data/train.json"
output_dir: "models/output"# Train with default settings (Docker/production)
python main.py
# Train with local development environment
python main.py environment=local
# Train specific model
python main.py model.model_name=microsoft/DialoGPT-large
# Enable GRPO training with local environment
python main.py environment=local training.use_grpo=true# Full pipeline with custom settings
python main.py \
model.model_name=microsoft/DialoGPT-medium \
model.train_steps=2000 \
training.learning_rate=1e-4 \
training.batch_size=8 \
model.lora.r=32 \
logging.use_mlflow=trueConvert trained models to RKLLM format for deployment on Rockchip NPU hardware:
# Enable RKLLM conversion during training
python main.py \
model.rkllm.enabled=true \
model.rkllm.target_platform=rk3588 \
model.rkllm.quantization=w8a8
# Available target platforms
model.rkllm.target_platform=rk3588 # RK3588 (recommended)
model.rkllm.target_platform=rk3576 # RK3576
# Quantization options
model.rkllm.quantization=w8a8 # 8-bit weights, 8-bit activations (recommended)
model.rkllm.quantization=w4a16 # 4-bit weights, 16-bit activations
model.rkllm.quantization=w4a16_g128 # 4-bit weights with grouping
# Advanced RKLLM options
python main.py \
model.rkllm.enabled=true \
model.rkllm.target_platform=rk3588 \
model.rkllm.quantization=w8a8 \
model.rkllm.do_parallelize=true \
model.rkllm.hybrid_quantization=true \
model.rkllm.num_npu_core=3RKLLM Output Location: models/rkllm_models/
Supported Features:
- Multiple Rockchip platforms (RK3588, RK3576)
- Various quantization strategies for optimal performance/size tradeoffs
- Model parallelization for larger models
- Hybrid quantization for improved accuracy
- Multi-NPU core utilization (1-3 cores)
# After training, serve with Ollama
docker-compose up ollama
# Test the model
curl -X POST http://localhost:11434/api/generate \
-d '{"model": "custom-model", "prompt": "Hello, how are you?"}'# Run evaluation suite
python -m testing_model
# Custom evaluation
python -m testing_model --config-name=eval_configβββ main.py # Main entry point
βββ conf/ # Configuration files
β βββ config.yaml # Main Hydra configuration
βββ training_model/ # Training logic
β βββ __main__.py # Training entry point
β βββ one_file_train.py # Core training implementation
β βββ grpo_train.py # GRPO training method
β βββ dpo_train.py # DPO training method
β βββ data_preparation.py # Data preprocessing
β βββ auth_utils.py # Authentication utilities
β βββ logging_utils.py # Logging configuration
β βββ optuna.py # Hyperparameter optimization
β βββ exceptions.py # Custom exceptions
β βββ types.py # Type definitions
βββ testing_model/ # Model evaluation
β βββ __main__.py # Testing entry point
β βββ models.py # Custom model implementations
βββ evaluation/ # Evaluation frameworks
β βββ deepeval_integration.py # DeepEval framework
β βββ model_evaluation.py # Core evaluation functions
β βββ game_evaluation.py # Game-specific evaluation
βββ tests/ # Test suite
β βββ unit/ # Unit tests
β βββ integration/ # Integration tests
β βββ test_requirements.txt # Test dependencies
βββ data/ # Training datasets (DVC tracked)
βββ models/ # Output models and checkpoints
βββ docs/ # Documentation
βββ llama.cpp/ # llama.cpp integration (local)
βββ pyproject.toml # Poetry dependencies
βββ docker-compose.yaml # Docker services
βββ Dockerfile # Main training container
βββ run_pipeline.sh # Training pipeline script
βββ CLAUDE.md # Project instructions
sequenceDiagram
autonumber
actor U as User
participant M as main.py (Fire)
participant C as LLMLoRaCLI
participant T as Training (SFT/DPO/GRPO)
participant CV as Converters (GGUF/RKLLM)
participant TE as Testing (evaluation)
participant LG as Logging (W&B/MLflow)
participant OL as Ollama API
U->>M: Run "pipeline" (args/overrides)
M->>C: Fire dispatch
C->>C: Load Hydra config + apply overrides
C->>LG: Init logging backend
C->>T: train()/optimize()
T-->>LG: log metrics/artifacts
C->>CV: convert(gguf?, rkllm?)
alt RKLLM enabled
CV-->>C: rkllm output path
end
C->>TE: test_llm / dataset_to_json_for_test
TE-->>LG: log evaluation metrics
C-->>M: pipeline complete
M-->>U: Exit status and outputs
- Any HuggingFace transformer model
- Full Documentation - Comprehensive guides and API reference
- Training Methods - SFT vs GRPO comparison
- Configuration Guide - Detailed config options
- Docker Deployment - Production deployment
- API Reference - Complete API documentation
We welcome contributions! Please see our Contributing Guide for details.
# Clone and setup
git clone https://github.com/Timik232/LLM_LoRa.git
cd LLM_LoRa
# Install with dev dependencies
poetry install
# Setup pre-commit hooks
pre-commit install
# Run tests
pytest tests/CUDA Out of Memory
# Reduce batch size or enable gradient accumulation
python main.py training.per_device_train_batch_size=1 training.gradient_accumulation_steps=8
# Enable gradient checkpointing for additional memory savings
python main.py training.gradient_checkpointing=trueDocker GPU Issues
# 1. Verify NVIDIA driver installation
nvidia-smi
# 2. Check NVIDIA Container Toolkit
nvidia-container-cli info
# 3. Test GPU access in Docker
docker run --rm --gpus all nvidia/cuda:12.1.0-base-ubuntu22.04 nvidia-smi
# 4. If GPU not accessible, restart Docker daemon
sudo systemctl restart docker
# 5. Check Docker Compose GPU configuration
docker-compose configDocker Build Issues
# Clear Docker cache and rebuild
docker system prune -a
docker-compose build --no-cache
# Check disk space (requires 70GB+)
df -h
# Monitor build progress with verbose output
docker-compose build --progress=plainllama.cpp Integration Issues
# Verify llama.cpp symlinks in container
docker-compose exec llm_training ls -la /app/llama.cpp
docker-compose exec llm_training ls -la /app/llama.cpp/llama-quantize.exe
# Check quantization executable
docker-compose exec llm_training file /llama.cpp/build/bin/llama-quantizeModel Loading/Conversion Errors
# Check available disk space for model downloads and conversion
du -sh models/
df -h .
# Verify HuggingFace authentication (for private models)
docker-compose exec llm_training python -c "from huggingface_hub import whoami; print(whoami())"
# Check GGUF conversion logs
docker-compose logs llm_training | grep -i gguf
# Verify RKLLM conversion (if enabled)
docker-compose logs llm_training | grep -i rkllmConfiguration Issues
# Validate Hydra configuration
python main.py --config-path=conf --config-name=config --help
# Check configuration override syntax
python main.py model.train_steps=10 --dry-run
# Debug data loading
python main.py training.logging_steps=1 training.eval_steps=5Container Service Issues
# Check all services status
docker-compose ps
# View specific service logs
docker-compose logs llm_training
docker-compose logs ollama
docker-compose logs mlflow
# Restart specific service
docker-compose restart llm_training
# Access container shell for debugging
docker-compose exec llm_training bashPerformance Issues
# Monitor GPU utilization during training
nvidia-smi -l 1
# Check container resource usage
docker stats
# Optimize for available GPU memory
python main.py training.per_device_train_batch_size=1 training.gradient_accumulation_steps=16 training.fp16=true- Check logs first:
docker-compose logs llm_training - Verify system requirements: NVIDIA GPU, 70GB+ disk space, CUDA toolkit
- Update drivers: Ensure latest NVIDIA drivers are installed
- GitHub Issues: Report bugs at GitHub Issues
- Include diagnostics: GPU info (
nvidia-smi), Docker version, error logs
For more issues, check our Issues page.
This project is licensed under the MIT License - see the LICENSE file for details.
- HuggingFace Transformers for the transformer implementations
- Microsoft PEFT for LoRa implementation
- bitsandbytes for quantization support
- TRL for reinforcement learning methods
- The open-source ML community
- Support for more model architectures (Mamba, RetNet)
- Multi-GPU training support
- Web UI for training management
- Advanced evaluation metrics
- Model compression techniques
- Edge deployment optimization
Star β this repository if it helped you!
Report Bug β’ Request Feature β’ Discussions