Files
Benjamin Bossan 25f97e663a ENH: Add set_requires_grad method (#2807)
This PR adds the set_requires_grad method to PEFT models (both PeftModel
and BaseTuner). As the name suggests, this is a method to set the
requires_grad attribute of the specified PEFT adapters.

For more general context, this is mostly relevant when dealing with
multiple adapters. As is, users can already set the active adapter(s)
with set_adapter, which automatically adjust the requires_grad attribute
too, so that only the active adapters will have grads enabled. However,
there can be situations where activity status and requires grad may
differ. Right now, users would need to manually set requires_grad to
deal with that, which is error prone (e.g. forgetting modules_to_save).
This PR closes this gap in the API.

As this functionality is quite general purpose, I added a
set_requires_grad function to functional.py for easier integration.

Note: The set_requires_grad method will raise an error when called with
prompt learning methods like prompt tuning. This is because these
methods don't have a universal base class (BaseTuner and BaseTunerLayer)
that would allow to add this API. Moreover, they only support a single
adapter at a time, hence there is not much need to have this method in
the first place.

A side effect of not supporting prompt learning is that on the
PeftModel, we are free to allow set_requires_grad to accept more than
one adapter, which would normally be difficult, because prompt learning
only allows one adapter.
2025-10-13 16:54:16 +02:00

1.2 KiB

Functions for PEFT integration

A collection of functions that could be useful for non-PeftModel models, e.g. transformers or diffusers integration

The functions provided here can be considered "public API" of PEFT and hence are safe to be used by packages that provide PEFT integrations.

Cast the adapter weight dtypes

autodoc functional.cast_adapter_dtype - all

Delete the PEFT adapter from model

autodoc functional.delete_adapter - all

Get the state dict of the PEFT adapter

autodoc functional.get_peft_model_state_dict - all

Inject a PEFT adapter into the model based on a PEFT config

autodoc functional.inject_adapter_in_model - all

Set the active PEFT adapter(s) of the model

autodoc functional.set_adapter - all

Set the requires_grad attribute of the specified adapters

autodoc functional.set_requires_grad - all

Load the weights of the PEFT state dict into the model

autodoc functional.set_peft_model_state_dict - all