-
Couldn't load subscription status.
- Fork 2.1k
Support for Activated LoRA (Issue https://github.com/huggingface/peft/issues/2523) #2609
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
Merged
Merged
Support for Activated LoRA (Issue https://github.com/huggingface/peft/issues/2523) #2609
Changes from all commits
Commits
Show all changes
113 commits
Select commit
Hold shift + click to select a range
cff31a4
add alora dir
beb92e4
Add files via upload
kgreenewald 0b445a4
initial alora-peft integration
kgreenewald 3625096
fix init files
bde5021
bugfixes
c43a6e1
Update layer.py
kgreenewald 063716d
Update __init__.py
kgreenewald 8cde2c0
Update model.py
kgreenewald 4ce95c7
Merge branch 'huggingface:main' into main
kgreenewald 00f9cff
Add tokenized invocation_tokens to config
kgreenewald b94faa7
Get rid of tokenizer argument, now use invocation_tokens
kgreenewald 26f5cf7
tokenizer code in warning
kgreenewald dcd78c5
Update test_custom_models.py
kgreenewald 087781f
alora tests
kgreenewald 89fd2b1
test debugging
b4b3465
refactor alora as lora variant
kgreenewald e9c0568
Merge pull request #1 from kgreenewald/codex/refactor-alora-method-to…
kgreenewald 4de01c7
add alora to lora config
kgreenewald f7cb9d8
Update model.py
kgreenewald 10d660f
Update model.py
kgreenewald e61a7b2
Update layer.py for alora
kgreenewald 94ebcb3
Update layer.py for use_alora
kgreenewald 99046cc
Update __init__.py
kgreenewald 628a84d
Update peft_model.py
kgreenewald c2f83f1
Update config.py
kgreenewald 9c73782
alora_offsets forward hook
kgreenewald 516d563
Merge branch 'huggingface:main' into main
kgreenewald 4a47414
Delete src/peft/tuners/alora directory
kgreenewald 4608351
Check use_alora flag for aLoRA
kgreenewald 3b2466e
Merge pull request #2 from kgreenewald/codex/update-tuners/peft_model…
kgreenewald faeadb1
Merge branch 'huggingface:main' into main
kgreenewald 191a605
inference working
475ee8f
update tests
kgreenewald 3781a81
tests passing
b9d1745
whitespace
22177f3
whitespace
498bdb1
format
6bb1d5b
make quality
9f26600
Update pyproject.toml
kgreenewald e613d02
streamline config
kgreenewald 9a0f9d9
decoder tests
3d1284a
make quality
3734623
moving more alora_offsets to variants.py
kgreenewald fa59f51
fixes
814b895
Merge branch 'huggingface:main' into main
kgreenewald 8e418c0
Update peft_model.py
kgreenewald dd6b670
Update config.py
kgreenewald 183c6a6
Update variants.py
kgreenewald 1df3c9c
Update variants.py
kgreenewald 6c129c0
Update testing_common.py
kgreenewald 61b44a1
Merge branch 'huggingface:main' into main
kgreenewald 9de9c18
Update testing_common.py
kgreenewald 6b7242c
Update config.py
kgreenewald 21a4054
Update model.py
kgreenewald c9fb085
Update peft_model.py
kgreenewald 3bde17d
Update lora.md
kgreenewald 0e50475
variants tests and example
kgreenewald 9d6e90f
Merge branch 'main' of https://github.com/kgreenewald/peft_alora
kgreenewald e77ab4a
Merge branch 'huggingface:main' into main
kgreenewald 0a24c72
fixes
6fe25db
amend
06bf2a2
new changes
2d49f38
Update lora.md
kgreenewald 180d9f5
Merge branch 'huggingface:main' into main
kgreenewald cff5b07
Update lora.md
kgreenewald 7924039
Update docs/source/developer_guides/lora.md
kgreenewald ac82acd
Merge branch 'huggingface:main' into main
kgreenewald 6f1e284
Update lora.md
kgreenewald 21ceb56
Update variants.py
kgreenewald d1d31e7
Update src/peft/tuners/lora/variants.py
kgreenewald 076411f
Update variants.py
kgreenewald 3c267ce
Update tests/test_lora_variants.py
kgreenewald 56455a8
Update test_custom_models.py
kgreenewald 14752be
Update model.py
kgreenewald 57313a5
Update testing_common.py
kgreenewald 089d304
Update bnb.py
kgreenewald 35c7aae
Update test_lora_variants.py
kgreenewald cb79411
Update test_lora_variants.py
kgreenewald 99dc4fa
Update test_lora_variants.py
kgreenewald 133183a
workaround for new tokens
kgreenewald 0b7b164
Update test_lora_variants.py
kgreenewald 7d05034
Update variants.py
kgreenewald 1d16e13
Update lora.md
kgreenewald 31fcfcc
tests and example
45be768
Update test_lora_variants.py
kgreenewald de4b886
offsets_change
0033694
Merge remote-tracking branch 'upstream/main'
kgreenewald 26287e8
Merge branch 'main' of https://github.com/kgreenewald/peft_alora
kgreenewald b2d16e5
Merge branch 'huggingface:main' into main
kgreenewald 5bba212
Update pyproject.toml
kgreenewald 3bd6196
Update test_lora_variants.py
kgreenewald b541cff
Update test_lora_variants.py
kgreenewald 783cf90
Update test_lora_variants.py
kgreenewald 92e1305
Update test_custom_models.py
kgreenewald e536b1a
Update test_decoder_models.py
kgreenewald 43a2fc2
Update variants.py
kgreenewald ea964fd
Update variants.py
kgreenewald 7bf2943
Update test_gpu_examples.py
kgreenewald c1e6a39
latest requests
af76162
latest requests
1ae7155
Update variants.py
kgreenewald bd15f77
Update variants.py
kgreenewald 4641d60
make test
c99cd90
Update variants.py
kgreenewald 0128793
Update lora.md
kgreenewald 4e79da0
make style
f2ab507
Update lora.md
kgreenewald b27a6dc
Update docs/source/developer_guides/lora.md
kgreenewald 082d417
Update docs/source/developer_guides/lora.md
kgreenewald 582b043
Update docs/source/developer_guides/lora.md
kgreenewald 2d3fadf
Update docs/source/developer_guides/lora.md
kgreenewald 9a20744
Update docs/source/developer_guides/lora.md
kgreenewald dbd56e7
Update lora.md
kgreenewald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Activated LoRA (aLoRA) | ||
|
|
||
| ## Introduction | ||
| Activated LoRA (aLoRA) is an adapter that selectively activates its weights only after a given invocation sequence, ensuring that hidden states match the base model prior to this point. This allows reusing the base model KVs (stored in the KV cache) for tokens before the invocation, | ||
| enabling much faster real-world inference (e.g. vLLM) when switching between generation with the base model and generation with adapters. | ||
| See the [paper](https://huggingface.co/papers/2504.12397) for more details. | ||
|
|
||
| ## Quick start (shown for Mistral 7B) | ||
| ```python | ||
| import torch | ||
| from peft import LoraConfig, get_peft_model | ||
| from transformers import AutoTokenizer, AutoModelForCausalLM, Trainer, DataCollatorForLanguageModeling | ||
| from datasets import load_dataset | ||
|
|
||
| model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3", device_map="cuda") | ||
| tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3") | ||
| dataset = load_dataset("Lots-of-LoRAs/task1660_super_glue_question_generation", split="train") | ||
|
|
||
| invocation_string = "[/INST]" # End of user turn in Mistral chat template | ||
| invocation_tokens = tokenizer.encode(invocation_string, add_special_tokens=False) | ||
|
|
||
| lora_config = LoraConfig( | ||
| task_type="CAUSAL_LM", | ||
| alora_invocation_tokens=invocation_tokens, | ||
| r=32, | ||
| target_modules=["q_proj", "k_proj", "v_proj"], | ||
| ) | ||
|
|
||
| peft_model = get_peft_model(model, lora_config) | ||
| data_collator = DataCollatorForLanguageModeling(tokenizer, mlm=False) | ||
| trainer = Trainer( | ||
| model=peft_model, | ||
| train_dataset=dataset, | ||
| dataset_text_field="text", | ||
| max_seq_length=2048, | ||
| tokenizer=tokenizer, | ||
| data_collator=data_collator, | ||
| ) | ||
| trainer.train() | ||
| peft_model.save_pretrained("alora-mistral-7b") | ||
| ``` | ||
|
|
||
| ### Use the training example script directly | ||
| Pass the invocation string with `--invocation_string` when running the training example | ||
| script. For Mistral 7B, do: | ||
| ```bash | ||
| python examples/alora_finetuning/alora_finetuning.py --base_model mistralai/Mistral-7B-Instruct-v0.3 --data_path Lots-of-LoRAs/task1660_super_glue_question_generation --invocation_string "[/INST]" | ||
| ``` | ||
githubnemo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| and similarly for Llama-3.2-3B-Instruct: | ||
| ```bash | ||
| python examples/alora_finetuning/alora_finetuning.py --base_model meta-llama/Llama-3.2-3B-Instruct --data_path Lots-of-LoRAs/task1660_super_glue_question_generation --invocation_string "<|start_header_id|>assistant<|end_header_id|>" | ||
| ``` | ||
|
|
||
| ### Full example of the script | ||
| ```bash | ||
| python alora_finetuning.py \ | ||
| --base_model "PATH_TO_MODEL" \ | ||
| --data_path "PATH_TO_DATASET" \ | ||
| --output_dir "PATH_TO_OUTPUT_DIR" \ | ||
| --batch_size 1 \ | ||
| --num_epochs 3 \ | ||
| --learning_rate 3e-4 \ | ||
| --cutoff_len 512 \ | ||
| --val_set_size 500 \ | ||
| --invocation_string "[/INST]" \ | ||
| --quantize \ | ||
| --eval_step 10 \ | ||
| --save_step 100 \ | ||
| --device "cuda:0" \ | ||
| --lora_r 32 \ | ||
| --lora_alpha 32 \ | ||
| --lora_dropout 0.05 \ | ||
| --lora_target_modules "q_proj,k_proj,v_proj,o_proj,gate_proj,up_proj,down_proj" \ | ||
| --hub_model_id "YOUR_HF_REPO" \ | ||
| --push_to_hub | ||
| ``` | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.