[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M6MAQSQT5Z211KEYFKVT01QZ":3,"skill-same:01M6MAQSQT5Z211KEYFKVT01QZ":84},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":39,"linkOnly":40,"markdownContent":41,"markdownBody":42,"frontMatter":43,"refName":50,"commitSha":51,"skillDirectory":52,"forksCount":53,"repositoryHtmlUrl":54,"repositoryHomepageUrl":55,"ownerHtmlUrl":56,"ownerBlogUrl":57,"ownerDescription":58,"updatedAt":59,"install":60,"resources":73},"01M6MAQSQT5Z211KEYFKVT01QZ","peft-fine-tuning",null,"Fine-tunes LLMs with Hugging Face PEFT, using LoRA, QLoRA, IA3, AdaLoRA, prefix tuning, and prompt tuning so that under 1% of parameters are trained. Covers rank, alpha, and target module selection, loading and merging adapters, multi-adapter serving, and integration with TRL SFTTrainer, Axolotl, and vLLM. Use when fine-tuning 7B-70B models on limited GPU memory, when running QLoRA on a single 24GB GPU, when choosing LoRA rank and alpha, when merging or swapping adapters on one base model, or when debugging CUDA OOM or an adapter that does not apply. Not for full fine-tuning of small models under 1B parameters or cases needing all weights updated.","hashgraph-online\u002Fawesome-codex-plugins","hashgraph-online","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F160371962?v=4","01M6CNB6NRJVNNGN14TR01Q6D3","HOL (Hashgraph Online)","01M6JT1HSKKG9V68KQAQGBA7XP",1575,"MIT","https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002FKalarisLabs\u002Fresearch-agent-skills\u002Fskills\u002Fpeft-fine-tuning\u002FSKILL.md",[],[19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38],"PEFT","tuning","fine tuning","peft fine tuning","use","apply","for","Bitsandbytes","Memory Optimization","module","full","selection","training","prompt","hugging face","vLLM","QLoRA","LoRA","Axolotl","Fine-Tuning","2026-11-10 10:12:30",false,"---\nname: peft-fine-tuning\ndescription: Fine-tunes LLMs with Hugging Face PEFT, using LoRA, QLoRA, IA3, AdaLoRA, prefix tuning, and prompt tuning so that under 1% of parameters are trained. Covers rank, alpha, and target module selection, loading and merging adapters, multi-adapter serving, and integration with TRL SFTTrainer, Axolotl, and vLLM. Use when fine-tuning 7B-70B models on limited GPU memory, when running QLoRA on a single 24GB GPU, when choosing LoRA rank and alpha, when merging or swapping adapters on one base model, or when debugging CUDA OOM or an adapter that does not apply. Not for full fine-tuning of small models under 1B parameters or cases needing all weights updated.\nlicense: MIT\nmetadata:\n  version: 1.0.0\n  category: ml-training\n  maintainer: Kalaris Labs\n  tags: Fine-Tuning, PEFT, LoRA, QLoRA, Parameter-Efficient, Adapters, Low-Rank, Memory Optimization, Multi-Adapter\n  dependencies: peft>=0.13.0, transformers>=4.45.0, torch>=2.0.0, bitsandbytes>=0.43.0\n---\n\n# PEFT (Parameter-Efficient Fine-Tuning)\n\nFine-tune LLMs by training \u003C1% of parameters using LoRA, QLoRA, and 25+ adapter methods.\n\n## When to use PEFT\n\n**Use PEFT\u002FLoRA when:**\n- Fine-tuning 7B-70B models on consumer GPUs (RTX 4090, A100)\n- Need to train \u003C1% parameters (6MB adapters vs 14GB full model)\n- Want fast iteration with multiple task-specific adapters\n- Deploying multiple fine-tuned variants from one base model\n\n**Use QLoRA (PEFT + quantization) when:**\n- Fine-tuning 70B models on single 24GB GPU\n- Memory is the primary constraint\n- Can accept ~5% quality trade-off vs full fine-tuning\n\n**Use full fine-tuning instead when:**\n- Training small models (\u003C1B parameters)\n- Need maximum quality and have compute budget\n- Significant domain shift requires updating all weights\n\n## Quick start\n\n### Installation\n\n```bash\n# Basic installation\npip install peft\n\n# With quantization support (recommended)\npip install peft bitsandbytes\n\n# Full stack\npip install peft transformers accelerate bitsandbytes datasets\n```\n\n### LoRA fine-tuning (standard)\n\n```python\nfrom transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer\nfrom peft import get_peft_model, LoraConfig, TaskType\nfrom datasets import load_dataset\n\n# Load base model\nmodel_name = \"meta-llama\u002FLlama-3.1-8B\"\nmodel = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=\"auto\", device_map=\"auto\")\ntokenizer = AutoTokenizer.from_pretrained(model_name)\ntokenizer.pad_token = tokenizer.eos_token\n\n# LoRA configuration\nlora_config = LoraConfig(\n    task_type=TaskType.CAUSAL_LM,\n    r=16,                          # Rank (8-64, higher = more capacity)\n    lora_alpha=32,                 # Scaling factor (typically 2*r)\n    lora_dropout=0.05,             # Dropout for regularization\n    target_modules=[\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"],  # Attention layers\n    bias=\"none\"                    # Don't train biases\n)\n\n# Apply LoRA\nmodel = get_peft_model(model, lora_config)\nmodel.print_trainable_parameters()\n# Output: trainable params: 13,631,488 || all params: 8,043,307,008 || trainable%: 0.17%\n\n# Prepare dataset\ndataset = load_dataset(\"databricks\u002Fdatabricks-dolly-15k\", split=\"train\")\n\ndef tokenize(example):\n    text = f\"### Instruction:\\n{example['instruction']}\\n\\n### Response:\\n{example['response']}\"\n    return tokenizer(text, truncation=True, max_length=512, padding=\"max_length\")\n\ntokenized = dataset.map(tokenize, remove_columns=dataset.column_names)\n\n# Training\ntraining_args = TrainingArguments(\n    output_dir=\".\u002Flora-llama\",\n    num_train_epochs=3,\n    per_device_train_batch_size=4,\n    gradient_accumulation_steps=4,\n    learning_rate=2e-4,\n    fp16=True,\n    logging_steps=10,\n    save_strategy=\"epoch\"\n)\n\ntrainer = Trainer(\n    model=model,\n    args=training_args,\n    train_dataset=tokenized,\n    data_collator=lambda data: {\"input_ids\": torch.stack([f[\"input_ids\"] for f in data]),\n                                 \"attention_mask\": torch.stack([f[\"attention_mask\"] for f in data]),\n                                 \"labels\": torch.stack([f[\"input_ids\"] for f in data])}\n)\n\ntrainer.train()\n\n# Save adapter only (6MB vs 16GB)\nmodel.save_pretrained(\".\u002Flora-llama-adapter\")\n```\n\n### QLoRA fine-tuning (memory-efficient)\n\n```python\nfrom transformers import AutoModelForCausalLM, BitsAndBytesConfig\nfrom peft import get_peft_model, LoraConfig, prepare_model_for_kbit_training\n\n# 4-bit quantization config\nbnb_config = BitsAndBytesConfig(\n    load_in_4bit=True,\n    bnb_4bit_quant_type=\"nf4\",           # NormalFloat4 (best for LLMs)\n    bnb_4bit_compute_dtype=\"bfloat16\",   # Compute in bf16\n    bnb_4bit_use_double_quant=True       # Nested quantization\n)\n\n# Load quantized model\nmodel = AutoModelForCausalLM.from_pretrained(\n    \"meta-llama\u002FLlama-3.1-70B\",\n    quantization_config=bnb_config,\n    device_map=\"auto\"\n)\n\n# Prepare for training (enables gradient checkpointing)\nmodel = prepare_model_for_kbit_training(model)\n\n# LoRA config for QLoRA\nlora_config = LoraConfig(\n    r=64,                              # Higher rank for 70B\n    lora_alpha=128,\n    lora_dropout=0.1,\n    target_modules=[\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\", \"gate_proj\", \"up_proj\", \"down_proj\"],\n    bias=\"none\",\n    task_type=\"CAUSAL_LM\"\n)\n\nmodel = get_peft_model(model, lora_config)\n# 70B model now fits on single 24GB GPU!\n```\n\n## LoRA parameter selection\n\n### Rank (r) - capacity vs efficiency\n\n| Rank | Trainable Params | Memory | Quality | Use Case |\n|------|-----------------|--------|---------|----------|\n| 4 | ~3M | Minimal | Lower | Simple tasks, prototyping |\n| **8** | ~7M | Low | Good | **Recommended starting point** |\n| **16** | ~14M | Medium | Better | **General fine-tuning** |\n| 32 | ~27M | Higher | High | Complex tasks |\n| 64 | ~54M | High | Highest | Domain adaptation, 70B models |\n\n### Alpha (lora_alpha) - scaling factor\n\n```python\n# Rule of thumb: alpha = 2 * rank\nLoraConfig(r=16, lora_alpha=32)  # Standard\nLoraConfig(r=16, lora_alpha=16)  # Conservative (lower learning rate effect)\nLoraConfig(r=16, lora_alpha=64)  # Aggressive (higher learning rate effect)\n```\n\n### Target modules by architecture\n\n```python\n# Llama \u002F Mistral \u002F Qwen\ntarget_modules = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\", \"gate_proj\", \"up_proj\", \"down_proj\"]\n\n# GPT-2 \u002F GPT-Neo\ntarget_modules = [\"c_attn\", \"c_proj\", \"c_fc\"]\n\n# Falcon\ntarget_modules = [\"query_key_value\", \"dense\", \"dense_h_to_4h\", \"dense_4h_to_h\"]\n\n# BLOOM\ntarget_modules = [\"query_key_value\", \"dense\", \"dense_h_to_4h\", \"dense_4h_to_h\"]\n\n# Auto-detect all linear layers\ntarget_modules = \"all-linear\"  # PEFT 0.6.0+\n```\n\n## Loading and merging adapters\n\n### Load trained adapter\n\n```python\nfrom peft import PeftModel, AutoPeftModelForCausalLM\nfrom transformers import AutoModelForCausalLM\n\n# Option 1: Load with PeftModel\nbase_model = AutoModelForCausalLM.from_pretrained(\"meta-llama\u002FLlama-3.1-8B\")\nmodel = PeftModel.from_pretrained(base_model, \".\u002Flora-llama-adapter\")\n\n# Option 2: Load directly (recommended)\nmodel = AutoPeftModelForCausalLM.from_pretrained(\n    \".\u002Flora-llama-adapter\",\n    device_map=\"auto\"\n)\n```\n\n### Merge adapter into base model\n\n```python\n# Merge for deployment (no adapter overhead)\nmerged_model = model.merge_and_unload()\n\n# Save merged model\nmerged_model.save_pretrained(\".\u002Fllama-merged\")\ntokenizer.save_pretrained(\".\u002Fllama-merged\")\n\n# Push to Hub\nmerged_model.push_to_hub(\"username\u002Fllama-finetuned\")\n```\n\n### Multi-adapter serving\n\n```python\nfrom peft import PeftModel\n\n# Load base with first adapter\nmodel = AutoPeftModelForCausalLM.from_pretrained(\".\u002Fadapter-task1\")\n\n# Load additional adapters\nmodel.load_adapter(\".\u002Fadapter-task2\", adapter_name=\"task2\")\nmodel.load_adapter(\".\u002Fadapter-task3\", adapter_name=\"task3\")\n\n# Switch between adapters at runtime\nmodel.set_adapter(\"task1\")  # Use task1 adapter\noutput1 = model.generate(**inputs)\n\nmodel.set_adapter(\"task2\")  # Switch to task2\noutput2 = model.generate(**inputs)\n\n# Disable adapters (use base model)\nwith model.disable_adapter():\n    base_output = model.generate(**inputs)\n```\n\n## PEFT methods comparison\n\n| Method | Trainable % | Memory | Speed | Best For |\n|--------|------------|--------|-------|----------|\n| **LoRA** | 0.1-1% | Low | Fast | General fine-tuning |\n| **QLoRA** | 0.1-1% | Very Low | Medium | Memory-constrained |\n| AdaLoRA | 0.1-1% | Low | Medium | Automatic rank selection |\n| IA3 | 0.01% | Minimal | Fastest | Few-shot adaptation |\n| Prefix Tuning | 0.1% | Low | Medium | Generation control |\n| Prompt Tuning | 0.001% | Minimal | Fast | Simple task adaptation |\n| P-Tuning v2 | 0.1% | Low | Medium | NLU tasks |\n\n### IA3 (minimal parameters)\n\n```python\nfrom peft import IA3Config\n\nia3_config = IA3Config(\n    target_modules=[\"q_proj\", \"v_proj\", \"k_proj\", \"down_proj\"],\n    feedforward_modules=[\"down_proj\"]\n)\nmodel = get_peft_model(model, ia3_config)\n# Trains only 0.01% of parameters!\n```\n\n### Prefix Tuning\n\n```python\nfrom peft import PrefixTuningConfig\n\nprefix_config = PrefixTuningConfig(\n    task_type=\"CAUSAL_LM\",\n    num_virtual_tokens=20,      # Prepended tokens\n    prefix_projection=True       # Use MLP projection\n)\nmodel = get_peft_model(model, prefix_config)\n```\n\n## Integration patterns\n\n### With TRL (SFTTrainer)\n\n```python\nfrom trl import SFTTrainer, SFTConfig\nfrom peft import LoraConfig\n\nlora_config = LoraConfig(r=16, lora_alpha=32, target_modules=\"all-linear\")\n\ntrainer = SFTTrainer(\n    model=model,\n    args=SFTConfig(output_dir=\".\u002Foutput\", max_seq_length=512),\n    train_dataset=dataset,\n    peft_config=lora_config,  # Pass LoRA config directly\n)\ntrainer.train()\n```\n\n### With Axolotl (YAML config)\n\n```yaml\n# axolotl config.yaml\nadapter: lora\nlora_r: 16\nlora_alpha: 32\nlora_dropout: 0.05\nlora_target_modules:\n  - q_proj\n  - v_proj\n  - k_proj\n  - o_proj\nlora_target_linear: true  # Target all linear layers\n```\n\n### With vLLM (inference)\n\n```python\nfrom vllm import LLM\nfrom vllm.lora.request import LoRARequest\n\n# Load base model with LoRA support\nllm = LLM(model=\"meta-llama\u002FLlama-3.1-8B\", enable_lora=True)\n\n# Serve with adapter\noutputs = llm.generate(\n    prompts,\n    lora_request=LoRARequest(\"adapter1\", 1, \".\u002Flora-adapter\")\n)\n```\n\n## Performance benchmarks\n\n### Memory usage (Llama 3.1 8B)\n\n| Method | GPU Memory | Trainable Params |\n|--------|-----------|------------------|\n| Full fine-tuning | 60+ GB | 8B (100%) |\n| LoRA r=16 | 18 GB | 14M (0.17%) |\n| QLoRA r=16 | 6 GB | 14M (0.17%) |\n| IA3 | 16 GB | 800K (0.01%) |\n\n### Training speed (A100 80GB)\n\n| Method | Tokens\u002Fsec | vs Full FT |\n|--------|-----------|------------|\n| Full FT | 2,500 | 1x |\n| LoRA | 3,200 | 1.3x |\n| QLoRA | 2,100 | 0.84x |\n\n### Quality (MMLU benchmark)\n\n| Model | Full FT | LoRA | QLoRA |\n|-------|---------|------|-------|\n| Llama 2-7B | 45.3 | 44.8 | 44.1 |\n| Llama 2-13B | 54.8 | 54.2 | 53.5 |\n\n## Common issues\n\n### CUDA OOM during training\n\n```python\n# Solution 1: Enable gradient checkpointing\nmodel.gradient_checkpointing_enable()\n\n# Solution 2: Reduce batch size + increase accumulation\nTrainingArguments(\n    per_device_train_batch_size=1,\n    gradient_accumulation_steps=16\n)\n\n# Solution 3: Use QLoRA\nfrom transformers import BitsAndBytesConfig\nbnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type=\"nf4\")\n```\n\n### Adapter not applying\n\n```python\n# Verify adapter is active\nprint(model.active_adapters)  # Should show adapter name\n\n# Check trainable parameters\nmodel.print_trainable_parameters()\n\n# Ensure model in training mode\nmodel.train()\n```\n\n### Quality degradation\n\n```python\n# Increase rank\nLoraConfig(r=32, lora_alpha=64)\n\n# Target more modules\ntarget_modules = \"all-linear\"\n\n# Use more training data and epochs\nTrainingArguments(num_train_epochs=5)\n\n# Lower learning rate\nTrainingArguments(learning_rate=1e-4)\n```\n\n## Best practices\n\n1. **Start with r=8-16**, increase if quality insufficient\n2. **Use alpha = 2 * rank** as starting point\n3. **Target attention + MLP layers** for best quality\u002Fefficiency\n4. **Enable gradient checkpointing** for memory savings\n5. **Save adapters frequently** (small files, easy rollback)\n6. **Evaluate on held-out data** before merging\n7. **Use QLoRA for 70B+ models** on consumer hardware\n\n## References\n\n- **[Advanced Usage](references\u002Fadvanced-usage.md)** - DoRA, LoftQ, rank stabilization, custom modules\n- **[Troubleshooting](references\u002Ftroubleshooting.md)** - Common errors, debugging, optimization\n\n## Resources\n\n- **GitHub**: https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Fpeft\n- **Docs**: https:\u002F\u002Fhuggingface.co\u002Fdocs\u002Fpeft\n- **LoRA Paper**: arXiv:2106.09685\n- **QLoRA Paper**: arXiv:2305.14314\n- **Models**: https:\u002F\u002Fhuggingface.co\u002Fmodels?library=peft\n\n## Related skills\n\n- `unsloth`: Provides guidance on fine-tuning large language models with Unsloth, a library for faster, lower-memory training using LoRA and QLoRA, base…\n- `axolotl`: Provides guidance for fine-tuning large language models with Axolotl, covering YAML training configs, LoRA and QLoRA, preference training w…\n- `stable-diffusion-image-generation`: Generates images with Stable Diffusion models (SD 1.5, SDXL, SD 3.0, Flux) through the HuggingFace Diffusers library, covering text-to-imag…\n","# PEFT (Parameter-Efficient Fine-Tuning)\n\nFine-tune LLMs by training \u003C1% of parameters using LoRA, QLoRA, and 25+ adapter methods.\n\n## When to use PEFT\n\n**Use PEFT\u002FLoRA when:**\n- Fine-tuning 7B-70B models on consumer GPUs (RTX 4090, A100)\n- Need to train \u003C1% parameters (6MB adapters vs 14GB full model)\n- Want fast iteration with multiple task-specific adapters\n- Deploying multiple fine-tuned variants from one base model\n\n**Use QLoRA (PEFT + quantization) when:**\n- Fine-tuning 70B models on single 24GB GPU\n- Memory is the primary constraint\n- Can accept ~5% quality trade-off vs full fine-tuning\n\n**Use full fine-tuning instead when:**\n- Training small models (\u003C1B parameters)\n- Need maximum quality and have compute budget\n- Significant domain shift requires updating all weights\n\n## Quick start\n\n### Installation\n\n```bash\n# Basic installation\npip install peft\n\n# With quantization support (recommended)\npip install peft bitsandbytes\n\n# Full stack\npip install peft transformers accelerate bitsandbytes datasets\n```\n\n### LoRA fine-tuning (standard)\n\n```python\nfrom transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer\nfrom peft import get_peft_model, LoraConfig, TaskType\nfrom datasets import load_dataset\n\n# Load base model\nmodel_name = \"meta-llama\u002FLlama-3.1-8B\"\nmodel = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=\"auto\", device_map=\"auto\")\ntokenizer = AutoTokenizer.from_pretrained(model_name)\ntokenizer.pad_token = tokenizer.eos_token\n\n# LoRA configuration\nlora_config = LoraConfig(\n    task_type=TaskType.CAUSAL_LM,\n    r=16,                          # Rank (8-64, higher = more capacity)\n    lora_alpha=32,                 # Scaling factor (typically 2*r)\n    lora_dropout=0.05,             # Dropout for regularization\n    target_modules=[\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"],  # Attention layers\n    bias=\"none\"                    # Don't train biases\n)\n\n# Apply LoRA\nmodel = get_peft_model(model, lora_config)\nmodel.print_trainable_parameters()\n# Output: trainable params: 13,631,488 || all params: 8,043,307,008 || trainable%: 0.17%\n\n# Prepare dataset\ndataset = load_dataset(\"databricks\u002Fdatabricks-dolly-15k\", split=\"train\")\n\ndef tokenize(example):\n    text = f\"### Instruction:\\n{example['instruction']}\\n\\n### Response:\\n{example['response']}\"\n    return tokenizer(text, truncation=True, max_length=512, padding=\"max_length\")\n\ntokenized = dataset.map(tokenize, remove_columns=dataset.column_names)\n\n# Training\ntraining_args = TrainingArguments(\n    output_dir=\".\u002Flora-llama\",\n    num_train_epochs=3,\n    per_device_train_batch_size=4,\n    gradient_accumulation_steps=4,\n    learning_rate=2e-4,\n    fp16=True,\n    logging_steps=10,\n    save_strategy=\"epoch\"\n)\n\ntrainer = Trainer(\n    model=model,\n    args=training_args,\n    train_dataset=tokenized,\n    data_collator=lambda data: {\"input_ids\": torch.stack([f[\"input_ids\"] for f in data]),\n                                 \"attention_mask\": torch.stack([f[\"attention_mask\"] for f in data]),\n                                 \"labels\": torch.stack([f[\"input_ids\"] for f in data])}\n)\n\ntrainer.train()\n\n# Save adapter only (6MB vs 16GB)\nmodel.save_pretrained(\".\u002Flora-llama-adapter\")\n```\n\n### QLoRA fine-tuning (memory-efficient)\n\n```python\nfrom transformers import AutoModelForCausalLM, BitsAndBytesConfig\nfrom peft import get_peft_model, LoraConfig, prepare_model_for_kbit_training\n\n# 4-bit quantization config\nbnb_config = BitsAndBytesConfig(\n    load_in_4bit=True,\n    bnb_4bit_quant_type=\"nf4\",           # NormalFloat4 (best for LLMs)\n    bnb_4bit_compute_dtype=\"bfloat16\",   # Compute in bf16\n    bnb_4bit_use_double_quant=True       # Nested quantization\n)\n\n# Load quantized model\nmodel = AutoModelForCausalLM.from_pretrained(\n    \"meta-llama\u002FLlama-3.1-70B\",\n    quantization_config=bnb_config,\n    device_map=\"auto\"\n)\n\n# Prepare for training (enables gradient checkpointing)\nmodel = prepare_model_for_kbit_training(model)\n\n# LoRA config for QLoRA\nlora_config = LoraConfig(\n    r=64,                              # Higher rank for 70B\n    lora_alpha=128,\n    lora_dropout=0.1,\n    target_modules=[\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\", \"gate_proj\", \"up_proj\", \"down_proj\"],\n    bias=\"none\",\n    task_type=\"CAUSAL_LM\"\n)\n\nmodel = get_peft_model(model, lora_config)\n# 70B model now fits on single 24GB GPU!\n```\n\n## LoRA parameter selection\n\n### Rank (r) - capacity vs efficiency\n\n| Rank | Trainable Params | Memory | Quality | Use Case |\n|------|-----------------|--------|---------|----------|\n| 4 | ~3M | Minimal | Lower | Simple tasks, prototyping |\n| **8** | ~7M | Low | Good | **Recommended starting point** |\n| **16** | ~14M | Medium | Better | **General fine-tuning** |\n| 32 | ~27M | Higher | High | Complex tasks |\n| 64 | ~54M | High | Highest | Domain adaptation, 70B models |\n\n### Alpha (lora_alpha) - scaling factor\n\n```python\n# Rule of thumb: alpha = 2 * rank\nLoraConfig(r=16, lora_alpha=32)  # Standard\nLoraConfig(r=16, lora_alpha=16)  # Conservative (lower learning rate effect)\nLoraConfig(r=16, lora_alpha=64)  # Aggressive (higher learning rate effect)\n```\n\n### Target modules by architecture\n\n```python\n# Llama \u002F Mistral \u002F Qwen\ntarget_modules = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\", \"gate_proj\", \"up_proj\", \"down_proj\"]\n\n# GPT-2 \u002F GPT-Neo\ntarget_modules = [\"c_attn\", \"c_proj\", \"c_fc\"]\n\n# Falcon\ntarget_modules = [\"query_key_value\", \"dense\", \"dense_h_to_4h\", \"dense_4h_to_h\"]\n\n# BLOOM\ntarget_modules = [\"query_key_value\", \"dense\", \"dense_h_to_4h\", \"dense_4h_to_h\"]\n\n# Auto-detect all linear layers\ntarget_modules = \"all-linear\"  # PEFT 0.6.0+\n```\n\n## Loading and merging adapters\n\n### Load trained adapter\n\n```python\nfrom peft import PeftModel, AutoPeftModelForCausalLM\nfrom transformers import AutoModelForCausalLM\n\n# Option 1: Load with PeftModel\nbase_model = AutoModelForCausalLM.from_pretrained(\"meta-llama\u002FLlama-3.1-8B\")\nmodel = PeftModel.from_pretrained(base_model, \".\u002Flora-llama-adapter\")\n\n# Option 2: Load directly (recommended)\nmodel = AutoPeftModelForCausalLM.from_pretrained(\n    \".\u002Flora-llama-adapter\",\n    device_map=\"auto\"\n)\n```\n\n### Merge adapter into base model\n\n```python\n# Merge for deployment (no adapter overhead)\nmerged_model = model.merge_and_unload()\n\n# Save merged model\nmerged_model.save_pretrained(\".\u002Fllama-merged\")\ntokenizer.save_pretrained(\".\u002Fllama-merged\")\n\n# Push to Hub\nmerged_model.push_to_hub(\"username\u002Fllama-finetuned\")\n```\n\n### Multi-adapter serving\n\n```python\nfrom peft import PeftModel\n\n# Load base with first adapter\nmodel = AutoPeftModelForCausalLM.from_pretrained(\".\u002Fadapter-task1\")\n\n# Load additional adapters\nmodel.load_adapter(\".\u002Fadapter-task2\", adapter_name=\"task2\")\nmodel.load_adapter(\".\u002Fadapter-task3\", adapter_name=\"task3\")\n\n# Switch between adapters at runtime\nmodel.set_adapter(\"task1\")  # Use task1 adapter\noutput1 = model.generate(**inputs)\n\nmodel.set_adapter(\"task2\")  # Switch to task2\noutput2 = model.generate(**inputs)\n\n# Disable adapters (use base model)\nwith model.disable_adapter():\n    base_output = model.generate(**inputs)\n```\n\n## PEFT methods comparison\n\n| Method | Trainable % | Memory | Speed | Best For |\n|--------|------------|--------|-------|----------|\n| **LoRA** | 0.1-1% | Low | Fast | General fine-tuning |\n| **QLoRA** | 0.1-1% | Very Low | Medium | Memory-constrained |\n| AdaLoRA | 0.1-1% | Low | Medium | Automatic rank selection |\n| IA3 | 0.01% | Minimal | Fastest | Few-shot adaptation |\n| Prefix Tuning | 0.1% | Low | Medium | Generation control |\n| Prompt Tuning | 0.001% | Minimal | Fast | Simple task adaptation |\n| P-Tuning v2 | 0.1% | Low | Medium | NLU tasks |\n\n### IA3 (minimal parameters)\n\n```python\nfrom peft import IA3Config\n\nia3_config = IA3Config(\n    target_modules=[\"q_proj\", \"v_proj\", \"k_proj\", \"down_proj\"],\n    feedforward_modules=[\"down_proj\"]\n)\nmodel = get_peft_model(model, ia3_config)\n# Trains only 0.01% of parameters!\n```\n\n### Prefix Tuning\n\n```python\nfrom peft import PrefixTuningConfig\n\nprefix_config = PrefixTuningConfig(\n    task_type=\"CAUSAL_LM\",\n    num_virtual_tokens=20,      # Prepended tokens\n    prefix_projection=True       # Use MLP projection\n)\nmodel = get_peft_model(model, prefix_config)\n```\n\n## Integration patterns\n\n### With TRL (SFTTrainer)\n\n```python\nfrom trl import SFTTrainer, SFTConfig\nfrom peft import LoraConfig\n\nlora_config = LoraConfig(r=16, lora_alpha=32, target_modules=\"all-linear\")\n\ntrainer = SFTTrainer(\n    model=model,\n    args=SFTConfig(output_dir=\".\u002Foutput\", max_seq_length=512),\n    train_dataset=dataset,\n    peft_config=lora_config,  # Pass LoRA config directly\n)\ntrainer.train()\n```\n\n### With Axolotl (YAML config)\n\n```yaml\n# axolotl config.yaml\nadapter: lora\nlora_r: 16\nlora_alpha: 32\nlora_dropout: 0.05\nlora_target_modules:\n  - q_proj\n  - v_proj\n  - k_proj\n  - o_proj\nlora_target_linear: true  # Target all linear layers\n```\n\n### With vLLM (inference)\n\n```python\nfrom vllm import LLM\nfrom vllm.lora.request import LoRARequest\n\n# Load base model with LoRA support\nllm = LLM(model=\"meta-llama\u002FLlama-3.1-8B\", enable_lora=True)\n\n# Serve with adapter\noutputs = llm.generate(\n    prompts,\n    lora_request=LoRARequest(\"adapter1\", 1, \".\u002Flora-adapter\")\n)\n```\n\n## Performance benchmarks\n\n### Memory usage (Llama 3.1 8B)\n\n| Method | GPU Memory | Trainable Params |\n|--------|-----------|------------------|\n| Full fine-tuning | 60+ GB | 8B (100%) |\n| LoRA r=16 | 18 GB | 14M (0.17%) |\n| QLoRA r=16 | 6 GB | 14M (0.17%) |\n| IA3 | 16 GB | 800K (0.01%) |\n\n### Training speed (A100 80GB)\n\n| Method | Tokens\u002Fsec | vs Full FT |\n|--------|-----------|------------|\n| Full FT | 2,500 | 1x |\n| LoRA | 3,200 | 1.3x |\n| QLoRA | 2,100 | 0.84x |\n\n### Quality (MMLU benchmark)\n\n| Model | Full FT | LoRA | QLoRA |\n|-------|---------|------|-------|\n| Llama 2-7B | 45.3 | 44.8 | 44.1 |\n| Llama 2-13B | 54.8 | 54.2 | 53.5 |\n\n## Common issues\n\n### CUDA OOM during training\n\n```python\n# Solution 1: Enable gradient checkpointing\nmodel.gradient_checkpointing_enable()\n\n# Solution 2: Reduce batch size + increase accumulation\nTrainingArguments(\n    per_device_train_batch_size=1,\n    gradient_accumulation_steps=16\n)\n\n# Solution 3: Use QLoRA\nfrom transformers import BitsAndBytesConfig\nbnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type=\"nf4\")\n```\n\n### Adapter not applying\n\n```python\n# Verify adapter is active\nprint(model.active_adapters)  # Should show adapter name\n\n# Check trainable parameters\nmodel.print_trainable_parameters()\n\n# Ensure model in training mode\nmodel.train()\n```\n\n### Quality degradation\n\n```python\n# Increase rank\nLoraConfig(r=32, lora_alpha=64)\n\n# Target more modules\ntarget_modules = \"all-linear\"\n\n# Use more training data and epochs\nTrainingArguments(num_train_epochs=5)\n\n# Lower learning rate\nTrainingArguments(learning_rate=1e-4)\n```\n\n## Best practices\n\n1. **Start with r=8-16**, increase if quality insufficient\n2. **Use alpha = 2 * rank** as starting point\n3. **Target attention + MLP layers** for best quality\u002Fefficiency\n4. **Enable gradient checkpointing** for memory savings\n5. **Save adapters frequently** (small files, easy rollback)\n6. **Evaluate on held-out data** before merging\n7. **Use QLoRA for 70B+ models** on consumer hardware\n\n## References\n\n- **[Advanced Usage](references\u002Fadvanced-usage.md)** - DoRA, LoftQ, rank stabilization, custom modules\n- **[Troubleshooting](references\u002Ftroubleshooting.md)** - Common errors, debugging, optimization\n\n## Resources\n\n- **GitHub**: https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Fpeft\n- **Docs**: https:\u002F\u002Fhuggingface.co\u002Fdocs\u002Fpeft\n- **LoRA Paper**: arXiv:2106.09685\n- **QLoRA Paper**: arXiv:2305.14314\n- **Models**: https:\u002F\u002Fhuggingface.co\u002Fmodels?library=peft\n\n## Related skills\n\n- `unsloth`: Provides guidance on fine-tuning large language models with Unsloth, a library for faster, lower-memory training using LoRA and QLoRA, base…\n- `axolotl`: Provides guidance for fine-tuning large language models with Axolotl, covering YAML training configs, LoRA and QLoRA, preference training w…\n- `stable-diffusion-image-generation`: Generates images with Stable Diffusion models (SD 1.5, SDXL, SD 3.0, Flux) through the HuggingFace Diffusers library, covering text-to-imag…\n",{"name":5,"license":15,"metadata":44,"description":7},{"tags":45,"version":46,"category":47,"maintainer":48,"dependencies":49},"Fine-Tuning, PEFT, LoRA, QLoRA, Parameter-Efficient, Adapters, Low-Rank, Memory Optimization, Multi-Adapter","1.0.0","ml-training","Kalaris Labs","peft>=0.13.0, transformers>=4.45.0, torch>=2.0.0, bitsandbytes>=0.43.0","main","2b63f856ce19c2a797929cacabd9e0fc936812fa","plugins\u002FKalarisLabs\u002Fresearch-agent-skills\u002Fskills\u002Fpeft-fine-tuning",491,"https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins","https:\u002F\u002Fhol.org\u002Fplugins\u002Fbest-codex-plugins","https:\u002F\u002Fgithub.com\u002Fhashgraph-online","https:\u002F\u002Fhol.org","Open-source standards and developer tools for interoperable AI agents: discovery, identity, communication, payments, privacy and security.","2026-11-14 14:06:13",{"source":8,"skillName":5,"commands":61,"prompt":71,"zipAvailable":72},[62,65,68],{"tool":63,"command":64},"npx","npx skills add hashgraph-online\u002Fawesome-codex-plugins --skill peft-fine-tuning",{"tool":66,"command":67},"bunx","bunx skills add hashgraph-online\u002Fawesome-codex-plugins --skill peft-fine-tuning",{"tool":69,"command":70},"pnpm","pnpm dlx skills add hashgraph-online\u002Fawesome-codex-plugins --skill peft-fine-tuning","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002FKalarisLabs\u002Fresearch-agent-skills\u002Fskills\u002Fpeft-fine-tuning\u002FSKILL.md",true,[74,80],{"path":75,"resourceType":76,"resourceTypeDesc":77,"sizeBytes":78,"sourceHtmlUrl":79},"references\u002Fadvanced-usage.md","REFERENCE","参考文档",12541,"https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002FKalarisLabs\u002Fresearch-agent-skills\u002Fskills\u002Fpeft-fine-tuning\u002Freferences\u002Fadvanced-usage.md",{"path":81,"resourceType":76,"resourceTypeDesc":77,"sizeBytes":82,"sourceHtmlUrl":83},"references\u002Ftroubleshooting.md",10344,"https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002FKalarisLabs\u002Fresearch-agent-skills\u002Fskills\u002Fpeft-fine-tuning\u002Freferences\u002Ftroubleshooting.md",[85,94,102,110,118,126],{"skillNo":86,"name":87,"nameZh":6,"description":88,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":89,"sourceHtmlUrl":90,"categories":91,"keywords":92,"publishedAt":93},"01M7DV332YQMGD8WTRJ9DZQXTB","worktable-review-with-annotations","Create, inspect, reply to, update, or resolve situated Worktable annotations. Use when feedback belongs to a Doc section, block, text range, or HTML Doc, or when the user asks to address existing review feedback and coordinate a requested artifact edit.","https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002Fworktable\u002Fworktable-plugin\u002Fskills\u002Fworktable-review-with-annotations\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002Fworktable\u002Fworktable-plugin\u002Fskills\u002Fworktable-review-with-annotations\u002FSKILL.md",[],[],"2026-11-15 14:19:17",{"skillNo":95,"name":96,"nameZh":6,"description":97,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":98,"sourceHtmlUrl":99,"categories":100,"keywords":101,"publishedAt":93},"01M7DV2YGHCZVBEB1AHYB6CGHE","worktable-find-and-synthesize-context","Find, assess, and synthesize context from Worktable Docs and Records. Use when the user asks what a workspace contains, wants workspace-backed research, needs current facts reconciled across artifacts, or requests a cited synthesis without changing workspace state.","https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002Fworktable\u002Fworktable-plugin\u002Fskills\u002Fworktable-find-and-synthesize-context\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002Fworktable\u002Fworktable-plugin\u002Fskills\u002Fworktable-find-and-synthesize-context\u002FSKILL.md",[],[],{"skillNo":103,"name":104,"nameZh":6,"description":105,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":106,"sourceHtmlUrl":107,"categories":108,"keywords":109,"publishedAt":93},"01M7DV2TQN1H11ME2SAF9MXN7V","worktable-create-or-update-docs","Create, edit, rename, or maintain narrative Worktable Docs. Use for plans, decisions, briefs, notes, research syntheses, and other durable prose when the user requests a new artifact or a safe update to an existing one.","https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002Fworktable\u002Fworktable-plugin\u002Fskills\u002Fworktable-create-or-update-docs\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002Fworktable\u002Fworktable-plugin\u002Fskills\u002Fworktable-create-or-update-docs\u002FSKILL.md",[],[],{"skillNo":111,"name":112,"nameZh":6,"description":113,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":114,"sourceHtmlUrl":115,"categories":116,"keywords":117,"publishedAt":93},"01M7DV2NE452NEA8TXATKSNAJH","worktable-create-or-manage-records","Organize repeated structured information as durable Worktable Records. Use for research datasets, inventories, directories, pipelines, trackers, and other collections whose items need identity, fields, filtering, links, validation, or individual updates.","https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002Fworktable\u002Fworktable-plugin\u002Fskills\u002Fworktable-create-or-manage-records\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002Fworktable\u002Fworktable-plugin\u002Fskills\u002Fworktable-create-or-manage-records\u002FSKILL.md",[],[],{"skillNo":119,"name":120,"nameZh":6,"description":121,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":122,"sourceHtmlUrl":123,"categories":124,"keywords":125,"publishedAt":93},"01M7DV2HENRBR2KSCVM064WH0Y","worktable-create-or-manage-interactive-html","Create or improve durable visual and interactive artifacts in Worktable. Use for dashboards, trackers, calculators, mini apps, slide presentations, and other requests that benefit from a polished HTML Doc.","https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002Fworktable\u002Fworktable-plugin\u002Fskills\u002Fworktable-create-or-manage-interactive-html\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002Fworktable\u002Fworktable-plugin\u002Fskills\u002Fworktable-create-or-manage-interactive-html\u002FSKILL.md",[],[],{"skillNo":127,"name":128,"nameZh":6,"description":129,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":130,"sourceHtmlUrl":131,"categories":132,"keywords":133,"publishedAt":93},"01M7DV2CADR5R7RQFFYPNAGR9Y","worktable-collaborate-threads","Read, answer, and hand off messages through durable Worktable threads with users or other authenticated participants. Use when the user asks to check addressed messages, reply to a participant, continue a thread, request another agent's help, wait briefly for a response, or watch for comments and messages sent to the agent.","https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002Fworktable\u002Fworktable-plugin\u002Fskills\u002Fworktable-collaborate-threads\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fhashgraph-online\u002Fawesome-codex-plugins\u002Fblob\u002Fmain\u002Fplugins\u002Fworktable\u002Fworktable-plugin\u002Fskills\u002Fworktable-collaborate-threads\u002FSKILL.md",[],[]]