Overview
Q-LoRA achieves extreme memory efficiency through:- 4-bit NormalFloat quantization of base model weights
- 16-bit LoRA adapters for maintained training quality
- Paged optimizers to handle memory spikes
- Single GPU training of 7B models on 12GB GPUs
- Minimal performance degradation compared to full LoRA
When to Use Q-LoRA
Choose Q-LoRA when:- You have limited GPU memory (12-24GB)
- You need cost-effective fine-tuning on consumer hardware
- You can tolerate 2-3x slower training than regular LoRA
- Your task has moderate quality requirements
- You want to fine-tune larger models on smaller GPUs
Hardware Requirements
Memory Requirements
Qwen-7B Q-LoRA Fine-tuning (Single GPU):GPU Recommendations
Minimum GPU requirements assume sequence length ≤ 1024. Longer sequences require more memory.
Installation
Version Compatibility Matrix
Q-LoRA Configuration
Q-LoRA configuration in the training script:Key Parameters
int
default:4
Quantization bit-width. Fixed at 4-bit for Q-LoRA.
bool
default:true
Disables ExLlama kernels for compatibility with training.
Single-GPU Training
Basic Training Script
finetune/finetune_qlora_single_gpu.sh
Running Single-GPU Q-LoRA
1
Prepare Quantized Model
Use official Int4 quantized models:
Only Chat models are available in Int4. Base models are not provided in quantized format.
2
Prepare Training Data
Use the same JSON format as regular LoRA:
3
Launch Training
4
Monitor Memory Usage
Watch GPU memory:Expected memory usage for Qwen-7B-Chat-Int4:
- Initial load: ~4GB
- During training: ~11-12GB
- Peak: ~13-14GB
Multi-GPU Training
For faster Q-LoRA training:finetune/finetune_qlora_ds.sh
Loading Q-LoRA Adapters
Inference with Q-LoRA Adapter
Q-LoRA Constraints
What You Cannot Do
Cannot Merge Adapters
Cannot Merge Adapters
Unlike regular LoRA, Q-LoRA adapters cannot be merged:Reason: Base model is quantized (4-bit), LoRA adapters are FP16. Merging requires same precision.Workaround: Always load adapter separately for inference.
Cannot Train Embedding Layers
Cannot Train Embedding Layers
Q-LoRA with Int4 models cannot make embedding/output layers trainable:Impact: Cannot add new tokens during Q-LoRA training.Solution: Use regular LoRA if you need to add custom tokens.
Must Use Int4 Chat Models
Must Use Int4 Chat Models
Q-LoRA requires official Int4 quantized chat models:
- ✓
Qwen/Qwen-7B-Chat-Int4(supported) - ✗
Qwen/Qwen-7B-Int4(does not exist) - ✗
Qwen/Qwen-7B(cannot be used directly)
Cannot Use BF16
Cannot Use BF16
Q-LoRA training must use FP16, not BF16:Reason: AutoGPTQ quantization is optimized for FP16 operations.
Performance Considerations
Q-LoRA vs LoRA Comparison
Qwen-7B Training (Sequence Length 1024):Speed-Memory Tradeoff
Q-LoRA trades speed for memory:- 2-3x slower than regular LoRA
- 40-50% less memory than regular LoRA
- Ideal when memory is the bottleneck
- Use Flash Attention 2 (if compatible)
- Enable gradient checkpointing
- Use
--lazy_preprocess True - Increase
gradient_accumulation_stepsto reduce step overhead
Hyperparameter Guide
Learning Rate
- Too high: Training loss oscillates or diverges
- Too low: Slow convergence, model doesn’t adapt
LoRA Configuration
Batch Size for Memory Constraints
If hitting memory limits:Sequence Length Optimization
Creating Custom Quantized Models
If you need to quantize a fine-tuned model:1
Train with Regular LoRA or Full Fine-tuning
2
Merge LoRA Adapter (if using LoRA)
3
Quantize to Int4
4
Use Quantized Model for Q-LoRA
See Full-Parameter Fine-tuning for detailed quantization instructions.
Model Quality
Benchmark Results
Qwen-7B-Chat Performance:
Quality degradation: ~1-3% across benchmarks
When Quality Matters
Q-LoRA is suitable for:
- Domain adaptation
- Style transfer
- Instruction following
- Task-specific fine-tuning
- RAG applications
- Mathematical reasoning (use LoRA or full fine-tuning)
- Complex code generation
- Tasks requiring maximum accuracy
- Production models with strict quality requirements
Troubleshooting
AutoGPTQ Installation Failed
AutoGPTQ Installation Failed
Issue: Cannot install auto-gptq or compilation errorsSolutions:
- Use pre-compiled wheels:
- Check CUDA version compatibility:
- Install build dependencies:
Out of Memory on 12GB GPU
Out of Memory on 12GB GPU
Solutions:
- Reduce sequence length:
- Reduce batch size:
- Reduce LoRA rank:
- Use smaller model:
Training Extremely Slow
Training Extremely Slow
Expected: Q-LoRA is 2-3x slower than LoRAOptimizations:
- Increase gradient accumulation (reduces overhead):
- Use lazy preprocessing:
- Reduce logging frequency:
- Disable evaluation:
Cannot Load Quantized Model
Cannot Load Quantized Model
Issue:
KeyError or missing files when loading Int4 modelSolutions:- Verify model is Int4 quantized:
- Install required packages:
- Copy missing files manually:
Loss Not Decreasing
Loss Not Decreasing
Debugging steps:
- Verify data quality:
- Increase learning rate:
- Increase LoRA rank:
- Train for more epochs:
Advanced: Manual Quantization Configuration
For custom quantization settings:Best Practices
Do’s
- Use official Int4 chat models for Q-LoRA
- Always use FP16 precision, never BF16
- Enable gradient checkpointing for memory savings
- Use DeepSpeed even for single-GPU training
- Monitor GPU memory usage during training
- Start with shorter sequences (512 tokens)
Don’ts
- Don’t try to merge Q-LoRA adapters (not supported)
- Don’t use Q-LoRA if you need to add custom tokens
- Don’t expect same speed as regular LoRA
- Don’t use Q-LoRA for production models if quality is critical
- Don’t use base models with Q-LoRA (embedding layers need training)
Next Steps
LoRA Fine-tuning
Compare with regular LoRA for better quality
Multi-node Training
Scale Q-LoRA training across multiple machines