PEFT documentation

Helper methods

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.14.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Helper methods

A collection of helper functions for PEFT.

Checking if a model is a PEFT model

peft.helpers.check_if_peft_model

< >

( model_name_or_path: str ) bool

Parameters

  • model_name_or_path (str) — Model id to check, can be local or on the Hugging Face Hub.

Returns

bool

True if the model is a PEFT model, False otherwise.

Check if the model is a PEFT model.

Temporarily Rescaling Adapter Scale in LoraLayer Modules

peft.helpers.rescale_adapter_scale

< >

( model multiplier )

Parameters

  • model — The model containing LoraLayer modules whose scaling is to be adjusted.
  • multiplier (float or int) — The multiplier that rescales the scaling attribute. Must be of type float or int.

Raises

ValueError

  • ValueError — If the model does not contain any LoraLayer instances, indicating that the model does not support scaling.

Context manager to temporarily rescale the scaling of the LoRA adapter in a model.

The original scaling values are restored when the context manager exits. This context manager works with the transformers and diffusers models that have directly loaded LoRA adapters.

For LoRA, applying this context manager with multiplier in [0, 1] is strictly equivalent to applying wise-ft (see #1940 for details). It can improve the performances of the model if there is a distribution shiftbetween the training data used for fine-tuning, and the test data used during inference.

Warning: It has been reported that when using Apple’s MPS backend for PyTorch, it is necessary to add a short sleep time after exiting the context before the scales are fully restored.

Example:

>>> model = ModelWithLoraLayer()
>>> multiplier = 0.5
>>> with rescale_adapter_scale(model, multiplier):
...     outputs = model(**inputs)  # Perform operations with the scaled model
>>> outputs = model(**inputs)  # The original scaling values are restored here
< > Update on GitHub