Add LoRA finetuning support for Contraction#1453
Open
hyjwpk wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends MACE’s LoRA fine-tuning utilities to also wrap Contraction layers (the core computation inside SymmetricContraction), enabling low-rank adaptation of the highest-order correlation weights and ensuring merge/unwrapping logic accounts for the new wrapper.
Changes:
- Add
LoRAContractionwrapper that applies a low-rank update toContraction.weights_max, including inference-time delta caching and attribute forwarding. - Extend
inject_lora()with awrap_contractionflag (defaultTrue) and update wrapper detection/merging to includeLoRAContraction. - Update LoRA merge wrapper-removal test to recognize the new wrapper type.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
mace/modules/lora.py |
Introduces LoRAContraction and extends LoRA injection/merge logic to handle Contraction layers. |
tests/test_lora.py |
Updates wrapper counting in merge tests to include LoRAContraction. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
364
to
372
| def inject_lora( | ||
| module: nn.Module, | ||
| rank: int = 4, | ||
| alpha: float = 1.0, | ||
| wrap_equivariant: bool = True, | ||
| wrap_dense: bool = True, | ||
| wrap_contraction: bool = True, | ||
| _is_root: bool = True, | ||
| ) -> None: |
Comment on lines
+332
to
+356
| effective = self.base.weights_max + self.scaling * delta | ||
|
|
||
| out = self.base.graph_opt_main( | ||
| self.base.U_tensors(self.base.correlation), | ||
| effective, | ||
| x, | ||
| y, | ||
| ) | ||
| for i, (weight, contract_weights, contract_features) in enumerate( | ||
| zip( | ||
| self.base.weights, | ||
| self.base.contractions_weighting, | ||
| self.base.contractions_features, | ||
| ) | ||
| ): | ||
| c_tensor = contract_weights( | ||
| self.base.U_tensors(self.base.correlation - i - 1), | ||
| weight, | ||
| y, | ||
| ) | ||
| c_tensor = c_tensor + out | ||
| out = contract_features(c_tensor, x) | ||
|
|
||
| return out.view(out.shape[0], -1) | ||
|
|
|
|
||
| def inject_LoRAs(model: nn.Module, rank: int = 4, alpha: int = 1): | ||
| inject_lora(model, rank=rank, alpha=alpha, wrap_equivariant=True, wrap_dense=True) | ||
| inject_lora(model, rank=rank, alpha=alpha, wrap_equivariant=True, wrap_dense=True, wrap_contraction=True) |
| count = 0 | ||
| for child in module.modules(): | ||
| if isinstance(child, (LoRADenseLinear, LoRAFCLayer, LoRAO3Linear)): | ||
| if isinstance(child, (LoRADenseLinear, LoRAFCLayer, LoRAO3Linear, LoRAContraction)): |
Comment on lines
264
to
269
| # Count LoRA wrappers before merge | ||
| def count_lora_wrappers(module): | ||
| count = 0 | ||
| for child in module.modules(): | ||
| if isinstance(child, (LoRADenseLinear, LoRAFCLayer, LoRAO3Linear)): | ||
| if isinstance(child, (LoRADenseLinear, LoRAFCLayer, LoRAO3Linear, LoRAContraction)): | ||
| count += 1 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Extends the existing LoRA implementation to cover
Contractionlayers insideSymmetricContraction.Changes:
LoRAContractioninmace/modules/lora.py: applies a low-rank update to the highest-order correlation weights(
weights_max), with inference-time delta caching and__getattr__forwarding for compatibility with model inspection utilitiesinject_lora()with a newwrap_contractionflag (defaultTrue)Related
Related to #1450