Fix: Respect inference_mode when setting adapters with modules_to_save (Issue #2928)
#2931
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.
Fix: Respect
inference_modewhen setting adapters withmodules_to_saveFixes #2928
Description
This PR fixes issue #2928 where
modules_to_savehadrequires_grad=Trueeven wheninference_mode=Truewas passed toset_adapter()caused issues when using quantized models (e.g., with bitsandbytes) in inference mode, as the quantized layers require parameters to haverequires_grad=False.Problem
When calling
model.set_adapter(adapter_name, inference_mode=True)with a model that hasmodules_to_saveconfigured (e.g., a classifichead), themodules_to_saveparameters would still haverequires_grad=Truedespite being in inference mode. This happened because:_set_adapter()was callingmodule.enable_adapters(True)unconditionallyModulesToSaveWrapper.enable_adapters(True)setsrequires_grad_(True)for all active adaptersset_adapter()was called withinference_mode, creating a conflictSolution
The fix ensures that
enable_adapters()is only called when not in inference mode:Modified
_set_adapter()insrc/peft/utils/other.py:enable_adapters(True)wheninference_mode=FalseTruewhen inference mode should keep themFalseUpdated
PeftModel.set_adapter()insrc/peft/peft_model.py:inference_modeparameter to match the API ofPeftMixedModel.set_adapter()inference_modeto both_set_adapter()andbase_model.set_adapter()Added comprehensive tests in
tests/test_other.py:test_modules_to_save_inference_mode_requires_grad_false: Verifiesrequires_grad=Falsein inference modetest_modules_to_save_training_mode_requires_grad_true: Verifiesrequires_grad=Truein training modetest_modules_to_save_inference_mode_with_torch_inference_mode: Verifies compatibility withtorch.inference_mode()Changes Made
Code Changes
src/peft/utils/other.py:_set_adapter()to conditionally callenable_adapters()based oninference_modeparametersrc/peft/peft_model.py:set_adapter()method signature to acceptinference_modeparameterinference_modeto underlying adapter setting functionstests/test_other.py:TestModulesToSaveInferenceModetest class with 3 comprehensive testsTesting
Test Results
All new tests pass:
test_modules_to_save_inference_mode_requires_grad_false- PASSEDtest_modules_to_save_training_mode_requires_grad_true- PASSEDtest_modules_to_save_inference_mode_with_torch_inference_mode- PASSEDAll existing
modules_to_savetests pass (11/11)Related tests pass (71/74 - 3 failures are unrelated BOFT dependency issues)
Code quality checks pass (
make quality)Test Coverage
The new tests verify:
modules_to_saveparameters correctly haverequires_grad=Falsewheninference_mode=Truemodules_to_saveparameters correctly haverequires_grad=Truewheninference_mode=False(training mode)torch.inference_mode()context managerExample Usage
Before this fix, the following code would fail with quantized models:
After this fix: