-
Notifications
You must be signed in to change notification settings - Fork 31.5k
FIX: Minimal fix for loading PEFT weights #42387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FIX: Minimal fix for loading PEFT weights #42387
Conversation
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.
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
|
One failing test in CI, |
vasqu
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preapproving but we need to sync with the other PR before it gets re-broken
Trusting you on the functionality here!
| elif len(patterns) > 1 and len(replacements) > 1: | ||
| raise ValueError( | ||
| "WeightConversion incorrectly initialized, please open an issue and report this error " | ||
| "here: https://github.com/huggingface/transformers/issues" | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There will be n:n patterns in #39585, so maybe we should add exceptions for those? Not sure when I get to merge that PR because handling memory issues on the converter which need to be resolved first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the code to not raise in case of n:n
| patterns = weight_converter.source_keys | ||
| replacements = weight_converter.target_keys |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's be careful here and sync with #42396 cc @Cyrilvallez
BenjaminBossan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preapproving but we need to sync with the other PR before it gets re-broken
I'll leave it up to the transformers devs to decide what to merge in what order, as I truly have no overview :) Please just ping me if this PR needs to be updated.
Trusting you on the functionality here!
At the end of the day, this functionality is pretty much a no-op for most cases. At the very least, the PR makes it so that loading works for those cases (right now it's broken). For full support of key mapping, there will be more work in the future, but from my POV, this PR is a strict improvement and (absent an alternative) should be in v5.
| elif len(patterns) > 1 and len(replacements) > 1: | ||
| raise ValueError( | ||
| "WeightConversion incorrectly initialized, please open an issue and report this error " | ||
| "here: https://github.com/huggingface/transformers/issues" | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the code to not raise in case of n:n
ArthurZucker
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you update the renaming using the alts and globs!
| # mapping is either 1:1 or 1:n or n:1 or n:n | ||
| if len(patterns) > 1 and len(replacements) == 1: | ||
| replacements = len(patterns) * replacements | ||
| elif len(patterns) == 1 and len(replacements) > 1: | ||
| patterns = len(replacements) * patterns | ||
| elif len(patterns) > 1 and len(replacements) > 1 and len(patterns) != len(replacements): | ||
| raise ValueError( | ||
| "WeightConversion incorrectly initialized, please open an issue and report this error " | ||
| "here: https://github.com/huggingface/transformers/issues" | ||
| ) | ||
|
|
||
| for pattern, replacement in zip(patterns, replacements, strict=True): | ||
| new_key, n_replace = re.subn(pattern, replacement, new_key) | ||
| # Early exit of the loop | ||
| if n_replace > 0: | ||
| break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine, but lets rather use this:
renamings = [entry for entry in weight_mapping if isinstance(entry, WeightRenaming)]
rename_alt, _, rename_by_group = build_glob_alternation(renamings)
renamed_key = rename_alt.sub(lambda m: repl(m, rename_by_group), original_key).replace("\\", "")this is the only think you need please!
We can leave the conversion as a todo.
Let's wait for Cyril's PR, we need a self.weigh_mapping!
|
@ArthurZucker Thanks for the feedback, this looks much more succinct, I changed the code accordingly. The failing test ( |
|
Merging, we still have todo for weight conversion |
* 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
What does this PR do?
After the weight conversion PR #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.
Before submitting
Pull Request section?
documentation guidelines, and
here are tips on formatting docstrings.