Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Environment:666ghj BettaFish PEFT

From Leeroopedia
Knowledge Sources
Domains Deep_Learning, NLP, Fine_Tuning
Last Updated 2026-10-08 21:30 GMT

Overview

HuggingFace PEFT (Parameter-Efficient Fine-Tuning) library for LoRA-based fine-tuning of Qwen3 sentiment models.

Description

This environment provides the PEFT library, which implements parameter-efficient fine-tuning techniques including LoRA (Low-Rank Adaptation). BettaFish uses PEFT to fine-tune Qwen3 models (0.6B, 4B, 8B) for sentiment classification. LoRA injects low-rank trainable matrices into the attention layers (`q_proj`, `k_proj`, `v_proj`, `o_proj`), enabling efficient training with a small fraction of total parameters. The library provides `LoraConfig`, `get_peft_model`, and `TaskType` for configuration.

Usage

Use this environment when training Qwen3 LoRA sentiment models. It is required by the Qwen3LoRAUniversal trainer.

System Requirements

Category Requirement Notes
OS Linux, macOS, Windows CUDA recommended for training
Hardware CUDA GPU recommended CPU training possible but very slow
Disk 1-16GB+ For model weights and LoRA adapters

Dependencies

System Packages

  • None beyond Python runtime

Python Packages

  • `peft` >= 0.7.0
  • `transformers` >= 4.51.0
  • `torch` >= 2.0.0
  • `accelerate` >= 0.25.0
  • `datasets` >= 2.14.0

Credentials

No credentials required.

Quick Install

pip install peft>=0.7.0 transformers>=4.51.0 torch>=2.0.0 accelerate>=0.25.0 datasets>=2.14.0

Code Evidence

LoRA configuration from `SentimentAnalysisModel/WeiboSentiment_SmallQwen/qwen3_lora_universal.py:167-180`:

from peft import LoraConfig, get_peft_model, TaskType, PeftModel

def _setup_lora(self, **kwargs):
    lora_r = kwargs.get('lora_r', self.config['lora_r'])
    lora_alpha = kwargs.get('lora_alpha', self.config['lora_alpha'])

    lora_config = LoraConfig(
        task_type=TaskType.CAUSAL_LM,
        r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=kwargs.get('lora_dropout', 0.1),
        target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
    )
    self.lora_model = get_peft_model(self.base_model, lora_config)

    total_params = sum(p.numel() for p in self.lora_model.parameters())
    trainable_params = sum(p.numel() for p in self.lora_model.parameters() if p.requires_grad)
    print(f"可训练参数比例: {trainable_params / total_params * 100:.2f}%")

Model size configurations from `SentimentAnalysisModel/WeiboSentiment_SmallQwen/models_config.py:8-38`:

QWEN3_MODELS = {
    "0.6B": {"lora_r": 16, "lora_alpha": 32, "recommended_batch_size": 32, "recommended_lr": 1e-3},
    "4B":  {"lora_r": 32, "lora_alpha": 64, "recommended_batch_size": 16, "recommended_lr": 5e-4},
    "8B":  {"lora_r": 64, "lora_alpha": 128, "recommended_batch_size": 8, "recommended_lr": 2e-4},
}

Common Errors

Error Message Cause Solution
`ImportError: cannot import name 'LoraConfig'` PEFT not installed or too old `pip install peft>=0.7.0`
`RuntimeError: CUDA out of memory` Insufficient VRAM for model size Use smaller model (0.6B) or reduce batch size
`ValueError: 不支持的模型大小` Invalid model_size argument Use one of: `0.6B`, `4B`, `8B`

Compatibility Notes

  • Target modules: LoRA targets `q_proj`, `k_proj`, `v_proj`, `o_proj` attention layers.
  • LoRA rank scaling: Larger models use higher LoRA rank (r=16 for 0.6B, r=32 for 4B, r=64 for 8B).
  • Gradient accumulation: Training uses `gradient_accumulation_steps=2` to effectively double batch size.
  • Learning rate: LoRA uses half the recommended learning rate (`recommended_lr / 2`).
  • Batch size: LoRA uses half the recommended batch size (`recommended_batch_size // 2`).

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment