-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
base: main
Are you sure you want to change the base?
Conversation
BenjaminBossan
left a comment
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.
Thanks for this PR to add HiRA to PEFT. The method looks promising and the provided code is already quite mature.
When I started reading the paper, I was at first reminded of FedPara, aka LoHa, which is already integrated into PEFT, as that method also relies on the Hadamard product. However, IIUC, the two methods are still distinct: HiRA basically corresponds to LoRA, but instead of adding dW, we multiply it. In that way, it is much closer to LoRA than to LoHa. Still, I wanted to flag this, as I'm not sure you are aware (your paper doesn't seem to be reference FedPara).
At the moment, I haven't done a full in-depth review, but I think that makes more sense once we have completed the next step.
I noticed that you have formatted some unrelated files in method_comparison, could you please undo those changes? Usually, when you run make style, that directory should not be included.
I think a good next step is to add HiRA to the testing matrix we have in PEFT. For now, let's add some entries similar to the ones you can find here:
peft/tests/test_custom_models.py
Lines 70 to 72 in 92d65ca
| ("Vanilla MLP 1 LoRA", "MLP", LoraConfig, {"target_modules": "lin0"}), | |
| ("Vanilla MLP 2 LoRA", "MLP", LoraConfig, {"target_modules": ["lin0"]}), | |
| ("Vanilla MLP 3 LoRA", "MLP", LoraConfig, {"target_modules": ["lin1"]}), |
Since you also support embedding and conv layers, please make sure to include examples with those layers as well (basically, copy the relevant examples from LoRA and adjust them).
Then, please run pytest tests/test_custom_models.py -k "hira and not shira" -v and see if those tests pass. Once we get there, we can discuss the best next steps.
|
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread. |
|
@hqsiswiliam Do you still plan on working on this PR? |
Hi, BenjaminBossan. Thanks for checking in! I’ll continue working on this PR over the next few days. |
# Conflicts: # src/peft/tuners/hira/config.py # src/peft/tuners/hira/model.py # tests/test_custom_models.py
|
Hi, sorry for the long delay. I have updated the code and synced it with the latest changes in the repository. Could you please reopen this PR so I can push the updates? Thanks a lot for your time and help. |
|
Thanks, done @hqsiswiliam |
- update r's description
Hi, thanks for reopening the PR! The latest changes have been pushed, and I’ve synced everything with the most recent updates in the repository. Please let me know if there are any additional suggestions or further steps needed. 😄 |
BenjaminBossan
left a comment
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.
Thanks for your work on adding HiRA to PEFT. I focused in this review on the core implementation for now. A lot of my comments stem from the fact that this PR is based on the LoRA code with changes made to accommodate HiRA, but some of the LoRA code doesn't make sense here. Please check my comments on this.
I also thought about suggesting to add HiRA as a LoRA variant, similar to how DoRA is currently implemented. I think this would be a possibility to save a lot of code. Conceptually, however, I think HiRA is sufficiently different from LoRA that I wouldn't consider it a LoRA variant. LMK what you think about it.
Please merge with/rebase on the latest main branch and, once you finish your changes, please call make style.
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.
We also need an entry in the toctree for this to show up in the docs.
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.
This file is based on lora/model.py with a few changes for HiRA. However, lora/model.py has changed significantly since, could you please update hira/model.py based on the latest lora/model.py? It should be simplified now because we removed many methods.
| `Conv1D` which stores weights like (fan_in, fan_out) and hence this should be set to `True`. | ||
| modules_to_save (`List[str]`): | ||
| List of modules apart from adapter layers to be set as trainable and saved in the final checkpoint. | ||
| init_hira_weights (`bool` | `Literal["gaussian"]`): |
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 rename this to init_weights.
| from .config import HiRAConfig | ||
|
|
||
|
|
||
| class HiRALayer(BaseTunerLayer): |
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 rename this to HiraLayer.
| self.active_adapter = adapter_name | ||
|
|
||
| @contextmanager | ||
| def _enable_peft_forward_hooks(self, *args, **kwargs): |
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.
I'd say let's remove _enable_peft_forward_hooks from HiRA for now. It's not trivial to support, we should get the basics first and then later we can think about whether we want to add it or not.
| msg = "Cannot pass `adapter_names` when there are merged adapters, please call `unmerge_adapter` first." | ||
| raise ValueError(msg) | ||
|
|
||
| def _mixed_batch_forward( |
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.
This method and its calls below can be removed if we drop _enable_peft_forward_hooks.
| weight_A = weight_A.float() | ||
| weight_B = weight_B.float() | ||
| output_tensor = transpose((weight_B @ weight_A), self.fan_in_fan_out) | ||
| assert self.get_base_layer().weight.shape == output_tensor.shape |
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 not use any asserts in the code (except for tests). I think this can be removed, if you want to keep it, raise a proper error.
| elif isinstance(base_layer, nn.MultiheadAttention): | ||
| if not base_layer._qkv_same_embed_dim: | ||
| raise ValueError(f"Only same dim for query/key/value is supported as of now for {self.__class__}.") | ||
| in_features, out_features = base_layer.embed_dim, 3 * base_layer.embed_dim | ||
| elif hasattr(base_layer, "infeatures") and hasattr(base_layer, "outfeatures"): | ||
| # QuantLinear | ||
| in_features, out_features = base_layer.infeatures, base_layer.outfeatures | ||
| elif hasattr(base_layer, "input_size") and hasattr(base_layer, "output_size"): | ||
| # Megatron ColumnParallelLinear,RowParallelLinear | ||
| in_features, out_features = base_layer.input_size, base_layer.output_size | ||
| elif hasattr(base_layer, "codebooks") and base_layer.__class__.__name__ == "QuantizedLinear": | ||
| # AQLM QuantLinear | ||
| in_features, out_features = base_layer.in_features, base_layer.out_features | ||
| elif hasattr(base_layer, "w_bit") and base_layer.__class__.__name__ == "WQLinear_GEMM": | ||
| # Awq layers | ||
| in_features, out_features = base_layer.in_features, base_layer.out_features | ||
| elif base_layer.__class__.__name__ == "EetqLinear": | ||
| # Eetq layers | ||
| in_features, out_features = base_layer.in_features, base_layer.out_features | ||
| elif hasattr(base_layer, "W_q") and base_layer.__class__.__name__ == "HQQLinear": | ||
| # HQQ layers | ||
| in_features, out_features = base_layer.in_features, base_layer.out_features | ||
| elif base_layer.__class__.__name__ == "PatchedLinear": | ||
| # INC layers | ||
| in_features, out_features = base_layer.in_features, base_layer.out_features |
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.
| elif isinstance(base_layer, nn.MultiheadAttention): | |
| if not base_layer._qkv_same_embed_dim: | |
| raise ValueError(f"Only same dim for query/key/value is supported as of now for {self.__class__}.") | |
| in_features, out_features = base_layer.embed_dim, 3 * base_layer.embed_dim | |
| elif hasattr(base_layer, "infeatures") and hasattr(base_layer, "outfeatures"): | |
| # QuantLinear | |
| in_features, out_features = base_layer.infeatures, base_layer.outfeatures | |
| elif hasattr(base_layer, "input_size") and hasattr(base_layer, "output_size"): | |
| # Megatron ColumnParallelLinear,RowParallelLinear | |
| in_features, out_features = base_layer.input_size, base_layer.output_size | |
| elif hasattr(base_layer, "codebooks") and base_layer.__class__.__name__ == "QuantizedLinear": | |
| # AQLM QuantLinear | |
| in_features, out_features = base_layer.in_features, base_layer.out_features | |
| elif hasattr(base_layer, "w_bit") and base_layer.__class__.__name__ == "WQLinear_GEMM": | |
| # Awq layers | |
| in_features, out_features = base_layer.in_features, base_layer.out_features | |
| elif base_layer.__class__.__name__ == "EetqLinear": | |
| # Eetq layers | |
| in_features, out_features = base_layer.in_features, base_layer.out_features | |
| elif hasattr(base_layer, "W_q") and base_layer.__class__.__name__ == "HQQLinear": | |
| # HQQ layers | |
| in_features, out_features = base_layer.in_features, base_layer.out_features | |
| elif base_layer.__class__.__name__ == "PatchedLinear": | |
| # INC layers | |
| in_features, out_features = base_layer.in_features, base_layer.out_features |
Since all these are not supported, let's remove them.
| if hasattr(target, "unload_and_optionally_merge_module"): | ||
| # if layers have special unloading method, like MultiheadAttention, use that | ||
| unloaded_module = target.unload_and_optionally_merge_module( | ||
| merge=merge, safe_merge=safe_merge, adapter_names=adapter_names | ||
| ) | ||
| self._replace_module(parent, target_name, unloaded_module, target) |
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.
We have no layers with unload_and_optionally_merge_module in HiRA, so let's remove this.
| from peft.tuners.hira.layer import Conv2d as HiraConv2d | ||
|
|
||
|
|
||
| def test_hira_linear_merge_unmerge_basic(): |
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.
I believe these tests should be redundant with the existing tests we have in PEFT. Or do you see a gap in the PEFT tests that would require these?
The only one that could be worth keeping is test_manual_hira_linear_equivalence.
Feature request
This request proposes integrating HiRA (Hadamard High-Rank Adaptation) as described in the ICLR 2025 oral paper (https://openreview.net/pdf?id=TwJrTz9cRS) (https://iclr.cc/virtual/2025/oral/31839) and implemented in the hqsiswiliam/hira repository into the core PEFT library. This will enable users to apply HiRA through the familiar
get_peft_modelAPI and benefit from its high-rank updates without adding any inference overhead.Motivation
General Motivation
PEFT methods like LoRA achieve parameter-efficient fine-tuning by injecting low-rank updates into pre-trained weights. While effective, purely low-rank adaptation can struggle to capture complex patterns in large language models.
1. Expressiveness grows with the rank
Empirically, increasing the LoRA rank in LLM training yields better downstream performance:
Higher LoRA rank correlates with improved task accuracy.
2. HiRA: Hadamard high-rank updates without extra parameters
HiRA sidesteps the expressiveness constraint by computing a Hadamard-enhanced update:
HiRA uses the Hadamard product to inject high-rank structure into the frozen weight matrix
3. Singular-value patterns
After training, HiRA exhibits a rich singular-value pattern, akin to full-rank fine-tuning (FFT), indicating its ability to model complex transformations without the expensive computational overhead:
HiRA’s singular-value distribution closely mirrors that of FFT.
4. Performance gains
Across commonsense reasoning benchmarks, HiRA outperforms LoRA and other PEFT baselines:
HiRA delivers notable accuracy improvements over baseline adapters.
5. No extra parameter or compute cost
Despite its high-rank behaviour, HiRA introduces no additional trainable parameters compared to LoRA:
HiRA matches LoRA’s GRAM usage and training hours.
6. Complementary with LoRA (HiLoRA)
Combining HiRA and LoRA into a hybrid “HiLoRA” setup yields even stronger results than either method alone:
HiLoRA leverages both low-rank and Hadamard high-rank updates for better expressiveness.
By integrating HiRA into PEFT, users gain richer adaptation capability without sacrificing the parameter efficiency and usability that PEFT provides.
Your contribution
We would be pleased to submit a pull request to integrate HiRA class implementation into the PEFT framework. We welcome any suggestions for alternative integration approaches and appreciate any guidance on best practices.