Overview
Prepares high-quality datasets and training configurations for fine-tuning LLMs (LoRA, QLoRA, full SFT). Covers when to fine-tune vs prompt engineering vs RAG, dataset formats (instruction-response JSONL), quality filtering, train/val/test splits, hyperparameter selection, LoRA config, Hugging Face Trainer / TRL setup, evaluation, and cost/latency estimation.
When to Use This Skill
- You have domain-specific data (support tickets, legal docs, internal knowledge, code style, product data) that a base model doesn't handle well.
- Prompt engineering + RAG has hit a quality ceiling.
- You need the model to output in a very specific style, format, or with deep domain knowledge.
Prerequisites
- A dataset of input → desired output pairs (hundreds to tens of thousands of examples).
- GPU access (single A100/H100 for QLoRA on 7-13B models is common; more for larger).
- Hugging Face account + transformers + peft + bitsandbytes + trl (or Axolotl/Unsloth for easier workflows).
Steps
Decide if fine-tuning is the right tool:
- Fine-tune when you need new behavior/knowledge that is hard to prompt or retrieve.
- Prefer RAG + good prompting for factual recall over large private corpora.
- Combine both when appropriate.
Dataset format:
- Instruction tuning:
{"instruction": "...", "input": "...", "output": "..."} or chat format.
- JSONL, one example per line.
- For chat models: messages array in the model's chat template.
Quality filtering (critical):
- Remove duplicates (exact + near-duplicate via embedding similarity).
- Length filters (too short or extremely long).
- Quality heuristics or LLM-as-judge scoring.
- Human review of a sample (aim for >95% high quality).
Split strategy:
- 80/10/10 or 90/5/5 (train/val/test).
- Ensure no leakage (same conversation or document not split across sets).
LoRA / QLoRA config:
- Rank (r): 8-64 (16-32 common starting point).
- Alpha, dropout.
- Target modules (q_proj, v_proj, etc. for most models).
- 4-bit or 8-bit quantization for QLoRA.
Training setup (Hugging Face):
- Use TRL SFTTrainer or Axolotl for simpler config.
- Learning rate, batch size, gradient accumulation, epochs (usually 1-3 for instruction tuning).
- Evaluation strategy, save best, early stopping.
- Packing / packing for efficiency.
Evaluation:
- Held-out test set with exact or fuzzy match + LLM judge.
- Qualitative review of outputs.
- Compare to base model + few-shot on the same test set.
Output:
- Cleaned, split dataset files.
config.yaml or Python training script (LoRA + QLoRA).
- Evaluation script.
- Cost estimate (GPU hours × price).
- Merging and inference notes (how to merge LoRA adapters back into base).
Examples
A complete pipeline for fine-tuning a 7B or 13B model on a customer support / internal knowledge dataset: dataset cleaning script, JSONL preparation, QLoRA config, TRL training script, and evaluation harness with LLM-as-judge is included.
Edge Cases & Error Handling
- Catastrophic forgetting: Use LoRA (low rank), small learning rate, short training, or replay of general data.
- Small datasets (<1k examples): Heavy augmentation or start with very low rank LoRA + more epochs.
- Evaluation contamination: Strict separation of train/val/test.
Verification
- Dataset passes quality filters and split checks.
- Training runs without OOM on the target hardware (with QLoRA).
- Validation loss decreases and plateaus reasonably.
- On the held-out test set, the fine-tuned model outperforms the base model + good prompting (by automatic metrics and human/LLM judgment).
- Merged model loads and generates correctly.
- Success: The fine-tuned model shows clear improvement on the target domain/task with acceptable generalization.
References
1---2name: fine-tuning-preparer3description: Prepares datasets and training configurations for fine-tuning LLMs. Use when customizing a base model on domain-specific data using LoRA, QLoRA, or supervised fine-tuning.4license: Apache-2.05compatibility: Claude Code, Codex, Gemini CLI6metadata:7 author: ai-skills8 version: "1.0"9 category: ai-ml10 tags: fine-tuning, lora, qlora, dataset, huggingface, sft, evaluation11---1213## Overview1415Prepares high-quality datasets and training configurations for fine-tuning LLMs (LoRA, QLoRA, full SFT). Covers when to fine-tune vs prompt engineering vs RAG, dataset formats (instruction-response JSONL), quality filtering, train/val/test splits, hyperparameter selection, LoRA config, Hugging Face Trainer / TRL setup, evaluation, and cost/latency estimation.1617## When to Use This Skill1819- You have domain-specific data (support tickets, legal docs, internal knowledge, code style, product data) that a base model doesn't handle well.20- Prompt engineering + RAG has hit a quality ceiling.21- You need the model to output in a very specific style, format, or with deep domain knowledge.2223## Prerequisites2425- A dataset of input → desired output pairs (hundreds to tens of thousands of examples).26- GPU access (single A100/H100 for QLoRA on 7-13B models is common; more for larger).27- Hugging Face account + transformers + peft + bitsandbytes + trl (or Axolotl/Unsloth for easier workflows).2829## Steps30311. **Decide if fine-tuning is the right tool**:32 - Fine-tune when you need new behavior/knowledge that is hard to prompt or retrieve.33 - Prefer RAG + good prompting for factual recall over large private corpora.34 - Combine both when appropriate.35362. **Dataset format**:37 - Instruction tuning: `{"instruction": "...", "input": "...", "output": "..."}` or chat format.38 - JSONL, one example per line.39 - For chat models: messages array in the model's chat template.40413. **Quality filtering** (critical):42 - Remove duplicates (exact + near-duplicate via embedding similarity).43 - Length filters (too short or extremely long).44 - Quality heuristics or LLM-as-judge scoring.45 - Human review of a sample (aim for >95% high quality).46474. **Split strategy**:48 - 80/10/10 or 90/5/5 (train/val/test).49 - Ensure no leakage (same conversation or document not split across sets).50515. **LoRA / QLoRA config**:52 - Rank (r): 8-64 (16-32 common starting point).53 - Alpha, dropout.54 - Target modules (q_proj, v_proj, etc. for most models).55 - 4-bit or 8-bit quantization for QLoRA.56576. **Training setup (Hugging Face)**:58 - Use TRL SFTTrainer or Axolotl for simpler config.59 - Learning rate, batch size, gradient accumulation, epochs (usually 1-3 for instruction tuning).60 - Evaluation strategy, save best, early stopping.61 - Packing / packing for efficiency.62637. **Evaluation**:64 - Held-out test set with exact or fuzzy match + LLM judge.65 - Qualitative review of outputs.66 - Compare to base model + few-shot on the same test set.67688. **Output**:69 - Cleaned, split dataset files.70 - `config.yaml` or Python training script (LoRA + QLoRA).71 - Evaluation script.72 - Cost estimate (GPU hours × price).73 - Merging and inference notes (how to merge LoRA adapters back into base).7475## Examples7677A complete pipeline for fine-tuning a 7B or 13B model on a customer support / internal knowledge dataset: dataset cleaning script, JSONL preparation, QLoRA config, TRL training script, and evaluation harness with LLM-as-judge is included.7879## Edge Cases & Error Handling8081- **Catastrophic forgetting**: Use LoRA (low rank), small learning rate, short training, or replay of general data.82- **Small datasets** (<1k examples): Heavy augmentation or start with very low rank LoRA + more epochs.83- **Evaluation contamination**: Strict separation of train/val/test.8485## Verification86871. Dataset passes quality filters and split checks.882. Training runs without OOM on the target hardware (with QLoRA).893. Validation loss decreases and plateaus reasonably.904. On the held-out test set, the fine-tuned model outperforms the base model + good prompting (by automatic metrics and human/LLM judgment).915. Merged model loads and generates correctly.926. Success: The fine-tuned model shows clear improvement on the target domain/task with acceptable generalization.9394## References9596- [Hugging Face PEFT](https://huggingface.co/docs/peft/index)97- [TRL - Supervised Fine-Tuning](https://huggingface.co/docs/trl/sft_trainer)98- [QLoRA Paper](https://arxiv.org/abs/2305.14314)99- [Axolotl](https://github.com/OpenAccess-AI-Collective/axolotl)100- [Unsloth](https://github.com/unslothai/unsloth)101- [Fine-tuning Guide (Hugging Face)](https://huggingface.co/docs/transformers/en/training)