j4flmao/agent-skills/llm-finetuning
Claude Code and agent skill by j4flmao/agent-skills
- community
Checking that the SKILL.md file is still in the repository.
Use this skill when the user says 'fine-tune an LLM', 'LoRA', 'QLoRA', 'PEFT', 'SFT', 'instruction tuning', 'TRL SFTTrainer', 'DPO', 'train on my data', 'merge adapter'. Decides whether fine-tuning is the right tool, prepares chat-format datasets, configures LoRA/QLoRA with PEFT and TRL, evaluates against the base model, and exports or serves adapters. Do NOT use for: pre-training or full-parameter distributed training (ai model-training skills), post-training quantization for serving (ai/llm-engineering/model-quantization), or prompt-only improvements (prompt-engineering/*).
Install
npx vskill@latest install j4flmao/agent-skills/llm-finetuningSKILL.md
LLM Fine-Tuning (PEFT / LoRA)
Purpose
Fine-tuning changes model behaviour (format, style, task skill, domain language) from examples. It is the wrong tool for injecting frequently changing facts; use retrieval for that. Parameter-efficient methods such as LoRA train small low-rank adapters on frozen base weights, making single-GPU training of 7-8B models routine and letting one base model serve many adapters.
Agent Protocol
Trigger
"fine-tune", "LoRA", "QLoRA", "PEFT", "SFT", "instruction tuning", "DPO", "preference tuning", "adapter", "merge_and_unload".
Input Context
- Goal and why prompting/RAG is insufficient (with failing examples).
- Dataset: size, source, license, format, PII status.
- Base model and license; GPU type/VRAM; serving target.
- Eval set and success metric.
Output Artifact
Go/no-go decision, dataset spec, training config, eval plan, export/serving instructions.
Response Format
Completion Criteria
- [ ] Baseline (prompted base model, RAG) measured first.
- [ ] Held-out test set never used for tuning decisions.
- [ ] Chat template of the base model applied consistently in training and inference.
- [ ] Tuned model beats baseline on the target metric without regressions on general checks.
Should You Fine-Tune?
| Goal | Best tool | |
|---|---|---|
| New/changing facts, citations | RAG | |
| Output format, tone, strict schema | Prompting + structured outputs first; SFT if still unreliable | |
| Narrow task at lower cost/latency | SFT a smaller model (often distilled from a larger one) | |
| Preference (helpfulness, style ranking) | DPO/ORPO after SFT | |
| Domain vocabulary/style | SFT (or continued pre-training for large corpora) |
Dataset
- Use the conversational format and let the tokenizer's chat template render it:
``json {"messages": [{"role": "system", "content": "You extract invoice fields as JSON."}, {"role": "user", "content": "<invoice text>"}, {"role": "assistant", "content": "{\"total\": 129.5, \"currency\": \"EUR\"}"}]} ``
- Quality over quantity: a few hundred to a few thousand clean, diverse examples often suffice for format/task tuning.
- Deduplicate (exact + near-duplicate), remove PII/secrets, check licenses, balance classes, and keep a stratified held-out test split.
- Train on assistant tokens only (loss masking of prompts) unless you have a reason not to; TRL supports this via its completion/assistant-only loss options.
LoRA / QLoRA Training (PEFT + TRL)
TRL and transformers rename arguments between releases (e.g. tokenizer → processing_class, max_seq_length → max_length); pin versions in pyproject.toml and check the installed release's docs.
Hyperparameter Guide
| Param | Start | Notes | |
|---|---|---|---|
r | 8-32 | Higher for harder/new skills; diminishing returns | |
lora_alpha | 2·r | Effective scale = alpha/r (or alpha/√r with rsLoRA) | |
| LR | 1e-4 to 2e-4 (LoRA) | Lower for larger models/datasets | |
| Epochs | 1-3 | Watch val loss; more epochs → memorization | |
| Max length | Cover p95 of examples | Truncation silently drops labels |
Evaluation
- Task metric on the held-out test set: base (best prompt) vs tuned.
- Regression checks: general instruction following, safety refusals, a few unrelated tasks.
- Inspect outputs manually; loss curves alone hide format collapse.
Export & Serving
- Adapter serving: vLLM
--enable-lora --lora-modules invoice=out/invoice-lora/finalserves many adapters on one base. - Merge:
PeftModel.from_pretrained(base_bf16, path).merge_and_unload()thensave_pretrained. Merge into a bf16 base, not the 4-bit one; quantize afterwards if needed.
Anti-Patterns
- Fine-tuning to teach facts that change weekly.
- Training on the test set or tuning hyperparameters against it.
- Inconsistent chat templates between training and inference.
- Only targeting
q_proj/v_projby habit when quality matters. - Hardcoding
.to("cuda"); usedevice_map="auto". - Ignoring base-model license terms for derivatives.
Checklist
- [ ] Baseline measured; decision documented.
- [ ] Dataset cleaned, deduped, PII-free, licensed, split.
- [ ] Chat template + assistant-only loss.
- [ ] Config versioned; library versions pinned.
- [ ] Eval: target metric + regressions.
- [ ] Export path tested in the serving engine.
References
- Hu et al., "LoRA" (ICLR 2022); Dettmers et al., "QLoRA" (NeurIPS 2023); Rafailov et al., "DPO" (NeurIPS 2023).
- Hugging Face PEFT and TRL documentation (SFTTrainer, DPOTrainer).
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/ai/llm-finetuning/SKILL.md
Popularity
- Repository stars
- 27
- Repository forks
- 1
- 7-day trend score
- 22
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
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/j4flmao/agent-skills/llm-finetuning)