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.05---67## Overview89Prepares 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.1011## When to Use This Skill1213- You have domain-specific data (support tickets, legal docs, internal knowledge, code style, product data) that a base model doesn't handle well.14- Prompt engineering + RAG has hit a quality ceiling.15- You need the model to output in a very specific style, format, or with deep domain knowledge.1617## Prerequisites1819- A dataset of input → desired output pairs (hundreds to tens of thousands of examples).20- GPU access (single A100/H100 for QLoRA on 7-13B models is common; more for larger).21- Hugging Face account + transformers + peft + bitsandbytes + trl (or Axolotl/Unsloth for easier workflows).2223## Steps24251. **Decide if fine-tuning is the right tool**:26 - Fine-tune when you need new behavior/knowledge that is hard to prompt or retrieve.27 - Prefer RAG + good prompting for factual recall over large private corpora.28 - Combine both when appropriate.29302. **Dataset format**:31 - Instruction tuning: `{"instruction": "...", "input": "...", "output": "..."}` or chat format.32 - JSONL, one example per line.33 - For chat models: messages array in the model's chat template.34353. **Quality filtering** (critical):36 - Remove duplicates (exact + near-duplicate via embedding similarity).37 - Length filters (too short or extremely long).38 - Quality heuristics or LLM-as-judge scoring.39 - Human review of a sample (aim for >95% high quality).40414. **Split strategy**:42 - 80/10/10 or 90/5/5 (train/val/test).43 - Ensure no leakage (same conversation or document not split across sets).44455. **LoRA / QLoRA config**:46 - Rank (r): 8-64 (16-32 common starting point).47 - Alpha, dropout.48 - Target modules (q_proj, v_proj, etc. for most models).49 - 4-bit or 8-bit quantization for QLoRA.50516. **Training setup (Hugging Face)**:52 - Use TRL SFTTrainer or Axolotl for simpler config.53 - Learning rate, batch size, gradient accumulation, epochs (usually 1-3 for instruction tuning).54 - Evaluation strategy, save best, early stopping.55 - Packing / packing for efficiency.56577. **Evaluation**:58 - Held-out test set with exact or fuzzy match + LLM judge.59 - Qualitative review of outputs.60 - Compare to base model + few-shot on the same test set.61628. **Output**:63 - Cleaned, split dataset files.64 - `config.yaml` or Python training script (LoRA + QLoRA).65 - Evaluation script.66 - Cost estimate (GPU hours × price).67 - Merging and inference notes (how to merge LoRA adapters back into base).6869## Examples7071A 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.7273## Edge Cases & Error Handling7475- **Catastrophic forgetting**: Use LoRA (low rank), small learning rate, short training, or replay of general data.76- **Small datasets** (<1k examples): Heavy augmentation or start with very low rank LoRA + more epochs.77- **Evaluation contamination**: Strict separation of train/val/test.7879## Verification80811. Dataset passes quality filters and split checks.822. Training runs without OOM on the target hardware (with QLoRA).833. Validation loss decreases and plateaus reasonably.844. On the held-out test set, the fine-tuned model outperforms the base model + good prompting (by automatic metrics and human/LLM judgment).855. Merged model loads and generates correctly.866. Success: The fine-tuned model shows clear improvement on the target domain/task with acceptable generalization.8788## References8990- [Hugging Face PEFT](https://huggingface.co/docs/peft/index)91- [TRL - Supervised Fine-Tuning](https://huggingface.co/docs/trl/sft_trainer)92- [QLoRA Paper](https://arxiv.org/abs/2305.14314)93- [Axolotl](https://github.com/OpenAccess-AI-Collective/axolotl)94- [Unsloth](https://github.com/unslothai/unsloth)95- [Fine-tuning Guide (Hugging Face)](https://huggingface.co/docs/transformers/en/training)