PEFT (Parameter-Efficient Fine-Tuning)

repository·main·Indexed 12 days ago

https://github.com/huggingface/peft

A library providing state-of-the-art methods to adapt large pretrained models by fine-tuning only a small subset of parameters, reducing computational and storage requirements. It includes support for advanced techniques such as Activated LoRA (aLoRA) for faster inference via KV cache reuse and AdaMSS (Adaptive Matrix Decomposition with Subspace Selection) using AdamssConfig and AdamssAsaCallback.

Tokens
139.5K
Snippets
344
Records
576
Agent score
97%

What's inside PEFT

  1. What is 🤗 PEFT?

    main

    🤗 PEFT (Parameter-Efficient Fine-Tuning) is a library designed to adapt large pretrained models to downstream tasks without the prohibitive cost of full fine-tuning.

    Instead of updating all model parameters, PEFT methods only fine-tune a small number of additional parameters. This approach:

    • Significantly decreases computational and storage costs.
    • Enables training and storing large language models (LLMs) and other large-scale models on consumer hardware.
    • Achieves performance comparable to fully fine-tuned models.

    PEFT acts as a framework for various model adaptation methods (such as modifying weights, wrapping layers, or manipulating KV-caches) and serves as a reference implementation for many fine-tuning techniques.

  2. Understand the KappaTune Experiment and Strategy Comparison

    main

    The KappaTune experiment compares different fine-tuning strategies to evaluate task adaptation versus catastrophic forgetting. It measures performance on a downstream task (e.g., gsm8k) against a general-knowledge control dataset (e.g., WikiText).

    Strategies compared:

    • Baseline: No adaptation.
    • LoRA_Global: Classic LoRA applied to common projections (q_proj, k_proj, o_proj, v_proj, gate_proj, up_proj, down_proj).
    • KappaTune_LoRA: Uses the KappaTuneSelector with relative selection (e.g., top_p=0.2).

    When to use KappaTune: Use KappaTune when catastrophic forgetting is a concern. If your fine-tuning data is closely aligned with the model's pretraining distribution (e.g., math data for certain models), unrestricted LoRA might be better as it reinforces pre-training, whereas KappaTune is designed to preserve original knowledge while adapting.

  3. What is BEFT and when to use it

    main

    BEFT (Bias-Efficient Fine-Tuning) is a parameter-efficient fine-tuning algorithm that specifically fine-tunes only the added bias terms of value projections ($b_v$) in pretrained transformer models.

    When to use BEFT:

    • Low-data regimes: BEFT is designed to excel when you have limited training data. It requires significantly fewer parameters than LoRA while maintaining competitive or superior performance.

    When to avoid BEFT:

    • High-data regimes: In scenarios with abundant data, BEFT may show limited effectiveness compared to LoRA or full-parameter fine-tuning. In these cases, consider using LoRA instead.
  4. What is LoKr (Low-Rank Kronecker Product)?

    main

    LoKr is a LoRA-variant method that approximates a large weight matrix using two low-rank matrices combined via a Kronecker product. It can optionally use a third low-rank matrix for better fine-tuning control.

    Key characteristics:

    • Rank Preservation: By expressing the weight update matrix as a decomposition of a Kronecker product (creating a block matrix), LoKr can preserve the rank of the original weight matrix.
    • Efficiency: The Kronecker product can be vectorized by stacking matrix columns, which avoids the need to fully reconstruct the weight update matrix $\Delta W$, speeding up the process.
    • Parameterization: The size of the smaller matrices is determined by the rank parameter r.
  5. What is PEANuT and when to use it

    main

    PEANuT (Parameter-Efficient Adaptation with Weight-aware Neural Tweakers) is a parameter-efficient fine-tuning (PEFT) technique that generates adapter updates conditioned on the frozen pretrained weights. Unlike LoRA, which uses weight-agnostic linear low-rank updates, PEANuT uses an input projection A, an output projection B, and optional intermediate residual encoder/decoder pairs with non-linear activations to model more complex update patterns.

    Use PEANuT if:

    • You need higher theoretical expressiveness than linear low-rank updates (like LoRA).
    • You want better performance on specific tasks under similar parameter budgets.
    • You are working in very low-parameter regimes (e.g., around 0.2M trainable parameters).

    Avoid PEANuT if:

    • Memory usage is a primary constraint (PEANuT has higher memory usage than LoRA because ΔW is explicitly constructed before application).
    • Training and inference speed are critical (PEANuT is slower than LoRA, especially with deeper intermediate layers).
    • You want a method that is easy to tune (the non-linearity may require more careful hyperparameter tuning, particularly for learning rates).
  6. What is Wavelet Fine-Tuning (WaveFT)?

    main

    WaveFT is a parameter-efficient fine-tuning (PEFT) method that learns sparse updates in the wavelet domain of residual matrices.

    Unlike LoRA, which relies on discrete low-rank matrices, WaveFT learns a sparse set of coefficients in a transformed space. These coefficients are mapped back to the weight domain using the Inverse Discrete Wavelet Transform (IDWT). This approach allows for high-rank updates without adding inference overhead and provides finer control over the number of trainable parameters, making it highly effective in extreme low-parameter regimes.

    Constraint:

    • Currently, only nn.Linear layers are supported.
  7. What is BD-LoRA and when to use it

    main

    Block-Diagonal LoRA (BD-LoRA) is a variant of LoRA where some LoRA factors are constrained to be block-diagonal.

    Key Benefits:

    • Faster Serving: Eliminates communication overheads when running inference across multiple GPUs.
    • Performance: Maintains finetuning performance comparable to vanilla LoRA.

    Requirements:

    • To realize inference speed-ups, you must use an inference engine compatible with BD-LoRA. Currently, experimental support is available via a specific vLLM fork.
  8. What is MonteCLoRA and when to use it

    main

    MonteCLoRA (Monte Carlo Low-Rank Adaptation) is a variant of LoRA that wraps a standard LoRA adapter with a variational module. During training, it draws Monte Carlo samples of stochastic perturbations on top of the LoRA A matrix.

    Key Characteristics:

    • Training-only overhead: It learns variational parameters (Wishart-based covariance, multivariate-normal noise, and Dirichlet weighting) and adds a KL-divergence + entropy term to the loss.
    • Zero inference cost: At inference time, the sampler is disabled. MonteCLoRA behaves exactly like a regular LoRA adapter with no extra parameters to merge and no extra latency.

    When to use MonteCLoRA:

    • When fine-tuning on small or noisy datasets and you need stronger regularization than vanilla LoRA.
    • When you want better uncertainty calibration or robustness without increasing inference cost.
    • When vanilla LoRA is overfitting and adjusting r or lora_dropout is insufficient.

    When to avoid it:

    • When you have a large, clean dataset where vanilla LoRA is already stable, as the extra parameters primarily add training overhead.
  9. What is AdaMSS?

    main

    AdaMSS (Adaptive Multi-Subspace Approach for Parameter-Efficient Fine-Tuning) is a PEFT method that decomposes weight matrices using SVD and clusters the decomposed space into multiple trainable subspaces. Each subspace learns independent low-rank updates while the original weights remain frozen.

    Key features include:

    • Subspace Segmentation: Leverages multiple smaller subspaces instead of a single large subspace.
    • Adaptive Subspace Allocation (ASA): Dynamically prunes less important subspaces during training based on gradient information to reduce trainable parameters.
    • Efficiency: Aims for more compact expressiveness and finer tuning compared to single-subspace methods like LoRA or PiSSA.

    Constraints:

    • Only nn.Linear layers are supported.
    • Requires scikit-learn for the KMeans clustering step.
  10. What is Activated LoRA (aLoRA)?

    main

    Activated LoRA (aLoRA) is a specialized adapter designed to selectively activate its weights only after a specific invocation sequence. This mechanism ensures that hidden states match the base model prior to the invocation point.

    Key Benefit: It allows for the reuse of base model Key-Value (KV) caches for all tokens preceding the invocation. This enables significantly faster real-world inference (e.g., in engines like vLLM) when switching between base model generation and adapter-based generation.

  11. What is MiSS (Matrix Shard Sharing)

    main

    MiSS (Matrix Shard Sharing) is a Parameter-Efficient Fine-Tuning (PEFT) method designed to balance model performance and computational efficiency. Unlike LoRA, which uses low-rank decomposition, MiSS decomposes weight matrices into multiple fragment matrices and utilizes a shared, trainable common fragment. This shard-sharing mechanism allows for faster initialization and higher training throughput compared to advanced LoRA schemes like PiSSA, LoRA-GA, or OLoRA.

    Use MiSS when:

    • You need faster initialization and higher training throughput than complex LoRA initialization schemes.
    • You want a drop-in alternative to LoRA with minimal configuration changes.
    • You want to balance expressive power with resource utilization.
  12. What is Lily (Low-Rank Interconnected Adaptation across Layers)

    main

    Lily is a parameter-efficient fine-tuning (PEFT) technique that improves upon LoRA by introducing cross-layer weight sharing for adapter matrices.

    How it works

    Instead of learning independent $A$ and $B$ matrices for every single layer, Lily uses:

    • Locally shared A adapters: Each $A$ matrix is shared across a block of stride_A consecutive layers.
    • Globally shared B experts: A small pool of num_B $B$ adapters is shared across all layers.
    • Data-dependent routing: At runtime, a lightweight router computes a softmax-weighted combination of the $B$ experts based on input activations to produce the effective $B$ for that specific layer and input.

    Key Advantages

    • Higher Effective Rank: By reducing the number of parameters used for $A$ and $B$, you can allocate more of your parameter budget to a much larger rank r (typically $2\times$ to $4\times$ larger than LoRA), leading to better adaptation performance.
    • Parameter Efficiency: Reduces the total adapter matrices from $2N$ (standard LoRA) to $N / \text{stride}_A + \text{num}_B$.

    Constraints and Limitations

    • No Merging: Because the $B$ combination is data-dependent (calculated at runtime), merge and unmerge operations are not supported. If you require weight merging for deployment, use LoRA instead.
    • Layer Support: Only nn.Linear layers are supported.
    • Quantization: Quantized layers are not supported.