-
Notifications
You must be signed in to change notification settings - Fork 2k
Pull Request: Adding HiRA integration into PEFT library #2668
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
Open
hqsiswiliam
wants to merge
25
commits into
huggingface:main
Choose a base branch
from
hqsiswiliam:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
bc16e34
- initial commit for hira adapter
hqsiswiliam 3c27937
- This initial modification of HiRA's config
hqsiswiliam aeb3d54
- update HiRA Model
hqsiswiliam d290008
- update HiRA Layer
hqsiswiliam dcdbe27
- update HiRA Layer partially
hqsiswiliam 8f48e2c
- update HiRA Layer partially (Embedding Layer)
hqsiswiliam 86e5195
- update HiRA Layer partially (ConvNd Layer)
hqsiswiliam da12aab
- update HiRA Layer partially (ConvNd Layer)
hqsiswiliam 69ace05
- update HiRA Layer partially (Conv1/2/3d Layer)
hqsiswiliam 2c53c8d
- update HiRA Layer partially (MultiheadAttention)
hqsiswiliam 32f6a4d
- remove HiRA Layer partially (MultiheadAttention)
hqsiswiliam f86c9a9
- update HiRA `layer`, `model`, and `config`
hqsiswiliam 54c8de7
- add bnb implementation and __init__.py
hqsiswiliam ef18d9f
- add HiRA's Linear8bitLt implementation
hqsiswiliam 7c4718b
- update HiRA's layer comment
hqsiswiliam 8506413
- add HiRA's Linear4bit
hqsiswiliam 9e8c017
- complete HiRA's Linear4bit
hqsiswiliam 71907b4
- add test_hira
hqsiswiliam ce782b6
- HiRA: updates to peft init, tuners, types, and GPU tests
hqsiswiliam d20332e
Merge remote-tracking branch 'upstream/main'
hqsiswiliam d76e328
- HiRA: updates to HiRA layer, and HiRA testing
hqsiswiliam e933f2a
- HiRA: formatting hira
hqsiswiliam 0a4b3aa
- HiRA: formatting hira
hqsiswiliam 6b4092a
- HiRA: add document
hqsiswiliam aab9204
- apply merge
hqsiswiliam 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# HiRA | ||
|
||
High-Rank Adaptation ([HiRA](https://openreview.net/pdf?id=TwJrTz9cRS)) is a PEFT method that extends the LoRA approach by applying an element-wise modulation on the original weight matrix. Instead of adding a low-rank update directly, HiRA computes: | ||
|
||
$$ | ||
W' = W_0 + W_0 \odot (B A) | ||
$$ | ||
|
||
where $W_0$ is the base weight, and $A, B$ are low-rank factors with rank $r \ll \min( \text{in_features}, \text{out_features})$. This formulation allows HiRA to adapt existing weights with a multiplicative, input-dependent modulation, often improving fine-tuning efficiency on downstream tasks. | ||
|
||
The abstract from the HiRA paper is: | ||
|
||
> *We propose Hadamard High-Rank Adaptation (HiRA), a parameter-efficient fine-tuning (PEFT) method that enhances the adaptability of Large Language Models (LLMs). While Low-rank Adaptation (LoRA) is widely used to reduce resource demands, its low-rank updates may limit its expressiveness for new tasks. HiRA addresses this by using a Hadamard product to retain high-rank update parameters, improving the model capacity. Empirically, HiRA outperforms LoRA and its variants on several tasks, with extensive ablation studies validating its effectiveness.* | ||
|
||
|
||
## Examples | ||
|
||
```python | ||
from transformers import AutoModelForCausalLM, AutoTokenizer | ||
from peft import get_peft_model | ||
from peft.tuners.hira import HiRAConfig | ||
|
||
# Example 1: HiRA on opt-125m for causal language modeling | ||
model_id = "facebook/opt-125m" | ||
base_model = AutoModelForCausalLM.from_pretrained(model_id) | ||
tokenizer = AutoTokenizer.from_pretrained(model_id) | ||
|
||
# Define HiRA configuration: apply to the MLP dense layers in each transformer block | ||
hira_config = HiRAConfig( | ||
r=32, | ||
target_modules=["k_proj", "q_proj","v_proj","fc1","fc2"], | ||
hira_dropout=0.0, | ||
init_hira_weights=True, | ||
) | ||
peft_model = get_peft_model(base_model, hira_config) | ||
|
||
peft_model.print_trainable_parameters() | ||
# trainable params: 4,718,592 || all params: 129,957,888 || trainable%: 3.6309 | ||
``` | ||
|
||
## HiRAConfig | ||
|
||
[[autodoc]] tuners.hira.config.HiRAConfig | ||
|
||
## Core Layers | ||
|
||
### HiRALayer | ||
|
||
[[autodoc]] tuners.hira.layer.HiRALayer | ||
|
||
### Linear Adapter | ||
|
||
[[autodoc]] tuners.hira.layer.Linear | ||
|
||
### Embedding Adapter | ||
|
||
[[autodoc]] tuners.hira.layer.Embedding | ||
|
||
### Convolutional Adapters | ||
|
||
[[autodoc]] tuners.hira.layer.Conv1d [[autodoc]] tuners.hira.layer.Conv2d [[autodoc]] tuners.hira.layer.ConvNd | ||
|
||
## BitsAndBytes Integration | ||
|
||
* **8-bit Quantized**: [[autodoc]] tuners.hira.bnb.Linear8bitLt | ||
* **4-bit Quantized**: [[autodoc]] tuners.hira.bnb.Linear4bit | ||
* **Dispatch Utilities**: | ||
|
||
* [[autodoc]] tuners.hira.bnb.dispatch_bnb_8bit | ||
* [[autodoc]] tuners.hira.bnb.dispatch_bnb_4bit | ||
|
||
## Dispatch Handler | ||
|
||
Default layer replacement for HiRA adapters: | ||
|
||
[[autodoc]] tuners.hira.dispatch.dispatch_default | ||
|
||
|
||
## Citation: | ||
If you found HiRA is useful, please cite HiRA as: | ||
``` | ||
@inproceedings{ | ||
huang2025hira, | ||
title={Hi{RA}: Parameter-Efficient Hadamard High-Rank Adaptation for Large Language Models}, | ||
author={Qiushi Huang and Tom Ko and Zhan Zhuang and Lilian Tang and Yu Zhang}, | ||
booktitle={The Thirteenth International Conference on Learning Representations}, | ||
year={2025}, | ||
url={https://openreview.net/forum?id=TwJrTz9cRS} | ||
} | ||
``` |
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
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
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,55 @@ | ||
# Copyright 2023-present the HuggingFace Inc. team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from peft.import_utils import is_bnb_4bit_available, is_bnb_available | ||
from peft.utils import register_peft_method | ||
|
||
from .config import HiRAConfig, HiRARuntimeConfig | ||
from .layer import Conv2d, Conv3d, Embedding, HiRALayer, Linear | ||
from .model import HiRAModel | ||
|
||
|
||
__all__ = [ | ||
"Conv2d", | ||
"Conv3d", | ||
"Embedding", | ||
"HiRAConfig", | ||
"HiRALayer", | ||
"HiRAModel", | ||
"HiRARuntimeConfig", | ||
"Linear", | ||
] | ||
|
||
register_peft_method(name="hira", config_cls=HiRAConfig, model_cls=HiRAModel, is_mixed_compatible=True) | ||
|
||
|
||
def __getattr__(name): | ||
if (name == "Linear8bitLt") and is_bnb_available(): | ||
from .bnb import Linear8bitLt | ||
|
||
return Linear8bitLt | ||
|
||
if (name == "Linear4bit") and is_bnb_4bit_available(): | ||
from .bnb import Linear4bit | ||
|
||
return Linear4bit | ||
|
||
|
||
# | ||
# if (name == "EetqLoraLinear") and is_eetq_available(): | ||
# from .eetq import EetqLoraLinear | ||
# | ||
# return EetqLoraLinear | ||
# | ||
# raise AttributeError(f"module {__name__} has no attribute {name}") |
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.
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 update every date to 2025.