一键导入
fine-tuning-patterns
Expert reference for LLM fine-tuning decisions, dataset curation, training, and deployment
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Expert reference for LLM fine-tuning decisions, dataset curation, training, and deployment
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Expert reference for application security — OWASP Top 10 mitigations, auth/authz, secrets management, cryptography, input validation, dependency hygiene, and secure-by-default code patterns
Expert reference for token counting, prompt compression, cost estimation, and quality preservation when optimizing prompts for Claude models
Experimentation design and A/B testing standards for product teams
Expert reference for digital accessibility — WCAG conformance, ARIA, and inclusive design patterns
Authoritative reference for agent architecture selection, multi-agent orchestration, tool design, memory systems, and failure mode prevention
Expert reference for evaluating LLM systems, RAG pipelines, and AI features in production
| name | fine-tuning-patterns |
| description | Expert reference for LLM fine-tuning decisions, dataset curation, training, and deployment |
| version | 1 |
If the goal is consistent output format/style → fine-tuning is a strong candidate. System prompts can enforce format but struggle with complex, lengthy, or highly specific structures at scale.
If the goal is domain knowledge injection → prefer RAG. Fine-tuning memorizes knowledge from training data but cannot be updated without retraining. RAG is updated by adding documents. Exception: if knowledge is stable and latency/cost of retrieval is prohibitive.
If the goal is behavior change (tone, persona, refusal patterns) → fine-tuning is appropriate if the behavior is consistent and can be demonstrated in training examples.
If the required dataset is <50 examples → use few-shot prompting, not fine-tuning. 50 examples is a floor; 100-500 is the effective range for style/format; 1,000+ for domain knowledge.
If using LoRA/QLoRA → appropriate for: adapting large models (7B-70B) with limited GPU memory; multi-tenant scenarios (one LoRA adapter per client on shared base model); rapid experimentation. Use QLoRA when GPU memory is the binding constraint (4-bit quantized base model + LoRA adapters).
If validation loss diverges from training loss after epoch 2 → overfitting. Stop training, reduce epochs, or add more training data diversity. Never continue training past the divergence point.
If fine-tuned model shows regression on general capabilities → the dataset lacked diversity. Add general instruction-following examples (10-20% of dataset) to prevent catastrophic forgetting.
Never fine-tune on data that contains PII, credentials, or proprietary information without explicit data governance approval and dataset sanitization.
Never use the OpenAI fine-tuning API for highly sensitive or regulated data — understand data handling policies before submitting training data to any third-party API.
The Fine-Tuning Decision Tree
Goal: Improve model behavior for specific task
↓
Can system prompt + few-shot examples achieve it?
├── YES → Use prompting. Fine-tuning overhead not justified.
└── NO ↓
Is the knowledge dynamic/frequently updated?
├── YES → RAG. Fine-tuning can't be updated without retraining.
└── NO ↓
Is the dataset curateable (500+ high-quality examples)?
├── NO → Collect more data first. Don't fine-tune on insufficient data.
└── YES ↓
Does cost/latency of prompting at scale justify training cost?
├── NO → Prompting is acceptable. Fine-tuning not needed.
└── YES → Fine-tune.
LoRA vs Full Fine-Tune Decision
GPU memory available for 7B model?
├── >80GB (A100 80GB) → Full fine-tune possible; use for maximum quality
├── 40-80GB → LoRA (rank 16-64) on full-precision base model
├── 16-40GB → QLoRA (4-bit base + LoRA adapters)
└── <16GB → QLoRA with aggressive quantization; consider smaller base model
Dataset Composition Guidelines
Task type | Min examples | Format | Diversity requirement
-------------------------|--------------|-----------------|----------------------
Style/format consistency | 100 | Instruction+Output | 20+ distinct input types
Domain Q&A | 500 | Question+Answer | Cover all sub-domains
Behavior change | 300 | Instruction+Output | Include edge cases
Classification | 200/class | Input+Label | Balanced + adversarial
| Term | Precise Meaning |
|---|---|
| LoRA | Low-Rank Adaptation — adds small trainable weight matrices to frozen base model layers |
| QLoRA | Quantized LoRA — base model quantized to 4-bit, LoRA adapters in full precision |
| PEFT | Parameter-Efficient Fine-Tuning — umbrella term for LoRA, adapters, prefix tuning |
| Catastrophic forgetting | Fine-tuned model loses general capabilities because training data was too narrow |
| Overfitting | Model memorizes training examples instead of generalizing — val loss rises while train loss falls |
| Instruction tuning | Fine-tuning on instruction-response pairs to improve instruction-following |
| Rank (LoRA) | Dimensionality of LoRA matrices; r=8 minimal, r=64 high-quality; higher = more parameters |
| Epoch | One full pass through the training dataset; 1-3 epochs typical for fine-tuning LLMs |
| RLHF | Reinforcement Learning from Human Feedback — training signal from human preference rankings |
| DPO | Direct Preference Optimization — fine-tuning from preference pairs without separate reward model |
| Adapter | A small set of trainable weights inserted between frozen model layers |
| Merged weights | LoRA adapters mathematically merged into the base model weights for faster inference |
Mistake 1: Fine-tuning without a baseline
Mistake 2: Low-quality scraped training data
Mistake 3: Forgetting catastrophic forgetting
Mistake 4: Training beyond the optimal checkpoint
Mistake 5: Ignoring inference cost after fine-tuning
BAD training example (instruction tuning):
{
"messages": [
{"role": "user", "content": "Summarize this."},
{"role": "assistant", "content": "Here is a summary."}
]
}
Problems: Input is generic (no actual text to summarize), output is useless, teaches nothing.
GOOD training example:
{
"messages": [
{
"role": "system",
"content": "You are a financial document analyst. Summarize earnings call transcripts in 3 bullet points: revenue trend, key risk, and management outlook. Be specific with numbers."
},
{
"role": "user",
"content": "Q3 2024 earnings call: Revenue grew 18% YoY to $2.4B. Gross margin compressed 200bps to 71% due to infrastructure investment. CEO highlighted AI integration roadmap and raised FY guidance to $9.8B."
},
{
"role": "assistant",
"content": "• Revenue: +18% YoY to $2.4B, above consensus\n• Risk: Gross margin -200bps to 71% from infra spend — watch Q4 for recovery\n• Outlook: FY guidance raised to $9.8B; AI integration cited as growth driver"
}
]
}