itsmostafa/llm-engineering-skills/qlora
- community
Checking that the SKILL.md file is still in the repository.
Memory-efficient fine-tuning with 4-bit quantization and LoRA adapters. Use when fine-tuning large models (7B+) on consumer GPUs, when VRAM is limited, or when standard LoRA still exceeds memory. Builds on the lora skill.
Install
npx vskill@latest install itsmostafa/llm-engineering-skills/qloraSKILL.md
QLoRA: Quantized Low-Rank Adaptation
QLoRA enables fine-tuning of large language models on consumer GPUs by combining 4-bit quantization with LoRA adapters. A 65B model can be fine-tuned on a single 48GB GPU while matching 16-bit fine-tuning performance.
Prerequisites: This skill assumes familiarity with LoRA. See theloraskill for LoRA fundamentals (LoraConfig, target_modules, training patterns).
Table of Contents
- Core Innovations
- BitsAndBytesConfig Deep Dive
- Memory Requirements
- Complete Training Example
- Inference and Merging
- Troubleshooting
- Best Practices
- References
Core Innovations
QLoRA introduces three techniques that reduce memory usage without sacrificing performance:
4-bit NormalFloat (NF4)
NF4 is an information-theoretically optimal quantization data type for normally distributed weights. Neural network weights are typically normally distributed, making NF4 more efficient than standard 4-bit floats.
The key insight: weights are stored in 4-bit but dequantized to bf16 for computation. Only the frozen base model is quantized; LoRA adapters remain in full precision.
NF4 vs FP4:
| Quantization | Description | Use Case | |
|---|---|---|---|
nf4 | Normalized Float 4-bit, optimal for normal distributions | Default, recommended | |
fp4 | Standard 4-bit float | Legacy, rarely needed |
Double Quantization
Standard quantization requires storing scaling constants (typically fp32) for each quantization block. Double quantization quantizes these constants too:
This saves approximately 0.37 bits per parameter—significant for billion-parameter models:
- 7B model: ~325 MB savings
- 70B model: ~3.2 GB savings
Paged Optimizers
During training, gradient checkpointing can cause memory spikes when processing long sequences. Paged optimizers use NVIDIA unified memory to automatically transfer optimizer states between GPU and CPU:
This is handled automatically by bitsandbytes when using 4-bit training.
BitsAndBytesConfig Deep Dive
All Parameters Explained
Compute Dtype Selection
| Dtype | Hardware | Notes | |
|---|---|---|---|
torch.bfloat16 | Ampere+ (RTX 30xx, A100) | Recommended, faster | |
torch.float16 | Older GPUs (V100, RTX 20xx) | Use if bf16 not supported | |
torch.float32 | Any | Slower, only for debugging |
Check bf16 support:
Comparison: Quantization Options
Memory Requirements
| Model Size | Full Fine-tuning | LoRA (16-bit) | QLoRA (4-bit) | |
|---|---|---|---|---|
| 7B | ~60 GB | ~16 GB | ~6 GB | |
| 13B | ~104 GB | ~28 GB | ~10 GB | |
| 34B | ~272 GB | ~75 GB | ~20 GB | |
| 70B | ~560 GB | ~160 GB | ~48 GB |
Notes:
- QLoRA memory includes model + optimizer states + activations
- Actual usage varies with batch size, sequence length, and gradient checkpointing
- Add ~20% buffer for safe operation
GPU Recommendations
| GPU VRAM | Max Model Size (QLoRA) | |
|---|---|---|
| 8 GB | 7B (tight) | |
| 16 GB | 7-13B | |
| 24 GB | 13-34B | |
| 48 GB | 34-70B | |
| 80 GB | 70B+ comfortably |
Complete Training Example
Inference and Merging
Inference with Quantized Model
Merging to Full Precision
To merge QLoRA adapters into a full-precision model (for deployment without bitsandbytes):
Note: Merging requires enough RAM to hold the full-precision model. For 70B models, this means ~140GB RAM.
Troubleshooting
CUDA Version Issues
"cannot find libcudart" or Missing Library Errors
Slow Training
Common cause: compute dtype mismatch
Out of Memory
Model Loads But Training Fails
Best Practices
- Always use `prepare_model_for_kbit_training`: This enables gradient computation through the frozen quantized layers
- Match compute dtype with training precision: If
bnb_4bit_compute_dtype=torch.bfloat16, usebf16=Truein training args
- Use paged optimizers for large models:
optim="paged_adamw_8bit"or"paged_adamw_32bit"handles memory spikes
- Start with NF4 + double quantization: This is the recommended default; only change if debugging
- Gradient checkpointing is essential: Always enable for QLoRA training to fit larger batch sizes
- Test inference before long training runs: Load the model and generate a few tokens to catch configuration issues early
- Monitor GPU memory: Use
nvidia-smiortorch.cuda.memory_summary()to track actual usage
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/qlora/SKILL.md
Popularity
- Repository stars
- 19
- 7-day trend score
- 15
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 12 Apr 2026, 6 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/itsmostafa/llm-engineering-skills/qlora)