Skip to main content
Q-LoRA (Quantized LoRA) combines 4-bit quantization with LoRA to enable fine-tuning of large language models on consumer GPUs. It uses quantized base models while training LoRA adapters in higher precision.

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
Q-LoRA enables fine-tuning of Qwen-7B on a single RTX 3090 (24GB) or even RTX 3060 (12GB) with reduced sequence length.

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

Critical: Use auto-gptq>=0.5.1 with torch==2.1 or auto-gptq<0.5.0 with torch>=2.0,<2.1 to avoid compatibility issues.

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
Important: Q-LoRA must use FP16 (--fp16 True), not BF16. This is due to AutoGPTQ quantization requirements.

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

Training will use DeepSpeed for mixed-precision training even on single GPU.
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
Run with:

Loading Q-LoRA Adapters

Inference with Q-LoRA Adapter

Q-LoRA Limitation: You cannot merge Q-LoRA adapters with the base model. The adapter must always be loaded separately.

Q-LoRA Constraints

What You Cannot Do

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.
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.
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)
Reason: Base models need trainable embeddings which Q-LoRA doesn’t support.
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
Optimization tips:
  1. Use Flash Attention 2 (if compatible)
  2. Enable gradient checkpointing
  3. Use --lazy_preprocess True
  4. Increase gradient_accumulation_steps to reduce step overhead

Hyperparameter Guide

Learning Rate

Same as regular LoRA. Adjust based on results:
  • Too high: Training loss oscillates or diverges
  • Too low: Slow convergence, model doesn’t adapt

LoRA Configuration

Q-LoRA uses the same LoRA hyperparameters as regular LoRA. The only difference is the quantized base model.

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

This requires a calibration dataset (can reuse training data).
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
Consider alternatives for:
  • Mathematical reasoning (use LoRA or full fine-tuning)
  • Complex code generation
  • Tasks requiring maximum accuracy
  • Production models with strict quality requirements

Troubleshooting

Issue: Cannot install auto-gptq or compilation errorsSolutions:
  1. Use pre-compiled wheels:
  1. Check CUDA version compatibility:
  1. Install build dependencies:
Solutions:
  1. Reduce sequence length:
  1. Reduce batch size:
  1. Reduce LoRA rank:
  1. Use smaller model:
Expected: Q-LoRA is 2-3x slower than LoRAOptimizations:
  1. Increase gradient accumulation (reduces overhead):
  1. Use lazy preprocessing:
  1. Reduce logging frequency:
  1. Disable evaluation:
Issue: KeyError or missing files when loading Int4 modelSolutions:
  1. Verify model is Int4 quantized:
  1. Install required packages:
  1. Copy missing files manually:
Debugging steps:
  1. Verify data quality:
  1. Increase learning rate:
  1. Increase LoRA rank:
  1. Train for more epochs:

Advanced: Manual Quantization Configuration

For custom quantization settings:
Custom quantization configurations are advanced. Use official Int4 models unless you have specific requirements.

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