Skip to content

Commit 35000fd

Browse files
authored
Fix #2634: Allow peft_method to be a string (#2635)
The auto-tagging code assumed that every `PeftConfig.peft_type` value is an Enum value but when adding custom types without modifying the enum it is possible to have strings as well (and the interface supports that). This change allows for string values of `PeftConfig.peft_type` in the auto-tagging code.
1 parent 0755ab9 commit 35000fd

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/peft/peft_model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,9 @@ def _get_peft_specific_model_tags(self):
14431443
base model is important for enabling support for HF inference providers but it also makes models more
14441444
searchable on the HF hub.
14451445
"""
1446-
peft_method = self.active_peft_config.peft_type.value
1446+
peft_method = self.active_peft_config.peft_type
1447+
if not isinstance(peft_method, str):
1448+
peft_method = peft_method.value
14471449

14481450
tags = []
14491451

tests/test_hub_features.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,19 @@ def test_custom_models_dont_have_transformers_tag(self, custom_model_cls, tmp_pa
218218

219219
assert model_card.data.tags is not None
220220
assert "transformers" not in model_card.data.tags
221+
222+
def test_custom_peft_type_does_not_raise(self, tmp_path):
223+
# Passing a string value as peft_type value in the config is valid, so it should work.
224+
# See https://github.com/huggingface/peft/issues/2634
225+
model_id = "hf-internal-testing/tiny-random-Gemma3ForCausalLM"
226+
with hub_online_once(model_id):
227+
base_model = AutoModelForCausalLM.from_pretrained(model_id)
228+
peft_config = LoraConfig()
229+
230+
# We simulate a custom PEFT type by using a string value of an existing method. This skips the need for
231+
# registering a new method but tests the case where we pass a string value instead of an enum.
232+
peft_type = "LORA"
233+
peft_config.peft_type = peft_type
234+
235+
peft_model = get_peft_model(base_model, peft_config)
236+
peft_model.save_pretrained(tmp_path)

0 commit comments

Comments
 (0)