236 Commits

Author SHA1 Message Date
5a4b9cade6 VeRA (Vector Based Random Matrix Adaption) (#1564)
Implements VeRA: https://huggingface.co/papers/2310.11454

VeRA is similar to LoRA but even more parameter efficient, while promising to
keep the same performance. In its current implementation, it has a few
limitations compared to LoRA:

- All targeted parameters must have the same shape.
- Only `nn.Linear` layers are supported.
- Quantized layers are not supported.

This PR is based on, and supersedes, #1039.

---------

Co-authored-by: Alex McKinney <alex.f.mckinney@gmail.com>
Co-authored-by: Dawid <20214809+dkopi@users.noreply.github.com>
2024-04-19 10:55:58 +02:00
bdb856786e MNT Remove dreambooth git submodule (#1660)
Leftover that was not removed in BOFT PR.
2024-04-18 13:47:47 +02:00
9f0cfc9919 Don't use deprecated Repository anymore (#1641)
* Don't use deprecated Repository anymore

* oops
2024-04-12 13:51:37 +02:00
811169939f BOFT: Orthogonal Finetuning via Butterfly Factorization (#1326)
Implements https://hf.co/papers/2311.06243.

---------

Co-authored-by: Zeju Qiu <zeju.qiu@gmail.com>
Co-authored-by: Yuliang Xiu <yuliangxiu@gmail.com>
Co-authored-by: Yao Feng <yaofeng1995@gmail.com>
2024-04-12 13:04:09 +02:00
16ec3f995a FIX: bnb config wrong argument names (#1603)
Several tests were using bnb_4bit_compute_type but the argument should
be called bnb_4bit_compute_dtype. Now the correct name is used.

This change should not affect the tests, because they were passing the
default value anyway. Therefore, the fact that this argument was passed
incorrectly (and thus, presumably, ignored) should not affect the
results.

Also, fix another incorrect argument to bnb config. These were caused by an
incorrect search and replace operation in #1552.
2024-04-05 12:26:52 +02:00
2e821c1dc8 FIX Use correct model in image clf notebook (#1624) 2024-04-05 11:59:58 +02:00
8452d71e14 fix the torch_dtype and quant_storage_dtype (#1614)
* fix the torch_dtype and quant_storage_dtype

Co-Authored-By: Gabriel Altay <gabriel.altay@gmail.com>

* quality

---------

Co-authored-by: Gabriel Altay <gabriel.altay@gmail.com>
2024-04-05 00:14:56 +05:30
8221246f2f Release: v0.10.0 (#1573)
Besides updating versions, removed 2 deprecations.
2024-03-21 10:16:25 +01:00
8e979fc732 More convenient way to initialize LoftQ (#1543)
Related to #1532

At the moment, using LoftQ is quite cumbersome, as shown in this
example:

7e84dec20b/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
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 #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 doesn't support 8bit quantization and the num_iter arguments of LoftQ.
However, the replace_lora_weights_loftq function can be called multiple
times in a row for slightly improved results.

---------

Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
2024-03-20 11:16:07 +01:00
a18734d87a Update style with ruff 0.2.2 (#1565)
This is necessary to add to main fast, or else all branches from main
will require these changes to pass the quality checks.
2024-03-15 10:20:41 +01:00
6008f272a5 Changes to support fsdp+qlora and dsz3+qlora (#1550)
* changes to support fsdp+qlora and dsz3+qlora

* address comments

* add example and start docs

* quality

* deepspeed fixes

* dsz3+qlora docs

* section link fix

* add fsdp+qlora docs

* Apply suggestions from code review

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>

* address comments

---------

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
2024-03-13 15:23:09 +05:30
6dca6d2292 MNT: Use BitsAndBytesConfig as load_in_* is deprecated (#1552)
Don't pass load_in_4bit or load_in_8bit to AutoModel*.from_pretrained,
as it is deprecated. Instead, pass the appropriate BitsAndBytesConfig to
the quantization_config argument of from_pretrained.
2024-03-11 15:24:48 +01:00
234bbabd9b FIX Allow AdaLoRA rank to be 0 (#1540)
Resolves #1539

Additionally, a small fix to the AdaLoRA training script was added.
2024-03-11 11:35:50 +01:00
7e84dec20b Optimize levenshtein algorithm in scripts (#1527)
This commit refines the levenshtein_distance algorithm implemented in peft_lora_seq2seq_accelerate_ds_zero3_offload.py to improve its space
complexity from O(n^2) to O(n). Additionally, thorough testing has been
conducted to ensure the correctness and reliability of the revised
implementation.
Also update peft_lora_clm_accelerate_ds_zero3_offload.py
2024-03-07 11:44:22 +01:00
aa2ca83ca7 add example and update deepspeed/FSDP docs (#1489)
* add example and update deepspeed docs

* fixes

* fixes and update FSDP docs

* fixes and addressing commentsa

* fixes

* resolve comments

* Apply suggestions from code review

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>

* address comments

* Update fsdp.md

* Update docs/source/accelerate/fsdp.md

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>

* addressing comments

* address comments

---------

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
2024-02-26 11:05:27 +05:30
b74c2f644d Update peft_bnb_whisper_large_v2_training.ipynb: Fix a typo (#1494) 2024-02-21 09:41:56 +01:00
a564779b67 Add files via upload (#1471) 2024-02-16 11:35:14 +05:30
c1a83fd692 Add new merging methods (#1364)
* add code

* update docstring

* quality

* fix test

* fix test

* fix svd embedding layer merging

* fixes

* fixes

* Update model.py

* Add test and example

* quality

* fix tests

* update the example

* Apply suggestions from code review

Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>

* address comments

* address comments and add co-authors

Co-Authored-By: Prateek Yadav <15224633+prateeky2806@users.noreply.github.com>
Co-Authored-By: Yu Le <55241218+yule-buaa@users.noreply.github.com>
Co-Authored-By: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>

* quality

* Update merge_utils.py

* revert

* address comments

* address comment

---------

Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Co-authored-by: Prateek Yadav <15224633+prateeky2806@users.noreply.github.com>
Co-authored-by: Yu Le <55241218+yule-buaa@users.noreply.github.com>
Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
2024-02-09 12:10:04 +05:30
fc78a2491e MNT Move code quality fully to ruff (#1421) 2024-02-07 12:52:35 +01:00
912ad41e96 Fix typos (#1435) 2024-02-06 01:54:06 +01:00
a43ec59762 FEAT Add Poly Adapter (#1129)
Implement the Poly (Polytropon) adapter.

Papers:

- https://arxiv.org/abs/2202.13914
- https://arxiv.org/abs/2211.03831

---------

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
2024-01-12 17:19:12 +01:00
7d28536b18 DOC Correct help for CLI args in script (#1338) 2024-01-10 11:44:25 +01:00
e745ffd7d0 FIX Errors in StableDiffusion adapter conversion script (#1281) 2023-12-20 12:00:05 +01:00
46a84bd395 LoftQ: edit README.md and example files (#1276)
* fix when num_bits == 2 or 8

* try 13b
2023-12-17 15:21:25 +01:00
bd544bb2ce LoftQ: Allow quantizing models on CPU (#1256) 2023-12-15 16:43:33 +01:00
55c37e9c0b feat: add apple silicon GPU acceleration (#1217)
* feat: add apple silicon GPU acceleration

* Fix device compatibility issue in
load_peft_weights function

* Update save_and_load.py

* Update save_and_load.py

* Update save_and_load.py

Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>

* Update src/peft/utils/save_and_load.py

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>

* Fix string formatting in image_classification_timm_peft_lora.ipynb and multilayer_perceptron_lora.ipynb

---------

Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
2023-12-15 13:05:06 +01:00
1b1091c158 remove HF tokens (#1207) 2023-12-04 15:15:19 +01:00
da17ac0f48 [Feature] Support OFT (#1160)
* Support OFT

* add test

* Update README

* fix code quality

* fix test

* Skip 1 test

* fix eps rule and add more test

* feat: added examples to new OFT method

* fix: removed wrong arguments from model example

* fix: changed name of inference file

* fix: changed prompt variable

* fix docs

* fix: dreambooth inference revision based on feedback

* fix: review from BenjaminBossan

* apply safe merge

* del partially

* refactor oft

* refactor oft

* del unused line

* del unused line

* fix skip in windows

* skip test

* Add comments about bias added place

* rename orig_weights to new_weights

* use inverse instead of linalg.inv

* delete alpha and scaling

---------

Co-authored-by: Lukas Kuhn <lukaskuhn.lku@gmail.com>
Co-authored-by: Lukas Kuhn <lukas.kuhn@deutschebahn.com>
2023-11-30 21:28:42 +05:30
2b901ee572 Add LoftQ initialization method for LoRA (#1150)
---------

Co-authored-by: Sourab Mangrulkar <13534540+pacman100@users.noreply.github.com>
Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
2023-11-29 17:08:17 +01:00
8298f1a366 Training PEFT models with new tokens being added to the embedding layers and tokenizer (#1147)
* add support for saving base layers weights along with adapter weights

* Update save_and_load.py

* Add an example showing the usage of the added feature

* refactor the functionality

* fix

* refactoring code

1. Add `is_embedding_layer_resized` parameter to `save_pretrained`
2. Fix the deduplication in README when adding PEFT details.
3. `save_pretrained` should only save the model when `is_main_process=True` which is one of the parameters of `save_pretrained`.

* update example

* fix the model card

* fix model card

* 😅

* fix model card

* automate setting `is_embedding_layer_resized`

* nits

* Update peft_lora_clm_with_additional_tokens.ipynb

* add test

* fix tests

* maybe fixes the issue?

* address comments

Co-Authored-By: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>

---------

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
2023-11-29 19:28:41 +05:30
04c411010b Examples: add options to save or push model (#1159) 2023-11-28 16:04:52 +01:00
b4ac2d840b FIX Dataset loaded twice in 4-bit finetuning script (#1164) 2023-11-22 12:23:50 +01:00
98429b8184 Fix: TorchTracemalloc ruins Windows performance (#1126)
* feat: added tracemalloc arg to train_dreambooth

* fix: added help for arg

* fix: changed arg name

* fix formatting

* fix: import order
2023-11-14 17:04:32 +05:30
49ddefa834 Add num_dataloader_workers arg to dreambooth script (#1107)
This is especially important for Windows users, who may have to set the
number of workers to 0.
2023-11-10 14:21:14 +01:00
884b1ac3a8 Add implementation of LyCORIS LoKr for SD&SDXL models (#978)
KronA-like adapter
2023-10-30 15:36:41 +01:00
894e68a408 FIX: wrong construction of LoHa weights (#1021)
Also: Update convert_sd_adapter_to_peft.py to account for a bug in
Lycoris-LoRA. See https://github.com/KohakuBlueleaf/LyCORIS/pull/115
2023-10-24 15:26:42 +02:00
3d0edccc4a Correct minor errors in example notebooks for causal language modelling (#926)
* updated Readme

* Corrected label bos token error; switched to eos token from pad token

* reverted readme change
2023-10-03 18:01:10 +05:30
7a5f17f39e FEAT Add LyCORIS LoHa for SD&SDXL models (#956)
https://arxiv.org/abs/2108.06098
2023-10-02 10:44:51 +02:00
1c0654b9a5 support multiple ranks and alphas for LoRA (#873)
* support multiple ranks and alphas

* Update lora.py

* Update lora.py

* commit suggestions

Co-Authored-By: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>

* address comments

Co-Authored-By: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>

* Fixed multirank + multialpha for sequential LoRAs, added correct support of LoRA-C3Lier conversion (#937)

* Fixed multirank multialpha for sequential loras, added tests, fixed docs

* Refactored kohya_ss conversion script for proper support of LoRA-C3Lier

* Fixed styling

* Removed old comment from docstring

* shift `scale_layer`/`unscale_layer` to `LoraLayer` class to support all the child classes

* support multiple active adapters

* add `active_adapters` property

Co-Authored-By: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>

* fix bug related to active adapter of `ModulesToSaveWrapper`

* revert the change wrt active_adapter assignment

Co-Authored-By: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>

* addressing comments

* address comments

* address comment

---------

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
Co-authored-by: Alexander Kovalchuk <kovalexal@gmail.com>
Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
2023-09-23 01:33:44 +05:30
93d0c03d5b Fixed LoRA conversion for kohya_ss (#916) 2023-09-14 11:00:24 +02:00
20d9c175e2 FIX linting issue in example (#908) 2023-09-06 11:59:46 +02:00
d4dbf684e0 FIX gradient_accumulation_steps in examples (#898)
* fix gradient_accumulation_steps in examples
* update accelerator init
2023-09-06 11:14:57 +02:00
0c9354bda9 DOC Fix for semantic_segmentation_lora (#891)
An argument was renamed.

---------

Co-authored-by: Raghavan <oneraghavan@gmail.com>
2023-08-31 14:07:19 +02:00
0e37b85609 🎉 Add Multitask Prompt Tuning (#400)
* mpt

* fix save

* fix save

* add jupyter notebook

* add jupyter notebook

* add jupyter notebook

* drop shuffling

* drop classify_dataset

* drop classify_dataset

* fix keys

* fix keys

* add comments

* use EXACT_SOURCE_TASK in the example

* formatting

* Fix dict index in embedding retrieval

* run style and quality

* run style and quality

* run style and quality

* style

* final fix

* style

* comment out failing tests

* fix generation tests

* fix style and save test

* all testcases

* fix import

* add license header

* reformat

* fix encoder-decoder models

* fix tests running multiple times

* fix paper name for IA3 and add MPT paper

* Trigger CI

* address the recommended changes

* reformat

* address suggestions

* address suggestions

* revert reformatting

* revert reformatting

---------

Co-authored-by: Alex-Brooks <Alex.Brooks@ibm.com>
2023-08-25 11:42:11 +05:30
0e33ac1efe DOC: Examples for LoRA with custom models (#724)
Example 1: training a multilayer perceptron
Example 2: fine-tuning a timm image classifier
New section "Developer Guides" in docs.

---------

Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
2023-07-27 15:28:33 +02:00
92d38b50af add support for Feature Extraction using PEFT (#647)
* add support for embedding with peft

* add example and resolve code quality issues

* update notebook example post fixing the loss

* adding full example with inference notebook

* quality 

* add tests, docs, guide and rename task_type to be inline with Hub

* fixes

* fixes

* Apply suggestions from code review

Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>

* Update peft_model.py

* fixes

* final fixes

* Update _toctree.yml

* fixes and make style and make quality

* deberta exception with checkpointing

* Update docs/source/task_guides/semantic-similarity-lora.md

Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>

* Update docs/source/task_guides/semantic-similarity-lora.md

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>

* resolve comments

* testing prompt learning methods

* Update testing_common.py

* fix the tests

---------

Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
2023-07-13 18:04:28 +05:30
c33c42f158 Add functionality to support IA3 (#578)
* Added initial ia3 code

* Implemented ia3 correctly for feedforward layers; Fixed regex matching

* Fixed module mapping for mt5

* Merged changes from huggingface:main

* Merged changes

* Fixed lora merge conflicts

* Different bloom config

* Added save option for ia3

* Added loading code for ia3

* Added feedforward implementation in utils and seq cls example

* Added feedforward implementation in utils and seq cls example

* Implemented merge, unmerge, enable/disable adapters functionality

* Fixed feedforward during merge

* Debugging Merge

* Removing debug messages

* Cleaned up repo

* Removed non-IA3 changes

* Refactor save and load

* Added support to all models in tests; Added IA3Config for common tests

* Added half-precision support and test for gradient checkpointing; Formatted jupyter notebooks

* Added target modules for new models GPTBigCode and LLama

* Cleaned up code

* Cleaned up code

* Cleaned up example notebook

* Cleaned up  seq2seq notebook

* Corrected function docstrings; refactored find_and_replace

* Corrected function docstrings; refactored find_and_replace

* Added basic docs for IA3

* Added new conceptual guide in source tree for documentation

* Minor fix to documentation

* Minor fixes to docstrings; Added error handling for 4bit quantization; Cleaned unused merge/unmerge methods

* styling changes after merge from main

* Update src/peft/tuners/ia3.py

Remove unused attribute merge_weights

Co-authored-by: Sourab Mangrulkar <13534540+pacman100@users.noreply.github.com>

---------

Co-authored-by: Abhishek2304 <abhishekgupta2304@gmail.com>
Co-authored-by: Sourab Mangrulkar <13534540+pacman100@users.noreply.github.com>
2023-07-13 13:15:50 +05:30
b5e341bb8a Added wandb support for lora train_dreambooth (#639)
* Update train_dreambooth.py

Accelerator init updated from logging_dir to project_dir. Newer versions of accelerate uses project_dir. logging_dir is deprecated

* Bugfix: Adapter name variable inserted, when changing LORA_ADAPTER_NAME it causes error

* Adapter name added as kwarg

* Black code formatted

* Style & Quality check

* Wandb import added for logging and project initialization

* Wandb import added for logging and project initialization

* fix project_name

* print tqdm progress to wandb
2023-07-11 13:56:03 +05:30
563acf0832 Remove loralib reqs from examples, small fixes (#640)
- As discussed, loralib is no longer required, so the examples from the
  docs have been updated to no longer require loralib as dependencies
- In one example, a missing torch import was added
- In another example, a missing line was added (output of that line is
  shown, but not the line itself)
2023-06-28 12:23:09 +05:30
a7ea02a709 [Bugfix] Inserted adapter_name to get_peft_model_state_dict function (#626)
* Update train_dreambooth.py

Accelerator init updated from logging_dir to project_dir. Newer versions of accelerate uses project_dir. logging_dir is deprecated

* Bugfix: Adapter name variable inserted, when changing LORA_ADAPTER_NAME it causes error

* Adapter name added as kwarg

* Black code formatted

* Style & Quality check
2023-06-27 13:56:54 +05:30