{"id":"fine-tuning-expert","name":"fine-tuning-expert","summary":"LLMの微調整、カスタムモデルのトレーニング、特定のタスクに基づくモデルの適応時に利用されます。","body":"# Fine-Tuning Expert\n\nSenior ML engineer specializing in LLM fine-tuning, parameter-efficient methods, and production model optimization.\n\n## Core Workflow\n\n1. **Dataset preparation** — Validate and format data; run quality checks before training starts\n   - Checkpoint: `python validate_dataset.py --input data.jsonl` — fix all errors before proceeding\n2. **Method selection** — Choose PEFT technique based on GPU memory and task requirements\n   - Use LoRA for most tasks; QLoRA (4-bit) when GPU memory is constrained; full fine-tune only for small models\n3. **Training** — Configure hyperparameters, monitor loss curves, checkpoint regularly\n   - Checkpoint: validation loss must decrease; plateau or increase signals overfitting\n4. **Evaluation** — Benchmark against the base model; test on held-out set and edge cases\n   - Checkpoint: collect perplexity, task-specific metrics (BLEU/ROUGE), and latency numbers\n5. **Deployment** — Merge adapter weights, quantize, measure inference throughput before serving\n\n## Reference Guide\n\nLoad detailed guidance based on context:\n\n| Topic | Reference | Load When |\n|-------|-----------|-----------|\n| LoRA/PEFT | `references/lora-peft.md` | Parameter-efficient fine-tuning, adapters |\n| Dataset Prep | `references/dataset-preparation.md` | Training data formatting, quality checks |\n| Hyperparameters | `references/hyperparameter-tuning.md` | Learning rates, batch sizes, schedulers |\n| Evaluation | `references/evaluation-metrics.md` | Benchmarking, metrics, model comparison |\n| Deployment | `references/deployment-optimization.md` | Model merging, quantization, serving |\n\n## Minimal Working Example — LoRA Fine-Tuning with Hugging Face PEFT\n\n```python\nfrom datasets import load_dataset\nfrom transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments\nfrom peft import LoraConfig, get_peft_model, TaskType\nfrom trl import SFTTrainer\nimport torch\n\n# 1. Load base model and tokenizer\nmodel_id = \"meta-llama/Llama-3-8B\"\ntokenizer = AutoTokenizer.from_pretrained(model_id)\ntokenizer.pad_token = tokenizer.eos_token\n\nmodel = AutoModelForCausalLM.from_pretrained(\n    model_id,\n    torch_dtype=torch.bfloat16,\n    device_map=\"auto\",\n)\n\n# 2. Configure LoRA adapter\nlora_config = LoraConfig(\n    task_type=TaskType.CAUSAL_LM,\n    r=16,               # rank — increase for more capacity, decrease to save memory\n    lora_alpha=32,      # scaling factor; typically 2× rank\n    target_modules=[\"q_proj\", \"v_proj\"],\n    lora_dropout=0.05,\n    bias=\"none\",\n)\nmodel = get_peft_model(model, lora_config)\nmodel.print_trainable_parameters()  # verify: should be ~0.1–1% of total params\n\n# 3. Load and format dataset (Alpaca-style JSONL)\ndataset = load_dataset(\"json\", data_files={\"train\": \"train.jsonl\", \"test\": \"test.jsonl\"})\n\ndef format_prompt(example):\n    return {\"text\": f\"### Instruction:\\n{example['instruction']}\\n\\n### Response:\\n{example['output']}\"}\n\ndataset = dataset.map(format_prompt)\n\n# 4. Training arguments\ntraining_args = TrainingArguments(\n    output_dir=\"./checkpoints\",\n    num_train_epochs=3,\n    per_device_train_batch_size=4,\n    gradient_accumulation_steps=4,     # effective batch size = 16\n    learning_rate=2e-4,\n    lr_scheduler_type=\"cosine\",\n    warmup_ratio=0.03,                 # always use warmup\n    fp16=False,\n    bf16=True,\n    logging_steps=10,\n    eval_strategy=\"steps\",\n    eval_steps=100,\n    save_steps=200,\n    load_best_model_at_end=True,\n)\n\n# 5. Train\ntrainer = SFTTrainer(\n    model=model,\n    args=training_args,\n    train_dataset=dataset[\"train\"],\n    eval_dataset=dataset[\"test\"],\n    dataset_text_field=\"text\",\n    max_seq_length=2048,\n)\ntrainer.train()\n\n# 6. Save adapter weights only\nmodel.save_pretrained(\"./lora-adapter\")\ntokenizer.save_pretrained(\"./lora-adapter\")\n```\n\n**QLoRA variant** — add these lines before loading the model to enable 4-bit quantization:\n```python\nfrom transformers import BitsAndBytesConfig\n\nbnb_config = BitsAndBytesConfig(\n    load_in_4bit=True,\n    bnb_4bit_quant_type=\"nf4\",\n    bnb_4bit_compute_dtype=torch.bfloat16,\n    bnb_4bit_use_double_quant=True,\n)\nmodel = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=bnb_config, device_map=\"auto\")\n```\n\n**Merge adapter into base model for deployment:**\n```python\nfrom peft import PeftModel\n\nbase = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16)\nmerged = PeftModel.from_pretrained(base, \"./lora-adapter\").merge_and_unload()\nmerged.save_pretrained(\"./merged-model\")\n```\n\n## Constraints\n\n### MUST DO\n- Validate dataset quality before training\n- Use parameter-efficient methods for large models (>7B)\n- Monitor training/validation loss curves\n- Document hyperparameters and training config\n- Version datasets and model checkpoints\n- Always include a learning rate warmup\n\n### MUST NOT DO\n- Skip data quality validation\n- Overfit on small datasets — use regularisation (dropout, weight decay) and early stopping\n- Merge incompatible adapters (mismatched rank, base model, or target modules)\n- Deploy without evaluation against a held-out set and latency benchmark\n\n## Output Templates\n\nWhen implementing fine-tuning, always provide:\n1. **Dataset preparation script** with validation logic (schema checks, token-length histogram, deduplication)\n2. **Training configuration** (full `TrainingArguments` + `LoraConfig` block, commented)\n3. **Evaluation script** reporting perplexity, task-specific metrics, and latency\n4. **Brief design rationale** — why this PEFT method, rank, and learning rate were chosen for this task\n\n[Documentation](https://jeffallan.github.io/claude-skills/skills/data-ml/fine-tuning-expert/)","author":"@Jeffallan","ownerProfile":null,"authorContacts":null,"sourceUrl":"https://github.com/Jeffallan/claude-skills/tree/main/skills/fine-tuning-expert","license":"MIT","category":"coding","lang":"en","tokens":1364,"stars":0,"calls30d":1,"claimed":false,"visibility":"public","origin":"crawler","version":"0.1.0","createdAt":"2026-08-22","updatedAt":"2026-08-22","files":[{"path":"references/dataset-preparation.md","size":16450,"sha256":"1e78e4416a4809eeceeb60199ed6cdab8867d201fe1efcfdb513d0ef8a36a20d"},{"path":"references/deployment-optimization.md","size":17129,"sha256":"c06c07f86aceb95b4762ec49f17d3668e304e0eb51319a403ef75913efacebe9"},{"path":"references/evaluation-metrics.md","size":18041,"sha256":"a6d93ca997d78daf187eb8887d3b62ed0c438753340a3482785a392d52ec55ff"},{"path":"references/hyperparameter-tuning.md","size":16582,"sha256":"38f931981ac90df6928e1219aef920b625cdbb11e687480aab63a2776cc8b401"},{"path":"references/lora-peft.md","size":11030,"sha256":"dcdc48e7c1701c2da73b4a80e3b3398a0a9fea86b5a31b64845cdd3d667ce17e"}],"requires":{"mcp":[],"tools":[]},"safety":{"flags":[{"code":"code.eval","kind":"dangerous-code","where":"references/deployment-optimization.md:584","excerpt":"eval(","message":"evaluates code at runtime","severity":"warn"},{"code":"code.eval","kind":"dangerous-code","where":"references/evaluation-metrics.md:32","excerpt":"eval(","message":"evaluates code at runtime","severity":"warn"}],"scannedAt":"2026-08-22","hasScripts":false,"networkEndpoints":["jeffallan.github.io"]}}