Failed config save leaves live adapter in inference mode
System Info
Reproduced on current main 85be3f3a1a58c4d153ffc4631c57196430af87a4 (PEFT 0.21.1.dev0), Windows 11, Python 3.10.11, PyTorch 2.13.0+cu130, Transformers 5.14.1, Accelerate 1.14.0. CPU-only reproduction with a locally initialized model; no model downloads.
Reproduction
PeftModel.save_pretrained() temporarily sets peft_config.inference_mode = True for config serialization, but restores it only after the config write succeeds. A config-write error leaves the live configuration changed. With projected Prefix Tuning, the next prompt/forward bypasses the projection and fails with a shape error.
This reproducer causes a real filesystem write error by placing a directory at the config-file path. It does not mock the saving code.
import tempfile
from pathlib import Path
from peft import PrefixTuningConfig, TaskType, get_peft_model
from transformers import GPT2Config, GPT2LMHeadModel
base = GPT2LMHeadModel(GPT2Config(
n_layer=2, n_head=2, n_embd=16, vocab_size=32,
bos_token_id=0, eos_token_id=1,
))
model = get_peft_model(base, PrefixTuningConfig(
task_type=TaskType.CAUSAL_LM, num_virtual_tokens=3,
prefix_projection=True, encoder_hidden_size=16,
))
model.get_prompt(batch_size=1) # succeeds
assert model.active_peft_config.inference_mode is False
with tempfile.TemporaryDirectory() as directory:
Path(directory, "adapter_config.json").mkdir()
try:
model.save_pretrained(directory)
except OSError as error:
print(type(error).__name__)
print(model.active_peft_config.inference_mode) # True, unexpectedly
model.get_prompt(batch_size=1) # RuntimeError: invalid shapeOutput ends with:
PermissionError
True
RuntimeError: shape '[1, 3, 4, 2, 8]' is invalid for input of size 48Prompt-path traceback from the diagnostic (local absolute paths shortened):
Traceback (most recent call last):
File "diagnostic_peft_save_failure_state_20261001.py", line 40, in <module>
m.get_prompt(batch_size=1)
File "src/peft/peft_model.py", line 801, in get_prompt
past_key_values = past_key_values.view(
RuntimeError: shape '[1, 3, 4, 2, 8]' is invalid for input of size 48A successful-save control writes inference_mode: true to disk, restores the live flag to False, and leaves the model's forward working. The failed-save case also makes an otherwise working forward fail on the same shape operation.
Expected behavior
Saving should propagate the original write error while restoring the live adapter's previous inference_mode. The model should remain usable after a failed save; successful checkpoint serialization should remain unchanged.
Proposed contribution
I would like to provide the focused fix and regression tests if this is approved: make restoration of this temporary config flag exception-safe, cover a real config-write failure with projected Prefix Tuning, and retain a successful-save control. I have not started the repair or opened a PR, and will wait for maintainer approval. I did not find an overlapping issue or PR; the config-encoding work in #3771 is a different scope.
Source: huggingface/peft