ENH: Add set_requires_grad method #2807
Merged
+230
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR adds the
set_requires_grad
method to PEFT models (bothPeftModel
andBaseTuner
). As the name suggests, this is a method to set therequires_grad
attribute of the specified PEFT adapters.Context
This method 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 setrequires_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.Notes
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
andBaseTunerLayer
) 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 allowset_requires_grad
to accept more than one adapter, which would normally be difficult, because prompt learning only allows one adapter.