Skip to content

Commit 4154af0

Browse files
BenjaminBossansarathc-cerebras
authored andcommitted
FIX: Minimal fix for loading PEFT weights (huggingface#42387)
* FIX Minimal fix for loading PEFT weights After the weight conversion PR huggingface#41580, some adjustments were still required for loading PEFT weights. This PR presents a minimal fix to make it work again. Besides renaming keys, this PR does not address possible conversions that might be required to be applied to the PEFT weights themselves (most wouldn't work anyway, but e.g. chunking should be possible to implement). As for test, the existing test_peft_from_pretrained in test_peft_integration.py actually fails on main right now, this PR fixes it. As the tests are slow tests, normal CI won't pick this up though. * Allow n:n matching * Reviewer feedback
1 parent 2b6519c commit 4154af0

File tree

1 file changed

+5
-7
lines changed
  • src/transformers/integrations

1 file changed

+5
-7
lines changed

src/transformers/integrations/peft.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import inspect
1616
import json
1717
import os
18-
import re
1918
from typing import Any, Literal
2019

20+
from ..core_model_loading import WeightRenaming, build_glob_alternation, repl
2121
from ..utils import (
2222
CONFIG_NAME,
2323
cached_file,
@@ -302,12 +302,10 @@ def load_adapter(
302302
else:
303303
new_key = key
304304

305-
if key_mapping: # TODO dynamic weight loader for adapters
306-
for pattern, replacement in key_mapping.items():
307-
new_key, n_replace = re.subn(pattern, replacement, new_key)
308-
# Early exit of the loop
309-
if n_replace > 0:
310-
break
305+
if key_mapping:
306+
renamings = [entry for entry in key_mapping if isinstance(entry, WeightRenaming)]
307+
rename_alt, _, rename_by_group = build_glob_alternation(renamings)
308+
new_key = rename_alt.sub(lambda m: repl(m, rename_by_group), new_key).replace("\\", "")
311309

312310
# For hotswapping, we need the adapter name to be present in the state dict keys
313311
if hotswap:

0 commit comments

Comments
 (0)