Skip to content
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 Bug with modules_to_save loading if substring #2334

Conversation

BenjaminBossan
Copy link
Member

Fixes #2289

This bug was the result of an error in the logic of modifying the state_dict for modules_to_save in set_peft_model_state_dict. The error in the logic was that it was checked if an entry from modules_to_save (a set of strings) is a substring of a key of the state_dict. If it was, a new name was assigned to that key in the state_dict, which would allow to load the weight later.

The issue that stems from the substring check occurs if there are multiple modules_to_save, and one of them has a name that is a substring of another. So e.g. if one is named "classifier" and the other is named "classifier2", there could be a false match.

This PR fixes the issue by enclosing the string with ".", i.e. we now check if ".classifier." is a substring instead, which avoid false matches.

What made this bug even harder to debug was that modules_to_save is a set and therefore has no predetermined order. Therefore, the bug would be flaky. To address this, modules_to_save is now sorted before iterating over it. That doesn't contribute to resolving the bug, but it makes the bug non-flaky, allowing future debugging to be easier.

Fixes huggingface#2289

This bug was the result of an error in the logic of modifying the
state_dict for modules_to_save in set_peft_model_state_dict. The error
in the logic was that it was checked if an entry from modules_to_save (a
set of strings) is a substring of a key of the state_dict. If it was, a
new name was assigned to that key in the state_dict, which would allow
to load the weight later.

The issue that stems from the substring check occurs if there are
multiple modules_to_save, and one of them has a name that is a substring
of another. So e.g. if one is named "classifier" and the other is named
"classifier2", there could be a false match.

This PR fixes the issue by enclosing the string with ".", i.e. we now
check if ".classifier." is a substring instead, which avoid false
matches.

What made this bug even harder to debug was that modules_to_save is a
set and therefore has no predetermined order. Therefore, the bug would
be flaky. To address this, modules_to_save is now sorted before
iterating over it. That doesn't contribute to resolving the bug, but it
makes the bug non-flaky, allowing future debugging to be easier.
Copy link
Collaborator

@githubnemo githubnemo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great debugging, thanks! As discussed offline we will accept the flakiness of the test as the chance is sufficiently low that we'll miss a regression with how many CI runs we do regularly.

src/peft/utils/save_and_load.py Show resolved Hide resolved
@BenjaminBossan BenjaminBossan merged commit da998c8 into huggingface:main Jan 20, 2025
14 checks passed
@BenjaminBossan BenjaminBossan deleted the fix-modules-to-save-adapter-name-substring branch January 20, 2025 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent Parameter Mismatches After Merging PEFT and Base Models
2 participants