-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
More convenient way to initialize LoftQ #1543
Merged
BenjaminBossan
merged 14 commits into
huggingface:main
from
BenjaminBossan:loftq-more-convenient-initialization
Mar 20, 2024
Merged
More convenient way to initialize LoftQ #1543
BenjaminBossan
merged 14 commits into
huggingface:main
from
BenjaminBossan:loftq-more-convenient-initialization
Mar 20, 2024
Commits on Mar 7, 2024
-
[WIP] More convenient way to initialize LoftQ
Related to huggingface#1532 At the moment, using LoftQ is quite cumbersome, as shown in this example: https://github.com/huggingface/peft/tree/7e84dec20b3106bdd0a90ba8e80187f0aec835b7/examples/loftq_finetuning Essentially, users have to: 1. Load the non-quantized model with LoftQ (which can be quite huge) 2. Modify the PEFT config 3. Save the adapter 4. Unwrap the base model with custom functions 5. Save the base model with modified weights (i.e. a whole copy of the base model) 6. Load the base model from step 5 with bnb quantization 7. Load the adapter from step 3 Yes, there is a helper script to do this, but this still has the advantage that we need to load the non-quantized model and that we have to create a completely new model checkpoint with the modified weights. This PR aims to make this process more convenient by adding a single function replace_lora_weights_loftq. This function takes the bnb-quantized LoRA model as input. Then it goes through each module with LoRA weights, lazily loads the corresponding non-quantized weights one at a time using safetensors, computes the quantization error, and replaces the LoRA weights with LoftQ-initialized LoRA weights. This is much more convenient because we only require very little extra memory thanks to lazy loading, and we don't have to keep an extra copy of the weights. While working on this, I still found that LoftQ initialization often did not seem to help a lot, as mentioned in huggingface#1532. I measured this by creating (1) logits with the base model, (2) with the quantized+LoRA model, and (3) with the quantized+LoRA+LoftQ model. The expectation is that (1) should be closer to (3) than to (2). This was often not the case. I therefore added the possibility to run a check each time that we replace a LoRA weight with the LoftQ weights. If this check returns True, we proceed to the next weight, otherwise we discard the change. That way, we only make the replacement with LoftQ weights if we see a real improvement. Of course, this is only a form of greedy optimization, but it seems to work in practice. And since it's optional, users can choose not to use it. This PR is not yet finished since I ran into an issue with matching the key names from safetensors not matching. Furthermore, for now this doesn't support 8bit quantization and the num_iter arguments of LoftQ, which I'm not sure is really working. However, I guess the replace_lora_weights_loftq function could be called multiple times in a row.
Configuration menu - View commit details
-
Copy full SHA for cd597ca - Browse repository at this point
Copy the full SHA cd597caView commit details -
Configuration menu - View commit details
-
Copy full SHA for 127bb44 - Browse repository at this point
Copy the full SHA 127bb44View commit details -
Configuration menu - View commit details
-
Copy full SHA for 897fd71 - Browse repository at this point
Copy the full SHA 897fd71View commit details
Commits on Mar 11, 2024
-
Apply suggestions from code review
Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Configuration menu - View commit details
-
Copy full SHA for 2f287da - Browse repository at this point
Copy the full SHA 2f287daView commit details
Commits on Mar 12, 2024
-
Configuration menu - View commit details
-
Copy full SHA for c52624a - Browse repository at this point
Copy the full SHA c52624aView commit details -
Configuration menu - View commit details
-
Copy full SHA for fc11323 - Browse repository at this point
Copy the full SHA fc11323View commit details
Commits on Mar 13, 2024
-
Configuration menu - View commit details
-
Copy full SHA for bc6e0ea - Browse repository at this point
Copy the full SHA bc6e0eaView commit details
Commits on Mar 19, 2024
-
Configuration menu - View commit details
-
Copy full SHA for e0cf8d2 - Browse repository at this point
Copy the full SHA e0cf8d2View commit details -
Configuration menu - View commit details
-
Copy full SHA for 8038a6d - Browse repository at this point
Copy the full SHA 8038a6dView commit details -
Configuration menu - View commit details
-
Copy full SHA for 3f39640 - Browse repository at this point
Copy the full SHA 3f39640View commit details -
Target all linear layers in test
Better results, bigger margins.
Configuration menu - View commit details
-
Copy full SHA for 0ee3e38 - Browse repository at this point
Copy the full SHA 0ee3e38View commit details -
Configuration menu - View commit details
-
Copy full SHA for d6f3d29 - Browse repository at this point
Copy the full SHA d6f3d29View commit details
Commits on Mar 20, 2024
-
Configuration menu - View commit details
-
Copy full SHA for eea22b1 - Browse repository at this point
Copy the full SHA eea22b1View commit details -
Configuration menu - View commit details
-
Copy full SHA for dd3cfab - Browse repository at this point
Copy the full SHA dd3cfabView commit details
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.